Skip to content
This repository has been archived by the owner on Dec 1, 2018. It is now read-only.
/ deepdo Public archive

UNMAINTAINED. Mess with the properties of deeply nested objects.

License

Notifications You must be signed in to change notification settings

pluma/deepdo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NOTE: This package is no longer being maintained. If you are interested in taking over as maintainer or are interested in the npm package name, get in touch by creating an issue.

Synopsis

deepdo lets you mess with deeply nested objects.

license - MIT Flattr this

Dependencies

NPM status

API

To support arbitrarily nested properties, keys can be defined as paths using the dot (".") as path separator and the asterisk ("*") as a wildcard to match any property name. If you need to use keys that would normally contain a dot or asterisk, you can escape these characters using the backslash ("\\"), i.e. "\\." and "\\*".

deepdo.pick(keys:Array, source):*

Returns a new object that is a copy of source with only the properties specified in keys.

If source is an array, a new array will be returned instead of an object.

If keys is a string, it will be wrapped in an array automatically.

Examples

var noisyData = {
    success: true,
    data: {
        meta: true,
        secrets: 'interesting'
    },
    lies: 'filthy lies',
};
var whatWeWant = deepdo.pick(['data.secrets', 'lies'], noisyData);
console.log(whatWeWant);
/*
{
    data: {
        secrets: 'interesting'
    },
    lies: 'filthy lies'
}
*/

deepdo.omit(keys:Array, source):*

Returns a new object that is a copy of source without the properties specified in keys.

If source is an array, a new array will be returned instead of an object.

If keys is a string, it will be wrapped in an array automatically.

Examples

var noisyData = {
    success: true,
    data: {
        meta: true,
        secrets: 'interesting'
    },
    lies: 'filthy lies',
};
var whatWeWant = deepdo.omit(['data.meta', 'success'], noisyData);
console.log(whatWeWant);
/*
{
    data: {
        secrets: 'interesting'
    },
    lies: 'filthy lies'
}
*/

deepdo.mutate(map, source):source

Applies the given transformation map on the source and modifies it in-place. Returns the modified source.

The map can be either an object mapping keys to functions that should be executed on each matching property or an array of key/function tuples.

Examples

var thing = {
    names: ['foo', 'bar', 'qux'],
    color: 'blue',
    some: {
        deep: {stuff: 'here'},
        more: {stuff: 'also'}
    }
};
var result = deepdo.mutate({
    'names.*': function (str) {return str.toUpperCase();},
    'color': function () {return 'yellow';},
    'some.*.stuff': function () {return 'chicken';}
}, thing);
console.log(result === thing); // true
console.log(thing);
/*
{
    names: ['FOO', 'BAR', 'QUX'],
    color: 'yellow',
    some: {
        deep: {stuff: 'chicken'},
        more: {stuff: 'chicken'}
    }
}
*/

License

The MIT/Expat license. For more information, see http://pluma.mit-license.org/ or the accompanying LICENSE file.

About

UNMAINTAINED. Mess with the properties of deeply nested objects.

Resources

License

Stars

Watchers

Forks

Packages

No packages published