npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gmobject

v1.0.1

Published

Create javascript objects using properties of another object.

Downloads

5

Readme

GMObject NPM version Build status Coverage Status

Create javascript objects using properties of another object.

Let's say you're gathering data as js objects from different sources. Chances are those objects are different from each other and you need to normalize them all. This module can help you do this by creating "mapping rules" for each such object describing what property to take and how to modify it.

Table of Contents

Usage

Install:

npm install --save gmobject

Require:

var GMObject = require('gmobject');

var gmo = new GMObject();

Have your object ready:

var myObject = {
    first: 'Hello',
    second: 'world',
    meaninglessNumber: 42
}

Make some mapping rules:

gmo.add('computedProperty')
    .use('first', 'second')
    .handler(function(first, second) {
        return first + ', ' + second + '!';
    });
  
gmo.add('meaningOfLife').use('meaninglessNumber');

var result = gmo.parse(myObject);
// result = {computedProperty: 'Hello, world!', meaningOfLife: 42}

What you can do

Add properties
var myObject = {hello: 'world'};

gmo.add('newProperty')
    .handler(function() {
        return 'new value';
    });

var result = gmo.parse(myObject)
// result = {newProperty: 'new value'}
Rename properties
var myObject = {hello: 'world'};

gmo.add('goodbye').use('hello');

var result = gmo.parse(myObject)
// result = {goodbye: 'world'}
Copy properties as is
var myObject = {hello: 'world'};

gmo.use('hello');

var result = gmo.parse(myObject)
// result = {hello: 'world'}
Make new properties from any number of old properties
var myObject = {first: 'Hello', second: 'world'};

gmo.add('computedProperty')
    .use('first', 'second')
    .handler(function(first, second) {
        return first + ', ' + second + '!';
    });

var result = gmo.parse(myObject)
// result = {computedProperty: 'Hello, world!'}

Available methods

.add({String} property)

Create new property in result object

.use(...args)

Get property values from source object. If there is handler call after use it'll receive these values as arguments.

.handler(function)

Returned value form this function will be used as property value in result object. If there is use before this method then it'll receive whatever it is in use as arguments:

gmo.add('megaArg').use('arg1', 'arg2', 'arg3').handler(function(arg1, arg2, arg3) {
    // Do something with arg1, arg2 and arg3
    // Whatever will be returned from here will be used as value of `megaArg` property
})

gmo.parse(object) => {Object}

Call this at the end. Executes all mapping rules and returns new object. Don't chain it with other rules.

Step by step

// var oldObject = {old: 122}
// new object = {}
gmo
    .add('key') // At this point new object is {key: undefined}
    .use('old') // Take "old" property value from old object.
    .handler(function(old) { // "old" passed here, old === 122
        return old + 1; // At this point new object is {key: 123}
    })
// You can continue to add new properties
gmo.add('anotherKey').use('old').handler(function(old){
    return old + 345 // At this point new object is {key: 123, anotherKey: 468}
})

var newObject = gmo.parse(oldObject); // Call "parse" to return new mapped object
// newObject === {key: 123, anotherKey: 467}

Notes

  1. You can be explicit and always go full chain add => use => handler or you can use some magic. For example to rename a property you can do this:
gmo.add('renamed').use('oldProp').handler(function(oldProp) {
    return oldProp;
})

or you can skip a step:

gmo.add('renamed').use('oldProp');

Look at tests to get the idea.

  1. Properties are not automatically copied to the new object. To create a property you need to explicitly add it or you can just grab everything you need from the old object using magic:
gmo.use('oldValue', 'anotherValue', 'someOtherValue')

Again, look at tests to get the idea.