js-tree-list
Convert list to tree, managing a tree and its nodes.
Fork from: https://github.com/DenQ/iron-tree https://github.com/DenQ/list-to-tree
The author of this project is DenQ. This project has only been improved a little.
Features
- Convert list to tree.
- Convert tree to list.
- Tree sort by last.
- UUID is support.
Installation
$ npm install js-tree-list
Usage
// JsTreeList.ListToTree Configconst defaultOptions = key_id: 'id' key_parent: 'parent' key_child: 'child' key_last: null uuid: false empty_children: false
var list = id: 1 parent: 0 id: 2 parent: 1 id: 3 parent: 1 id: 4 parent: 2 id: 5 parent: 2 id: 6 parent: 0 id: 7 parent: 0 id: 8 parent: 7 id: 9 parent: 8 id: 10 parent: 0 const tree = list key_id: "id" key_parent: "parent" key_child: "children" key_last: "last" const list = tree key_child: "children" empty_children: true consoleconsole
Result
[{
"id": 1,
"parent": 0,
"child": [
{
"id": 2,
"parent": 1,
"child": [
{
"id": 4,
"parent": 2
}, {
"id": 5,
"parent": 2
}
]
},
{
"id": 3,
"parent": 1
}
]
}, {
"id": 6,
"parent": 0
}, {
"id": 7,
"parent": 0,
"child": [
{
"id": 8,
"parent": 7,
"child": [
{
"id": 9,
"parent": 8
}
]
}
]
}, {
"id": 10,
"parent": 0
}];
Methods
- constructor(list, options)
- params:
list
- array list with elements. Like{ id: 5: parent: 1 }
.options
- optional parameter. Object for describe flags and field names for tree.
- params:
- .GetTree() This method will be return json tree
- example:
tree.GetTree()
- example:
- .sort(callback) The custom sort method
- callback(a, b) - a and b have
Node
type and have methods: add, remove, get, set, sort, traversal, etc... - example:{return {const aid = Numberaconst bid = Numberbif aid > bidreturn vector ? 1 : -1else if aid < bidreturn vector ? -1 : 1elsereturn 0}}ltt
- callback(a, b) - a and b have
The Tree and Node Base usage
// create treeconst object = id: 1 title: "Root" const tree = object // add nodesconst regularObject = id: 2 title: "Node 2" tree // contains nodeconst targetNode = tree // remove nodeconst result = tree // traversalconst criteria = currentNode === 1tree
{ return { const aid = Numbera const bid = Numberb if aid > bid return vector ? 1 : -1 else if aid < bid return vector ? -1 : 1 else return 0 }}tree // desc
The following are the other methods available.
Tree
This is the class of tree management
Properties
- rootNode Root tree node
- type
Node
- type
Methods
-
contstructor(object)
- params
- object - json
object
. Optional
- object - json
- return
Three
- example
const object = id: 1 title: "Root"const tree = object - params
-
.add(criteria, object) Adds a node to the tree if the criterion is true
- params
- criteria(Node) -
function
orstring
. Ifstring
then criteria is "root" - object - content for the node
- criteria(Node) -
- return
Three
- examples
const object = id: 1 title: "Root"const tree =const resultTree = treeconst regularObject = id: 2 title: "Node 2"const resultTree = tree - params
-
.remove(criteria) Removes a node from a tree if the criterion is true
- params
- criteria(Node) - return
boolean
- criteria(Node) - return
- return
boolean
- examples
const result = tree - params
-
.contains(criteria) Searches for a node in a tree according to the criterion
- params
- criteria(Node) - return
boolean
- criteria(Node) - return
- return
Node
- examples
const targetNode = tree - params
-
.sort(compare) Sorts a tree
- params
- compare(a:Node, b:Node) - comparison function
- return
null
- examples
{return {const aid = Numberaconst bid = Numberbif aid > bidreturn vector ? 1 : -1else if aid < bidreturn vector ? -1 : 1elsereturn 0}}tree //Desc - params
-
.move(criteria, destination) Moves the desired branch or node to the node or branch of the destination, according to the criteria
- params
- criteria(Node) - callback
- destination(Node) - callback
- return
boolean
- examples
const search = currentNode === 7const destination = currentNode === 3const result = tree - params
-
.traversal(criteria, callback) Bypasses the tree and, according to the criterion, calls a function for each node
- params
- criteria(Node) - return
boolean
- callback(Node)
- criteria(Node) - return
- return
null
- examples
const criteria = currentNode === 7treetree - params
-
.toJson(options) Represents a tree in the form of a json format
- params
- options -
object
. Optional- empty_children - Type
boolean
. Allow empty children. Defaulttrue
- key_children - Type
string
. Field name for children. Defaultchildren
- empty_children - Type
- options -
- return
object
- examples
const json = tree - params
Node
This is the node management class
Properties
- content Content of the node
- type
object
- type
- children Children of the node
- type
array
- type
- length Number children of the node
- type
number
- type
Methods
-
constructor(json)
- params
- json - simple
json
object
- json - simple
- examples
const rootContent =id: 1name: "Root"let node = rootContent - params
-
.add(child) Adding a child to the node
- return
Node
- created node - params
- child - type
object
/json
- child - type
- examples
const rootContent =id: 1name: "Root"let node = rootContentconst childNode = node - return
-
.remove(criteria) Removing a child node according to the criterion
- return - removed
Node
- params
- criteria - criteria function for removing nodes
- examples
const removedNodes = node - return - removed
-
.get(path) Access to node content by field name
- return
mixed
- params
- path - key name for object in node. For example
id
orfullname
, etc...
- path - key name for object in node. For example
- examples
node // 1node // "Some name" - return
-
.set(path, value) Setting a value or creating a new field in the contents of a node
- return
boolean
- params
- path -
String
field name - value -
mixed
- path -
- examples
node); // returned `true`. Node.content.id = 100node; // 100 - return
-
.sort(compare) Sorting child nodes
- return
null
- params
- compare - custom function for sorting
- examples
{return {const aid = Numberaconst bid = Numberbif aid > bidreturn vector ? 1 : -1else if aid < bidreturn vector ? -1 : 1elsereturn 0}}node - return
-
.traversal(criteria, callback) Bypassing child nodes according to the criterion and applying function to them
- return
null
- params
- criteria -
function
criteria each nodes - callback -
function
fire when criteria is true for node
- criteria -
- examples
// for all nodesnode// only for node.id == 3node - return