Reformat javascript objects easily
npm install reformat
var reformat = require('reformat')
Let's say you have some data that looks like this:
{
some: {deeply: {nested: {field: 'FOOBAR'}}},
arr: ['thing1', 'thing2'],
top_level_key: 'toplev',
other_key: {things: ['every', 'day']},
prices: {foo:111, bar:222},
wacky: {field: 'ThIs Is SoMe WiErD sTuFf'}
}
and you want data that looks like this:
{
flattened_field: 'FOOBAR',
first_in_array: 'thing1',
nested_obj: {
nested: 'toplev',
fields: 'day'
},
prices: [111, 222],
munged_field: 'this is some wierd stuff'
}
Should be an object whose keys are either "dot.separated.paths"
,
or further nested structures.
{
flattened_field: 'some.deeply.nested.field',
first_in_array: 'arr.0',
nested_obj: {
nested: 'top_level_key',
fields: 'other_key.things.1'
},
arr: ['prices.foo', 'prices.bar']
}
If one of your fields needs some additional conversion, you can
use the reformat.convert(path, fn)
function like this:
{
munged_field: reformat.convert('wacky.field', (x) => x.toLowerCase())
}
- better docs
- more thorough tests