# @colin-luo/tree

> Tree utils.

Latest version **4.3.0** (published 2019-08-28) · ISC license · 0 weekly downloads

## Install

```sh
npm install @colin-luo/tree
pnpm add @colin-luo/tree
yarn add @colin-luo/tree
bun add @colin-luo/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 | 4.3.0 |
| Published | 2019-08-28 |
| First published | 2019-08-20 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 130.3 KB |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Colin Luo |
| Maintainers | colin-luo |
| Keywords | tree, utils, tree-utils, tree-sort, tree-walker, tree-nodes |

## Links

- npm: https://www.npmjs.com/package/@colin-luo/tree
- Repository: https://github.com/luozhihua/tree-utils
- Homepage: https://github.com/luozhihua/tree-utils#readme
- Issues: https://github.com/luozhihua/tree-utils/issues
- npm.io page: https://npm.io/package/@colin-luo/tree

## Dependencies (1)

- [marked](https://npm.io/package/marked.md) ^0.7.0

## Recent versions

- 4.3.0 (latest) — 2019-08-28
- 4.2.3 — 2019-08-23
- 4.2.2 — 2019-08-23
- 4.2.1 — 2019-08-23
- 4.2.0 — 2019-08-23
- 4.1.0 — 2019-08-23
- 4.0.2 — 2019-08-21
- 4.0.1 — 2019-08-21
- 4.0.0 — 2019-08-20
- 3.0.3 — 2019-08-20

## README

# Tree utils [![Build Status](https://travis-ci.org/luozhihua/tree-utils.svg?branch=master)](https://travis-ci.org/luozhihua/tree-utils)

Tree utils for `Typescript` and `Javascript`;

## Install

```bash
npm install @colin-luo/tree
```

## Examples

### Javascript

with default data structure (has `key` and `children` properties).

```Javascript
import Tree from '@colin-luo/tree';

const data = [
  {key: 'a', label: 'A', icon: 'a.svg'},
  {key: 'b', label: 'B', icon: 'b.svg', children: [
    {key: 'b-a', label: 'B-A', icon: 'b-a.svg'},
    {key: 'b-b', label: 'B-B', icon: 'b-b.svg'},
  ]},
  {key: 'c', label: 'C', icon: 'c.svg', children: []},
];

const tree2 = new Tree();

tree.hasChildren(data[0]) // => false
tree.hasChildren(data[1]) // => true
tree.hasChildren(data[2]) // => false

tree2.hasChildren(data[0]) // => false
tree2.hasChildren(data[1]) // => true
tree2.hasChildren(data[2]) // => false
```

with custom data structure (`id` as key fieldname and `sublist` as children fieldname)

```Javascript
import Tree from '@colin-luo/tree';

const data = [
  {id: 'a', label: 'A', icon: 'a.svg'},
  {id: 'b', label: 'B', icon: 'b.svg', sublist: [
    {id: 'b-a', label: 'B-A', icon: 'b-a.svg'},
    {id: 'b-b', label: 'B-B', icon: 'b-b.svg'},
  ]},
  {id: 'c', label: 'C', icon: 'c.svg', sublist: []},
];

const tree = new Tree('id', 'sublist');

tree.hasChildren(data[0]) // => false
tree.hasChildren(data[1]) // => true
tree.hasChildren(data[2]) // => false
```

### Typescript

with default data structure (has `key` and `children` properties).

```typescript
import Tree from '@colin-luo/tree';

innterface NodeProps {
  key: string;
  children: NodeProps[];
  label: string;
  icon: string;
}

const data = [
  {key: 'a', label: 'A', icon: 'a.svg'},
  {key: 'b', label: 'B', icon: 'b.svg', children: [
    {key: 'b-a', label: 'B-A', icon: 'b-a.svg'},
    {key: 'b-b', label: 'B-B', icon: 'b-b.svg'},
  ]},
  {key: 'c', label: 'C', icon: 'c.svg', children: []},
];

const tree = new Tree<'key', 'children', NodeProps>('key', 'children');
const tree2 = new Tree<NodeProps>();

tree.hasChildren(data[0]) // => false
tree.hasChildren(data[1]) // => true
tree.hasChildren(data[2]) // => false

tree2.hasChildren(data[0]) // => false
tree2.hasChildren(data[1]) // => true
tree2.hasChildren(data[2]) // => false
```

with custom data structure (`id` as key fieldname and `sublist` as children fieldname)

```typescript
import Tree from '@colin-luo/tree';

innterface NodeProps {
  id: string;
  sublist: NodeProps[];
  text: string;
  avatar: string;
}

const data = [
  {id: 'a', text: 'A', avatar: 'a.svg'},
  {id: 'b', text: 'B', avatar: 'b.svg', sublist: [
    {id: 'b-a', text: 'B-A', avatar: 'b-a.svg'},
    {id: 'b-b', text: 'B-B', avatar: 'b-b.svg'},
  ]},
  {id: 'c', text: 'C', avatar: 'c.svg', sublist: []},
];

const tree = new Tree<'id', 'sublist', NodeProps>('id', 'sublist');

tree.hasChildren(data[0]) // => false
tree.hasChildren(data[1]) // => true
tree.hasChildren(data[2]) // => false
```

## Api documentation

[Api documentation](./documentation.md)

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