1.0.2 • Published 7 years ago

reformat v1.0.2

Weekly downloads
6
License
MIT
Repository
github
Last release
7 years ago

reformat

Reformat javascript objects easily

npm install reformat
var reformat = require('reformat')

Usage

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'
}

reformat(output, input)

output: the final output structure you want

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())
}

input: the data to be reformatted


TODO:

  • better docs
  • more thorough tests
1.0.2

7 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.0.3

11 years ago

0.0.2

11 years ago

0.0.1

11 years ago