Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 | 3x 101x 101x 101x 101x 101x 101x 101x 101x 101x 55x 55x 330x 330x 4x 201x 330x 330x 330x 330x 330x 616022x 616022x 157089x 22549903x 616016x 758x 432x 45x 45x 94x 45x 432x 432x 326x 758x 685x 685x 685x 685x 685x 685x 685x 751x 749x 749x 748x 748x 748x 748x 748x 748x 682x 13160x 9088x 9088x 9088x 1326x 7762x 7762x 3021x 4741x 4741x 4741x 7762x 9090x 9090x 2x 9088x 9088x 9088x 9088x 9088x 9088x 9088x 9088x 9088x 9088x 9088x 22175x 6375x 15800x 1387x 14413x 1326x 13087x 13087x 13087x 13087x 13087x 13087x 13087x 13087x 13087x 13087x 22175x 22175x 470x 228x 228x 226x 226x 398x 226x 244x 244x 243x 243x 241x 1x 240x 240x 123x 123x 117x 117x 117x 117x 117x 117x 117x 240x 240x 200x 200x 200x 200x 200x 200x 116x 116x 84x 84x 84x 84x 84x 200x 200x 5x 5x 5x 3x 3x 3x 9x 9x 9x 9x 3x 602833x 602833x 1x 1x 1x 602832x 466008x 10713x 466008x 136824x 136692x 136692x 136679x 136679x 5004x 5004x 131675x 131675x 131675x 131675x 131675x 602181x 602181x 602181x 602181x 131031x 131031x 131675x 131675x 131675x 131675x 131675x 131675x 18x 18x 18x 18x 566x 566x 566x 4x 4x 562x 1x 562x 562x 18x 18x 18x 18x 18x 18x 2x 2x 2x 2x 2x 17x 17x 14x 14x 21x 21x 21x 7x 14x 8x 8x 8x 8x 16x 16x 7x 7x 9x 9x 9x 11x 11x 10x 10x 11x 8x 8x 8x 8x 13x 13x 12x 12x 12x 12x 12x 12x 9x 9x 6x 6x 6x 6x 5x 5x 12x 12x 12x 12x 12x 12x 12x 101x 86x 86x 101x 8x 8x 662x | /**
* The tree engine (SPEC.md §4.2) — the reason this package exists.
*
* ## The idea in one paragraph
*
* The parsed value **is** the tree; it is already in memory and nothing is
* gained by copying it. A collapsed container is exactly **one** row no matter
* how much it contains. So the only state worth keeping is bookkeeping for the
* nodes the user actually expanded — an "expanded spine" that is typically a
* few dozen records deep even when the document has millions of nodes. Row
* lookup then walks that spine instead of an array: `rowAt(i)` is
* `O(depth · log e)`, and memory is proportional to what was clicked, not to
* the size of the data.
*
* That is the whole differentiator. Every incumbent renders the entire tree.
*
* ## What is *not* stored
*
* No row array. No per-node descriptor. No path strings. A {@link JsonRow} is
* built during descent and handed out; keeping 4.5 M of them is exactly the
* problem being avoided.
*/
import {
DenseOrder,
SparseOrder,
toDense,
type OrderIndex,
} from "./order";
import { pathToPointer, pointerToSegments } from "./path";
import {
bracketsOf,
childAccessor,
childCountOf,
classify,
displayOf,
isContainerKind,
previewOf,
type ChildAccessor,
} from "./value";
import type {
ExpandResult,
JsonKind,
JsonPath,
JsonRow,
JsonTreeModel,
JsonTreeOptions,
PathSegment,
} from "../types";
/**
* Recursion ceiling for *bulk* expansion (`expandAll`, `setExpandedPaths`).
*
* Manual expansion is iterative and has no such limit — this only stops a
* pathologically nested document from overflowing the call stack during a
* whole-tree walk, and a document nested deeper than this is not one anybody
* expands in full anyway.
*/
const MAX_BULK_DEPTH = 512;
/** One expanded container. Leaves and collapsed containers have no record. */
interface Node {
/** The value this node was built over, for cycle checks and re-reads. */
value: unknown;
kind: JsonKind;
accessor: ChildAccessor;
order: OrderIndex;
/** Expanded children, by child index. */
children: Map<number, Node>;
/**
* Children that were expanded and then collapsed, kept so re-expanding
* restores what was inside.
*
* Collapsing a big object to scroll past it and losing every expansion under
* it is the small papercut every viewer that forgets has. The data is
* read-only by contract, so a retired subtree's row counts stay valid and
* restoring it is a map move.
*
* The cost is honest bookkeeping: memory is proportional to what has *ever*
* been expanded in this model, not to what is expanded now. `collapseAll`,
* `expandAll` and `setExpandedPaths` all drop these — the first because it
* is meant to be a reset, the other two because they rebuild the spine.
*/
hidden: Map<number, Node>;
/** Rows this node occupies: opening + children + closing. */
rows: number;
/** Lazy key → child-index map; objects only (see {@link childIndexOf}). */
keyIndex: Map<string, number> | null;
}
/** Where a path lands in the current expansion state. */
interface Located {
/** Expanded nodes from the root down to the path's parent. */
chain: Node[];
/** Child index of each path segment within `chain[i]`. */
indices: number[];
value: unknown;
kind: JsonKind;
/** The node's own record, when it is expanded. */
node: Node | null;
}
/**
* Create a tree model over `data`.
*
* The model holds a **live reference**; it never clones or mutates the input,
* and the caller keeps ownership. Changing `data` means creating a new model.
*
* @example
* ```ts
* const tree = createJsonTree({ users: [{ id: 1 }] }, { defaultExpandDepth: 2 });
* tree.rowCount(); // 5 → { · users: [ · { 1 key } · ] · }
* tree.rowAt(2).display;
* ```
*/
export function createJsonTree(
data: unknown,
options: JsonTreeOptions = {},
): JsonTreeModel {
const maxValueLength = options.maxValueLength ?? 120;
const sortKeys = options.sortKeys ?? false;
const defaultExpandDepth = options.defaultExpandDepth ?? 1;
const maxExpandedRows = options.maxExpandedRows ?? 100_000;
// 1024 is where `pnpm bench` measures the crossover — see JsonTreeOptions.
const denseThreshold = options.denseThreshold ?? 1024;
const rootKind = classify(data);
let root: Node | null = null;
/** The root's retired subtree, when the root itself was collapsed. */
let hiddenRoot: Node | null = null;
let ver = 0;
// ── construction ────────────────────────────────────────────────────────
function makeNode(value: unknown, kind: JsonKind): Node {
const accessor = childAccessor(value, kind, sortKeys);
return {
value,
kind,
accessor,
order: new SparseOrder(accessor.count),
children: new Map(),
hidden: new Map(),
rows: accessor.count + 2,
keyIndex: null,
};
}
/**
* Record a child's row count on its parent, promoting the parent's index to
* dense once enough siblings are expanded (SPEC.md §4.2).
*/
function setChildRows(parent: Node, child: number, rows: number): void {
parent.order.set(child, rows);
if (
parent.order instanceof SparseOrder &&
parent.order.expandedCount > denseThreshold
) {
parent.order = toDense(parent.order);
}
}
/** Push a subtree's new row count back up to the root. */
function propagate(chain: Node[], indices: number[]): void {
for (let i = chain.length - 1; i >= 0; i--) {
const parent = chain[i]!;
const index = indices[i]!;
const child = parent.children.get(index);
setChildRows(parent, index, child ? child.rows : 1);
parent.rows = parent.order.totalRows() + 2;
}
}
// ── kinds and cycles ────────────────────────────────────────────────────
/**
* Classify `value`, reporting `"circular"` when it is one of its own
* ancestors.
*
* A cycle is a property of the path, not of the value, so this cannot live
* in `classify`. The check is `O(depth)` against a chain that is a couple of
* dozen entries in any realistic document — and without it, `expandAll` on a
* self-referential object never terminates.
*/
function kindOf(value: unknown, ancestors: readonly unknown[]): JsonKind {
const kind = classify(value);
if (value !== null && typeof value === "object") {
for (let i = ancestors.length - 1; i >= 0; i--) {
if (ancestors[i] === value) return "circular";
}
}
return kind;
}
// ── path resolution ─────────────────────────────────────────────────────
/**
* Child index for one path segment.
*
* Arrays, `Map`s and `Set`s are addressed positionally, so the segment *is*
* the index. Objects need a key → index lookup, which is built once per
* expanded node and cached: a linear scan would make `rowIndexOf` `O(k)` on
* a wide object, and search jumps call it on every match.
*/
function childIndexOf(node: Node, segment: PathSegment): number {
if (node.kind === "object") {
if (!node.keyIndex) {
const map = new Map<string, number>();
for (let i = 0; i < node.accessor.count; i++) {
map.set(String(node.accessor.keyAt(i)), i);
}
node.keyIndex = map;
}
const index = node.keyIndex.get(String(segment));
return index === undefined ? -1 : index;
}
const index = typeof segment === "number" ? segment : Number(segment);
return Number.isInteger(index) && index >= 0 && index < node.accessor.count
? index
: -1;
}
function locate(path: JsonPath): Located | null {
let value: unknown = data;
let kind = rootKind;
let node: Node | null = root;
const chain: Node[] = [];
const indices: number[] = [];
const ancestors: unknown[] = [];
for (const segment of path) {
if (!node) return null; // an ancestor is collapsed
const index = childIndexOf(node, segment);
if (index < 0) return null;
chain.push(node);
indices.push(index);
ancestors.push(node.value);
value = node.accessor.valueAt(index);
kind = kindOf(value, ancestors);
node = node.children.get(index) ?? null;
}
return { chain, indices, value, kind, node };
}
// ── reads ───────────────────────────────────────────────────────────────
function rowCount(): number {
return root ? root.rows : 1;
}
function makeRow(
index: number,
path: PathSegment[],
key: PathSegment | undefined,
label: string,
value: unknown,
kind: JsonKind,
depth: number,
node: Node | null,
closing: boolean,
posInSet: number,
setSize: number,
): JsonRow {
const container = isContainerKind(kind);
const childCount = node ? node.accessor.count : container ? childCountOf(value, kind) : 0;
if (closing) {
return {
index,
path,
key,
label,
kind,
depth,
closing: true,
expandable: false,
expanded: true,
childCount,
posInSet,
setSize,
display: bracketsOf(kind)[1],
truncated: false,
value,
};
}
let display: string;
let truncated = false;
if (container) {
display = node
? bracketsOf(kind)[0]
: previewOf(value, kind, childCount);
} else {
const rendered = displayOf(value, kind, maxValueLength);
display = rendered.text;
truncated = rendered.truncated;
}
return {
index,
path,
key,
label,
kind,
depth,
closing: false,
expandable: container && childCount > 0,
expanded: node !== null,
childCount,
posInSet,
setSize,
display,
truncated,
value,
};
}
function rowAt(index: number): JsonRow {
const total = rowCount();
if (!Number.isInteger(index) || index < 0 || index >= total) {
throw new RangeError(
`json-viewer: row ${index} is out of range (0…${total - 1})`,
);
}
const path: PathSegment[] = [];
const ancestors: unknown[] = [];
let value: unknown = data;
let kind = rootKind;
let key: PathSegment | undefined;
let label = "";
let node: Node | null = root;
let depth = 0;
let local = index;
// The root is an only child of nothing; 1 of 1 is the honest answer.
let posInSet = 1;
let setSize = 1;
// Descend the expanded spine. Each iteration consumes one level.
for (;;) {
if (!node) {
return makeRow(index, path, key, label, value, kind, depth, null, false, posInSet, setSize);
}
if (local === 0) {
return makeRow(index, path, key, label, value, kind, depth, node, false, posInSet, setSize);
}
if (local === node.rows - 1) {
return makeRow(index, path, key, label, value, kind, depth, node, true, posInSet, setSize);
}
const { child, offset } = node.order.locate(local - 1);
ancestors.push(node.value);
key = node.accessor.keyAt(child);
label = node.accessor.labelAt(child);
path.push(key);
value = node.accessor.valueAt(child);
kind = kindOf(value, ancestors);
posInSet = child + 1;
setSize = node.accessor.count;
node = node.children.get(child) ?? null;
depth++;
local = offset;
}
}
function rowIndexOf(path: JsonPath): number {
if (path.length === 0) return 0;
const found = locate(path);
if (!found) return -1;
let row = 0;
for (let i = 0; i < found.chain.length; i++) {
row += 1 + found.chain[i]!.order.rowStartOf(found.indices[i]!);
}
return row;
}
// ── mutations ───────────────────────────────────────────────────────────
function expand(path: JsonPath): ExpandResult {
const found = locate(path);
if (!found) return { delta: 0, anchorRow: -1, refused: { reason: "unreachable" } };
const anchorRow = rowIndexOf(path);
if (found.node) return { delta: 0, anchorRow };
if (!isContainerKind(found.kind) || childCountOf(found.value, found.kind) === 0) {
return { delta: 0, anchorRow, refused: { reason: "not-expandable" } };
}
const before = rowCount();
if (found.chain.length === 0) {
// Restore what was inside before, if this node was collapsed rather
// than never opened.
root = hiddenRoot && hiddenRoot.value === found.value
? hiddenRoot
: makeNode(found.value, found.kind);
hiddenRoot = null;
} else {
const parent = found.chain[found.chain.length - 1]!;
const index = found.indices[found.indices.length - 1]!;
const remembered = parent.hidden.get(index);
const node =
remembered && remembered.value === found.value
? remembered
: makeNode(found.value, found.kind);
parent.hidden.delete(index);
parent.children.set(index, node);
propagate(found.chain, found.indices);
}
ver++;
return { delta: rowCount() - before, anchorRow };
}
function collapse(path: JsonPath): ExpandResult {
const found = locate(path);
Iif (!found) return { delta: 0, anchorRow: -1, refused: { reason: "unreachable" } };
const anchorRow = rowIndexOf(path);
Iif (!found.node) return { delta: 0, anchorRow };
const before = rowCount();
if (found.chain.length === 0) {
hiddenRoot = root;
root = null;
} else {
const parent = found.chain[found.chain.length - 1]!;
const index = found.indices[found.indices.length - 1]!;
parent.hidden.set(index, found.node);
parent.children.delete(index);
propagate(found.chain, found.indices);
}
ver++;
return { delta: rowCount() - before, anchorRow };
}
function toggle(path: JsonPath): ExpandResult {
const found = locate(path);
Iif (!found) return { delta: 0, anchorRow: -1, refused: { reason: "unreachable" } };
return found.node ? collapse(path) : expand(path);
}
function expandTo(path: JsonPath): ExpandResult {
const before = rowCount();
let anchorRow = -1;
for (let i = 0; i < path.length; i++) {
const prefix = path.slice(0, i);
const result = expand(prefix);
Iif (result.refused) {
// A refusal on a proper ancestor means the path is unreachable; a
// refusal on a leaf ancestor should not happen, but reporting it beats
// silently expanding half the chain.
if (result.refused.reason === "unreachable") {
return { delta: rowCount() - before, anchorRow, refused: result.refused };
}
}
if (anchorRow < 0 && result.delta !== 0) anchorRow = result.anchorRow;
}
return { delta: rowCount() - before, anchorRow: anchorRow < 0 ? 0 : anchorRow };
}
// ── bulk expansion ──────────────────────────────────────────────────────
interface Budget {
rows: number;
limit: number;
exceeded: boolean;
/** True when {@link MAX_BULK_DEPTH} stopped the walk before `maxDepth` did. */
depthCapped: boolean;
/**
* True when this pass's `maxDepth` — not the budget — left an expandable
* container closed. It is what tells the iterative deepening that another
* level is worth trying.
*/
depthBlocked: boolean;
}
/**
* Build a whole expanded subtree in one pass.
*
* Bulk expansion must never go through repeated {@link expand}: each call
* re-walks the spine and each `SparseOrder.set` is an `O(e)` splice, so
* expanding 100 000 siblings one at a time is quadratic. Here every
* container's index is constructed once, from the finished child list.
*
* Two rules keep the row budget from doing harm:
*
* - **Anything in `keep` is rebuilt regardless of the budget.** A bulk
* expansion adds; it must never *remove* what the user already opened.
* Without this, "expand all" on a document that is already past the
* budget would collapse the whole tree back to the root.
* - **Depth 0 is free.** Opening the root is one container, exactly like
* the click that opens any other container — and a single expansion has
* never been budgeted (SPEC §4.6: the ceiling is `expandAll`'s). Charging
* it here meant `createJsonTree(arrayOfTwoHundredThousand)` rendered a
* single collapsed row and said nothing about why.
*/
function build(
value: unknown,
kind: JsonKind,
depth: number,
maxDepth: number,
ancestors: unknown[],
budget: Budget,
keep: Node | null,
): Node | null {
const keeping = keep !== null && keep.value === value;
if (depth >= MAX_BULK_DEPTH) {
// Report it only when there was something down there to expand — a cap
// that stops at a leaf has not truncated anything.
Eif (isContainerKind(kind) && childCountOf(value, kind) > 0) {
budget.depthCapped = true;
}
return null;
}
if (!keeping && depth >= maxDepth) {
if (isContainerKind(kind) && childCountOf(value, kind) > 0) {
budget.depthBlocked = true;
}
return null;
}
if (!isContainerKind(kind)) return null;
const accessor = childAccessor(value, kind, sortKeys);
if (accessor.count === 0) return null;
// Children plus the closing row. Checked before any of them is built, so
// the budget can never be blown by a single wide container.
const cost = accessor.count + 1;
if (!keeping && depth > 0 && budget.rows + cost > budget.limit) {
budget.exceeded = true;
return null;
}
budget.rows += cost;
const children = new Map<number, Node>();
const pairs: [number, number][] = [];
ancestors.push(value);
for (let i = 0; i < accessor.count; i++) {
const childValue = accessor.valueAt(i);
const childKind = kindOf(childValue, ancestors);
const child = build(
childValue,
childKind,
depth + 1,
maxDepth,
ancestors,
budget,
keep?.children.get(i) ?? null,
);
if (child) {
children.set(i, child);
pairs.push([i, child.rows]);
}
}
ancestors.pop();
let order: OrderIndex;
Iif (pairs.length > denseThreshold) {
order = new DenseOrder(accessor.count, pairs);
} else {
const sparse = new SparseOrder(accessor.count);
sparse.bulk(pairs);
order = sparse;
}
return {
value,
kind,
accessor,
order,
children,
// Retired subtrees do not survive a bulk rebuild; the operation is an
// explicit, whole-tree state change.
hidden: new Map(),
rows: order.totalRows() + 2,
keyIndex: null,
};
}
/**
* Expand as deeply as the row budget allows, and say so when it stops.
*
* **Iterative deepening**, not one greedy pass: "the deepest level that
* fits" (SPEC §4.6) means every node at depth *d* is open before any node at
* depth *d + 1* is considered. A single greedy DFS instead expands whatever
* it meets first in document order and abandons the rest of the document
* once the budget runs out, which is arbitrary rather than deep.
*
* Each pass unions with the current spine, so the row count can only go up.
*/
function expandAll(maxDepth = Number.POSITIVE_INFINITY): ExpandResult {
const before = rowCount();
const cap = Math.min(
Number.isFinite(maxDepth) ? maxDepth : MAX_BULK_DEPTH,
MAX_BULK_DEPTH,
);
let committed = root;
let refused: ExpandResult["refused"];
for (let depth = 1; depth <= cap; depth++) {
const budget: Budget = {
rows: 1,
limit: maxExpandedRows,
exceeded: false,
depthCapped: false,
depthBlocked: false,
};
const candidate = build(data, rootKind, 0, depth, [], budget, root);
if (budget.exceeded) {
refused = { reason: "row-budget", budget: maxExpandedRows };
break;
}
if (budget.depthCapped) {
refused = { reason: "depth-cap", budget: MAX_BULK_DEPTH };
}
committed = candidate;
// Stop on "nothing was held back by the depth limit", not on "the row
// count did not grow": a subtree the user had already opened is kept
// regardless of depth, so an early pass can reproduce the current row
// count exactly while there is still plenty below it to find.
if (!budget.depthBlocked) break;
}
root = committed;
hiddenRoot = null;
ver++;
const result: ExpandResult = { delta: rowCount() - before, anchorRow: 0 };
if (refused) result.refused = refused;
return result;
}
/**
* Collapse everything **and forget what was open**.
*
* Unlike collapsing one node, this is a reset: keeping the retired subtrees
* would mean re-opening the root silently restores the whole previous state,
* which is not what anybody pressing "collapse all" is asking for.
*/
function collapseAll(): ExpandResult {
const before = rowCount();
root = null;
hiddenRoot = null;
ver++;
return { delta: rowCount() - before, anchorRow: 0 };
}
// ── expansion state as strings ──────────────────────────────────────────
/**
* The expanded set as **JSON Pointers** (RFC 6901).
*
* Pointers rather than the `pathFormat` used for copying: they are
* unambiguous, cheap to parse, and a segment's array-index-vs-object-key
* meaning is recovered from the container it lands in, so the round trip is
* exact.
*
* Only the *live* spine is reported. Subtrees that were expanded and then
* collapsed are remembered internally (see `Node.hidden`) but are not part of
* the expansion state a consumer controls or persists.
*/
function getExpandedPaths(): string[] {
const out: string[] = [];
if (!root) return out;
const stack: { node: Node; path: PathSegment[] }[] = [{ node: root, path: [] }];
while (stack.length) {
const { node, path } = stack.pop()!;
out.push(pathToPointer(path));
for (const [index, child] of node.children) {
stack.push({ node: child, path: [...path, node.accessor.keyAt(index)] });
}
}
return out.sort();
}
/** A prefix tree of the pointers to restore. */
interface Trie {
children: Map<string, Trie>;
}
function setExpandedPaths(paths: readonly string[]): ExpandResult {
const before = rowCount();
const trie: Trie = { children: new Map() };
let wantsRoot = false;
for (const pointer of paths) {
const segments = pointerToSegments(pointer);
if (segments.length === 0) {
wantsRoot = true;
continue;
}
wantsRoot = true;
let cursor = trie;
for (const segment of segments) {
let next = cursor.children.get(segment);
if (!next) {
next = { children: new Map() };
cursor.children.set(segment, next);
}
cursor = next;
}
}
root = wantsRoot ? buildFromTrie(data, rootKind, trie, 0, []) : null;
hiddenRoot = null;
ver++;
return { delta: rowCount() - before, anchorRow: 0 };
}
function buildFromTrie(
value: unknown,
kind: JsonKind,
trie: Trie,
depth: number,
ancestors: unknown[],
): Node | null {
Iif (depth >= MAX_BULK_DEPTH) return null;
if (!isContainerKind(kind)) return null;
const accessor = childAccessor(value, kind, sortKeys);
Iif (accessor.count === 0) return null;
const node: Node = {
value,
kind,
accessor,
order: new SparseOrder(accessor.count),
children: new Map(),
hidden: new Map(),
rows: accessor.count + 2,
keyIndex: null,
};
const pairs: [number, number][] = [];
ancestors.push(value);
for (const [segment, sub] of trie.children) {
const index = childIndexOf(node, segment);
if (index < 0) continue; // the data changed under a stale pointer
const childValue = accessor.valueAt(index);
const childKind = kindOf(childValue, ancestors);
const child = buildFromTrie(childValue, childKind, sub, depth + 1, ancestors);
if (child) {
node.children.set(index, child);
pairs.push([index, child.rows]);
}
}
ancestors.pop();
Iif (pairs.length > denseThreshold) {
node.order = new DenseOrder(accessor.count, pairs);
} else {
const sparse = new SparseOrder(accessor.count);
sparse.bulk(pairs);
node.order = sparse;
}
node.rows = node.order.totalRows() + 2;
return node;
}
// ── initial state ───────────────────────────────────────────────────────
if (defaultExpandDepth > 0) {
const budget: Budget = {
rows: 1,
limit: maxExpandedRows,
exceeded: false,
depthCapped: false,
depthBlocked: false,
};
root = build(data, rootKind, 0, defaultExpandDepth, [], budget, null);
}
return {
data,
rowCount,
rowAt,
rowIndexOf,
isExpanded(path) {
const found = locate(path);
return found?.node != null;
},
isExpandable(path) {
const found = locate(path);
if (!found) return false;
return (
isContainerKind(found.kind) && childCountOf(found.value, found.kind) > 0
);
},
toggle,
expand,
collapse,
expandTo,
expandAll,
collapseAll,
getExpandedPaths,
setExpandedPaths,
version: () => ver,
};
}
|