Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

flatley

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

flatley

Take a nested Javascript object and flatten it, or unflatten an object with delimited keys

  • 5.1.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

flatley Build Status

Take a nested Javascript object and flatten it, or unflatten an object with delimited keys.

Credits

Based on 'flatley' by Hugh Kennedy (http://hughskennedy.com)

Installation

$ npm install flatley

Methods

flatten(original, options)

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

var flatten = require('flatley')

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)

Flattening is reversible too, you can call flatten.unflatten() on an object:

var unflatten = require('flatley').unflatten

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

safe

When enabled, both flat and unflatten will preserve arrays and their contents. This is disabled by default.

var flatten = require('flatley')

flatten({
    this: [
        { contains: 'arrays' },
        { preserving: {
              them: 'for you'
        }}
    ]
}, {
    safe: true
})

// {
//     'this': [
//         { contains: 'arrays' },
//         { preserving: {
//             them: 'for you'
//         }}
//     ]
// }

object

When enabled, arrays will not be created automatically when calling unflatten, like so:

unflatten({
    'hello.you.0': 'ipsum',
    'hello.you.1': 'lorem',
    'hello.other.world': 'foo'
}, { object: true })

// hello: {
//     you: {
//         0: 'ipsum',
//         1: 'lorem',
//     },
//     other: { world: 'foo' }
// }

overwrite

When enabled, existing keys in the unflattened object may be overwritten if they cannot hold a newly encountered nested value:

unflatten({
    'TRAVIS': 'true',
    'TRAVIS_DIR': '/home/travis/build/kvz/environmental'
}, { overwrite: true })

// TRAVIS: {
//     DIR: '/home/travis/build/kvz/environmental'
// }

Without overwrite set to true, the TRAVIS key would already have been set to a string, thus could not accept the nested DIR element.

This only makes sense on ordered arrays, and since we're overwriting data, should be used with care.

maxDepth

Maximum number of nested objects to flatten.

var flatten = require('flatley')

flatten({
    key1: {
        keyA: 'valueI'
    },
    key2: {
        keyB: 'valueII'
    },
    key3: { a: { b: { c: 2 } } }
}, { maxDepth: 2 })

// {
//   'key1.keyA': 'valueI',
//   'key2.keyB': 'valueII',
//   'key3.a': { b: { c: 2 } }
// }

coercion

Optionally run a test/set of tests on your incoming key/value(s) and transform the resulting value if it matches.

This is particularly useful in the case of transforming https://www.npmjs.com/package/mongoose ObjectIds

var ObjectId = mongoose.Types.ObjectId

var coercion = [{
    test: function (key, value) { return key === '_id' && ObjectId.isValid(value) }
    transform: function (value) { return value.valueOf() }
}]
var options = { coercion: coercion }

flatten({
    group1: {
        prop1: ObjectId('aaabbbcccdddeee')
    }
}, options)

// {
//    'group1.prop1': 'aaabbbcccdddeee'
// }

Keywords

FAQs

Package last updated on 08 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc