Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
tree-model-improved
Advanced tools
Readme
Manipulate and traverse tree-like structures in javascript.
For download and demos, please visit TreeModel website.
TreeModel is available as an npm module so you can install it with npm install tree-model
and use it in your script:
var TreeModel = require('tree-model'),
tree = new TreeModel(),
root = tree.parse({name: 'a', children: [{name: 'b'}]});
Type definitions are already bundled with the package, which should just work with npm install.
You can maually find the definition files in the types
folder.
Visit TreeModel website to download browser-ready bundles.
If you have any doubt using this library please post a question on stackoverflow tagged with treemodel
.
Create a new TreeModel with the given options.
var tree = new TreeModel(options)
Valid properties for the options object are:
childrenPropertyName
- The name for the children array property. Default is children
;modelComparatorFn
- 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 Array.sort function. The provided sort algorithm is stable.Parse the given user defined model and return the root Node object.
Node tree.parse(model)
Return true
if this Node is the root, false
otherwise.
Boolean node.isRoot()
Return true
if this Node has one or more children, false
otherwise.
Boolean node.hasChildren()
Add the given node as child of this one. Return the child Node.
Node parentNode.addChild(childNode)
Add the given node as child of this one at the given index. Return the child Node.
Node parentNode.addChildAtIndex(childNode, index)
Sets the index of the node among its siblings to the given value. Return the node itself.
Node node.setIndex(index)
Gets the index of the node relative to its siblings. Return the index value.
Int node.getIndex()
Get the array of Nodes representing the path from the root to this Node (inclusive).
Array<Node> node.getPath()
Drop the subtree starting at this node. Returns the node itself, which is now a root node.
Node node.drop()
Warning - Dropping a node while walking the tree is not supported. You must first collect the nodes to drop using one of the traversal functions and then drop them. Example:
root.all( /* predicate */ ).forEach(function (node) {
node.drop();
});
Starting from this node, find the first Node that matches the predicate and return it. The predicate is a function wich receives the visited Node and returns true
if the Node should be picked and false
otherwise.
Node node.first(predicate)
Starting from this node, find all Nodes that match the predicate and return these.
Array<Node> node.all(predicate)
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 false
from the action.
node.walk([options], action, [context])
Note - first
, all
and walk
can optionally receive as first argument an object with traversal options. Currently the only supported option is the traversal strategy
which can be any of the following:
{strategy: 'pre'}
- Depth-first pre-order [default];{strategy: 'post'}
- Depth-first post-order;{strategy: 'breadth'}
- Breadth-first.These functions can also take, as the last parameter, the context on which the action will be called.
Fork this repository and run npm install
on the project root folder to make sure you have all project dependencies installed.
Run npm run lint
This will check both source and tests for code correctness and style compliance.
Run npm test
To modify the type definitions, look inside the types
folder.
index.d.ts
contains the definition and tree-model-tests.ts
contains type tests.
To verify changes:
Run npm run dtslint
.
FAQs
Manipulate and traverse tree-like structures in javascript.
The npm package tree-model-improved receives a total of 358 weekly downloads. As such, tree-model-improved popularity was classified as not popular.
We found that tree-model-improved demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.