# treechop: A Lightweight TypeScript Utility for Tree Data Structures

This repository profile is provided by osrepos.com, an open source repository discovery platform.

Source: osrepos.com
Repository profile: https://osrepos.com/repo/jinghaihan-treechop
Generated for open source discovery and AI-assisted research.

treechop is a lightweight TypeScript utility library designed to simplify working with tree data structures in JavaScript and TypeScript projects. It offers a comprehensive set of common operations, including counting, deleting, filtering, mapping, and converting between flat arrays and tree formats. Developers can efficiently manage hierarchical data with customizable options for keys and traversal strategies.

GitHub: https://github.com/jinghaihan/treechop
OSRepos URL: https://osrepos.com/repo/jinghaihan-treechop

## Summary

treechop is a lightweight TypeScript utility library designed to simplify working with tree data structures in JavaScript and TypeScript projects. It offers a comprehensive set of common operations, including counting, deleting, filtering, mapping, and converting between flat arrays and tree formats. Developers can efficiently manage hierarchical data with customizable options for keys and traversal strategies.

## Topics

- TypeScript
- Tree Data Structure
- Utility Library
- JavaScript
- Data Manipulation
- Hierarchical Data

## Repository Information

Last analyzed by OSRepos: Wed Apr 22 2026 12:24:18 GMT+0100 (Western European Summer Time)
Detail views: 1
GitHub clicks: 2

## Safety Notice

OSRepos shares public repositories for knowledge and discovery only. Review source code, dependencies, licenses, and security implications before running or installing anything.

## Content

## Introduction

`treechop` is a powerful, lightweight TypeScript utility library specifically crafted for handling tree data structures. It provides a comprehensive suite of functions to perform common operations such as traversal, manipulation, and conversion of hierarchical data. With `treechop`, developers can easily count nodes, delete specific branches, filter elements, map values, and transform data between flat arrays and nested tree formats, all while offering flexible customization options for keys and traversal methods.

## Installation

To integrate `treechop` into your project, use npm or yarn:

bash
npm install treechop
# or
yarn add treechop


## Examples

`treechop` offers a wide range of functions to manage your tree data. Here are a few examples demonstrating its versatility:

**Counting Nodes:**
ts
import { treeCount } from 'treechop';

const tree = [
  { id: 1, value: 10, children: [{ id: 2, value: 20 }] },
  { id: 3, value: 30 }
];

const count = treeCount(tree, node => node.value % 10 === 0);
// count will be 3


**Deleting Nodes:**
ts
import { treeDelete } from 'treechop';

const tree = [
  { id: 1, children: [{ id: 2 }] },
  { id: 3 }
];

const newTree = treeDelete(tree, node => node.id === 2);
// newTree will be [{ id: 1, children: [] }, { id: 3 }]


**Converting Flat Array to Tree:**
ts
import { treeFromArray } from 'treechop';

const flatNodes = [
  { id: '1', name: 'Node 1' },
  { id: '1-1', name: 'Node 1-1', pid: '1' },
  { id: '2', name: 'Node 2' }
];

const tree = treeFromArray(flatNodes, { idKey: 'id', parentKey: 'pid' });
/*
tree will be:
[
  { id: '1', name: 'Node 1', children: [{ id: '1-1', name: 'Node 1-1', pid: '1' }] },
  { id: '2', name: 'Node 2' }
]
*/


**Mapping Tree Nodes:**
ts
import { treeMap } from 'treechop';

const tree = [
  { id: 1, name: 'alpha', children: [{ id: 2, name: 'beta' }] }
];

const upperTree = treeMap(tree, node => ({ ...node, name: node.name.toUpperCase() }));
/*
upperTree will be:
[
  { id: 1, name: 'ALPHA', children: [{ id: 2, name: 'BETA' }] }
]
*/


## Why Use `treechop`?

`treechop` stands out for several reasons:

*   **Lightweight and Efficient:** Designed to be minimal and performant for common tree operations.
*   **Comprehensive Functionality:** Offers a rich set of utilities, from basic traversal to complex transformations.
*   **TypeScript Support:** Fully typed, providing excellent developer experience and compile-time safety.
*   **Customizable:** Most methods allow customization of key names (`childrenKey`, `idKey`, `parentKey`) and traversal strategies (`pre`, `post`, `breadth`), making it adaptable to various tree structures.
*   **Clear API:** The functions are intuitive and well-documented, making them easy to learn and use.

## Links

*   [GitHub Repository](https://github.com/jinghaihan/treechop){:target="_blank"}
*   [npm Package](https://npmjs.com/package/treechop){:target="_blank"}
*   [JSDocs Reference](https://www.jsdocs.io/package/treechop){:target="_blank"}