mathsy-tree
v1.0.0
Published
Tree utility functions for Mathsy applications
Maintainers
Readme
Mathsy Tree
A lightweight tree utility package for Mathsy applications.
Installation
npm install mathsy-tree
# or
yarn add mathsy-treeUsage
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<M>: Generic interface for tree itemsid: string- Unique identifiername: string- Display nametype: 'folder' | 'content' | 'utility'- Item typechildren?: TreeItem<M>[]- Optional child itemsisPublic?: boolean- Optional public flagmetadata: M- Generic metadataicon?: string | null- Optional icon
TreeItemMeta<I>: Type utility to extract metadata type from a TreeItemfind<M>(tree: TreeItem<M>, test: (item: TreeItem<M>) => boolean): TreeItem<M> | undefined- Find first node matching the test function
findAncestry<M>(id: string, items: TreeItem<M>[]): TreeItem<M>[]- Get array of nodes in the path to the target ID
allIds<M>(tree: TreeItem<M>[]): string[]- Get all IDs in the tree
Development
- Clone the repository
- Install dependencies:
yarn install - Build the package:
yarn build
License
MIT
