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

obj-transmute

v2.0.10

Published

Transmute object to another one using defined rules

Downloads

30

Readme

ObjTransmute

Transmutes ( converts ) object to another one using defined rules and config ( optional )

Usage:

const ObjTransmute = require( 'obj-transmute' );

// ObjTransmute( {obj|Object}, {rules|Object}[, {config|Object}] ) => {Object}

var obj = {
        a: 1,
        b: 'Hello world!',
        other1: null
    },
    rules = {
        c: 'a',  // 'a' - originProp => result.c = obj.a
        d: function ( d, obj, originProp, prop ) {
            // context:
            // this.obj = obj
            // this.rules = rules
            // this.config = config
            // this.result - result object in current state
            // arguments:
            // d = obj[ originProp ] = obj.d = undefined - obj value of same property 'd'
            // originProp = 'd'
            // obj = this.obj
            // prop = 'd'
            return obj.b + this.obj.a;
        },
        e: {
            from: 'a1',             // originProp
            default: 'Bye world!',  // working only when get() returns undefined
            descriptor: { enumerable: false }, // result.e will be nonenumerable
            get: function ( a1, obj, originProp, prop ) {
                // arguments:
                // a1 = obj[ originProp ] = obj.a1 = undefined
                // originProp = 'a1'
                // obj = this.obj
                // prop = 'e'
                return a1 === undefined ? null : a1 + '!!!';
            }
        },
        f: {
            from: 'a1',             // originProp
            default: 'Bye world!',  // value by default if obj.a1 === undefined
        },
        g: { // same as d example but on undefined returns default value
            default: 'default',  // working only when get() returns undefined
            get: function ( g, obj, originProp, prop ) { return g && g + '!!!' || undefined }
        },
    };

ObjTransmute( obj, rules ) => { c: 1, d: 'Hello world!1', e: null, f: 'Bye world!', g: 'default' }


/* ------------ Using config ------------- */

var config = {
        otherProps: true,
        descriptor: { enumerable: false } // now all options by default will be nonenumerable
    };

ObjTransmute( obj, rules, config ) => {
        b: 'Hello world!',  // not defined in rules - passes through
        c: 1,
        d: 'Hello world!1',
        e: null,
        f: 'Bye world!',
        g: 'default',
        other1: null        // not defined in rules - passes through
    }


/* ------------ Using Default Object ------------- */

var defaultOptions = {
        other1: 123
    };

var config = {
        otherProps: true,
        saveOrigin: true,
        // define get by default
        get: function ( value, obj, originProp, prop ) { return value || defaultOptions[ prop ] }
    };

ObjTransmute( obj, rules, config ) => {
        a: 1,               // origin
        b: 'Hello world!',  // origin
        c: 1,
        d: 'Hello world!1',
        e: null,
        f: 'Bye world!',
        g: 'default',
        other1: 123                // unused value but using default get() returns value from defaultOptions
    }
    
/* ------------ Using Require Option ------------- */

// Require option is used when we need other property to be calculated before current property
var obj = {
        a: 'Hello world',
        b: 1
    },
    rules = {
        c: {
            require: 'd',           // tells that first we need to get result.d
            get: function () { return this.result.d + 1 }
        },
        d: {
            require: 'e',           // tells that first we need to get result.e
            get: function () { return this.result.e * 2 }
        },
        e: function () { return this.obj.b + 2 },
        f: {
            from: 'a',              // we get value from obj.a
            require: [ 'c', 'e' ],  // and require result.c and result.e to be already calculated
            get: function ( a ) { return a + ( this.result.c + this.result.e ) }
        }
    };
    
ObjTransmute( obj, rules, config ) => {
     c: 7,
     d: 6,
     e: 3,
     f: 'Hello world10'
}