
Security News
The Code You Didn't Write Is Still Yours to Defend
AI agents are pulling packages into environments no scanner is watching, creating exposure before security teams can see it.
nested-sets-tree
Advanced tools
It's a small module for getting elements of Nested Sets Tree. It works both in browsers and NodeJS. Includes types definition for Typescript.
npm install nested-sets-tree
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'
});
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;
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
All methods are effective for taking both element's ID and element itself. All methods returns the NestedSets instance.
getRootCats(): NestedSets;
getRootEl(): NestedSets;
static isChild(parent: CollectionEl, child: CollectionEl, {lft, rgt}: Keys): boolean;
getChilds(el: stringOrNumberType | CollectionEl, hide: boolean): NestedSets;
isValidId(id: string | number): boolean;
getAllChilds(el: stringOrNumberType | CollectionEl, hide: boolean): NestedSets;
getDepth(): number;
getParent(el: stringOrNumberType | CollectionEl): NestedSets;
getAllParents(el: stringOrNumberType | CollectionEl, hide: boolean): NestedSets;
FAQs
class for search in nested sets tree
The npm package nested-sets-tree receives a total of 175 weekly downloads. As such, nested-sets-tree popularity was classified as not popular.
We found that nested-sets-tree demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
AI agents are pulling packages into environments no scanner is watching, creating exposure before security teams can see it.

Security News
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.

Product
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.