Tsentials tree

From senrecep/tsentials · View interactive version

Tsentials tree is an AI agent skill from the GitHub repository senrecep/tsentials. Use when modelling recursive hierarchies: categories, comments, org charts.

How to use this skill

Skill file

tsentials/tree — Skill

Use when modelling recursive hierarchies: categories, comments, org charts.

API

import { Tree } from 'tsentials/tree';
import type { Tree as TreeType } from 'tsentials/tree'; // interface: { value: T; forest: ReadonlyArray<Tree<T>> }

// Construction
const t = Tree.of('root', [
  Tree.of('a', [Tree.leaf('a1')]),
  Tree.leaf('b'),
]);

// Query
Tree.root(t);        // 'root' — get root value
Tree.children(t);    // immediate child Tree nodes
Tree.size(t);        // total nodes
Tree.isLeaf(t);      // has no children?
Tree.find(t, v => v === 'a1');
Tree.findAll(t, v => v.startsWith('a'));

// Transform
Tree.map(t, v => v.toUpperCase());
Tree.filter(t, v => v !== 'b');
Tree.toArray(t);     // pre-order flatten
Tree.toArrayWithDepth(t); // [{ value: 'root', depth: 0 }, { value: 'a', depth: 1 }, ...]

// Fold
Tree.fold(t, (value, children) => value + children.reduce((a, b) => a + b, 0));

// Pretty print
Tree.drawTree(t);                       // uses String() by default
Tree.drawTree(t, v => v.toUpperCase()); // custom show function

Patterns

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.