taproot

A tree manipulation library.

taproot

Tree manipulation made easy.

npm install taproot

Simply source the file in a script tag:

<script src='lib/taproot.js'></script>

createNode(value, identifier, [parent = null])

Note that you can only have one root node per tree.

tree = new Tree();
tree.createNode('root', 'root');
tree.createNode('1st level', '1a', 'root');
tree.createNode(['value', 'can', 'be', 'anything'], '2a', '1a');
tree.createNode({objects: 'yes'}, '1b', 'root');
tree.createNode('deep nesting', '2b', '1b');

expand([position = tree.root], [mode = 'DEPTH'], [filter = function(identifier) { return true; }])

Supported modes are 'DEPTH' and 'WIDTH'. The result list will be passed through the filter function if one is provided.

tree.expand();
 
// ["root", "1a", "2a", "1b", "2b"] 
 
tree.expand(tree.root, 'WIDTH');
 
// ["root", "1a", "1b", "2a", "2b"] 

reverse(position, [filter = function() { return true; }])

tree.reverse('2b', function(identifier) { return identifier !== 'root'});
 
// ["2b", "1b"] 

toObj([position = tree.root], [nodeKey = 'data'], [childrenKey = 'children'])

tree.toObj();
 
// { 
//   "data": "root", 
//   "children": [ 
//     { 
//       "data": "1st level", 
//       "children": [ 
//         { 
//           "data": [ 
//             "value", 
//             "can", 
//             "be", 
//             "anything" 
//           ] 
//         } 
//       ] 
//     }, 
//     { 
//       "data": { 
//         "objects": "yes" 
//       }, 
//       "children": [ 
//         { 
//           "data": "deep nesting" 
//         } 
//       ] 
//     } 
//   ] 
// } 

subTree(position)

subtree = tree.subTree('1a');
 
// { 
//   "data": "1st level", 
//   "children": [ 
//     { 
//       "data": [ 
//         "value", 
//         "can", 
//         "be", 
//         "anything" 
//       ] 
//     } 
//   ] 
// } 

Removing a node will also remove all of its children.

removeNode(identifier)

tree.removeNode('1a');
 
// { 
//   "data": "root", 
//   "children": [ 
//     { 
//       "data": { 
//         "objects": "yes" 
//       }, 
//       "children": [ 
//         { 
//           "data": "deep nesting" 
//         } 
//       ] 
//     } 
//   ] 
// } 

moveNode(node, destinationParent)

tree.moveNode('2b', 'root');
 
// { 
//   "data": "root", 
//   "children": [ 
//     { 
//       "data": { 
//         "objects": "yes" 
//       } 
//     }, 
//     { 
//       "data": "deep nesting" 
//     } 
//   ] 
// } 

transplant(destinationParent, newTree)

tree = new Tree();
tree.createNode('root', 'root');
tree.createNode('1st level', '1', 'root');
tree.createNode('2nd level', '2', '1');
 
newTree = new Tree();
newTree.createNode('root', 'rootn');
newTree.createNode('1st level new', '1n', 'rootn');
newTree.createNode('2nd level new', '2n', '1n');
 
tree.transplant('1', newTree);
 
// { 
//   "data": "root", 
//   "children": [ 
//     { 
//       "data": "1st level", 
//       "children": [ 
//         { 
//           "data": "2nd level" 
//         }, 
//         { 
//           "data": "root", 
//           "children": [ 
//             { 
//               "data": "1st level new", 
//               "children": [ 
//                 { 
//                   "data": "2nd level new" 
//                 } 
//               ] 
//             } 
//           ] 
//         } 
//       ] 
//     } 
//   ] 
// } 
npm install
npm test

Special thanks to the folks behind pyTree, from which this code is largely derived.