treechop: A Lightweight TypeScript Utility for Tree Data Structures

treechop: A Lightweight TypeScript Utility for Tree Data Structures

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.

Repository Info

Updated on April 22, 2026
View on GitHub

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:

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:

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:

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:

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:

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?

  • 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