# 2tree

> Easily transform any arrays into trees.

Latest version **1.0.3** (published 2022-05-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install 2tree
pnpm add 2tree
yarn add 2tree
bun add 2tree
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2022-05-06 |
| First published | 2022-05-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | CMTV |
| Maintainers | cmtv |
| Keywords | tree, build, recursive, nested, ts, typescript |

## Links

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

## Alternatives

- [@progress/kendo-ooxml](https://npm.io/package/@progress/kendo-ooxml.md) — 152.1K weekly downloads
- [@progress/kendo-react-ripple](https://npm.io/package/@progress/kendo-react-ripple.md) — 8.0K weekly downloads
- [@progress/kendo-react-orgchart](https://npm.io/package/@progress/kendo-react-orgchart.md) — 4.3K weekly downloads
- [@praxisui/dynamic-fields](https://npm.io/package/@praxisui/dynamic-fields.md) — 2.4K weekly downloads
- [@progress/kendo-vue-listbox](https://npm.io/package/@progress/kendo-vue-listbox.md) — 932 weekly downloads

## Recent versions

- 1.0.3 (latest) — 2022-05-06
- 1.0.2 — 2022-05-05
- 1.0.1 — 2022-05-05
- 1.0.0 — 2022-05-05

## README

# 2Tree

With this package you can easily transform any arrays into trees.

## Usage

You need to do three thins:

1. Create a class ("product") instances of which will be used as tree items. This call must have a `children` property.
2. Create a class that inherits `TreeMaker` and implement `rawToProduct()`, `isContainer()` and `isChild()` method.
3. Create an instance of your "maker" class and call `make()` method.

That's all!
You will get your tree in result!

## Example

Let's say we want to make a tree from the following array:

```json
[
    {
        "id": 1,
        "value": "Value 1"
    },
    {
        "id": 2,
        "value": "Value 2",
        "parent": 1
    },
    {
        "id": 3,
        "value": "Value 3"
    }
]
```

Firstly, we create a class to represent tree items:

```typescript
class MyItem
{
    id: number;
    value: string;
    children: MyItem[];
}
```

Secondly, we need to create a maker class:

```typescript
import TreeMaker from "2tree";

// ... MyItem class ...

class MyTreeMaker extends TreeMaker<any, MyItem>
{
    rawToProduct(raw: any): MyItem
    {
        let myItem = new MyItem;
            myItem.id = raw.id;
            myItem.value = raw.value;
        
        return myItem;
    }

    isContainer(raw: any)
    {
        return true;
    }

    isChild(raw: any, container: any)
    {
        return raw['parent'] === container['id'];
    }
}
```
Finally, let's make a tree:

```typescript
let maker = new MyTreeMaker;

console.log(maker.make(json));
```

## Limitation

Currenlty you can only select children consequentially.
Returning `false` in `isChild()` method will close current container.
I looking for a way select children anywhere in raw array.

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