translate-object
v0.0.1
Published
Translate an object representation into another based on a map.
Downloads
4
Readme
translate-object
Translate an object representation into another based on a map.
Installation
Install the package with: npm install translate-object
.
Then:
var translate = require('translate-object');
It hasn't been tested but should work fine on a browser. You can use it with browserify :).
Usage
It works with simple keys:
translate({ 'key1': 'something' }, { something: 'value' }),
// => { key1: 'value' }
It works with nested keys:
translate({ key1: 'something.nested' }, { something: { nested: 'here' } }),
// => { key1: 'here' }
It works with array indexes:
translate({ key1: 'something.1' }, { something: [1, 'value'] }),
// => { key1: 'value' }
It works with mixed input:
translate(
{ 'key1': 'something', 'key2': 'nested.severally', key3: 'array.1' },
{ something: 'value', nested: { severally: 'here' }, array: [1, 2] }
),
// => { key1: 'value', key2: 'here', key3: 2 }
And the maps may be complex objects too:
translate(
{ key1: ['an-array.2', 'an-array.1'],
key2: { alternative: 'syntax', here: 'hell' } },
{ 'an-array': [1, 'value', 3], syntax: 'ugly', hell: 'bleh' }
),
// => { key1: [ 3, 'value' ], key2: { alternative: 'ugly', here: 'bleh' } }
License
Copyright (c) 2014 Pedro Yamada. Licensed under the MIT license.