Gathering detailed insights and metrics for tree-kit
Gathering detailed insights and metrics for tree-kit
Gathering detailed insights and metrics for tree-kit
Gathering detailed insights and metrics for tree-kit
npm install tree-kit
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
29 Stars
161 Commits
7 Forks
4 Watching
1 Branches
1 Contributors
Updated on 20 Jun 2024
JavaScript (98.49%)
Makefile (1.51%)
Cumulative downloads
Total Downloads
Last day
-1%
21,728
Compared to previous day
Last week
-9.9%
117,921
Compared to previous week
Last month
4.6%
546,162
Compared to previous month
Last year
41.6%
6,026,456
Compared to previous year
2
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.
Some tutorials are available at blog.soulserv.net/tag/tree-kit.
Use Node Package Manager:
npm install tree-kit
In all examples below, it is assumed that you have required the lib into the tree
variable:
1var tree = require( 'tree-kit' ) ;
Object
extend options, it supports the properties:
boolean
only copy enumerable own properties from the sourcesboolean
copy non-enumerable properties as well, works only with own:trueboolean
preserve property's descriptor (i.e. writable, enumerable, configurable, get & set)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)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 clonedinteger
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 onboolean
move properties from the sources object to the target object (delete properties from the sources object)boolean
existing properties in the target object will not be overwrittenboolean
skip properties that are functionsboolean
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' optionsboolean
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.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.boolean
prevent the prototype of the target root object from mutation.
Only nested objects' prototype will be mutated.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 separatorboolean|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 separatorObject
the target of the extend, properties will be copied to this objectObject
the source of the extend, properties will be copied from this objectThis 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:
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:
1var o = { 2 one: 1, 3 sub: { 4 two: 2, 5 three: 3 6 } 7} ; 8 9var flatCopy = tree.extend( { flat: true } , {} , o ) ;
... it will produce:
1{ 2 one: 1, 3 "sub.two": 2, 4 "sub.three": 3 5}
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:
1var o = { 2 one: '1' , 3 buf: new Buffer( "My buffer" ) , 4 subtree: { 5 two: 2 , 6 three: 'THREE' 7 } 8} ; 9 10// either 11var extended1 = tree.extend( { deep: true, deepFilter: { whitelist: [ Object.prototype ] } } , {} , o ) ; 12// or 13var 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
.
Object
the source object to cloneboolean
(default to false) if true then circular references are checked and each identical objects are reconnected
(referenced), if false then nested object are blindly clonedIt 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:
1var o = { 2 a: 'a', 3 sub: { 4 b: 'b' 5 }, 6 sub2: { 7 c: 'c' 8 } 9} ; 10 11o.loop = o ; 12o.sub.loop = o ; 13o.subcopy = o.sub ; 14o.sub.link = o.sub2 ; 15o.sub2.link = o.sub ; 16 17var c = tree.clone( o , true ) ; 18 19expect( c.loop ).to.be( c ) ; 20expect( c.sub ).to.be( c.subcopy ) ; 21expect( c.sub.loop ).to.be( c ) ; 22expect( c.subcopy.loop ).to.be( c ) ; 23expect( c.sub.link ).to.be( c.sub2 ) ; 24expect( 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.
Object
the left-hand side object structureObject
the right-hand side object structureObject
containing options, it supports:
string
the initial path, default: empty stringstring
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:
1var left = { 2 a: 'a', 3 b: 2, 4 c: 'three', 5 sub: { 6 e: 5, 7 f: 'six', 8 } 9} ; 10 11var right = { 12 b: 2, 13 c: 3, 14 d: 'dee', 15 sub: { 16 e: 5, 17 f: 6, 18 } 19} ; 20 21console.log( tree.diff( a , b ) ) ;
It will output:
1{ '.a': { path: '.a', message: 'does not exist in right-hand side' }, 2 '.c': { path: '.c', message: 'different typeof: string - number' }, 3 '.sub.f': { path: '.sub.f', message: 'different typeof: string - number' }, 4 '.d': { path: '.d', message: 'does not exist in left-hand side' } }
The latest stable version of the package.
Stable Version
1
9.8/10
Summary
tree-kit Prototype Pollution vulnerability
Affected Versions
< 0.7.5
Patched Versions
0.7.5
1
7.8/10
Summary
tree-kit vulnerable to Prototype Pollution
Affected Versions
< 0.7.0
Patched Versions
0.7.0
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn Moreast-kit
A toolkit for easy Babel AST generation and manipulation.
@sanity/preview-kit
General purpose utils for live content and visual editing
@dnd-kit/core
dnd kit – a lightweight React library for building performant and accessible drag and drop experiences
@dnd-kit/accessibility
A generic toolkit to help with accessibility