https://github.com/grantila/node-property-tree-merge
Tree merge for trees of property-wrapped objects
https://github.com/grantila/node-property-tree-merge
Last synced: about 2 months ago
JSON representation
Tree merge for trees of property-wrapped objects
- Host: GitHub
- URL: https://github.com/grantila/node-property-tree-merge
- Owner: grantila
- License: mit
- Created: 2016-05-16T12:09:34.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-05-20T09:26:26.000Z (over 9 years ago)
- Last Synced: 2025-07-31T02:21:04.668Z (3 months ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# property-tree-merge
This package contains a way to merge a tree of normal objects, with a tree of `property` objects. The result is a tree of `property` objects. When the new merged tree is created, each changed `property` object will notify to its listener that changes have occured.
The API contains pre-defined default functions for getting a list of children per node, for merging nodes, etc, but they can all be customized.
## API
```js
var propertyTreeMerge = require( 'property-tree-merge' );dest propertyTreeMerge( dest, src, opts = { } );
```The `propertyTreeMerge` function takes a property tree as `dest` (which will be in-place modified) and a normal object tree `src`. The `dest` can be `null`, in which case a new property tree is returned.
The `opts` is an optional object defining options to the merge:
```js
sorter: ( a, b ) => a - b // Comparison function for sort()
identifyer: obj => obj.id // Function which returns a unique ID for the
// node (must be unique per shared parent-
// node, not necessary in the entire tree)
changed: ( a, b ) => ... // Function which checks if a and b have
// diverged and therefore needs to be merged
childRefs: obj => obj.children // Function returning the children array per
// node
merge: ( dest, src ) => ... // Merge node src into dest, but not the
// children. property-tree-merge handles the
// children
```## Usage
### Create a new property tree from a normal object tree:
```js
var newPropertyTree = propertyTreeMerge( null, tree );
```### Merge trees
```js
/*var propertyTree = */ propertyTreeMerge( propertyTree, tree );
// The return value is the same as propertyTree, since merging is in-place.
```