Nested sets structure with separated items and nodes. Source here Armasheko Artem / nestedsetsjs.
๐ฒ
npm install nestedsetsjs
var ns = require('nestedsetsjs')
var NestedSet = ns();
NestedSet.setItem(1, {title: "Root"})
NestedSet.setItem(2, {title: "Type"})
NestedSet.setItem(3, {title: "Type2"})
NestedSet.setItem(4, {title: "Group"})
NestedSet.setItem(5, {title: "Group2"})
NestedSet.setItem(6, {title: "SubGroup"})
var root_node_id = NestedSet.addRoot(1)
var type_node_id = NestedSet.addNode(root_node_id, 2)
var type_node2_id = NestedSet.addNode(root_node_id, 3)
var group_node_id = NestedSet.addNode(type_node_id, 4)
var group_node2_id = NestedSet.addNode(type_node2_id, 5)
var subgroup_node_id = NestedSet.addNode(group_node_id, 6)
var subgroup_node_id2 = NestedSet.addNode(group_node2_id, 6)
console.log(NestedSet.debug())
Sets data for an item.
- id - ID item
- data - Stored data
NestedSet.setItem(1, { title: "Root"})
Returns Stored data in Data propert.
Removes an item with its dependent nodes
- id - ID item
NestedSet.removeItem(1)
Returns TRUE if item exists or FALSE if not exists
Initializes the Nested Sets structure. Adds a root node and sets an item for it
- item_id - item ID
NestedSet.addRoot(1)
Returns node ID = 1
Adds a new node to the specified parent node and sets an item for it
- target_node_id - parent node ID
- item_id - item ID
NestedSet.addNode(1, 1)
Returns node ID
Gets a node by ID
- node_id - node ID
- asCopy - true (will return a copy of the object) / false or undefined (will return the object itself)
NestedSet.getNode(1)
Returns node object
Removes a node by its ID
- node_id - node ID
NestedSet.removeNode(1)
Returns updated Structure
Removes a node by its ID
- node_id - node ID
- target_id - node ID
NestedSet.moveNode(5, 2)
Returns new Structure
Returns all nodes (Structure property)
Removes all nodes (items are not destroyed)
- node_id - node ID
NestedSet.getParent(5)
Returns the first parent for the node
- node_id - node ID
NestedSet.getParents(5)
Returns all parents of the node
- node_id - node ID
- depth - nesting depth
NestedSet.getChilds(1, 1)
Returns nested nodes in a node
- node_id - node ID
NestedSet.getBranch(5)
Returns the entire branch that contains the node
Returns the entire structure (nodes and items)
Removes all items and all nodes
Checks if a node is root
- node_id - node ID
if the node is Root, returns TRUE, otherwise FALSE
Checks if a node is a parent
- node_id - node ID
if the node contains nested nodes, returns TRUE, otherwise FALSE
Checks if a node is the end of a branch
- node_id - node ID
if the node is end of bracnch, returns TRUE, otherwise FALSE
Returns MAX right key in the tree
Returns MAX left key in the tree
Returns count nodes of tree
Checks a tree according to three rules
- The left node key is always less than the right key
- The remainder of the division of the difference between the right and left keys must be 0
- The remainder of dividing the difference between the left key and the nesting depth + 2 must be 0
Displays information about nodes and items
Returns array with nodes information
- Data (Items)
- Structure (Nodes)
[
{
_id: 1, // ID node
lkey: 1, // left key
rkey: 14, // right key
depth: 0, // depth
childs: 2, // count childs
parent_id: 0, // ID parent node
item_id: 1, // ID item from property Data
data: { title: 'Root' } // item with ID item_id from property Data
},
...
]
Returns this scheme for methods
- getParent
- getParents
- getChilds
- getBranch
- getTree
npm install
npm test
npm install
npx eslint ./src/nestedsets.js
npx eslint ./test/test.js
npm install
npm run minify