structz

1.0.1 • Public • Published

structz

ES2015 Class versions of a Node and Tree

Build Status codecov

Install

npm install structz or yarn add structz

ES Module

import { Node, Tree } from 'structz';

CJS

const Structz = require('structz'),
  Node = Structz.Node,
  Tree = Structz.Tree;

Getting Started

After importing the library, you can create as many nodes as you'd like.

let rootSoilNode = new Node('soil');
let fertilizerChildNode = new Node('fertilizer');
let waterChildNode = new Node('h2o');

The new Node() constructor accepts an argument of any Javascript type.

You can append child nodes to a root node like so:

rootSoilNode.append(fertilizerChildNode);
rootSoilNode.append(waterChildNode);

You can detect if a Node is a root or not as well.

rootSoilNode.isRoot(); // -> true
waterChildNode.isRoot(); // -> false

Accessing child nodes is simple.

rootSoilNode.children // 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 = new Node(1);
rootNode.append(new Node(2));
let newRootNode = Tree.map(rootNode, (node) => {
  return node + 10;
}); // this returns a `new Tree()` with the newly created `Node` as the `.root` value.
newRootNode.root // 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

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

node.isRoot() ⇒ boolean

Detects if this Node is a parent node or not.

Kind: instance method of Node

node.hasChildren() ⇒ boolean

Detects if this node has children or not.

Kind: instance method of Node

node.append(child) ⇒ Node

Appends a child Node to this Node.

Kind: instance method of Node

Param
child

node.toString() ⇒ string

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

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 if empty
    ✓ Returns [] children if not empty
    ✓ successfully can append a child
    ✓ returns false if there are no children
    ✓ returns true if there are children
    ✓ can return its own value
    ✓ can set its own value
    ✓ can output itself as a String
 
  A Tree data structure type that contains a Root node data structure
    ✓ can instantiate itself
    ✓ returns the root Node
    ✓ maps over a Node
 
 
  12 passing (12ms)
 
----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s | 
----------|----------|----------|----------|----------|-------------------|
All files |    88.46 |    71.43 |    91.67 |       92 |                   |
 Node.js  |       80 |        0 |     87.5 |    85.71 |             23,24 |
 Tree.js  |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|

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.

Package Sidebar

Install

npm i structz

Weekly Downloads

11

Version

1.0.1

License

MIT

Unpacked Size

106 kB

Total Files

33

Last publish

Collaborators

  • codeforcoffee