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.
transform-object transforms objects.
npm install transform-object
git clone https://github.com/pluma/transform-object.git
cd transform-object
npm install
make test
Transforms the given object by mapping it against the given transformation recursively.
If obj
is an object and transformation
is an object, returns a new object with each property set to the result of applying the property of the transformation
to the respective property of the obj
:
function upper(s) {return s.toUpperCase();}
function lower(s) {return s.toLowerCase();}
var result = transform({a: 'Foo', b: 'Bar', c: 'Qux'}, {a: upper, b: lower});
console.log(result); // {a: 'FOO', b: 'bar', c: 'Qux'}
If obj
is an array and transformation
is an array or object, returns a new array with each item set to the result of applying the matching property or item of the transformation
to the respective item in the obj
array:
function upper(s) {return s.toUpperCase();}
function lower(s) {return s.toLowerCase();}
var result1 = transform(['Foo', 'Bar', 'Qux'], {0: upper, 2: lower});
console.log(result1); // ['FOO', 'Bar', 'qux']
var result2 = transform(['Foo', 'Bar', 'Qux'], [upper, undefined, lower]);
console.log(result2); // ['FOO', 'Bar', 'qux']
If transformation
is a Function
, returns the result of calling it with the given obj
as argument:
function upper(s) {return s.toUpperCase();}
var result = transform('foo', upper);
console.log(result); // 'FOO'
If transformation
is undefined
, returns the obj
:
var result = transform('foo', undefined);
console.log(result); // 'foo'
Otherwise returns the transformation
:
var result = transform('foo', 'bar');
console.log(result); // 'bar'
This is free and unencumbered public domain software. For more information, see http://unlicense.org/ or the accompanying UNLICENSE file.