# nested-sets-tree

> class for search in nested sets tree

Latest version **2.0.0** (published 2018-03-21) · ISC license · 0 weekly downloads

## Install

```sh
npm install nested-sets-tree
pnpm add nested-sets-tree
yarn add nested-sets-tree
bun add nested-sets-tree
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2018-03-21 |
| First published | 2018-03-21 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 958.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | aanarion |
| Maintainers | aanarion |
| Keywords | nested, sets, tree, typescript |

## Links

- npm: https://www.npmjs.com/package/nested-sets-tree
- Repository: https://github.com/aanation/nested-sets-tree
- Homepage: https://github.com/aanation/nested-sets-tree#readme
- Issues: https://github.com/aanation/nested-sets-tree/issues
- npm.io page: https://npm.io/package/nested-sets-tree

## Dependencies (3)

- [lodash](https://npm.io/package/lodash.md) ^4.7.0
- [@types/node](https://npm.io/package/@types/node.md) ^9.4.6
- [@types/lodash](https://npm.io/package/@types/lodash.md) ^4.14.104

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2018-03-21

## README

# 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:
```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:
```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:
```javascript 
let array = [
    {
        id: 1, 
        lvl: 0, 
        parent_id: 0
        //...
    }//...
];

tree.loadTree(array, {});
```

Typescript:
```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.   

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


## Search

```javascript 
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:

```javascript
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:
```javascript
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:
```typescript
getRootCats(): NestedSets;
```

2) get root element
```typescript
getRootEl(): NestedSets;
```

3) check child element:
```typescript
static isChild(parent: CollectionEl, child: CollectionEl, {lft, rgt}: Keys): boolean;
```

4) get childs (with el):
```typescript
getChilds(el: stringOrNumberType | CollectionEl, hide: boolean): NestedSets;
```

5) is valid id:
```typescript
isValidId(id: string | number): boolean;
```

6) get all childs:
```typescript
getAllChilds(el: stringOrNumberType | CollectionEl, hide: boolean): NestedSets;
```

7) get depth of tree:
```typescript
getDepth(): number;
```

8) get parent of element:
```typescript
getParent(el: stringOrNumberType | CollectionEl): NestedSets;
```

9) get all parents chain:
```typescript
getAllParents(el: stringOrNumberType | CollectionEl, hide: boolean): NestedSets;
```

---
_Source: https://npm.io/package/nested-sets-tree · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
