# dendrologist

> A simple, fast, and lightweight tree modification library for JavaScript.

Latest version **0.1.5** (published 2023-09-03) · MIT license · 0 weekly downloads

## Install

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

## Health

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

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

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.5 |
| Published | 2023-09-03 |
| First published | 2023-08-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 20.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Gaven Heim |
| Maintainers | gavenheim |
| Keywords | tree manipulation, node operations, depth-first search, tree data structure, node identification, node update, node deletion, node addition, node movement, tree traversal, recursive algorithms, tree modification, error handling, TypeScript library, JavaScript utility |

## Links

- npm: https://www.npmjs.com/package/dendrologist
- Homepage: https://github.com/gaven/dendrologist
- npm.io page: https://npm.io/package/dendrologist

## Dependencies (1)

- [vite-plugin-dts](https://npm.io/package/vite-plugin-dts.md) ^3.5.2

## 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

- 0.1.5 (latest) — 2023-09-03
- 0.1.4 — 2023-09-01
- 0.1.3 — 2023-09-01
- 0.1.2 — 2023-09-01
- 0.1.0 — 2023-09-01
- 0.0.0 — 2023-08-29

## README

# Dendrologist 🌲

This library provides a set of functions to manipulate a tree data structure where each node has an `id` and a list of `children`. The tree is represented by a Node interface:

```ts
interface Node {
  id: string;
  [key: string]: any;
}
```

### Installation

```shell
npm install dendrologist
yarn install dendrologist
pnpm install dendrologist
```

### Functions

```ts
export function getNodeById(
  node: Node | null | undefined,
  id: string,
  childrenKey: string = "children"
): Node | null {
  // Implementation details...
}
```

This function takes a `node` (the root of the tree), an `id` to search for, and an optional `childrenKey` that specifies the property name for the children array in each node. It performs a depth-first search (DFS) to find the node with the matching id and returns it. If the node is not found, it returns `null`.

```ts
export function updateNode(
  node: Node,
  id: string,
  newData: Node,
  childrenKey: string = "children"
): Node | null {
  // Implementation details...
}
```

This function takes a `node`, an `id` to identify the node to update, `newData` containing the updated properties, and an optional `childrenKey` to specify the property name for the children array. It performs a DFS to find the node with the matching id, updates its properties with newData, and returns the updated node. If the node is not found, it returns `null`.

```ts
export function deleteNode(
  node: Node | null | undefined,
  id: string,
  childrenKey: string = "children"
): Node | null {
  // Implementation details...
}
```

This function takes a `node`, an `id` to identify the node to delete, and an optional `childrenKey` to specify the property name for the children array. It performs a DFS to find the node with the matching id, removes it from its parent's children array, and returns the modified tree. If the node is not found, it returns `null`.

```ts
export function addNode(
  tree: Node | null | undefined,
  parentId: string,
  newNode: Node,
  position: number,
  childrenKey: string = "children"
): Node {
  // Implementation details...
}
```

This function takes a `tree` (the root of the tree), a `parentId` to identify the parent node where the new node will be added, `newNode` containing the properties of the new node, `position` specifying the index at which the new node should be inserted, and an optional `childrenKey` to specify the property name for the children array. It performs a DFS to find the parent node with the matching parentId, inserts the newNode at the specified position in the parent's children array, and returns the modified tree.

```ts
export function moveNode(
  node: Node | null | undefined,
  nodeId: string,
  newParentId: string,
  childrenKey: string = "children"
): Node | null {
  // Implementation details...
}
```

This function takes a `node` (the root of the tree), a `nodeId` to identify the node to move, a `newParentId` to identify the new parent node, and an optional `childrenKey` to specify the property name for the children array. It performs a DFS to find the node with the matching `nodeId`, removes it from its current parent's children array, and adds it to the children array of the node with the matching `newParentId`. It returns the modified tree. If either the node or the new parent is not found, it throws an error.

### Error Handling

All functions throw an error if any of the input parameters are null or undefined. The `moveNode` and `addNode` functions also throw an error if the target parent node is not found in the tree.

### Performance

All functions use an iterative DFS approach with a stack for tree traversal, which is efficient in terms of memory usage. They also create a deep copy of the tree before making modifications, ensuring that the original tree is not mutated.

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