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:
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:
add(name, data, path) → {Node}
Adds a node to the tree and returns it
This:
- {Tree}
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
get(path, error) → {Node|null}
Gets the node at the specified path
This:
- {Tree}
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
has(path) → {boolean}
Checks a node for existence in a tree
This:
- {Tree}
Parameters:
Name | Type | Description |
---|---|---|
path |
string |
The path to the node to check |
- Source:
remove(path) → {Node}
Deletes the node and returns it at the specified path
This:
- {Tree}
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
toJSON() → {object}
Returns a tree object suitable for storage in JSON format. This method is mainly used by the JSON.stringify function
This:
- {Tree}
- 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:
- {Tree}
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: