treeish
v2.0.1
Published
An array to tree generator with utility functions.
Downloads
2
Maintainers
Readme
An array to tree generator utility.
Install
$ npm install treeish
Usage
Building the tree
import TreeUtil from 'treeish';
const collection = [
{ id: '3', name: 'C', x: '1', selected: true, type: 'campaign' },
{ id: '6', name: 'F', x: '3', selected: true, type: 'adset' },
{ id: '1', name: 'A', selected: true, type: 'mediaplan' },
{ id: '2', name: 'B', selected: true, type: 'mediaplan' },
{ id: '7', name: 'G', x: '4', selected: true, type: 'adset' },
{ id: '4', name: 'D', x: '1', selected: true, type: 'campaign' },
{ id: '5', name: 'E', x: '2', selected: true, type: 'campaign' },
{ id: '8', name: 'G', x: '6', selected: true, type: 'ad' },
{ id: '9', name: 'H', x: '6', selected: true, type: 'ad' },
{ id: '10', name: 'I', x: '7', selected: true, type: 'ad' },
{ id: '11', name: 'J', x: '7', selected: true, type: 'ad' },
];
const treeUtil = new TreeUtil(collection, { parentId: 'x' }); //parentId key is 'x'
Tree utility functions
const node = treeUtil.findNodeById('2') // returns the node with ID 2
const nodes = treeUtil.findAllNodesByProperty('type', 'campaign')
/* nodes will contain
* [
* { id: '3', name: 'C', x: '1', selected: true, type: 'campaign' },
* { id: '4', name: 'D', x: '1', selected: true, type: 'campaign' },
* { id: '5', name: 'E', x: '2', selected: true, type: 'campaign' }
* ]
*/
const nestedNodes = treeUtil.getNestedNodesByProperty(
{ id: '2', name: 'B', selected: true, type: 'mediaplan' },
'type',
'campaign');
/* nestedNodes will contain
* [
* { id: '5', name: 'E', x: '2', selected: true, type: 'campaign' }
* ]
*/
const depth = treeUtil.maxDepth(); //depth is 4
treeUtil.updateNodes(
treeUtil.children[1].children, //pass collection of nodes
function(node, level){ node.model.selected = !node.model.selected; }
)
//will update all children of the second child of the root (node id: 2)
TODOs
- Add node creation and removal functionalities