decycle
Version:
JSON decycle replaces circular references with JSON path references
32 lines (31 loc) • 825 B
JavaScript
function p(n, o) {
return f(n, "$", /* @__PURE__ */ new WeakMap(), o);
}
function f(n, o, r, i) {
if (typeof i == "function" && (n = i(n)), !d(n))
return n;
const e = r.get(n);
if (e !== void 0)
return { $ref: e };
if (r.set(n, o), Array.isArray(n)) {
const t = [];
for (const [c, g] of n.entries()) {
const $ = `${o}[${c.toString()}]`;
t[c] = f(g, $, r, i);
}
return t;
}
const s = n, y = {};
for (const t of Object.keys(s)) {
const c = `${o}[${JSON.stringify(t)}]`;
y[t] = f(s[t], c, r, i);
}
return y;
}
function d(n) {
return typeof n == "object" && n !== null && !(n instanceof Boolean) && !(n instanceof Date) && !(n instanceof Number) && !(n instanceof RegExp) && !(n instanceof String);
}
export {
p as decycle
};
//# sourceMappingURL=index.mjs.map