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 | 423x 423x 423x 2487x 2487x 2487x 2487x 2487x 2487x 2487x 2487x 2487x 748x 748x 748x 2064x 2064x 2064x 2064x 748x 748x 423x 423x | /**
* The reference model — the internal oracle (SPEC.md §4.2, §7).
*
* It does the obvious, unusable thing: flatten the entire tree into an array
* for a given expansion state. That is precisely what the real engine refuses
* to do, which is what makes this a useful check — the two implementations
* share no code and agree only if the index arithmetic is right.
*
* Test-only. Never exported from the package.
*/
import {
bracketsOf,
childAccessor,
childCountOf,
classify,
displayOf,
isContainerKind,
previewOf,
} from "../model/value";
import { pathToPointer } from "../model/path";
import type { JsonKind, PathSegment } from "../types";
/** The subset of a row that both implementations must agree on exactly. */
export interface NaiveRow {
path: PathSegment[];
key: PathSegment | undefined;
label: string;
kind: JsonKind;
depth: number;
closing: boolean;
expanded: boolean;
expandable: boolean;
childCount: number;
posInSet: number;
setSize: number;
display: string;
}
export interface NaiveOptions {
maxValueLength?: number;
sortKeys?: boolean;
}
/**
* Flatten `data` into the row list implied by `expanded`.
*
* @param expanded RFC 6901 pointers of the containers that are open.
*
* @example
* ```ts
* naiveRows({ a: [1, 2] }, new Set(["", "/a"]));
* // { · a: [ · 1 · 2 · ] · } → 6 rows
* ```
*/
export function naiveRows(
data: unknown,
expanded: ReadonlySet<string>,
options: NaiveOptions = {},
): NaiveRow[] {
const maxValueLength = options.maxValueLength ?? 120;
const sortKeys = options.sortKeys ?? false;
const rows: NaiveRow[] = [];
function kindOf(value: unknown, ancestors: readonly unknown[]): JsonKind {
const kind = classify(value);
Iif (value !== null && typeof value === "object" && ancestors.includes(value)) {
return "circular";
}
return kind;
}
function walk(
value: unknown,
key: PathSegment | undefined,
label: string,
path: PathSegment[],
depth: number,
ancestors: unknown[],
posInSet: number,
setSize: number,
): void {
const kind = kindOf(value, ancestors);
const container = isContainerKind(kind);
const childCount = container ? childCountOf(value, kind) : 0;
const isOpen = container && childCount > 0 && expanded.has(pathToPointer(path));
rows.push({
path: path.slice(),
key,
label,
kind,
depth,
closing: false,
expanded: isOpen,
expandable: container && childCount > 0,
childCount,
posInSet,
setSize,
display: isOpen
? bracketsOf(kind)[0]
: container
? previewOf(value, kind, childCount)
: displayOf(value, kind, maxValueLength).text,
});
if (!isOpen) return;
const accessor = childAccessor(value, kind, sortKeys);
ancestors.push(value);
for (let i = 0; i < accessor.count; i++) {
const childKey = accessor.keyAt(i);
path.push(childKey);
walk(
accessor.valueAt(i),
childKey,
accessor.labelAt(i),
path,
depth + 1,
ancestors,
i + 1,
accessor.count,
);
path.pop();
}
ancestors.pop();
rows.push({
path: path.slice(),
key,
label,
kind,
depth,
closing: true,
expanded: true,
expandable: false,
childCount,
posInSet,
setSize,
display: bracketsOf(kind)[1],
});
}
walk(data, undefined, "", [], 0, [], 1, 1);
return rows;
}
|