svenschoenung/object-path-expand


Expand JS objects containing deep path properties

License: MIT

Language: JavaScript


npm Version Build Status Coverage Status Dependency Status devDependency Status Code Climate

object-path-expand

Expand JS objects containing deep path properties.

Installation

npm install object-path-expand --save

Usage

> var expand = require('object-path-expand')

Deep paths in object properties are expanded (using object-path):

> expand({ 'foo.bar' : 42 })
{ 'foo' : { 'bar': 42 } }
> expand({ 'foo.2.bar' : 42 })
{ 'foo': [ , , { 'bar': 42 } ] }
> expand({ 'foo.bar': 42, 'foo.quux': 23 })
{ 'foo': { 'bar': 42, 'quux': 23 } }

Expanded objects are recursively merged (using merge):

> expand({ 'foo' : { 'bar': 42 }, 'foo.quux': 23 })
{ 'foo': { 'bar': 42, 'quux': 23 } }
> expand({ 'foo.quux': 23, 'foo': { 'bar': 42 } })
{ 'foo': { 'bar': 42, 'quux': 23 } }

HERE BE DRAGONS

Since iteration order for properties of JS objects is unspecified and can differ between JS implementations, strange things might happen if two different paths refer to the same object:

> expand({ 'foo.bar': { 'quux': 42 }, 'foo': { 'bar.quux': 23 } })
// Depending on the JS engine this could result in either of these:
// { 'foo': { 'bar': { 'quux': 42 } } }
// { 'foo': { 'bar': { 'quux': 23 } } }

> expand({ 'foo': { 'bar.quux': 23 }, 'foo.bar': { 'quux': 42 } })
// Switching the order of the two properties might or might not
// yield a different result than the previous one above

Even stranger things might happen if two things cannot be sensibly merged:

> expand({ 'foo': [ 'a', 'b' ], 'foo.bar': 23 })
// Depending on the JS engine this could result in either of these:
// { 'foo': { '0': 'a', '1': 'b', 'bar': 23 } }
// { 'foo': { 'bar': 23 } }

> expand({ 'foo.bar': 23, 'foo': [ 'a', 'b' ] })
// Again, switching the order of the two properties above
// might yield a different result than it did previously

Proceed at your own peril.

License

MIT

Project Statistics

Sourcerank 2
Repository Size 5.86 KB
Stars 0
Forks 0
Watchers 1
Open issues 0
Dependencies 7
Contributors 1
Tags 1
Created
Last updated
Last pushed

Top Contributors See all

Sven Schoenung

Packages Referencing this Repo

object-path-expand
Expand JS objects containing deep path properties
Latest release 0.1.0 - Published

Recent Tags See all

v0.1.0 February 05, 2016

Something wrong with this page? Make a suggestion

Last synced: 2019-05-03 21:39:21 UTC

Login to resync this repository