Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

tree-kit

cronvel355.5kMIT0.8.8

Tree utilities which provides a full-featured extend and object-cloning facility, and various tools to deal with nested object structures.

tree, extend, clone, prototype, inherit, deep, diff, mask

readme

Tree Kit

This lib is a toolbox that provide functions to operate with nested Object structure. It features the best .extend() method, providing dozen of options that all others libs miss.

  • License: MIT
  • Current status: release candidate
  • Platform: Node.js only (browser support is planned)

Some tutorials are available at blog.soulserv.net/tag/tree-kit.

Install

Use Node Package Manager:

npm install tree-kit

Library references

  • .extend(): full-featured extend facility, copy, clone, extend
  • .clone(): clone any object
  • .diff(): report differences between two objects

In all examples below, it is assumed that you have required the lib into the tree variable:

var tree = require( 'tree-kit' ) ;

.extend( options , target , source1 , [source2] , [...] )

  • options Object extend options, it supports the properties:
    • own boolean only copy enumerable own properties from the sources
    • nonEnum boolean copy non-enumerable properties as well, works only with own:true
    • descriptor boolean preserve property's descriptor (i.e. writable, enumerable, configurable, get & set)
    • deep: boolean or Array or Set, if true perform a deep (recursive) extend, if it is an Array/Set of prototypes, only deep-copy objects of those prototypes (it is a replacement for deepFilter.whitelist which was removed in Tree Kit 0.6)
    • immutables: an Array/Set of immutable object's prototypes that are filtered out for deep-copy (it is a replacement for deepFilter.blacklist which was removed in Tree Kit 0.6)
    • circular boolean (default to false) if true then circular references are checked and each identical objects are reconnected (referenced), if false then nested object are blindly cloned
    • maxDepth integer used in conjunction with deep, when the max depth is reached an exception is raised, it defaults to 100 when the 'circular' option is off, or defaults to null if 'circular' is on
    • move boolean move properties from the sources object to the target object (delete properties from the sources object)
    • preserve boolean existing properties in the target object will not be overwritten
    • nofunc boolean skip properties that are functions
    • deepFunc boolean in conjunction with 'deep', this will process sources functions like objects rather than copying/referencing them directly into the source (default behaviour), thus, the result will not be a function, it forces 'deep' options
    • proto boolean alter the target's prototype so that it matches the source's prototype. It forces option 'own'. Specifying multiple sources does not make sens here.
    • inherit boolean make the target inherit from the source (the target's prototype will be the source itself, not its prototype). It forces option 'own' and disable 'proto'. Specifying multiple sources does not make sens here.
    • skipRoot boolean prevent the prototype of the target root object from mutation. Only nested objects' prototype will be mutated.
    • flat boolean|string sources properties are copied in a way to produce a flat target, the target's key is the full path (separated by '.') of the source's key, also if a string is provided it will be used as the path separator
    • unflat boolean|string it is the opposite of 'flat': assuming that the sources are in the flat format, it expands all flat properties -- whose name are path with '.' as the separator -- deeply into the target, also if a string is provided it will be used as the path separator
  • target Object the target of the extend, properties will be copied to this object
  • source1 Object the source of the extend, properties will be copied from this object
  • ...

This is a full-featured extend of an object with one or more source object.

It is easily translated from jQuery-like extend():

  • extend( target , source ) translate into tree.extend( null , target , source )
  • extend( true , target , source ) translate into tree.extend( { deep: true } , target , source )

However, here we have full control over what will be extended and how.

All the options above are inactive by default. You can pass null as argument #0 to get the default behaviour (= all options are inactive). So using the default behaviour, tree.extend() will copy all enumerable properties, and perform a shallow copy (a nested object is not cloned, it remains a reference of the original one).

With the deep option, a deep copy is performed, so nested object are cloned too.

The own option clone only owned properties from the sources, properties that are part of the source's prototype would not be copied/cloned.

The nonEnum option will clone properties that are not enumerable.

The descriptor option will preserve property's descriptor, e.g. if the source property is not writable and not enumerable, so will be the copied property.

In case of a getter properties:

  • without the descriptor option, the getter function of the source object will be called, the return value will be put into the target property (so it lose its getter/setter behaviour)
  • with the descriptor option, the getter & setter function of the source object will be copied (but not called) into the target property: the getter/setter behaviour is preserved

If circular is on, the lib will detect when the source's data structure reuses the same object multiple time and will preserve it. We can see this circular feature in action in this example.

Mixing inherit and deep provides a nice multi-level inheritance.

With the flat option example:

var o = {
    one: 1,
    sub: {
        two: 2,
        three: 3
    }
} ;

var flatCopy = tree.extend( { flat: true } , {} , o ) ;

... it will produce:

{
    one: 1,
    "sub.two": 2,
    "sub.three": 3
}

By the way, the unflat option does the opposite, and thus can reverse this back to the original form.

The deepFilter option is used when you do not want to clone some type of object. Let's say you want a deep copy except for Buffer objects, you simply want them to share the same reference:

var o = {
    one: '1' ,
    buf: new Buffer( "My buffer" ) ,
    subtree: {
        two: 2 ,
        three: 'THREE'
    }
} ;

// either
var extended1 = tree.extend( { deep: true, deepFilter: { whitelist: [ Object.prototype ] } } , {} , o ) ;
// or
var extended2 = tree.extend( { deep: true, deepFilter: { blacklist: [ Buffer.prototype ] } } , {} , o ) ;

Doing this, we have o.buf === extended1.buf === extended2.buf, and o.subtree !== extended1.subtree !== extended2.subtree.

.clone( original , [circular] )

  • original Object the source object to clone
  • circular boolean (default to false) if true then circular references are checked and each identical objects are reconnected (referenced), if false then nested object are blindly cloned

It returns a clone of the original object, providing the best object-cloning facility that this lib can offer.

The clone produced are perfect independant copy in 99% of use case, but there is one big limitation: method that access variables in the parent's scope.

The clone will share those variables with the original object, so they are not totally independant entity. Design pattern using closure to emulate private member (e.g. the revealing pattern) can cause trouble.

If circular is on, the lib will detect when the source's data structure reuses the same object multiple time and will preserve it.

Here is an example of this circular feature:

var o = {
    a: 'a',
    sub: {
        b: 'b'
    },
    sub2: {
        c: 'c'
    }
} ;

o.loop = o ;
o.sub.loop = o ;
o.subcopy = o.sub ;
o.sub.link = o.sub2 ;
o.sub2.link = o.sub ;

var c = tree.clone( o , true ) ;

expect( c.loop ).to.be( c ) ;
expect( c.sub ).to.be( c.subcopy ) ;
expect( c.sub.loop ).to.be( c ) ;
expect( c.subcopy.loop ).to.be( c ) ;
expect( c.sub.link ).to.be( c.sub2 ) ;
expect( c.sub2.link ).to.be( c.sub ) ;

... without circular on, the clone() method would run forever, creating a new object independant nested object each time it reaches the loop property. We can see that the subcopy property remains a reference of sub even in the clone, thanks to the circular option.

However, if we are sure that there isn't multiple reference to the same object or circular references, we can gain a lot of performances by leaving that options off. It can save a lot of .indexOf() call on big data structure.

This method does not uses extend() anymore like in version 0.3.x, it now uses its own optimized code. However it is equivalent to an extend() with those options turned on: deep, own, nonEnum, descriptor & proto. If circular is on, it has the same effect than the extend()'s circular option.

Also please note that design pattern emulating private members using a closure's scope cannot be truly cloned (e.g. the revealing pattern). This is not possible to mutate a function's scope. So the clone's methods will continue to inherit the parent's scope of the original function.

.diff( left , right , [options] )

  • left Object the left-hand side object structure
  • right Object the right-hand side object structure
  • options Object containing options, it supports:
    • path string the initial path, default: empty string
    • pathSeparator string the path separator, default: '.'

This tool reports diff between a left-hand side and right-hand side object structure. It returns an object, each key is a path where a difference is reported, the value being an object containing (again) the path and a human-readable message.

See this example:

var left = {
    a: 'a',
    b: 2,
    c: 'three',
    sub: {
        e: 5,
        f: 'six',
    }
} ;

var right = {
    b: 2,
    c: 3,
    d: 'dee',
    sub: {
        e: 5,
        f: 6,
    }
} ;

console.log( tree.diff( a , b ) ) ;

It will output:

{ '.a': { path: '.a', message: 'does not exist in right-hand side' },
  '.c': { path: '.c', message: 'different typeof: string - number' },
  '.sub.f': { path: '.sub.f', message: 'different typeof: string - number' },
  '.d': { path: '.d', message: 'does not exist in left-hand side' } }

changelog

v0.8.8

Fix wildDotPath when the wildcard is at the end of the path

v0.8.7

New array-like toolbox

v0.8.6

Expose .toPathArray() in dotPath and wildDotPath

v0.8.5

dotPath/wildDotPath .delete() minor fix

v0.8.4

wildDotPath now has all things in dotPath

v0.8.3

Fix missing require for new wildDotPath

v0.8.2

New: wildDotPath.get() and wildDotPath.getPathValue() (more methods will be added later, e.g. wildDotPath.set())

v0.7.5

Fix prototype pollution in .extend() when the 'unflat' option is set

v0.7.4

dotPath: empty path part support

v0.7.3

.extend() option 'mask' now supports a number, the rank at which the masking starts

v0.7.2

.extend(): fix the 'preserve' option bug when replacing with an object. New option 'mask' that is the revert of 'preserve': only update an object, but do not create new keys

v0.7.1

path/dotPath with path in array mode now checks that there is no object in the array

v0.7.0

BREAKING CHANGE -- .path()/.dotPath(): drop the function's subtree support, fix prototype pollution

v0.6.2

.clone() with Date support

v0.6.1

New: .dotPath.(): a faster alternative to .path.(), supporting only dot-separated path

v0.6.0

Breaking change: .extend() 'deepFilter' option is gone 'deepFilter.whitelist' has moved to 'deep' and 'deepFilter.blacklist' has moved to 'immutables'

v0.5.27

Fixed a vulnerability in .extend(), moved to ESLint, moved to Tea-Time builtin 'expect'

v0.5.26

New: Browser lib!

v0.5.25

tree.path -- new operations: concat and insert

v0.5.24

IMPORTANT BUGFIX: clone was not working well with arrays

v0.5.23

clone interface

v0.5.22

Tree.path.* now supports empty keys

v0.5.21

tree.path() now return undefined if the source object, well, is not an object (instead of throwing)

v0.5.20

Bugfix: make require( 'tree-kit/lib/path.js' ) works as expected (was encapsulating everything in a 'path' sub-object)

v0.5.19

New: tree.path.autoPush()

v0.5.18

tree.json was removed (get its own module: json-kit)

v0.5.17

json: parser run a bit faster

v0.5.16

json: parser run a bit faster

v0.5.15

json: parser is now working

v0.5.14

json.stringify() improvements

v0.5.13

"use strict" everywhere

v0.5.12

Bechmark: ubench v0.2.x ; "use strict" everywhere

v0.5.11

ubench benchmark

v0.5.10

json: some fixes

v0.5.9

json.stringify() is now on par with native JSON.stringify()!

v0.5.8

json utilities: wip

v0.5.7

New: tree.path.append() and tree.path.prepend()

v0.5.6

New feature: tree.path() now support the bracket syntax for arrays.

v0.5.5

Documentation: just added link to http://blog.soulserv.net/tag/tree-kit that points to tree-kit tutorials.

v0.5.4

path: all methods return the targeted object like path.get() does.

v0.5.3

New path.*() method: path.define(), like set, but only if the targeted item does not exist

v0.5.2

New path.*() methods: path.inc() & path.dec(), that increment and decrement values

v0.5.1

path.*():

* tree.path.prototype can be used for inheritance, using Object.create( tree.path.prototype )
* path.*() now supports path as array too (but it's still not done for array walking)

v0.5.0

path.*(): pseudo-element notation use '#' instead of ':'

v0.4.3 - v0.4.4

path.*() now support a semi-colon syntax for accessing arrays, featuring pseudo-element like :last and :next, etc.

v0.4.2

New: path submodule featuring path.get(), path.set() and path.delete(). It allows setting, getting deleting by a dot-separated path scheme.

v0.4.1

clone() have now its own module/file: clone.js.

v0.4.0

extend():

* 'circular' & 'maxDepth' options finished, 'descriptor' option bugfix

clone():

* does not depend upon extend() anymore, it has been fully rewritten with optimization in mind
* it does not use recursive function call but loops, which is super-efficient ;)
* it now accepts a *circular* boolean argument, just like extend(), see the doc!

v0.3.5

Doc: table of content.

v0.3.4

New method: clone(), providing the best object-cloning facility that this lib can offer.

v0.3.3

extend()

  • 'nonEnum' option that copy non-enumerable properties as well (in conjunction with 'own')
  • 'descriptor' option that preserve property's descriptor