UNPKG

diginext-utils

Version:
41 lines 1.09 kB
/** * Creates a deep clone of an object, array, or primitive value. * Handles nested objects and arrays recursively. * * @template T - The type of value to clone * @param value - The value to clone * @returns A deep clone of the value * * @example * ```ts * const obj = { a: 1, b: { c: 2 } }; * const cloned = deepClone(obj); * cloned.b.c = 3; * console.log(obj.b.c); // 2 (original unchanged) * ``` */ export function deepClone(value) { // Handle primitives and null if (value === null || typeof value !== "object") { return value; } // Handle Date if (value instanceof Date) { return new Date(value.getTime()); } // Handle Array if (Array.isArray(value)) { return value.map((item) => deepClone(item)); } // Handle RegExp if (value instanceof RegExp) { return new RegExp(value.source, value.flags); } // Handle Object const cloned = {}; Object.keys(value).forEach((key) => { cloned[key] = deepClone(value[key]); }); return cloned; } //# sourceMappingURL=deepClone.js.map