@contrast/flat

4.2.0 • Public • Published

Take a nested Javascript object and flatten it. Modified to not traverse into domains.

Installation

$ npm install @contrast/flat

Methods

flatten(original, options)

Flattens the object - it'll return an object one level deep, regardless of how nested the original object was (will not traverse into domains stored on the object):

const flatten = require('flat');

flatten({
    key1: {
        keyA: 'valueI'
    },
    key2: {
        keyB: 'valueII'
    },
    key3: { a: { b: { c: 2 } } }
});

// {
//   'key1.keyA': 'valueI',
//   'key2.keyB': 'valueII',
//   'key3.a.b.c': 2
// }

Options

delimiter

Use a custom delimiter for (un)flattening your objects, instead of ..

safe

When enabled, flat will preserve arrays and their contents. This is disabled by default.

const flatten = require('flat');

flatten({
    this: [
        { contains: 'arrays' },
        { preserving: {
              them: 'for you'
        }}
    ]
}, {
    safe: true
});

// {
//     'this': [
//         { contains: 'arrays' },
//         { preserving: {
//             them: 'for you'
//         }}
//     ]
// }

Readme

Keywords

none

Package Sidebar

Install

npm i @contrast/flat

Weekly Downloads

4,811

Version

4.2.0

License

BSD-3-Clause

Unpacked Size

4.83 kB

Total Files

4

Last publish

Collaborators

  • mhenry-contrast
  • tough-griff
  • michaelwitz
  • contrast_admin
  • jcolekaplan
  • bmacnaughton