— MIT Licensed — 100% TypeScript

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.

$npm install @powerduck/tree
Pure FunctionsOpenAPI AdapterSchema AdapterDoc AdapterImmutable
Quick Start

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));
Features

Tree operations, built right

Pure functions that never mutate the input tree. Built for navigation UIs, schema browsers, and API explorers.

02

JSON Schema Tree Adapter

buildSchemaTree() visualizes JSON Schemas as expandable trees. Use findSchemaNodeByPath() and formatSchemaType() for precise navigation.

03

Doc Tree Adapter

buildDocTree() turns file lists into hierarchical documentation navigation with DocNodeKind metadata (page, section, group).

04

Immutable Operations

insertChild(), removeNode(), moveNode(), updateNode(), reorderNode() — all return new tree structures. No in-place mutations.

05

Filter & Search

filterTree() prunes branches matching a predicate. findNode() and findPath() locate nodes by ID or path. getExpandableIds() for expand-all UI state.

06

Analysis Helpers

countNodes(), getLeaves(), getMaxDepth(), getBranchIds(), flattenTree() — introspect and analyze tree structure.

API Reference

Core functions

functionbuildOpenApiTree(doc)Build a navigation tree from an OpenAPI document
Returns
OpenApiTreeBuildResult { tree: OpenApiTreeNode[]; warnings: OpenApiNodeMetadata[] }
functionbuildSchemaTree(schema)Build a tree from a JSON Schema
Returns
SchemaTreeBuildResult { tree: SchemaTreeNode[] }
functionfindNode(tree, id)Find a node by its ID
Returns
TreeNode | null
functionfilterTree(tree, predicate)Prune tree keeping only nodes matching predicate

Returns a new tree with branches that don't match removed. Parent nodes with matching descendants are preserved.

functionflattenTree(tree)Flatten a tree into a depth-ordered array
Returns
TreeNode[] — flat array in depth-first order
functionsortTree(tree, comparator)Sort children at every level

Immutable sort. Also available: sortNodes() for a single level.

functiongetExpandableIds(tree)Get IDs of all nodes with children

Useful for initializing expand-all state in tree UI components.

3
Built-in Adapters
15+
Tree Functions
0.7.7
Current Version
MIT
License