Tools to manage deeply nested collection tree structures.
Install via
npm install tree-tools
Then just use as
var treeTools = require('tree-tools');
treeTools.flatten(treeTools.find(tree, {path: '/bar/baz'}));
- Include
dist/ngTreeTools.js
somewhere in your build chain - Add
ngTreeTools
as a module in your mainangular.module('app', [modules])
list - Require the module as
TreeTools
in your controller e.g.app.controller('myController', function($scope, TreeTools) { // Controller // })
In all the examples below the example tree structure is similar to the main test case.
Find a single node deeply within a tree structure.
treeTools.find(tree, {path: '/bar/baz'});
Return all branches of a tree as a flat array.
treeTools.flatten(treeTools.find(tree, {path: '/bar/baz'}));
Utility function to deep search a tree structure for a matching query and find parents up to the given query. If found this function will return an array of all generations with the found branch as the last element of the array.
treeTools.parents(tree, {path: '/bar/baz'})
Utility function to deep search a tree structure for a matching query and find all children after the given query. If found this function will return an array of all child elements NOT including the query element.
treeTools.children(tree, {path: '/foo'});
Utility function to determines whether a given node has children.
treeTools.hasChildren(someBranch); // Returns a boolean
Utility function to sort tree by specific property.
treeTools.sortBy(tree, ['id', 'title']); // Returns new tree sorted