UNPKG

decycle

Version:

JSON decycle replaces circular references with JSON path references

31 lines (28 loc) 1.19 kB
/** * Makes a deep copy of an object or array, assuring that there is at most * one instance of each object or array in the resulting structure. The * duplicate references (which might be forming cycles) are replaced with * an object of the form `{"$ref": PATH}` where the PATH is a JSONPath * string that locates the first occurance. * * @example * ```ts * var a = []; * a[0] = a; * JSON.stringify(decycle(a)); // '[{"$ref":"$"}]' * ``` * * If a replacer function is provided, then it will be called for each value. * A replacer function receives a value and returns a replacement value. * * JSONPath is used to locate the unique object. `$` indicates the top level of * the object or array. `[NUMBER]` or `[STRING]` indicates a child element or * property. * * @param value - The object or array to decycle. * @param replacer - Optional replacer function called for each value. * @returns - A deep copy of the object with circular references replaced by `$ref` objects. */ export declare function decycle(value: unknown, replacer?: ReplacerFunction): unknown; declare type ReplacerFunction = (value: unknown) => unknown; export { }