2.0.0 • Published 6 years ago

nested-sets-tree v2.0.0

Weekly downloads
16
License
ISC
Repository
github
Last release
6 years ago

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;

2) get root element

getRootEl(): NestedSets;

3) check child element:

static isChild(parent: CollectionEl, child: CollectionEl, {lft, rgt}: Keys): boolean;

4) get childs (with el):

getChilds(el: stringOrNumberType | CollectionEl, hide: boolean): NestedSets;

5) is valid id:

isValidId(id: string | number): boolean;

6) get all childs:

getAllChilds(el: stringOrNumberType | CollectionEl, hide: boolean): NestedSets;

7) get depth of tree:

getDepth(): number;

8) get parent of element:

getParent(el: stringOrNumberType | CollectionEl): NestedSets;

9) get all parents chain:

getAllParents(el: stringOrNumberType | CollectionEl, hide: boolean): NestedSets;