@mojule/flatten

0.1.3 • Public • Published

flatten

Flattens and expands nested objects

Install

npm install @mojule/flatten

Example

const flatten = require( '@mojule/flatten' )
const obj = {
  person: {
    firstName: 'Nik',
    lastName: 'Coughlin'
  }
}

const { expand, match } = flatten

const firstName = match( obj, 'person.firstName' )

// "Nik"
console.log( firstName )

const flat = flatten( obj )

/*
{
  "person.firstName": "Nik",
  "person.lastName": "Coughlin"
}
*/
console.log( flat )

const expanded = expand( flat )

/*
{
  "person": {
    "firstName": "Nik",
    "lastName": "Coughlin"
  }
}
*/
console.log( expanded )

You can also require flatten, expand, match and parse on their own:

const flatten = require( '@mojule/flatten/dist/flatten' )
const expand = require( '@mojule/flatten/dist/expand' )
const match = require( '@mojule/flatten/dist/match' )
const parse = require( '@mojule/flatten/dist/parse' )

Usage

Handles objects and arrays. Probably fails on property names that contain square brackets or dots - todo.

/@mojule/flatten/

    Package Sidebar

    Install

    npm i @mojule/flatten

    Weekly Downloads

    1

    Version

    0.1.3

    License

    MIT

    Last publish

    Collaborators

    • nrkn
    • andybell