1.0.2 • Published 6 years ago

flat-wrap v1.0.2

Weekly downloads
2
License
BSD-3-Clause
Repository
github
Last release
6 years ago

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 ..