Proper hierarchy management for Node, Typescript and the Browser.
js-hierarchy enables you to build tree-like data structures and work with them properly. It supports walking in multiple directions, storage of arbitrary data and can be used as a base framework for your special needs.
Install the module using npm:
npm install --save js-hierarchy
The module includes the complete type definitions for Typescript applications. Simply install the module and you're ready to go:
After installing the module, use it with require():
var hierarchy = ;var rootNode = ;
Install the module and include the browser script:
or use the jsDelivr CDN hosted version:
With this the global namespace will include a "jshierarchy" object:
var rootNode = ;
After that, everything's right as working in Node.js.
The tree-like data structures, that js-hierarchy can build are build up from "Nodes". There can be several implementations of the Node interface, but the simplest one is called "SimpleNode".
Just instantiate it to create a new node:
rootNode = ;
If a node has no parent, it is considered the "root node". A node can include a number of children, obviously. Simply use the "addChild"- method to add one:
rootNode = ;rootNode;
Adding the child will automatically set the child's parent to the node it was added to.
Because nodes without any information aren't quite useful, js-hierarchy supports storing arbitrary data in a key/value manner.
To add a new data to a node, use the method "setData":
rootNode = ;rootNode;
You can also directly set the data when instantiating the node:
rootNode = 'mykey': 'myValue';
Use "getData" to retrieve node data again later:
rootNode = 'mykey': 'myValue';console;// will output: myValue
Most tree-like structures are used to store hierarchical data and walk the tree from one node to the upper, lower or sibling nodes.
The "walk" method of a node will just do that and call a user defined function on each node it encounters. Please note, that this will not include the original node, the walk method was called from.
rootNode = 'name': 'root';childNode = 'name': 'child';childNode;rootNode;rootNode;// will output both 'child' and 'grandChild'
Please look at this example tree:
+----------------+ + | grandchild 3.1 | | +-------^--------+ down | | | | | | | | | +---------+ +---------+ +----+----+ v | child 1 | | child 2 | | child 3 | +---^-----+ +----^----+ +----^----+ ^ | | | | | | | | | <----+ | +-----> | up | | left | right | | | | | | | | | | | +--+---+ | + +----------+ root +---------+ +------+
Now, these directions are available:
You can also specify an array of directions, meaning that, after the last node has been reached in the first direction, the next direction is walked and so on.
If you need a specialized nodes with functions suitable for your usage, you should extend "AbstractNode" and implement "Node". AbstractNode already has all needed features, so you can just concentrate on the needed features without caring about the basic ones.
Please read the API documentation for details.
To test and build this package, simply use grunt:
This will run all unit tests and a coverage report.
First of all: Thank you for your will to contribute!
To request a new feature or report a bug, please create a github issue.
Pull requests are always welcome!
If you can, please submit a pull request adding the feature or solving the bug you just reported.
While developing for your pull request, please first create a test, that produces your error or checks your new feature, run the test suite using
to see them fail. Then, solve the bug or implement the feature and run the tests once again to see them succeed. Please write good tests and keep the coverage up.
We will review the pull request and optionally comment your code and probably ask you to fix one or two things, before we can merge your code.
Browser compatibility tests are run using SauceLabs. To run them, first add this line to your local hosts file:
Then, when running
grunt browsertest, set these two environment
SAUCE_USERNAME=<your SauceLabs username> SAUCE_ACCESS_KEY=<your SauceLabs access key>