structz
ES2015 Class versions of a Node and Tree
Install
npm install structz
or yarn add structz
ES Module
import { Node, Tree } from 'structz';
CJS
const Structz = Node = StructzNode Tree = StructzTree;
Getting Started
After importing the library, you can create as many nodes as you'd like.
let rootSoilNode = 'soil';let fertilizerChildNode = 'fertilizer';let waterChildNode = 'h2o';
The new Node()
constructor accepts an argument of any Javascript type.
You can append child nodes to a root node like so:
rootSoilNode;rootSoilNode;
You can detect if a Node
is a root or not as well.
rootSoilNode; // -> truewaterChildNode; // -> false
Accessing child nodes is simple.
rootSoilNodechildren // returns [fertilizerChildNode, waterChildNode]
You can perform immutable operations on the Node
data structure using the Tree
class. You instantiate it with a root Node.
let rootNode = 1;rootNode;let newRootNode = Tree; // this returns a `new Tree()` with the newly created `Node` as the `.root` value.newRootNoderoot // returns Node of modified type// in this case, our new structure looks like:11 root|12 child
Documentation
See docs/index.html
for up to date documentation.
Node
Class representing a Node data structure type
Kind: global class
- Node
- .children ⇒
Array
- .value ⇒
*
- .value
- .isRoot() ⇒
boolean
- .hasChildren() ⇒
boolean
- .append(child) ⇒
Node
- .toString() ⇒
string
- .children ⇒
Array
node.children ⇒ Returns an array of child nodes.
Kind: instance property of Node
*
node.value ⇒ Returns the value of the this.attributes.
Kind: instance property of Node
node.value
Sets the value of this.attributes.
Kind: instance property of Node
Param |
---|
attribute |
boolean
node.isRoot() ⇒ Detects if this Node is a parent node or not.
Kind: instance method of Node
boolean
node.hasChildren() ⇒ Detects if this node has children or not.
Kind: instance method of Node
Node
node.append(child) ⇒ Appends a child Node to this Node.
Kind: instance method of Node
Param |
---|
child |
string
node.toString() ⇒ Returns a String that represents this Node.
Kind: instance method of Node
Classes
- Tree
Class representing a Tree data structure type that contains a Root node
Tree
Class representing a Tree data structure type that contains a Root node
Kind: global class
- Tree
- new Tree(root)
- instance
- .root ⇒
*
- .root ⇒
- static
new Tree(root)
Instantiates a Node as a Binary Tree
Param |
---|
root |
*
tree.root ⇒ Returns the root of the Node.
Kind: instance property of Tree
*
Tree.map(node, func, tree) ⇒ Performs a function on a Node with the provided function and returns the output. Optionally accepts a Tree to perform this otherwise a new Tree is instantiated and returned (breaks immutability).
Kind: static method of Tree
Param | Default |
---|---|
node | |
func | |
tree |
|
Current Test Coverage
> structz@1.0.0 test ~/github/structz> nyc mocha A Node data structure ✓ Can instantiate a new Node ✓ Returns [] children
Changelog
- 1.0.0 - Preparing initial release, Updating to support CJS + ES Modules
- 0.2.4 - Updated JSDoc Generation
- 0.2.3 - Updated README with starter guide
- 0.2.2 - Docs updated
- 0.2.1 - Corrected bug with exports
- 0.2.0 - Changed package structure for modularity
- 0.1.1 - Added Test Coverage
- 0.1.0 - Initial release.