Tree

Tree

The class of the TreeWithPath tree

Constructor

new Tree(data)

Creates a tree

Parameters:
Name Type Description
data

Tree root node data. The name of the root node is always root

Source:
Example
const Tree = require("treewithpath");
const tree = new Tree({ text: "Hello, world!", "otherText": "hoI!" });

Members

root

Root node of this tree

Source:

Methods

(static) fromJSON(json) → {Tree}

Creates a tree from an object that returns the toJSON() method

Parameters:
Name Type Description
json object

A tree object suitable for storage in JSON format

Source:
Example
const tree = Tree.fromJSON({ name: "root", data: { text: "Hello, world!", "otherText": "hoI!" }, children: [{name: "node1", data: {text: "Hello, world!", "otherText": "hoI!"}, children: [{name: "node2", data: {text: "Hello, world!", "otherText": "hoI!"}, children: [] }] });

(static) joinPath(firstPath, secondPath) → {string}

Connects the two specified paths into one.

Parameters:
Name Type Description
firstPath string

First path

secondPath sting

Second path

Source:
Example
Tree.joinPath("/node1", "node2") // /node1/node2

add(name, data, path) → {Node}

Adds a node to the tree and returns it

This:
Parameters:
Name Type Description
name string

The name of the node to add

data

The data of the node to be created

path string

The path to the parent of the node to create

Source:
Throws:

In case the node already exists

Type
TreeError
Example
tree.add("node2", { text: "Hello, world!", "otherText": "hoI!" }, "/node1");

get(path, error) → {Node|null}

Gets the node at the specified path

This:
Parameters:
Name Type Default Description
path string

The path to the node to receive

error boolean true

Optional parameter. The default is true. If true, an exception will be thrown if the path is incorrect. Otherwise, null will be returned

Source:
Throws:

In case the node is not found and error = true

Type
TreeError
Example
tree.get("/node1");

has(path) → {boolean}

Checks a node for existence in a tree

This:
Parameters:
Name Type Description
path string

The path to the node to check

Source:
Example
tree.has("/notExists/child") // false
tree.has("/exists") // true

remove(path) → {Node}

Deletes the node and returns it at the specified path

This:
Parameters:
Name Type Description
path string

The path to the node to be deleted

Source:
Throws:

In case the node is not found

Type
TreeError
Example
tree.remove("/node1");

toJSON() → {object}

Returns a tree object suitable for storage in JSON format. This method is mainly used by the JSON.stringify function

This:
Source:
Example
tree.toJSON(); // { name: "root", data: { text: "Hello, world!", "otherText": "hoI!" }, children: [{ name: "node1", data: { text: "Hello, world!", "otherText": "hoI!" }, children: [{ name: "node2", data: {text: "Hello, world!", "otherText": "hoI!" }, children: [] }] }

traverse(callback)

Calls a callback for each node in the tree

This:
Parameters:
Name Type Description
callback function

A function called for each node of the tree. The node in the first argument is passed to the function

Source:
Example
tree.traverse(node => {
  console.log(node.name);
});