nested-sets-tree
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

NESTED SETS TREE

It's a small module for getting elements of Nested Sets Tree. It works both in browsers and NodeJS. Includes types definition for Typescript.

Installation

npm install nested-sets-tree

Initialization (create NestedSets instance)

When initialized, you must define the keys used in your nested sets tree array:

Javascript:

const NestedSets = require('nested-sets-tree');
 
const tree = new NestedSets({
    id: 'id',
    lvl: 'depth',
    parentId: 'parent_id', 
    lft: 'lft', 
    rgt: 'rgt', 
    hide: 'hide'
}); 

In typescript + es6 import + nodejs you should use "esModuleInterop": true option in your tsconfig.json.

Typescript:

import NestedSets from 'nested-sets-tree'; 
import {CollectionEl} from 'nested-sets-tree'; 
 
const tree:NestedSets = new NestedSets({
    id: 'id',
    lvl: 'depth',
    parentId: 'parent_id', 
    lft: 'lft', 
    rgt: 'rgt', 
    hide: 'hide'
});

Load tree

Javascript:

let array = [
    {
        id: 1, 
        lvl: 0, 
        parent_id: 0
        //...
    }//...
];
 
tree.loadTree(array, {});

Typescript:

let array:CollectionEl[] = [
    {
        id: 1, 
        lvl: 0, 
        parent_id: 0
        //...
    }//...
];
 
tree.loadTree(array, {});

You can set the options' object together with your tree array:

validate true/false - if true your tree is validated. By default is false.

createIndexes true/false - if true the indexes for quick binary search are created (recommended for huge amount of operations). By default false.

indexes {} - object includes ready-made indexes. Set it if you already have got sorted tree.

tree.loadTree(array, {
    id: [] //nested sets tree array sorted by id 
});

Search

tree.getChilds(5).ids; 
tree.getChilds(5).results; 
tree.getChilds(5, true).resutls; 
tree.getAllChilds(5).results; 
tree.getChilds({
    id: 1, 
    lvl: 0, 
    parent_id: 0
    lft: 1,
    rgt: 20, 
    name: 'parent element'
    hide: false
}).results;
 

Hidden elements

If you want to exclude some element's in search results, you should use hide: true flag in element:

const NestedSets = require('nested-sets-tree');
 
const tree = new NestedSets({
    id: 'id',
    lvl: 'lvl',
    parentId: 'parent_id', 
    lft: 'lft', 
    rgt: 'rgt', 
    hide: 'hide'
}); 
 
const treeArray = [
    {
        id: 1, 
        lvl: 0, 
        parent_id: 0
        lft: 1,
        rgt: 20, 
        name: 'parent element'
        hide: false
    }, 
    {
        id: 2, 
        lvl: 1, 
        parent_id: 1, 
        lft: 2,
        rgt: 3,
        hide: false, 
        name: 'first child'
    }, 
    //exclude second element
    {
        id: 3,
        lvl: 1, 
        parent_id: 1, 
        lft: 4,
        rgt: 5,
        hide: true,
        name: 'second'
    },
    //...
]; 
 
tree.loadTree(treeArray);
//get all childs exclude second element
let childsWithoutHidden = tree.getAllChilds(1, true).results; 
//get all childs, ignore hide flag 
let childs = tree.getAllChilds(1).results; 
 

in search method:

let childs = tree
 

Search methods

All methods are effective for taking both element's ID and element itself. All methods returns the NestedSets instance.

  1. get childs of root element:
getRootCats()NestedSets;
  1. get root element
getRootEl()NestedSets;
  1. check child element:
static isChild(parentCollectionEl, childCollectionEl, {lft, rgt}Keys)boolean;
  1. get childs (with el):
getChilds(elstringOrNumberType | CollectionEl, hideboolean)NestedSets;
  1. is valid id:
isValidId(idstring | number)boolean;
  1. get all childs:
getAllChilds(elstringOrNumberType | CollectionEl, hideboolean)NestedSets;
  1. get depth of tree:
getDepth()number;
  1. get parent of element:
getParent(elstringOrNumberType | CollectionEl)NestedSets;
  1. get all parents chain:
getAllParents(elstringOrNumberType | CollectionEl, hideboolean)NestedSets;

Package Sidebar

Install

npm i nested-sets-tree

Weekly Downloads

28

Version

2.0.0

License

ISC

Unpacked Size

981 kB

Total Files

36

Last publish

Collaborators

  • aanarion