## Classes
Tree

Class Tree

## Typedefs
KeyField : string

defaults 'key'

ChildrenField : string

defaults 'children'

TreeNode : Object
## Tree

Class Tree

**Kind**: global class * [Tree](#Tree) * [new Tree([nodes], [keyField], [childrenField])](#new_Tree_new) * [.setData(nodes)](#Tree+setData) * [.toJSON()](#Tree+toJSON) ⇒ [Array.<TreeNode>](#TreeNode) * [.toString([indent])](#Tree+toString) ⇒ string * [.walker(iterator, [nodes], [mode], [breakable])](#Tree+walker) * [.hasChildren(nodeOrKey)](#Tree+hasChildren) ⇒ boolean * [.isBranch(nodeOrKey)](#Tree+isBranch) ⇒ boolean * [.isRoot(nodeOrKey)](#Tree+isRoot) ⇒ boolean * [.isFirstChild(nodeOrKey)](#Tree+isFirstChild) ⇒ boolean * [.isLastChild(nodeOrKey)](#Tree+isLastChild) ⇒ boolean * [.getNode(key)](#Tree+getNode) ⇒ [TreeNode](#TreeNode) \| null * [.contains(parent, child)](#Tree+contains) ⇒ boolean * [.containsDeeply(parent, child)](#Tree+containsDeeply) ⇒ boolean * [.getParent(key, [nodes])](#Tree+getParent) ⇒ [TreeNode](#TreeNode) \| null * [.siblingsAndSelf(nodeOrKey, [nodes])](#Tree+siblingsAndSelf) ⇒ [Array.<TreeNode>](#TreeNode) * [.siblings(nodes, nodeOrKey)](#Tree+siblings) ⇒ [Array.<TreeNode>](#TreeNode) * [.prevSiblings(nodeOrKey, [nodes])](#Tree+prevSiblings) ⇒ [TreeNode](#TreeNode) \| null * [.nextSiblings(nodeOrKey, [nodes])](#Tree+nextSiblings) ⇒ [TreeNode](#TreeNode) \| null * [.nextSiblingsAll(nodeOrKey, [nodes])](#Tree+nextSiblingsAll) ⇒ [TreeNode](#TreeNode) \| null * [.prevSiblingsAll(nodeOrKey, [nodes])](#Tree+prevSiblingsAll) ⇒ [TreeNode](#TreeNode) \| null * [.indexOf(siblings, nodeOrKey)](#Tree+indexOf) ⇒ number * [.append(node, [tagart])](#Tree+append) * [.prepend(node, [tagart])](#Tree+prepend) * [.insertBefore(node, target)](#Tree+insertBefore) * [.insertAfter(node, target)](#Tree+insertAfter) * [.forward(node)](#Tree+forward) * [.backward(node)](#Tree+backward) * [.remove(node)](#Tree+remove) * [.removeNodes(node)](#Tree+removeNodes) * [.levelUp(node)](#Tree+levelUp) * [.levelDown(node)](#Tree+levelDown) * [.findNodes(nodes, predicate, [parents])](#Tree+findNodes) ⇒ [Array.<TreeNode>](#TreeNode) * [.filterNode(node, predicate, [parents])](#Tree+filterNode) ⇒ [TreeNode](#TreeNode) \| null * [.filterNodes(predicate, [nodes], [parents])](#Tree+filterNodes) ⇒ [Array.<TreeNode>](#TreeNode) * [.sortNode(node, compareFunction, [parents])](#Tree+sortNode) ⇒ [TreeNode](#TreeNode) * [.sortNodes(compareFunction, [nodes], [parents])](#Tree+sortNodes) ⇒ [Array.<TreeNode>](#TreeNode) * [.mapNode(node, mapFunction, [parents])](#Tree+mapNode) ⇒ [TreeNode](#TreeNode) ### new Tree([nodes], [keyField], [childrenField])

Creates an instance of Tree.

| Param | Type | Default | Description | | --- | --- | --- | --- | | [nodes] | Array | [] |

tree data.

| | [keyField] | [KeyField](#KeyField) | 'key' |

Key fieldname of each tree node (value of key should be unique in all tree nodes.)

| | [childrenField] | [ChildrenField](#ChildrenField) | 'children' |

children field name of tree node.

| **Example** *(Install)* ```typescript npm install @colin-luo/tree ``` **Example** *(Typescript)* with default data structure: `{key: '', children: []}`. ```typescript import Tree from '@colin-luo/tree'; const data = [ {key: 'a', label: 'A', icon: 'a.svg'}, {key: 'b', label: 'B', icon: 'b.svg', children: [ {key: 'b-a', label: 'B-A', icon: 'b-a.svg'}, {key: 'b-b', label: 'B-B', icon: 'b-b.svg'}, ]}, {key: 'c', label: 'C', icon: 'c.svg', children: []}, ]; const tree = new Tree(data); ``` with custom data structure: `{id: '', members: []}`. ```typescript import Tree from '@colin-luo/tree'; type KeyField = 'id'; type ChildrenField = 'members'; type NodeProps = { label: string; icon: string; } const data = [ {id: 'a', label: 'A', icon: 'a.svg'}, {id: 'b', label: 'B', icon: 'b.svg', members: [ {id: 'b-a', label: 'B-A', icon: 'b-a.svg'}, {id: 'b-b', label: 'B-B', icon: 'b-b.svg'}, ]}, {id: 'c', label: 'C', icon: 'c.svg', members: []}, ]; const tree = new Tree(data, 'id', 'members'); ``` **Example** *(Javascript)* ```typescript import Tree from '@colin-luo/tree'; const data = [ {key: 'a', label: 'A', icon: 'a.svg'}, {key: 'b', label: 'B', icon: 'b.svg', children: [ {key: 'b-a', label: 'B-A', icon: 'b-a.svg'}, {key: 'b-b', label: 'B-B', icon: 'b-b.svg'}, ]}, {key: 'c', label: 'C', icon: 'c.svg', children: []}, ]; const tree = new Tree(data); const tree2 = new Tree(data, 'key', 'children'); ``` ### @.setData(nodes)
set tree nodes. **Kind**: instance method of [Tree](#Tree) | Param | Type | | --- | --- | | nodes | [Array.<TreeNode>](#TreeNode) | **Example** *(Example usage of `setData`.)* ```javascript const treeData = [ {key: 'foo', text: 'foo'}, {key: 'bar', text: 'bar', children: { {key: 'baz', text: 'baz'}, }}, ]; tree.setData(treeData); ``` ### @.toJSON() ⇒ [Array.<TreeNode>](#TreeNode)

Get tree as JSON.

**Kind**: instance method of [Tree](#Tree) ### @.toString([indent]) ⇒ string
Get tree as string. **Kind**: instance method of [Tree](#Tree) **Returns**: string -

string

| Param | Type | Default | | --- | --- | --- | | [indent] | number | 4 | **Example** *(Typescript)* ```javascript const treeData: string = tree.toString(); ``` **Example** *(Javascript use 4 space indents.)* ```javascript const treeData = tree.toString(4); ``` ### @.walker(iterator, [nodes], [mode], [breakable])
Touch every node in tree. **Kind**: instance method of [Tree](#Tree) | Param | Type | Default | Description | | --- | --- | --- | --- | | iterator | function | |

(node, index, level) => void; If return true and breakable if true will break traversing.

| | [nodes] | Array.<Node> \| null | 'null' |

Nodes to traversing, defaults is root nodes of the tree.

| | [mode] | 'depth' \| 'breadth' | 'depth' |

Indicates depth-first or breadth-first priority when traversing.

| | [breakable] | boolean | false |

Break traversing when iterator return true;

| **Example** *(echo nodes)* ```javascript const iterator = function(node, index, parent, level) { console.log(node) }; tree.walker(iterator, null, 'depth'); ``` **Example** break when `iterator(...)` return `true`. ```javascript const iterator = function(node, index, parent, level) { if (node.key === 'bar') { return true; } }; tree.walker(iterator, null, 'depth'); ``` ### @.hasChildren(nodeOrKey) ⇒ boolean
Checks if a node contains children. **Kind**: instance method of [Tree](#Tree) | Param | Type | | --- | --- | | nodeOrKey | [TreeNode](#TreeNode) \| String | **Example** ```typescript tree.hasChildren('nodeKey'); // return true || false tree.hasChildren({ key: 'bar' }); // return false tree.hasChildren({ key: 'bar', children: [] }); // return false tree.hasChildren({ key: 'bar', children: [{ key: '...'}] }); // return true ``` ### @.isBranch(nodeOrKey) ⇒ boolean
Checks if a node has children property (whether the children's length is 0 or not) **Kind**: instance method of [Tree](#Tree) | Param | Type | | --- | --- | | nodeOrKey | [TreeNode](#TreeNode) \| String | **Example** ```typescript tree.isBranch('nodeKey'); // return true || false tree.isBranch({key: '' }); // return false tree.isBranch({key: '', children: [] }); // return true ``` ### @.isRoot(nodeOrKey) ⇒ boolean

Checks if given node is the root.

**Kind**: instance method of [Tree](#Tree) | Param | Type | | --- | --- | | nodeOrKey | [TreeNode](#TreeNode) \| string | ### @.isFirstChild(nodeOrKey) ⇒ boolean

Checks if given node is the first child.

**Kind**: instance method of [Tree](#Tree) | Param | Type | | --- | --- | | nodeOrKey | [TreeNode](#TreeNode) \| string | ### @.isLastChild(nodeOrKey) ⇒ boolean

Checks if given node is the last child.

**Kind**: instance method of [Tree](#Tree) | Param | Type | | --- | --- | | nodeOrKey | [TreeNode](#TreeNode) \| string | ### @.getNode(key) ⇒ [TreeNode](#TreeNode) \| null
Get single node with a specific key. **Kind**: instance method of [Tree](#Tree) | Param | Type | | --- | --- | | key | string | ### @.contains(parent, child) ⇒ boolean
Checks if a node contains another node as children. **Kind**: instance method of [Tree](#Tree) | Param | Type | Description | | --- | --- | --- | | parent | string \| [TreeNode](#TreeNode) |

Parent node or key of parent node.

| | child | string \| [TreeNode](#TreeNode) |

Child node or key of child node.

| ### @.containsDeeply(parent, child) ⇒ boolean
Checks if a node contains another node deeply. **Kind**: instance method of [Tree](#Tree) | Param | Type | Description | | --- | --- | --- | | parent | string \| [TreeNode](#TreeNode) |

Parent node or key of parent node.

| | child | string \| [TreeNode](#TreeNode) |

Child node or key of child node.

| ### @.getParent(key, [nodes]) ⇒ [TreeNode](#TreeNode) \| null
Get parent node by given key. **Kind**: instance method of [Tree](#Tree) **Returns**: [TreeNode](#TreeNode) \| null -

return parent node of given key.

| Param | Type | Description | | --- | --- | --- | | key | string |

node key.

| | [nodes] | [Array.<TreeNode>](#TreeNode) |

Tree root nodes or specific nodes.

| ### @.siblingsAndSelf(nodeOrKey, [nodes]) ⇒ [Array.<TreeNode>](#TreeNode)
Get siblings and itself of given key or node. **Kind**: instance method of [Tree](#Tree) **Returns**: [Array.<TreeNode>](#TreeNode) -

return siblings node and itself of given node or key.

| Param | Type | Description | | --- | --- | --- | | nodeOrKey | string |

node ke y.

| | [nodes] | [Array.<TreeNode>](#TreeNode) |

Tree root nodes or specific nodes.

| ### @.siblings(nodes, nodeOrKey) ⇒ [Array.<TreeNode>](#TreeNode)
Get siblings of given key or node. **Kind**: instance method of [Tree](#Tree) **Returns**: [Array.<TreeNode>](#TreeNode) -

return siblings node of given node or key.

| Param | Type | Description | | --- | --- | --- | | nodes | [Array.<TreeNode>](#TreeNode) |

Tree root nodes or specific nodes.

| | nodeOrKey | string |

node or key of node.

| ### @.prevSiblings(nodeOrKey, [nodes]) ⇒ [TreeNode](#TreeNode) \| null
Get node before of given node. **Kind**: instance method of [Tree](#Tree) **Returns**: [TreeNode](#TreeNode) \| null -

return previous siblings node of given key or NULL.

| Param | Type | Description | | --- | --- | --- | | nodeOrKey | string |

node key.

| | [nodes] | [Array.<TreeNode>](#TreeNode) |

Tree root nodes or specific nodes.

| ### @.nextSiblings(nodeOrKey, [nodes]) ⇒ [TreeNode](#TreeNode) \| null
Get next node of given node. **Kind**: instance method of [Tree](#Tree) **Returns**: [TreeNode](#TreeNode) \| null -

return next siblings node of given key or null.

| Param | Type | Description | | --- | --- | --- | | nodeOrKey | string |

node or key.

| | [nodes] | [Array.<TreeNode>](#TreeNode) |

Tree root nodes or specific nodes.

| ### @.nextSiblingsAll(nodeOrKey, [nodes]) ⇒ [TreeNode](#TreeNode) \| null
Get all next siblings node of given node or key. **Kind**: instance method of [Tree](#Tree) **Returns**: [TreeNode](#TreeNode) \| null -

return next siblings node of given key or null.

| Param | Type | Description | | --- | --- | --- | | nodeOrKey | string |

node key.

| | [nodes] | [Array.<TreeNode>](#TreeNode) |

Tree root nodes or specific nodes.

| ### @.prevSiblingsAll(nodeOrKey, [nodes]) ⇒ [TreeNode](#TreeNode) \| null
Get all previous siblings of given node. **Kind**: instance method of [Tree](#Tree) **Returns**: [TreeNode](#TreeNode) \| null -

return next siblings node of given key or null.

| Param | Type | Description | | --- | --- | --- | | nodeOrKey | string |

node or key.

| | [nodes] | [Array.<TreeNode>](#TreeNode) |

Tree root nodes or specific nodes.

| ### @.indexOf(siblings, nodeOrKey) ⇒ number
Get index of node. **Kind**: instance method of [Tree](#Tree) | Param | Type | | --- | --- | | siblings | [Array.<TreeNode>](#TreeNode) | | nodeOrKey | [TreeNode](#TreeNode) \| string | ### @.append(node, [tagart])
Append a new node into target node as end. **Kind**: instance method of [Tree](#Tree) | Param | Type | Description | | --- | --- | --- | | node | [TreeNode](#TreeNode) |

A new node to append.

| | [tagart] | [TreeNode](#TreeNode) \| string |

where for appending to.

| ### @.prepend(node, [tagart])
Prepend a node into target node at head. **Kind**: instance method of [Tree](#Tree) | Param | Type | Description | | --- | --- | --- | | node | [TreeNode](#TreeNode) |

A new node to prepend.

| | [tagart] | [TreeNode](#TreeNode) \| string |

Where to prepend.

| ### @.insertBefore(node, target)
Insert a new node before target node **Kind**: instance method of [Tree](#Tree) | Param | Type | | --- | --- | | node | [TreeNode](#TreeNode) | | target | [TreeNode](#TreeNode) \| string | ### @.insertAfter(node, target)
Insert a new node after target node **Kind**: instance method of [Tree](#Tree) | Param | Type | | --- | --- | | node | [TreeNode](#TreeNode) | | target | [TreeNode](#TreeNode) \| string | ### @.forward(node)
Exchange location with the previous node. **Kind**: instance method of [Tree](#Tree) | Param | Type | Description | | --- | --- | --- | | node | [TreeNode](#TreeNode) \| string |

Node or key.

| ### @.backward(node)
Exchange location with the next node. **Kind**: instance method of [Tree](#Tree) | Param | Type | Description | | --- | --- | --- | | node | [TreeNode](#TreeNode) \| string |

Node or key.

| ### @.remove(node)
Remove the given node **Kind**: instance method of [Tree](#Tree) | Param | Type | Description | | --- | --- | --- | | node | [TreeNode](#TreeNode) \| string |

Node or key.

| ### @.removeNodes(node)
Remove the given nodes. **Kind**: instance method of [Tree](#Tree) | Param | Type | Description | | --- | --- | --- | | node | [TreeNode](#TreeNode) \| string |

Node or key.

| ### @.levelUp(node)
Move the given node to the back of the parent node as the sibling of the parent node. **Kind**: instance method of [Tree](#Tree) | Param | Type | Description | | --- | --- | --- | | node | [TreeNode](#TreeNode) \| string |

Node or key.

| ### @.levelDown(node)
Move the given node down to end of children of previous siblings. **Kind**: instance method of [Tree](#Tree) | Param | Type | Description | | --- | --- | --- | | node | [TreeNode](#TreeNode) \| string |

Node or key.

| ### @.findNodes(nodes, predicate, [parents]) ⇒ [Array.<TreeNode>](#TreeNode)
Find nodes via a custom function. **Kind**: instance method of [Tree](#Tree) | Param | Type | Default | | --- | --- | --- | | nodes | [Array.<TreeNode>](#TreeNode) | | | predicate | [NodesFinder.<TreeNode>](#TreeNode) | | | [parents] | [Array.<TreeNode>](#TreeNode) | [] | ### @.filterNode(node, predicate, [parents]) ⇒ [TreeNode](#TreeNode) \| null
Filter for a single node and its children. **Kind**: instance method of [Tree](#Tree) | Param | Type | Default | | --- | --- | --- | | node | [TreeNode](#TreeNode) | | | predicate | [NodesFinder.<TreeNode>](#TreeNode) | | | [parents] | [Array.<TreeNode>](#TreeNode) | [] | ### @.filterNodes(predicate, [nodes], [parents]) ⇒ [Array.<TreeNode>](#TreeNode)
Filter the given list of nodes and their children. **Kind**: instance method of [Tree](#Tree) | Param | Type | Default | | --- | --- | --- | | predicate | [NodesFinder.<TreeNode>](#TreeNode) | | | [nodes] | [Array.<TreeNode>](#TreeNode) | this.nodes | | [parents] | [Array.<TreeNode>](#TreeNode) | [] | ### @.sortNode(node, compareFunction, [parents]) ⇒ [TreeNode](#TreeNode)
Sort children of givin node and return a new node with sorted children. **Kind**: instance method of [Tree](#Tree) | Param | Type | Default | | --- | --- | --- | | node | [TreeNode](#TreeNode) | | | compareFunction | [NodeSorter.<TreeNode>](#TreeNode) | | | [parents] | [Array.<TreeNode>](#TreeNode) | [] | ### @.sortNodes(compareFunction, [nodes], [parents]) ⇒ [Array.<TreeNode>](#TreeNode)
Sort node list and their children. **Kind**: instance method of [Tree](#Tree) | Param | Type | Default | | --- | --- | --- | | compareFunction | [NodeSorter.<TreeNode>](#TreeNode) | | | [nodes] | [Array.<TreeNode>](#TreeNode) | | | [parents] | [Array.<TreeNode>](#TreeNode) | [] | ### @.mapNode(node, mapFunction, [parents]) ⇒ [TreeNode](#TreeNode)
Map node **Kind**: instance method of [Tree](#Tree) | Param | Type | Default | | --- | --- | --- | | node | [TreeNode](#TreeNode) | | | mapFunction | [NodeMapper.<TreeNode>](#TreeNode) | | | [parents] | [Array.<TreeNode>](#TreeNode) | [] | ## KeyField : string

defaults 'key'

**Kind**: global typedef ## ChildrenField : string

defaults 'children'

**Kind**: global typedef ## TreeNode : Object **Kind**: global typedef **Properties** | Name | Type | | --- | --- | | key | [KeyField](#KeyField) | | children | [ChildrenField](#ChildrenField) | | others | Any |