-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.ts
39 lines (35 loc) · 1019 Bytes
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import {packageTracer} from '@alwatr/package-tracer';
packageTracer.add(__package_name__, __package_version__);
/**
* Clone deeply nested objects and arrays.
*
* @param obj The object to clone.
* @returns A clone of the object.
* @example
* ```typescript
* const obj2 = deepClone(obj1);
* ```
*/
export function deepClone<T>(obj: T): T;
/**
* Clone deeply nested objects and arrays.
*
* if the object is null or undefined, it returns null.
*
* @param obj The object to clone.
* @returns A clone of the object.
* @example
* ```typescript
* const obj2 = deepClone(obj1);
* ```
*/
export function deepClone<T>(obj: T | null | undefined): T | null;
export function deepClone<T>(obj: T | null | undefined): T | null {
if (obj == null) return null;
// I don`t know why structuredClone is slower than stringify! but I think its changes in the future.
// if (typeof structuredClone === 'function') {
// return structuredClone(obj);
// }
// else
return JSON.parse(JSON.stringify(obj));
}