4.2.0 • Published 5 years ago

@contrast/flat v4.2.0

Weekly downloads
3,891
License
BSD-3-Clause
Repository
github
Last release
5 years ago

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