Skip to content
This repository has been archived by the owner on Nov 5, 2020. It is now read-only.
/ flat-wrap Public archive

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

License

Notifications You must be signed in to change notification settings

piyush44903/flat-wrap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published