flat-wrap

1.0.2 • Public • Published

flat-wrap Build Status

Flattening/unflattening nested objects without losing keys for blank objects / empty arrays

Installation

$ npm install flat-wrap

The catch

A manual updation on the flatten/unflatten logic to mark empty objects ({}) and empty arrays([]) while flattening and replacing them with appropriate values upon unflattening

Methods

flatten(original, options)

Flattens the object - it'll return an object one level deep, regardless of how nested the original object was:

const { flatten } = require('flat-wrap')
 
flatten({
    key1: {
        keyA: 'valueI'
    },
    key2: {
        keyB: 'valueII'
    },
    key3: { a: { b: { c: 2 } } }
})
 
// {
//   'key1.keyA': 'valueI',
//   'key2.keyB': 'valueII',
//   'key3.a.b.c': 2
// }

unflatten(original, options)

const { unflatten } = require('flat-wrap');
 
unflatten({
    'three.levels.deep': 42,
    'three.levels': {
        nested: true
    }
})
 
// {
//     three: {
//         levels: {
//             deep: 42,
//             nested: true
//         }
//     }
// }

Options

delimiter

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

Package Sidebar

Install

npm i flat-wrap

Weekly Downloads

1

Version

1.0.2

License

BSD-3-Clause

Unpacked Size

11.6 kB

Total Files

6

Last publish

Collaborators

  • piyush44903