@gvray/eskit
Version:
A rich and colorful toolkit about typescript and javascript.
40 lines • 1.44 kB
TypeScript
/**
* Creates a deep clone of the given value, handling circular references.
* 创建给定值的深度克隆,处理循环引用。
*
* This function recursively copies all properties of objects and arrays,
* including nested objects, while maintaining the original data types
* for dates, regular expressions, and other built-in objects.
* 此函数递归复制对象和数组的所有属性,包括嵌套对象,
* 同时保持日期、正则表达式和其他内置对象的原始数据类型。
*
* @template T - The type of the value to clone / 要克隆的值的类型
* @param obj - The value to deep clone / 要深度克隆的值
* @returns A deep copy of the input value / 输入值的深度副本
*
* @example
* ```typescript
* const original = {
* name: 'John',
* age: 30,
* hobbies: ['reading', 'gaming'],
* address: { city: 'New York', zip: '10001' },
* createdAt: new Date(),
* pattern: /test/gi
* }
*
* const cloned = deepClone(original)
* cloned.hobbies.push('swimming') // doesn't affect original
* console.log(original.hobbies) // ['reading', 'gaming']
*
* // Handles circular references
* const circular: any = { name: 'test' }
* circular.self = circular
* const clonedCircular = deepClone(circular) // works without infinite recursion
* ```
*
* @since 1.0.0
*/
declare function deepClone<T>(obj: T): T;
export default deepClone;
//# sourceMappingURL=deepClone.d.ts.map