🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

nested-sets-tree

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nested-sets-tree

class for search in nested sets tree

latest
Source
npmnpm
Version
2.0.0
Version published
Maintainers
1
Created
Source

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 
});
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.

  • get childs of root element:
getRootCats(): NestedSets;
  • get root element
getRootEl(): NestedSets;
  • check child element:
static isChild(parent: CollectionEl, child: CollectionEl, {lft, rgt}: Keys): boolean;
  • get childs (with el):
getChilds(el: stringOrNumberType | CollectionEl, hide: boolean): NestedSets;
  • is valid id:
isValidId(id: string | number): boolean;
  • get all childs:
getAllChilds(el: stringOrNumberType | CollectionEl, hide: boolean): NestedSets;
  • get depth of tree:
getDepth(): number;
  • get parent of element:
getParent(el: stringOrNumberType | CollectionEl): NestedSets;
  • get all parents chain:
getAllParents(el: stringOrNumberType | CollectionEl, hide: boolean): NestedSets;

Keywords

nested

FAQs

Package last updated on 21 Mar 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts