this repo has no description
at main 174 lines 4.5 kB view raw view rendered
1# 🪾 `array-treeify` 2 3**Simple text trees from arrays using Unicode box-drawing characters. For your terminal and console displays.** 4 5[![typescript](https://img.shields.io/badge/TypeScript-007ACC?style=flat&logo=typescript&logoColor=white)](https://www.typescriptlang.org/) 6[![npm](https://img.shields.io/npm/v/array-treeify.svg)](https://www.npmjs.com/package/array-treeify) 7[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/tbeseda/array-treeify/blob/main/LICENSE) 8 9## Overview 10 11`array-treeify` transforms nested arrays into text trees with proper branching characters. Perfect for CLIs, debug outputs, or anywhere you need to visualize hierarchical data. 12 13```typescript 14treeify([ 15 'Lumon Industries', 16 [ 17 'Board of Directors', 18 ['Natalie (Representative)'], 19 'Departments', 20 [ 21 'Macrodata Refinement (Cobel)', 22 ['Milchick', 'Mark S.', ['Dylan G.', 'Irving B.', 'Helly R.']], 23 ], 24 'Other Departments', 25 [ 26 'Optics & Design', 27 'Wellness Center', 28 'Mammalians Nurturable', 29 'Choreography and Merriment', 30 ], 31 ], 32]) 33``` 34 35 36``` 37Lumon Industries 38├─ Board of Directors 39│ └─ Natalie (Representative) 40├─ Departments 41│ └─ Macrodata Refinement (Cobel) 42│ ├─ Milchick 43│ └─ Mark S. 44│ ├─ Dylan G. 45│ ├─ Irving B. 46│ └─ Helly R. 47└─ Other Departments 48 ├─ Optics & Design 49 ├─ Wellness Center 50 ├─ Mammalians Nurturable 51 └─ Choreography and Merriment 52``` 53 54## Installation 55 56```bash 57npm install array-treeify 58``` 59 60## Usage 61 62```typescript 63function treeify(input: TreeInput, options?: { 64 chars?: TreeChars, // Custom characters for the tree 65 plain?: boolean // Use plain whitespace instead of Unicode box-drawing characters 66}): string 67``` 68 69`array-treeify` accepts a simple, intuitive array structure that's easy to build and manipulate: 70 71```typescript 72import {treeify} from 'array-treeify' 73 74// Basic example 75const eagan = [ 76 'Kier Eagan', 77 [ 78 '...', 79 [ 80 '...', 81 'Jame Eagan', 82 ['Helena Eagan'] 83 ], 84 'Ambrose Eagan', 85 ], 86] 87console.log(treeify(eagan)) 88/* 89Kier Eagan 90├─ ... 91│ ├─ ... 92│ └─ Jame Eagan 93│ └─ Helena Eagan 94└─ Ambrose Eagan 95*/ 96 97// Using custom characters 98const resultCustomChars = treeify( 99 eagan, 100 { chars: { branch: '├• ', lastBranch: '└• ', pipe: '│ ', space: ' ' }, 101}) 102/* 103Kier Eagan 104├• ... 105│ ├• ... 106│ └• Jame Eagan 107│ └• Helena Eagan 108└• Ambrose Eagan 109*/ 110 111// Using plain whitespace characters 112console.log(treeify(eagan, { plain: true })) 113/* 114Kier Eagan 115 ... 116 ... 117 Jame Eagan 118 Helena Eagan 119 Ambrose Eagan 120*/ 121 122// Nested example 123const orgChart = [ 124 'Lumon Industries', 125 [ 126 'Board of Directors', 127 ['Natalie (Representative)'], 128 'Department Heads', 129 [ 130 'Cobel (MDR)', 131 ['Milchick', 'Mark S.', ['Dylan G.', 'Irving B.', 'Helly R.']] 132 ] 133 ] 134] 135console.log(treeify(orgChart)) 136/* 137Lumon Industries 138├─ Board of Directors 139│ └─ Natalie (Representative) 140└─ Department Heads 141 └─ Cobel (MDR) 142 ├─ Milchick 143 └─ Mark S. 144 ├─ Dylan G. 145 ├─ Irving B. 146 └─ Helly R. 147*/ 148``` 149 150## Input Format 151 152> **Disclaimer:** 153> The exported `TreeInput` type (`Array<string | TreeInput>`) is intentionally flexible to support dynamic and programmatic tree construction. However, TypeScript cannot enforce at the type level that the first element is a string. This requirement is checked at runtime by the `treeify` function, which will throw an error if the first element is not a string. Please ensure your input arrays follow this convention. 154 155The `treeify` function accepts arrays with the following structure: 156 1571. First element must be a string (the root node) 1582. Subsequent elements can be strings (nodes at same level) or arrays (children of previous node) 1593. Arrays can be nested to any depth 160 161```typescript 162['root', 'sibling', ['child1', 'child2']] // Root with 2 children 163['root', ['child'], 'sibling', ['nephew', 'niece']] // 2 root nodes with children 164['root', ['child', ['grandchild']]] // Grandchildren 165``` 166 167## Options 168 169- `chars`: Custom characters for the tree. Defaults to Unicode box-drawing characters. 170- `plain`: When true, uses plain whitespace characters instead of Unicode box-drawing characters. 171 172## License 173 174MIT © tbeseda