Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128// Stable, compact JSON output: a value whose one-line form fits in `width`// columns stays on one line, anything larger is indented by two spaces.//// JavaScript objects list integer-like keys first, whatever order they were// written in. A hand case whose key order matters (design §2, D6) writes its// machine as JSON text; `parseOrdered` reads that text into `OrderedObject`s,// which this module writes back in the authored order.
/** A JSON object that keeps its keys in the order they were written. */export class OrderedObject { constructor(entries) { this.entries = entries; }}
/** Parses JSON text, keeping every object's key order. */export function parseOrdered(text) { JSON.parse(text); // reject invalid JSON with the usual message let i = 0; const ws = () => { while (/\s/.test(text[i])) i++; }; const value = () => { ws(); const c = text[i]; if (c === '{') { i++; const entries = []; ws(); if (text[i] === '}') { i++; return new OrderedObject(entries); } for (;;) { ws(); const key = string(); ws(); i++; // ':' entries.push([key, value()]); ws(); if (text[i++] === '}') return new OrderedObject(entries); } } if (c === '[') { i++; const items = []; ws(); if (text[i] === ']') { i++; return items; } for (;;) { items.push(value()); ws(); if (text[i++] === ']') return items; } } if (c === '"') return string(); const m = /^(?:-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?|true|false|null)/.exec(text.slice(i)); i += m[0].length; return JSON.parse(m[0]); }; const string = () => { const m = /^"(?:[^"\\]|\\.)*"/.exec(text.slice(i)); i += m[0].length; return JSON.parse(m[0]); }; const result = value(); return result;}
/** A plain JavaScript value for an ordered one (JavaScript key order). */export function toPlain(value) { if (value instanceof OrderedObject) { return Object.fromEntries(value.entries.map(([k, v]) => [k, toPlain(v)])); } if (Array.isArray(value)) return value.map(toPlain); return value;}
function entriesOf(v) { return v instanceof OrderedObject ? v.entries : Object.entries(v).filter(([, x]) => x !== undefined);}
function flat(v) { if (v instanceof OrderedObject) { return `{${v.entries.map(([k, x]) => `${JSON.stringify(k)}:${flat(x)}`).join(',')}}`; } if (Array.isArray(v)) return `[${v.map((x) => flat(x) ?? 'null').join(',')}]`; if (v !== null && typeof v === 'object') { return `{${entriesOf(v).map(([k, x]) => `${JSON.stringify(k)}:${flat(x)}`).join(',')}}`; } return JSON.stringify(v);}
export function stringify(value, width = 100) { const out = []; const write = (v, indent, prefix) => { if (v === undefined) return; const line = flat(v); if ( v === null || typeof v !== 'object' || indent.length + prefix.length + line.length <= width ) { out.push(indent + prefix + line); return; } const inner = indent + ' '; const isArray = Array.isArray(v); const entries = isArray ? v.map((x) => ['', x]) : entriesOf(v); if (!entries.length) { out.push(indent + prefix + (isArray ? '[]' : '{}')); return; } out.push(indent + prefix + (isArray ? '[' : '{')); entries.forEach(([k, x], i) => { write(x, inner, isArray ? '' : `${JSON.stringify(k)}: `); if (i < entries.length - 1) out[out.length - 1] += ','; }); out.push(indent + (isArray ? ']' : '}')); }; write(value, '', ''); return out.join('\n') + '\n';}