Tree utility functions for Mathsy applications
npm install mathsy-treebash
npm install mathsy-tree
or
yarn add mathsy-tree
`
Usage
`typescript
import { TreeItem, find, findAncestry, allIds } from 'mathsy-tree';
// Define your tree structure
const tree: TreeItem<{ description: string }> = {
id: 'root',
name: 'Root',
type: 'folder',
metadata: { description: 'Root folder' },
children: [
{
id: 'child1',
name: 'Child 1',
type: 'content',
metadata: { description: 'First child' }
}
]
};
// Find a node by condition
const found = find(tree, item => item.id === 'child1');
// Find ancestry chain for a node
const ancestry = findAncestry('child1', [tree]);
// Get all IDs in the tree
const ids = allIds([tree]);
`
Available Types and Functions
- TreeItem: Generic interface for tree items
- id: string - Unique identifier
- name: string - Display name
- type: 'folder' | 'content' | 'utility' - Item type
- children?: TreeItem - Optional child items
- isPublic?: boolean - Optional public flag
- metadata: M - Generic metadata
- icon?: string | null - Optional icon
- TreeItemMeta: Type utility to extract metadata type from a TreeItem
- find
- Find first node matching the test function
- findAncestry
- Get array of nodes in the path to the target ID
- allIds
- Get all IDs in the tree
Development
1. Clone the repository
2. Install dependencies: yarn install
3. Build the package: yarn build`