@naman_deep_singh/js-extensions 1.1.0 → 1.2.0
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/README.md +1 -1
- package/dist/cjs/object-extensions.js +18 -5
- package/dist/esm/object-extensions.js +18 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,18 +25,31 @@ function extendObject() {
|
|
|
25
25
|
return result;
|
|
26
26
|
};
|
|
27
27
|
Object.prototype.deepClone = function () {
|
|
28
|
-
|
|
29
|
-
return (0, performance_1.withCache)(`clone_${objStr}`, () => {
|
|
28
|
+
return (0, performance_1.withCache)(`clone_${JSON.stringify(this)}`, () => {
|
|
30
29
|
if (this === null || typeof this !== 'object')
|
|
31
30
|
return this;
|
|
31
|
+
// Handle Date objects
|
|
32
32
|
if (this instanceof Date)
|
|
33
33
|
return new Date(this.getTime());
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
// Handle Array objects
|
|
35
|
+
if (Array.isArray(this)) {
|
|
36
|
+
return this.map(item => {
|
|
37
|
+
if (item && typeof item === 'object' && typeof item.deepClone === 'function') {
|
|
38
|
+
return item.deepClone();
|
|
39
|
+
}
|
|
40
|
+
return item;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
// Handle regular objects
|
|
36
44
|
const cloned = {};
|
|
37
45
|
Object.keys(this).forEach(key => {
|
|
38
46
|
const value = this[key];
|
|
39
|
-
|
|
47
|
+
if (value && typeof value === 'object' && typeof value.deepClone === 'function') {
|
|
48
|
+
cloned[key] = value.deepClone();
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
cloned[key] = value;
|
|
52
|
+
}
|
|
40
53
|
});
|
|
41
54
|
return cloned;
|
|
42
55
|
});
|
|
@@ -22,18 +22,31 @@ export function extendObject() {
|
|
|
22
22
|
return result;
|
|
23
23
|
};
|
|
24
24
|
Object.prototype.deepClone = function () {
|
|
25
|
-
|
|
26
|
-
return withCache(`clone_${objStr}`, () => {
|
|
25
|
+
return withCache(`clone_${JSON.stringify(this)}`, () => {
|
|
27
26
|
if (this === null || typeof this !== 'object')
|
|
28
27
|
return this;
|
|
28
|
+
// Handle Date objects
|
|
29
29
|
if (this instanceof Date)
|
|
30
30
|
return new Date(this.getTime());
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
// Handle Array objects
|
|
32
|
+
if (Array.isArray(this)) {
|
|
33
|
+
return this.map(item => {
|
|
34
|
+
if (item && typeof item === 'object' && typeof item.deepClone === 'function') {
|
|
35
|
+
return item.deepClone();
|
|
36
|
+
}
|
|
37
|
+
return item;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
// Handle regular objects
|
|
33
41
|
const cloned = {};
|
|
34
42
|
Object.keys(this).forEach(key => {
|
|
35
43
|
const value = this[key];
|
|
36
|
-
|
|
44
|
+
if (value && typeof value === 'object' && typeof value.deepClone === 'function') {
|
|
45
|
+
cloned[key] = value.deepClone();
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
cloned[key] = value;
|
|
49
|
+
}
|
|
37
50
|
});
|
|
38
51
|
return cloned;
|
|
39
52
|
});
|