{"name":"treechop: A Lightweight TypeScript Utility for Tree Data Structures","description":"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","url":"https://osrepos.com/repo/jinghaihan-treechop","source":"osrepos.com","sourceDescription":"This repository profile is provided by osrepos.com, an open source repository discovery platform.","repositoryProfile":"https://osrepos.com/repo/jinghaihan-treechop","generatedFor":"open source discovery and AI-assisted research","markdown":"https://osrepos.com/repo/jinghaihan-treechop.md","json":"https://osrepos.com/repo/jinghaihan-treechop.json","topics":["TypeScript","Tree Data Structure","Utility Library","JavaScript","Data Manipulation","Hierarchical Data"],"keywords":["TypeScript","Tree Data Structure","Utility Library","JavaScript","Data Manipulation","Hierarchical Data"],"stars":null,"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.","content":"## Introduction\n\n`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.\n\n## Installation\n\nTo integrate `treechop` into your project, use npm or yarn:\n\nbash\nnpm install treechop\n# or\nyarn add treechop\n\n\n## Examples\n\n`treechop` offers a wide range of functions to manage your tree data. Here are a few examples demonstrating its versatility:\n\n**Counting Nodes:**\nts\nimport { treeCount } from 'treechop';\n\nconst tree = [\n  { id: 1, value: 10, children: [{ id: 2, value: 20 }] },\n  { id: 3, value: 30 }\n];\n\nconst count = treeCount(tree, node => node.value % 10 === 0);\n// count will be 3\n\n\n**Deleting Nodes:**\nts\nimport { treeDelete } from 'treechop';\n\nconst tree = [\n  { id: 1, children: [{ id: 2 }] },\n  { id: 3 }\n];\n\nconst newTree = treeDelete(tree, node => node.id === 2);\n// newTree will be [{ id: 1, children: [] }, { id: 3 }]\n\n\n**Converting Flat Array to Tree:**\nts\nimport { treeFromArray } from 'treechop';\n\nconst flatNodes = [\n  { id: '1', name: 'Node 1' },\n  { id: '1-1', name: 'Node 1-1', pid: '1' },\n  { id: '2', name: 'Node 2' }\n];\n\nconst tree = treeFromArray(flatNodes, { idKey: 'id', parentKey: 'pid' });\n/*\ntree will be:\n[\n  { id: '1', name: 'Node 1', children: [{ id: '1-1', name: 'Node 1-1', pid: '1' }] },\n  { id: '2', name: 'Node 2' }\n]\n*/\n\n\n**Mapping Tree Nodes:**\nts\nimport { treeMap } from 'treechop';\n\nconst tree = [\n  { id: 1, name: 'alpha', children: [{ id: 2, name: 'beta' }] }\n];\n\nconst upperTree = treeMap(tree, node => ({ ...node, name: node.name.toUpperCase() }));\n/*\nupperTree will be:\n[\n  { id: 1, name: 'ALPHA', children: [{ id: 2, name: 'BETA' }] }\n]\n*/\n\n\n## Why Use `treechop`?\n\n`treechop` stands out for several reasons:\n\n*   **Lightweight and Efficient:** Designed to be minimal and performant for common tree operations.\n*   **Comprehensive Functionality:** Offers a rich set of utilities, from basic traversal to complex transformations.\n*   **TypeScript Support:** Fully typed, providing excellent developer experience and compile-time safety.\n*   **Customizable:** Most methods allow customization of key names (`childrenKey`, `idKey`, `parentKey`) and traversal strategies (`pre`, `post`, `breadth`), making it adaptable to various tree structures.\n*   **Clear API:** The functions are intuitive and well-documented, making them easy to learn and use.\n\n## Links\n\n*   [GitHub Repository](https://github.com/jinghaihan/treechop){:target=\"_blank\"}\n*   [npm Package](https://npmjs.com/package/treechop){:target=\"_blank\"}\n*   [JSDocs Reference](https://www.jsdocs.io/package/treechop){:target=\"_blank\"}","metrics":{"detailViews":1,"githubClicks":2},"dates":{"published":null,"modified":"2026-04-22T11:24:18.000Z"}}