# ilib-tree-node

> A package to build, construct, and deconstruct an arbitrary tree of nodes.

Latest version **2.0.2** (published 2026-11-10) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install ilib-tree-node
pnpm add ilib-tree-node
yarn add ilib-tree-node
bun add ilib-tree-node
```

## Health

**Score 65/100 (B)** — status: active.

Positive: esm support; no vulnerabilities; has provenance; recently updated; high maintenance score.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2026-11-10 |
| First published | 2019-01-18 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=12 <27 |
| Dependencies | 0 |
| Unpacked size | 34.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 7 |
| Author | Edwin Hoogerbeets |
| Maintainers | ehoogerbeets |
| Keywords | tree, node |

## Links

- npm: https://www.npmjs.com/package/ilib-tree-node
- Repository: https://github.com/iLib-js/ilib-mono
- Homepage: https://github.com/iLib-js/ilib-mono/blob/main/packages/ilib-tree-node
- Issues: https://github.com/iLib-js/ilib-mono/issues
- npm.io page: https://npm.io/package/ilib-tree-node

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 2.0.2 (latest) — 2026-11-10
- 2.0.1 — 2024-12-19
- 2.0.0 — 2024-02-05
- 1.3.2 — 2024-02-04
- 1.3.1 — 2024-02-02
- 1.3.0 — 2021-07-13
- 1.2.2 — 2019-06-18
- 1.2.1 — 2019-04-12
- 1.2.0 — 2019-03-15
- 1.1.0 — 2019-02-15
- 1.0.0 — 2019-01-18

## README

# iLib Tree Node

Simple class to build, construct, and deconstruct an arbitrary tree
structure.

A node in the tree may have an arbitrary number of children. Each node
must have a type property and can optionally have any number of other
properties. There will also be a "children" property which will contain
any possible children, as well as a "use" and a "parent" property, so
the properties "children" and "use" and "parent" are reserved.

There is no tree structure per-se, only nodes. Any node can be considered
to be the root of a tree. Even a single node is a tree of size 1.

### Building a Tree from Scratch

To build a tree from scratch, you can use the following methods:

```
node.add() - add a child node to the current node
```

To create a tree, create a new Node instance and then add it as
a child to another Node instance.

Example of building a tree from scratch:

```
import Node from 'ilib-tree-node';

let parent = new Node({
    type: 'x'
});

let child = new Node({
    type: 'text',
    value: "this is a string"
});

parent.add(child);

// add an anonymous node
parent.add(new Node({
    type: 'text',
    value: "another string"
}));
```

### Deconstructing the Tree to an Array of Nodes

To deconstruct (flatten) the tree into an array of nodes, simply use:

```
node.toArray() - deconstruct the tree into an array of nodes
```

If a node has children, the toArray method will add a start node with
the "use" property set to "start", followed
by the children within that node, followed finally by an end node with
the "use" property set to "end". This happens recursively for all the
children such that the entire tree underneath the node is flattened as
well.

The array can be reconstructed back
into a tree using the information in the "use" property. Any node that
does not have children will get added to the array as-is.

### Reconstructing the Tree from an Array of Nodes

To reconstruct a tree from an array of nodes, use the following static method:

```
Node.fromArray(array) - reconstruct the tree from an array of nodes
```

This method will use the "use" property to rebuild a tree of varying depths.

You may add a "use" property to any
nodes in your array, as long as the start and end nodes for any
node type are balanced and well-formed. (ie. they are properly nested
like XML open and closing tags.). If they are not well-formed, this
method will create a tree with an unexpected structure. Any array
that is a result of a toArray call is guaranteed to be well-formed.

### Unist Trees

Note that this type of tree follows the [unist](https://github.com/syntax-tree/unist)
interface for a tree, and therefore any of the unist utilities may be
used to manipulate the tree.

To convert a regular unist tree into a ilib-tree-node tree, simply
do the following:

```
import Node from 'ilib-tree-node';
import map from 'unist-utils-map';

let ast = X; // some unist tree, probably from a parser

let treeNode = map(ast, node => new Node(node));
```

## License

Copyright © 2018-2024, 2026, JEDLSoft

This package is released under the [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0). The full license text is available in the [LICENSE](https://github.com/iLib-js/ilib-mono/blob/main/packages/ilib-tree-node/LICENSE) file in the ilib-mono repository on GitHub.

## Release Notes

See [CHANGELOG.md](https://github.com/iLib-js/ilib-mono/blob/main/packages/ilib-tree-node/CHANGELOG.md).

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