Immutable Tree Data Structures for API Workflows
Pure-function tree manipulation with built-in adapters for OpenAPI documents, JSON Schemas, and documentation hierarchies. Find nodes, filter branches, sort, flatten, and build navigation trees — no mutations, no framework lock-in.
Build trees, manipulate trees
import { findNode, filterTree, flattenTree, sortTree } from "@powerduck/tree"; const tree = { id: "root", label: "API", children: [ { id: "users", label: "Users", children: [ { id: "get-users", label: "GET /users" }, { id: "create-user", label: "POST /users" }, ]}, ], }; const node = findNode(tree, "get-users"); const flat = flattenTree(tree); const sorted = sortTree(tree, (a, b) => a.label.localeCompare(b.label));
Tree operations, built right
Pure functions that never mutate the input tree. Built for navigation UIs, schema browsers, and API explorers.
OpenAPI Tree Adapter
buildOpenApiTree() converts an OpenAPI document into a navigation tree grouped by tags → paths → operations. Includes metadata like HTTP method, summary, and deprecated flags.
JSON Schema Tree Adapter
buildSchemaTree() visualizes JSON Schemas as expandable trees. Use findSchemaNodeByPath() and formatSchemaType() for precise navigation.
Doc Tree Adapter
buildDocTree() turns file lists into hierarchical documentation navigation with DocNodeKind metadata (page, section, group).
Immutable Operations
insertChild(), removeNode(), moveNode(), updateNode(), reorderNode() — all return new tree structures. No in-place mutations.
Filter & Search
filterTree() prunes branches matching a predicate. findNode() and findPath() locate nodes by ID or path. getExpandableIds() for expand-all UI state.
Analysis Helpers
countNodes(), getLeaves(), getMaxDepth(), getBranchIds(), flattenTree() — introspect and analyze tree structure.
Core functions
OpenApiTreeBuildResult { tree: OpenApiTreeNode[]; warnings: OpenApiNodeMetadata[] }SchemaTreeBuildResult { tree: SchemaTreeNode[] }TreeNode | null
Returns a new tree with branches that don't match removed. Parent nodes with matching descendants are preserved.
TreeNode[] — flat array in depth-first order
Immutable sort. Also available: sortNodes() for a single level.
Useful for initializing expand-all state in tree UI components.