mybase 1.1.50 → 1.1.51
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/package.json +1 -1
- package/ts/funcs/deepCopy.d.ts +1 -0
- package/ts/funcs/deepCopy.js +34 -0
- package/ts/funcs/deepCopy.test.ts +108 -0
- package/ts/funcs/deepCopy.ts +34 -0
- package/ts/index.d.ts +1 -0
- package/ts/index.js +1 -0
- package/ts/index.ts +1 -0
package/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function deepCopy<T>(obj: T, visited?: WeakMap<object, any>): T;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deepCopy = deepCopy;
|
|
4
|
+
function deepCopy(obj, visited = new WeakMap()) {
|
|
5
|
+
// Handle null and non-objects
|
|
6
|
+
if (obj === null || typeof obj !== 'object') {
|
|
7
|
+
return obj;
|
|
8
|
+
}
|
|
9
|
+
// Handle circular references
|
|
10
|
+
if (visited.has(obj)) {
|
|
11
|
+
return visited.get(obj);
|
|
12
|
+
}
|
|
13
|
+
// Handle Date
|
|
14
|
+
if (obj instanceof Date) {
|
|
15
|
+
return new Date(obj.getTime());
|
|
16
|
+
}
|
|
17
|
+
// Handle Array
|
|
18
|
+
if (Array.isArray(obj)) {
|
|
19
|
+
const copy = [];
|
|
20
|
+
visited.set(obj, copy);
|
|
21
|
+
obj.forEach((item, i) => {
|
|
22
|
+
copy[i] = deepCopy(item, visited);
|
|
23
|
+
});
|
|
24
|
+
return copy;
|
|
25
|
+
}
|
|
26
|
+
// Handle Object
|
|
27
|
+
const copy = Object.create(Object.getPrototypeOf(obj));
|
|
28
|
+
visited.set(obj, copy);
|
|
29
|
+
for (const key of Object.keys(obj)) {
|
|
30
|
+
copy[key] = deepCopy(obj[key], visited);
|
|
31
|
+
}
|
|
32
|
+
return copy;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=deepCopy.js.map
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { Unixtime } from "../models/Unixtime";
|
|
2
|
+
import { deepCopy } from "./deepCopy"
|
|
3
|
+
|
|
4
|
+
describe('deepCopy', () => {
|
|
5
|
+
it('deep-copies plain objects', () => {
|
|
6
|
+
const src = { a: 1, b: { c: 2 } };
|
|
7
|
+
const copy = deepCopy(src) as typeof src;
|
|
8
|
+
|
|
9
|
+
expect(copy).toEqual(src);
|
|
10
|
+
expect(copy).not.toBe(src);
|
|
11
|
+
|
|
12
|
+
copy.b.c = 3;
|
|
13
|
+
expect(src.b.c).toBe(2); // original stays intact
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
it('Date objects are copied as Date objects', () => {
|
|
18
|
+
const src = { date: new Date() }
|
|
19
|
+
const copy = deepCopy(src) as typeof src
|
|
20
|
+
expect(copy.date).toBeInstanceOf(Date)
|
|
21
|
+
expect(copy.date).not.toBe(src.date)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('deep-copies arrays', () => {
|
|
25
|
+
const src = [1, [2, 3]];
|
|
26
|
+
const copy = deepCopy(src) as typeof src;
|
|
27
|
+
|
|
28
|
+
expect(copy).toEqual(src);
|
|
29
|
+
//@ts-ignore
|
|
30
|
+
copy[1][0] = 99;
|
|
31
|
+
//@ts-ignore
|
|
32
|
+
expect(src[1][0]).toBe(2);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('returns primitives unchanged', () => {
|
|
36
|
+
expect(deepCopy(42 as any)).toBe(42);
|
|
37
|
+
expect(deepCopy('hello' as any)).toBe('hello');
|
|
38
|
+
expect(deepCopy(null as any)).toBeNull();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('should handle circular references', () => {
|
|
42
|
+
const obj: any = { a: 1 };
|
|
43
|
+
obj.self = obj;
|
|
44
|
+
const copy = deepCopy(obj);
|
|
45
|
+
expect(copy).toEqual({ a: 1, self: copy });
|
|
46
|
+
expect(copy.self).toBe(copy);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('should preserve prototype chain', () => {
|
|
50
|
+
class MyClass { x = 1; }
|
|
51
|
+
const obj = new MyClass();
|
|
52
|
+
const copy = deepCopy(obj);
|
|
53
|
+
expect(copy).toBeInstanceOf(MyClass);
|
|
54
|
+
expect(copy.x).toBe(1);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('changing Date at src should not change copy', () => {
|
|
58
|
+
const src = { a: new Date("2024-05-05"), b: { c: 2 } };
|
|
59
|
+
const copy = deepCopy(src) as typeof src;
|
|
60
|
+
expect(copy).toEqual(src);
|
|
61
|
+
expect(copy).not.toBe(src);
|
|
62
|
+
src.a.setFullYear(2025)
|
|
63
|
+
expect(src.a.getFullYear()).toBe(2025)
|
|
64
|
+
expect(copy.a.getFullYear()).toBe(2024)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it('changing src integer should not change copy', () => {
|
|
68
|
+
const src = { a: 1, b: { c: 2 } };
|
|
69
|
+
const copy = deepCopy(src) as typeof src;
|
|
70
|
+
expect(copy).toEqual(src);
|
|
71
|
+
expect(copy).not.toBe(src);
|
|
72
|
+
src.a = 2
|
|
73
|
+
expect(src.a).toBe(2)
|
|
74
|
+
expect(copy.a).toBe(1)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('changing src string should not change copy', () => {
|
|
78
|
+
const src = { a: "hello", b: { c: 2 } };
|
|
79
|
+
const copy = deepCopy(src) as typeof src;
|
|
80
|
+
expect(copy).toEqual(src);
|
|
81
|
+
expect(copy).not.toBe(src);
|
|
82
|
+
src.a = "world"
|
|
83
|
+
expect(src.a).toBe("world")
|
|
84
|
+
expect(copy.a).toBe("hello")
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('changing src boolean should not change copy', () => {
|
|
88
|
+
const src = { a: true, b: { c: 2 } };
|
|
89
|
+
const copy = deepCopy(src) as typeof src;
|
|
90
|
+
expect(copy).toEqual(src);
|
|
91
|
+
expect(copy).not.toBe(src);
|
|
92
|
+
src.a = false
|
|
93
|
+
expect(src.a).toBe(false)
|
|
94
|
+
expect(copy.a).toBe(true)
|
|
95
|
+
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
it('changing src Unixtime should not change copy', () => {
|
|
99
|
+
const src = { a: new Unixtime("2024-05-02"), b: { c: 2 } };
|
|
100
|
+
const copy = deepCopy(src) as typeof src;
|
|
101
|
+
expect(copy).toEqual(src);
|
|
102
|
+
expect(copy).not.toBe(src);
|
|
103
|
+
src.a.addYears(-1)
|
|
104
|
+
expect(src.a.toDate().getFullYear()).toBe(2023)
|
|
105
|
+
expect(copy.a.toDate().getFullYear()).toBe(2024)
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export function deepCopy<T>(obj: T, visited = new WeakMap()): T {
|
|
2
|
+
// Handle null and non-objects
|
|
3
|
+
if (obj === null || typeof obj !== 'object') {
|
|
4
|
+
return obj;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
// Handle circular references
|
|
8
|
+
if (visited.has(obj)) {
|
|
9
|
+
return visited.get(obj);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Handle Date
|
|
13
|
+
if (obj instanceof Date) {
|
|
14
|
+
return new Date(obj.getTime()) as any;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Handle Array
|
|
18
|
+
if (Array.isArray(obj)) {
|
|
19
|
+
const copy: any[] = [];
|
|
20
|
+
visited.set(obj, copy);
|
|
21
|
+
obj.forEach((item, i) => {
|
|
22
|
+
copy[i] = deepCopy(item, visited);
|
|
23
|
+
});
|
|
24
|
+
return copy as T;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Handle Object
|
|
28
|
+
const copy = Object.create(Object.getPrototypeOf(obj));
|
|
29
|
+
visited.set(obj, copy);
|
|
30
|
+
for (const key of Object.keys(obj)) {
|
|
31
|
+
copy[key] = deepCopy((obj as any)[key], visited);
|
|
32
|
+
}
|
|
33
|
+
return copy;
|
|
34
|
+
}
|
package/ts/index.d.ts
CHANGED
|
@@ -26,6 +26,7 @@ export * from "./funcs/randomUTFString";
|
|
|
26
26
|
export * from "./funcs/MaxRuntimeHours";
|
|
27
27
|
export * from "./funcs/ensureFolder";
|
|
28
28
|
export * from "./funcs/knexConnection";
|
|
29
|
+
export * from "./funcs/deepCopy";
|
|
29
30
|
export * from "./models/Unixtime";
|
|
30
31
|
export * from "./models/Timespan";
|
|
31
32
|
export * from "./models/IPAddress";
|
package/ts/index.js
CHANGED
|
@@ -42,6 +42,7 @@ __exportStar(require("./funcs/randomUTFString"), exports);
|
|
|
42
42
|
__exportStar(require("./funcs/MaxRuntimeHours"), exports);
|
|
43
43
|
__exportStar(require("./funcs/ensureFolder"), exports);
|
|
44
44
|
__exportStar(require("./funcs/knexConnection"), exports);
|
|
45
|
+
__exportStar(require("./funcs/deepCopy"), exports);
|
|
45
46
|
// models
|
|
46
47
|
__exportStar(require("./models/Unixtime"), exports);
|
|
47
48
|
__exportStar(require("./models/Timespan"), exports);
|