Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

reformat

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

reformat

Reformat javascript objects declaratively

latest
Source
npmnpm
Version
1.0.2
Version published
Maintainers
1
Created
Source

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

FAQs

Package last updated on 22 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts