16 lines
799 B
JavaScript
16 lines
799 B
JavaScript
// Compact JSON layout for the card files: anything that fits on one line stays on one line.
|
|
|
|
const inline = (v) => {
|
|
if (Array.isArray(v)) return "[" + v.map(inline).join(", ") + "]";
|
|
if (v && typeof v === "object") return "{ " + Object.entries(v).map(([k, x]) => JSON.stringify(k) + ": " + inline(x)).join(", ") + " }";
|
|
return JSON.stringify(v);
|
|
};
|
|
|
|
export function format(v, indent = "") {
|
|
const flat = inline(v);
|
|
if (flat.length + indent.length <= 118 || typeof v !== "object" || v === null) return flat;
|
|
const next = indent + " ";
|
|
if (Array.isArray(v)) return "[\n" + v.map((x) => next + format(x, next)).join(",\n") + "\n" + indent + "]";
|
|
return "{\n" + Object.entries(v).map(([k, x]) => next + JSON.stringify(k) + ": " + format(x, next)).join(",\n") + "\n" + indent + "}";
|
|
}
|