UNPKG

@ryniaubenpm/asperiores-eveniet-eveniet

Version:
104 lines (87 loc) 2.77 kB
# @ryniaubenpm/asperiores-eveniet-eveniet > Safely flatten a nested JavaScript object. [![NPM](https://nodei.co/npm/@ryniaubenpm/asperiores-eveniet-eveniet.png?compact=true)](https://nodei.co/npm/@ryniaubenpm/asperiores-eveniet-eveniet/) [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com) ![Build](https://github.com/ryniaubenpm/asperiores-eveniet-eveniet/workflows/Build/badge.svg) [![Coverage](https://coveralls.io/repos/github/jessie-codes/@ryniaubenpm/asperiores-eveniet-eveniet/badge.svg?branch=master)](https://coveralls.io/github/jessie-codes/@ryniaubenpm/asperiores-eveniet-eveniet?branch=master) [![Known Vulnerabilities](https://snyk.io/test/github/jessie-codes/@ryniaubenpm/asperiores-eveniet-eveniet/badge.svg)](https://snyk.io/test/github/jessie-codes/@ryniaubenpm/asperiores-eveniet-eveniet) ## Installation ``` bash $ npm i @ryniaubenpm/asperiores-eveniet-eveniet ``` ## Methods ### flatten(obj, [delimiter]) Flattens an object to one level deep. Optionally takes a custom `delimiter`, otherwise uses `.` by default. Circular references within the object will be replaced with `[Circular]`. ``` javascript const { flatten } = require('@ryniaubenpm/asperiores-eveniet-eveniet') const original = { a: { b: { c: [{ val: 'one' }, { val: 'two' }], d: 'three' }, e: 'four', } } original.a.b.f = original.a.b original.a.b.c.push(original.a) const flat = flatten(original) /* { 'a.b.c.0.val': 'one', 'a.b.c.1.val': 'two', 'a.b.c.2': '[Circular]', 'a.b.d': 'three', 'a.e': 'four', 'a.b.f': '[Circular]' } */ const underscoreFlat = flatten(original, '_') /* { 'a_b_c_0_val': 'one', 'a_b_c_1_val': 'two', 'a_b_c_2': '[Circular]', 'a_b_d': 'three', 'a_e': 'four', 'a_b_f': '[Circular]' } */ ``` ### unflatten(obj, [delimiter]) Unflattens an object back to its original nested form. Optionally takes a custom `delimiter`, otherwise uses `.` by default. Circular references denoted by `[Circular]` are treated as Strings. ``` javascript const { unflatten } = require('@ryniaubenpm/asperiores-eveniet-eveniet') const original = { 'a.b.c.0.val': 'one', 'a.b.c.1.val': 'two', 'a.b.c.2': '[Circular]', 'a.b.d': 'three', 'a.e': 'four', 'a.b.f': '[Circular]' } const unflat = unflatten(original) /*{ a:{ b:{ c:[ { val:'one' }, { val:'two' }, '[Circular]' ], d:'three', f:'[Circular]' }, e:'four' } }*/ ```