@andranik-arakelyan/js-utilities
Version:
Javascript utilities
31 lines (30 loc) • 974 B
TypeScript
/**
* Creates a deep clone of an object or array, creating a new copy of all nested objects and arrays.
* This function handles circular references and complex JavaScript types.
*
* @template T - The type of value to clone
* @param value - The value to clone
* @returns A deep clone of the input value
*
* @example
* // Simple object cloning
* const obj = { a: 1, b: { c: 2 } };
* const clone = deepClone(obj);
* clone.b.c = 3;
* console.log(obj.b.c); // Still 2
*
* @example
* // Handling arrays and nested structures
* const arr = [1, { x: [2, 3] }];
* const cloneArr = deepClone(arr);
* cloneArr[1].x[0] = 10;
* console.log(arr[1].x[0]); // Still 2
*
* @example
* // Handling circular references
* const circular = { a: 1 };
* circular.self = circular;
* const cloneCircular = deepClone(circular);
* console.log(cloneCircular.self === cloneCircular); // true
*/
export declare function deepClone<T>(value: T, refs?: WeakMap<WeakKey, any>): T;