this repo has no description

more permissive input type

+41 -19
+3
README.md
··· 149 149 150 150 ## Input Format 151 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 + 152 155 The `treeify` function accepts arrays with the following structure: 153 156 154 157 1. First element must be a string (the root node)
+10 -10
src/index.ts
··· 1 1 /** 2 - * Represents a node in the tree structure. 3 - * Can be either a string (a leaf node) or an array of TreeNodes (a branch with children). 2 + * The strict tree input format. Must start with a string. 3 + * This type is exported for testing purposes and advanced usage. 4 4 */ 5 - type TreeNode = string | TreeNode[] 5 + export type TreeInput = Array<string | TreeInput> 6 6 7 7 /** 8 - * The strict tree input format. Must start with a string. 9 - * This type is exported for testing purposes and advanced usage. 8 + * Represents a node in the tree structure. 9 + * Can be either a string (a leaf node) or an array of TreeNodes (a branch with children). 10 10 */ 11 - export type TreeInput = [string, ...Array<string | TreeNode[]>] | string[] 11 + type TreeNode = string | TreeNode[] 12 12 13 13 /** 14 14 * Flexible input type that accepts any array. ··· 16 16 */ 17 17 type FlexibleTreeInput = readonly (string | unknown[])[] 18 18 19 - type TreeChars = { 19 + /** 20 + * ASCII characters used to render the tree. 21 + */ 22 + export type TreeChars = { 20 23 branch: string 21 24 lastBranch: string 22 25 pipe: string 23 26 space: string 24 27 } 25 28 26 - /** 27 - * @description ASCII characters used to render the tree. 28 - */ 29 29 const DEFAULT_CHARS: TreeChars = { 30 30 branch: '├─ ', 31 31 lastBranch: '└─ ',
+28 -9
src/types.test.ts
··· 4 4 import type { TreeInput } from './index.js' 5 5 6 6 describe('treeify types', () => { 7 - test('input with values', () => { 7 + test('input values', () => { 8 8 const inputWithValues: TreeInput = ['root', ['child1', 'child2', 'child3']] 9 9 let result = treeify(inputWithValues) 10 10 assert.ok(result) 11 11 12 - const inputWithoutValues: TreeInput = [] 13 - result = treeify(inputWithoutValues) 12 + const emptyInput: TreeInput = [] 13 + result = treeify(emptyInput) 14 14 assert.equal(result, '') // empty string 15 15 16 + const inputBuildUp: TreeInput = [] 17 + inputBuildUp.push('root') 18 + inputBuildUp.push(['child1', 'child2', 'child3']) 19 + inputBuildUp.push('root2', ['cousin1', 'cousin2', 'cousin3']) 20 + result = treeify(inputBuildUp) 21 + assert.ok(result) 22 + 16 23 // @ts-expect-error 17 24 const inputWithoutRootString: TreeInput = [{ bad: 'root' }, 'root2'] 18 25 assert.throws(() => treeify(inputWithoutRootString), { 19 26 message: 'First element must be a string', 20 27 }) 21 28 29 + const inputWithRootStringAndInvalidValues: TreeInput = ['root'] 22 30 // @ts-expect-error 23 - const inputWithRootStringAndInvalidValues: TreeInput = [ 24 - 'root', 25 - 1, 26 - {}, 27 - ['child1', 'child2', 'child3'], 28 - ] 31 + inputWithRootStringAndInvalidValues.push(1) 32 + // @ts-expect-error 33 + inputWithRootStringAndInvalidValues.push({}, [], Number.POSITIVE_INFINITY) 29 34 result = treeify(inputWithRootStringAndInvalidValues) 30 35 assert.ok(result) // non-strings are ignored 36 + }) 37 + 38 + test('generated inputs', () => { 39 + function generateInput(): TreeInput { 40 + const out: TreeInput = [] 41 + out.push('root') 42 + out.push(['child1', 'child2', 'child3']) 43 + return out 44 + } 45 + 46 + const input: TreeInput = ['root', generateInput()] 47 + input.push(generateInput()) 48 + 49 + assert.ok(treeify(input)) 31 50 }) 32 51 })