Manipulate and traverse tree-like structures in javascript.

npm install tree-model
1 downloads in the last day
3 downloads in the last week
654 downloads in the last month

<p>Manipulate and traverse tree-like structures in javascript.</p>

<p>For download and demos, please <a href="">visit TreeModel website</a>.</p>

<p><a href="
[![Bitdeli Badge](](" title="Bitdeli Badge"><img alt="Build Status" src=""/></a></p>



<p>TreeModel is available as an npm module so you can install it with <code>npm install tree-model</code> and use it in your script:</p>

var TreeModel = require(&#39;tree-model&#39;),
    tree = new TreeModel(),
    root = tree.parse({name: &#39;a&#39;, children: [{name: &#39;b&#39;}]});


<h4>Using <a href="">requirejs</a></h4>

&lt;script src=&quot;path/to/require.js&quot;&gt;&lt;/script&gt;
    require([&quot;path/to/TreeModel&quot;], function(TreeModel) {
        var tree = new TreeModel(),
            root = tree.parse({name: &#39;a&#39;, children: [{name: &#39;b&#39;}]});

<h4>As a global variable</h4>

&lt;script src=&quot;path/to/TreeModel.js&quot;&gt;&lt;/script&gt;
    var tree = new TreeModel(),
        root = tree.parse({name: &#39;a&#39;, children: [{name: &#39;b&#39;}]});

<h2>API Reference</h2>

<h4><code>var tree = new TreeModel(options)</code></h4>

<p>&lt;p&gt;Create a new TreeModel with the given options.&lt;/p&gt;</p>

<p>Valid properties for the options object are:</p>

<ul><li><code>childrenPropertyName</code> - The name for the children array property. Default is <code>children</code>;</li><li><code>modelComparatorFn</code> - A comparator function to sort the children when parsing the model and adding children. The default order policy is to keep the parsed order and append new children. The comparator function receives the model for two nodes just like the <a href="">Array.sort</a> function. The provided sort algorithm is <strong>stable</strong>.</li></ul>

<h4><code>Node tree.parse(model)</code></h4>

<p>&lt;p&gt;Parse the given user defined model and return the root Node object.&lt;/p&gt;</p>

<h4><code>Boolean node.isRoot()</code></h4>

<p>&lt;p&gt;Return <code>true</code> if this Node is the root, <code>false</code> otherwise.&lt;/p&gt;</p>

<h4><code>Node node.addChild(node)</code></h4>

<p>&lt;p&gt;Add the given node as child of this one. Return the child Node.&lt;/p&gt;</p>

<h4><code>Array&lt;Node&gt; node.getPath()</code></h4>

<p>&lt;p&gt;Get the array of Nodes representing the path from the root to this Node (inclusive).&lt;/p&gt;</p>

<h4><code>Node node.drop()</code></h4>

<p>&lt;p&gt;Drop the subtree starting at this node. Returns the node itself, which is now a root node.&lt;/p&gt;</p>

<h4><code>Node node.first(predicate)</code></h4>

<p>&lt;p&gt;Starting from this node, find the first Node that matches the predicate and return it.&lt;/p&gt;&lt;p&gt;The <strong>predicate</strong> is a function wich receives the visited Node and returns <code>true</code> if the </p>

<p>Node should be picked and <code>false</code> otherwise.&lt;/p&gt;</p>

<h4><code>Array&lt;Node&gt; node.all(predicate)</code></h4>

<p>&lt;p&gt;Starting from this node, find all Nodes that match the predicate and return these.&lt;/p&gt;</p>


<p>&lt;p&gt;Starting from this node, traverse the subtree calling the action for each visited node. The action is a function which receives the visited Node as argument. The traversal can be halted by returning <code>false</code> from the action.&lt;/p&gt;
&lt;br /&gt;</p>

<p><strong>Note</strong> - <code>first</code>, <code>all</code> and <code>walk</code> can optionally receive as first argument an object with traversal options. Currently the only supported option is the traversal <code>strategy</code> which can be any of the following:</p>

<ul><li><code>{strategy: &#39;pre&#39;}</code> - Depth-first pre-order <em>[default]</em>;</li><li><code>{strategy: &#39;post&#39;}</code> - Depth-first post-order;</li><li><code>{strategy: &#39;breadth&#39;}</code> - Breadth-first.</li></ul>


<h4>Running Tests</h4>

<p>The tests require npm libraries mocha, chai, and sinon. To install them run: <code>npm install</code></p>

<p>To run tests, type: <code>npm test</code></p>

<h4>Code Linting</h4>

<p>Install <a href="">jshint</a>: <code>npm install -g jshint</code></p>

<p>Check src and tests: <code>jshint src/TreeModel.js test/test.js</code></p>
npm loves you