# treeish

> An array to tree generator with utility functions.

Latest version **2.0.1** (published 2020-08-02) · ISC license · 0 weekly downloads

## Install

```sh
npm install treeish
pnpm add treeish
yarn add treeish
bun add treeish
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2020-08-02 |
| First published | 2019-04-28 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Anirvann Das |
| Maintainers | anirvann |
| Keywords | array to tree, tree, tree traversal, tree node type search |

## Links

- npm: https://www.npmjs.com/package/treeish
- Repository: https://github.com/anirvann/treeish
- Homepage: https://github.com/anirvann/treeish#readme
- Issues: https://github.com/anirvann/treeish/issues
- npm.io page: https://npm.io/package/treeish

## Dependencies (1)

- [eslint-plugin-jest](https://npm.io/package/eslint-plugin-jest.md) ^23.20.0

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 2.0.1 (latest) — 2020-08-02
- 2.0.0 — 2020-08-02
- 1.0.1 — 2019-04-28
- 1.0.0 — 2019-04-28

## README

[![npm (version)](https://img.shields.io/npm/v/treeish.svg)](https://img.shields.io/npm/v/treeish.svg)
[![Build Status](https://travis-ci.org/anirvann/treeish.svg?branch=master)](https://travis-ci.org/anirvann/treeish)
[![Coverage Status](https://coveralls.io/repos/github/anirvann/treeish/badge.svg?branch=master)](https://coveralls.io/github/anirvann/treeish?branch=master)

An array to tree generator utility.

## Install
```
$ npm install treeish
```

## Usage

Building the tree
```js
import TreeUtil from 'treeish';

const collection = [
  { id: '3', name: 'C', x: '1', selected: true, type: 'campaign' },
  { id: '6', name: 'F', x: '3', selected: true, type: 'adset' },
  { id: '1', name: 'A', selected: true, type: 'mediaplan' },
  { id: '2', name: 'B', selected: true, type: 'mediaplan' },
  { id: '7', name: 'G', x: '4', selected: true, type: 'adset' },
  { id: '4', name: 'D', x: '1', selected: true, type: 'campaign' },
  { id: '5', name: 'E', x: '2', selected: true, type: 'campaign' },
  { id: '8', name: 'G', x: '6', selected: true, type: 'ad' },
  { id: '9', name: 'H', x: '6', selected: true, type: 'ad' },
  { id: '10', name: 'I', x: '7', selected: true, type: 'ad' },
  { id: '11', name: 'J', x: '7', selected: true, type: 'ad' },
];
const treeUtil = new TreeUtil(collection, { parentId: 'x' }); //parentId key is 'x'
```
Tree utility functions
```js
const node = treeUtil.findNodeById('2') // returns the node with ID 2
const nodes = treeUtil.findAllNodesByProperty('type', 'campaign')
/* nodes will contain 
* [
*   { id: '3', name: 'C', x: '1', selected: true, type: 'campaign' },
*   { id: '4', name: 'D', x: '1', selected: true, type: 'campaign' },
*   { id: '5', name: 'E', x: '2', selected: true, type: 'campaign' }
* ]
*/

const nestedNodes = treeUtil.getNestedNodesByProperty(
  { id: '2', name: 'B', selected: true, type: 'mediaplan' },
  'type', 
  'campaign');
/* nestedNodes will contain 
* [
*   { id: '5', name: 'E', x: '2', selected: true, type: 'campaign' }
* ]
*/

const depth = treeUtil.maxDepth(); //depth is 4

treeUtil.updateNodes(
  treeUtil.children[1].children, //pass collection of nodes
  function(node, level){ node.model.selected = !node.model.selected; }
)
//will update all children of the second child of the root (node id: 2)
```

### TODOs
* Add node creation and removal functionalities

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