import { Value } from "./values"; import { Annotated } from "./annotated"; import { Record, Tuple } from "./record"; import { Set, Dictionary } from "./dictionary"; import type { GenericEmbedded } from "./embedded"; export function unannotate(v: Value): Value { return Annotated.isAnnotated(v) ? v.item : v; } export function peel(v: Value): Value { return strip(v, 1); } export function strip( v: Value, depth: number = Infinity): Value { function step(v: Value, depth: number): Value { if (depth === 0) return v; if (!Annotated.isAnnotated(v)) return v; const nextDepth = depth - 1; function walk(v: Value): Value { return step(v, nextDepth); } if (Record.isRecord, Tuple>, T>(v.item)) { return Record(step(v.item.label, depth), v.item.map(walk)); } else if (Annotated.isAnnotated(v.item)) { throw new Error("Improper annotation structure"); } else if (nextDepth === 0) { return v.item; } else if (Array.isArray(v.item)) { return (v.item as Value[]).map(walk); } else if (Set.isSet(v.item)) { return v.item.map(walk); } else if (Dictionary.isDictionary(v.item)) { return v.item.mapEntries((e) => [walk(e[0]), walk(e[1])]); } else { return v.item; } } return step(v, depth); }