@naverpay/hidash
Version:
improved lodash
31 lines (29 loc) • 2.01 kB
text/typescript
/**
* @description
* Creates a deep clone of `value`. This function recursively clones nested objects and arrays.
* It includes basic handling for circular references by returning an empty object (`{}`)
* for objects already encountered during the current cloning process.
*
* Key behaviors and limitations for specific types:
* - **Primitives** (string, number, boolean, null, undefined, symbol, bigint): Returned as-is.
* - **Arrays**: A new array is created, and its elements are deep cloned.
* - **Maps**: A new Map is created. Its values are deep cloned; keys are assigned by reference.
* - **Sets**: A new Set is created, and its values are deep cloned.
* - **Plain Objects** (specifically, those where `Object.prototype.toString.call(v)` returns `[object Object]`):
* A new object is created with the same prototype, and its own enumerable properties are deep cloned.
* - **Functions, WeakMaps, WeakSets**: An empty object (`{}`) is returned. These types are not deeply cloned by this implementation.
* - **Circular References**: When a circular reference is detected, an empty object (`{}`) is returned for that reference point
* to break the cycle, meaning the original circular structure is not fully replicated.
* - **Other Object Types (e.g., Date, RegExp, TypedArray, ArrayBuffer, custom class instances not matching `[object Object]`):
* These types are **returned by reference (i.e., NOT cloned)**. This is a significant limitation for a "deep" clone.
*
* This function is conceptually similar to lodash's `_.cloneDeep` but has notable
* differences in its handling of certain types, circular references, and overall completeness.
* It is a simplified implementation.
*
* @template T - The type of the value to clone.
* @param {T} value - The value to deeply clone.
* @returns {T} Returns the deeply cloned value, subject to the specific behaviors and limitations noted above.
*/
declare function cloneDeep<T>(value: T): T;
export { cloneDeep, cloneDeep as default };