sm-utility 2.3.6 → 2.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/general/index.d.ts +34 -0
- package/general/index.js +60 -1
- package/package.json +1 -1
package/general/index.d.ts
CHANGED
|
@@ -3,3 +3,37 @@ export declare function randomSixDigitNumber(): number;
|
|
|
3
3
|
export declare function timeout(ms: number): Promise<any>;
|
|
4
4
|
export declare function mergeDeep(target: Record<string, any>, source: Record<string, any>): Record<string, any>;
|
|
5
5
|
export declare function shortUniqueId(): string;
|
|
6
|
+
/**
|
|
7
|
+
* Creates a deep copy of any JavaScript value, handling nested objects and various built-in types.
|
|
8
|
+
*
|
|
9
|
+
* @template T - The type of the value being cloned
|
|
10
|
+
* @param {T} obj - The value to clone
|
|
11
|
+
* @returns {T} A deep copy of the input value
|
|
12
|
+
*
|
|
13
|
+
* @description
|
|
14
|
+
* Supports deep cloning of:
|
|
15
|
+
* - Primitive values (numbers, strings, booleans)
|
|
16
|
+
* - Plain objects
|
|
17
|
+
* - Arrays
|
|
18
|
+
* - Dates
|
|
19
|
+
* - Sets
|
|
20
|
+
* - Maps
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* // Clone a simple object
|
|
24
|
+
* const original = { a: 1, b: { c: 2 } };
|
|
25
|
+
* const clone = copy(original);
|
|
26
|
+
*
|
|
27
|
+
* // Clone an array with nested objects
|
|
28
|
+
* const arr = [{ x: 1 }, { y: 2 }];
|
|
29
|
+
* const clonedArr = copy(arr);
|
|
30
|
+
*
|
|
31
|
+
* // Clone complex structures
|
|
32
|
+
* const complex = {
|
|
33
|
+
* date: new Date(),
|
|
34
|
+
* set: new Set([1, 2, 3]),
|
|
35
|
+
* map: new Map([['key', 'value']])
|
|
36
|
+
* };
|
|
37
|
+
* const clonedComplex = copy(complex);
|
|
38
|
+
*/
|
|
39
|
+
export declare function copy<T>(obj: T): T;
|
package/general/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.shortUniqueId = exports.mergeDeep = exports.timeout = exports.randomSixDigitNumber = exports.pipe = void 0;
|
|
3
|
+
exports.copy = exports.shortUniqueId = exports.mergeDeep = exports.timeout = exports.randomSixDigitNumber = exports.pipe = void 0;
|
|
4
4
|
const nanoid_1 = require("nanoid");
|
|
5
5
|
function pipe(...fns) {
|
|
6
6
|
return (x) => fns.reduce((v, f) => f(v), x);
|
|
@@ -43,3 +43,62 @@ function shortUniqueId() {
|
|
|
43
43
|
return nanoid();
|
|
44
44
|
}
|
|
45
45
|
exports.shortUniqueId = shortUniqueId;
|
|
46
|
+
/**
|
|
47
|
+
* Creates a deep copy of any JavaScript value, handling nested objects and various built-in types.
|
|
48
|
+
*
|
|
49
|
+
* @template T - The type of the value being cloned
|
|
50
|
+
* @param {T} obj - The value to clone
|
|
51
|
+
* @returns {T} A deep copy of the input value
|
|
52
|
+
*
|
|
53
|
+
* @description
|
|
54
|
+
* Supports deep cloning of:
|
|
55
|
+
* - Primitive values (numbers, strings, booleans)
|
|
56
|
+
* - Plain objects
|
|
57
|
+
* - Arrays
|
|
58
|
+
* - Dates
|
|
59
|
+
* - Sets
|
|
60
|
+
* - Maps
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* // Clone a simple object
|
|
64
|
+
* const original = { a: 1, b: { c: 2 } };
|
|
65
|
+
* const clone = copy(original);
|
|
66
|
+
*
|
|
67
|
+
* // Clone an array with nested objects
|
|
68
|
+
* const arr = [{ x: 1 }, { y: 2 }];
|
|
69
|
+
* const clonedArr = copy(arr);
|
|
70
|
+
*
|
|
71
|
+
* // Clone complex structures
|
|
72
|
+
* const complex = {
|
|
73
|
+
* date: new Date(),
|
|
74
|
+
* set: new Set([1, 2, 3]),
|
|
75
|
+
* map: new Map([['key', 'value']])
|
|
76
|
+
* };
|
|
77
|
+
* const clonedComplex = copy(complex);
|
|
78
|
+
*/
|
|
79
|
+
function copy(obj) {
|
|
80
|
+
if (obj === null || typeof obj !== 'object') {
|
|
81
|
+
return obj;
|
|
82
|
+
}
|
|
83
|
+
if (Array.isArray(obj)) {
|
|
84
|
+
return obj.map((item) => copy(item));
|
|
85
|
+
}
|
|
86
|
+
if (obj instanceof Date) {
|
|
87
|
+
return new Date(obj.getTime());
|
|
88
|
+
}
|
|
89
|
+
if (obj instanceof Set) {
|
|
90
|
+
return new Set(Array.from(obj).map((item) => copy(item)));
|
|
91
|
+
}
|
|
92
|
+
if (obj instanceof Map) {
|
|
93
|
+
return new Map(Array.from(obj.entries()).map(([key, value]) => [
|
|
94
|
+
copy(key),
|
|
95
|
+
copy(value),
|
|
96
|
+
]));
|
|
97
|
+
}
|
|
98
|
+
const clone = {};
|
|
99
|
+
Object.keys(obj).forEach((key) => {
|
|
100
|
+
clone[key] = copy(obj[key]);
|
|
101
|
+
});
|
|
102
|
+
return clone;
|
|
103
|
+
}
|
|
104
|
+
exports.copy = copy;
|