@mongez/reinforcements 2.3.2 → 2.3.4
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 +4 -0
- package/cjs/mixed/clone/clone.d.ts +1 -4
- package/cjs/mixed/clone/clone.js +20 -7
- package/cjs/object/merge.d.ts +3 -0
- package/cjs/object/merge.js +8 -16
- package/esm/mixed/clone/clone.d.ts +1 -4
- package/esm/mixed/clone/clone.js +20 -7
- package/esm/object/merge.d.ts +3 -0
- package/esm/object/merge.js +8 -16
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2057,6 +2057,10 @@ To run tests run `npm run test` or `yarn test`
|
|
|
2057
2057
|
|
|
2058
2058
|
## Change Log
|
|
2059
2059
|
|
|
2060
|
+
- 2.3.4 (06 May 2023)
|
|
2061
|
+
- Fixed clone function array cloning does not lose reference for nested objects.
|
|
2062
|
+
- 2.3.3 (06 May 2023)
|
|
2063
|
+
- Fixed clone function array cloning does not lose reference for nested objects.
|
|
2060
2064
|
- 2.3.2 (06 May 2023)
|
|
2061
2065
|
- Fixed clone function array cloning does not lose reference for nested objects.
|
|
2062
2066
|
- 2.3.0 (25 Mar 2023)
|
package/cjs/mixed/clone/clone.js
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var merge = require("../../object/merge.js");
|
|
3
|
-
/**
|
|
4
|
-
* Make a deep copy for the given object/array and its nested objects/arrays as well
|
|
5
|
-
*/
|
|
6
2
|
function clone(value) {
|
|
7
|
-
if (
|
|
8
|
-
|
|
3
|
+
if (typeof value === "object" && value !== null) {
|
|
4
|
+
if (Array.isArray(value)) {
|
|
5
|
+
return value.map(item => clone(item));
|
|
6
|
+
} else if (value instanceof Date) {
|
|
7
|
+
return new Date(value.getTime());
|
|
8
|
+
} else if (value instanceof Set) {
|
|
9
|
+
return new Set(Array.from(value, clone));
|
|
10
|
+
} else if (value instanceof Map) {
|
|
11
|
+
return new Map(Array.from(value, ([key, val]) => [key, clone(val)]));
|
|
12
|
+
} else {
|
|
13
|
+
const clonedObject = Object.create(Object.getPrototypeOf(value));
|
|
14
|
+
return Object.assign(
|
|
15
|
+
clonedObject,
|
|
16
|
+
...Object.keys(value).map(key => ({
|
|
17
|
+
[key]: clone(value[key]),
|
|
18
|
+
})),
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
} else {
|
|
22
|
+
return value;
|
|
9
23
|
}
|
|
10
|
-
return value;
|
|
11
24
|
}
|
|
12
25
|
module.exports = clone;
|
package/cjs/object/merge.d.ts
CHANGED
package/cjs/object/merge.js
CHANGED
|
@@ -10,6 +10,7 @@ var Is__default = /*#__PURE__*/ _interopDefault(Is);
|
|
|
10
10
|
* But removed unnecessary parts and added some types, also add some comments and updated constant/variable names and types
|
|
11
11
|
*/
|
|
12
12
|
const isObject = Is__default.default.plainObject;
|
|
13
|
+
const isArray = Array.isArray;
|
|
13
14
|
function isDescriptor(object) {
|
|
14
15
|
if (!isObject(object)) {
|
|
15
16
|
return false;
|
|
@@ -23,11 +24,14 @@ function isDescriptor(object) {
|
|
|
23
24
|
"configurable" in object))
|
|
24
25
|
);
|
|
25
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Merge deep objects or arrays
|
|
29
|
+
*/
|
|
26
30
|
function merge(...objects) {
|
|
27
|
-
if (objects.length === 0) return objects[0];
|
|
31
|
+
if (objects.length === 0 || !objects[0]) return objects[0];
|
|
28
32
|
const refinedObjects = [];
|
|
29
33
|
for (let object of objects) {
|
|
30
|
-
if (isObject(object)) {
|
|
34
|
+
if (isObject(object) || isArray(object)) {
|
|
31
35
|
refinedObjects.push(object);
|
|
32
36
|
}
|
|
33
37
|
}
|
|
@@ -45,10 +49,10 @@ function merge(...objects) {
|
|
|
45
49
|
const srcValue = srcObject[key];
|
|
46
50
|
const targetValue = targetObject[key];
|
|
47
51
|
let mergedObject = srcValue;
|
|
48
|
-
if (
|
|
52
|
+
if (isArray(mergedObject)) {
|
|
49
53
|
mergedObject = [...mergedObject];
|
|
50
54
|
} else if (isObject(mergedObject) && !isDescriptor(mergedObject)) {
|
|
51
|
-
if (isObject(targetValue)) {
|
|
55
|
+
if (isObject(targetValue) || isArray(targetValue)) {
|
|
52
56
|
mergedObject = merge(targetValue, mergedObject);
|
|
53
57
|
} else {
|
|
54
58
|
mergedObject = merge(mergedObject);
|
|
@@ -56,18 +60,6 @@ function merge(...objects) {
|
|
|
56
60
|
}
|
|
57
61
|
// New descriptor returned via callback
|
|
58
62
|
if (isDescriptor(mergedObject)) {
|
|
59
|
-
// Defining properties using Object.defineProperty() works
|
|
60
|
-
// different than using the assignment operator (obj.a = 1).
|
|
61
|
-
// Specifically, the descriptor properties 'configurable',
|
|
62
|
-
// 'enumerable', and 'writable' default to 'false' when
|
|
63
|
-
// using Object.defineProperty() but to 'true' when using
|
|
64
|
-
// the assignment operator. The code below ensures that
|
|
65
|
-
// descriptors returned from callbacks are treated as if
|
|
66
|
-
// they were assigned using the assignment operator unless
|
|
67
|
-
// those properties are explicitly defined in the
|
|
68
|
-
// descriptor. This allow merging properties that may
|
|
69
|
-
// otherwise fail due to 'configurable' or 'writable' being
|
|
70
|
-
// set to 'false'.
|
|
71
63
|
// Accessor and data descriptor
|
|
72
64
|
mergedObject.configurable = !("configurable" in mergedObject)
|
|
73
65
|
? true
|
package/esm/mixed/clone/clone.js
CHANGED
|
@@ -1,11 +1,24 @@
|
|
|
1
|
-
import merge from "../../object/merge.js";
|
|
2
|
-
/**
|
|
3
|
-
* Make a deep copy for the given object/array and its nested objects/arrays as well
|
|
4
|
-
*/
|
|
5
1
|
function clone(value) {
|
|
6
|
-
if (
|
|
7
|
-
|
|
2
|
+
if (typeof value === "object" && value !== null) {
|
|
3
|
+
if (Array.isArray(value)) {
|
|
4
|
+
return value.map(item => clone(item));
|
|
5
|
+
} else if (value instanceof Date) {
|
|
6
|
+
return new Date(value.getTime());
|
|
7
|
+
} else if (value instanceof Set) {
|
|
8
|
+
return new Set(Array.from(value, clone));
|
|
9
|
+
} else if (value instanceof Map) {
|
|
10
|
+
return new Map(Array.from(value, ([key, val]) => [key, clone(val)]));
|
|
11
|
+
} else {
|
|
12
|
+
const clonedObject = Object.create(Object.getPrototypeOf(value));
|
|
13
|
+
return Object.assign(
|
|
14
|
+
clonedObject,
|
|
15
|
+
...Object.keys(value).map(key => ({
|
|
16
|
+
[key]: clone(value[key]),
|
|
17
|
+
})),
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
} else {
|
|
21
|
+
return value;
|
|
8
22
|
}
|
|
9
|
-
return value;
|
|
10
23
|
}
|
|
11
24
|
export { clone as default };
|
package/esm/object/merge.d.ts
CHANGED
package/esm/object/merge.js
CHANGED
|
@@ -5,6 +5,7 @@ import Is from "@mongez/supportive-is";
|
|
|
5
5
|
* But removed unnecessary parts and added some types, also add some comments and updated constant/variable names and types
|
|
6
6
|
*/
|
|
7
7
|
const isObject = Is.plainObject;
|
|
8
|
+
const isArray = Array.isArray;
|
|
8
9
|
function isDescriptor(object) {
|
|
9
10
|
if (!isObject(object)) {
|
|
10
11
|
return false;
|
|
@@ -18,11 +19,14 @@ function isDescriptor(object) {
|
|
|
18
19
|
"configurable" in object))
|
|
19
20
|
);
|
|
20
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Merge deep objects or arrays
|
|
24
|
+
*/
|
|
21
25
|
function merge(...objects) {
|
|
22
|
-
if (objects.length === 0) return objects[0];
|
|
26
|
+
if (objects.length === 0 || !objects[0]) return objects[0];
|
|
23
27
|
const refinedObjects = [];
|
|
24
28
|
for (let object of objects) {
|
|
25
|
-
if (isObject(object)) {
|
|
29
|
+
if (isObject(object) || isArray(object)) {
|
|
26
30
|
refinedObjects.push(object);
|
|
27
31
|
}
|
|
28
32
|
}
|
|
@@ -40,10 +44,10 @@ function merge(...objects) {
|
|
|
40
44
|
const srcValue = srcObject[key];
|
|
41
45
|
const targetValue = targetObject[key];
|
|
42
46
|
let mergedObject = srcValue;
|
|
43
|
-
if (
|
|
47
|
+
if (isArray(mergedObject)) {
|
|
44
48
|
mergedObject = [...mergedObject];
|
|
45
49
|
} else if (isObject(mergedObject) && !isDescriptor(mergedObject)) {
|
|
46
|
-
if (isObject(targetValue)) {
|
|
50
|
+
if (isObject(targetValue) || isArray(targetValue)) {
|
|
47
51
|
mergedObject = merge(targetValue, mergedObject);
|
|
48
52
|
} else {
|
|
49
53
|
mergedObject = merge(mergedObject);
|
|
@@ -51,18 +55,6 @@ function merge(...objects) {
|
|
|
51
55
|
}
|
|
52
56
|
// New descriptor returned via callback
|
|
53
57
|
if (isDescriptor(mergedObject)) {
|
|
54
|
-
// Defining properties using Object.defineProperty() works
|
|
55
|
-
// different than using the assignment operator (obj.a = 1).
|
|
56
|
-
// Specifically, the descriptor properties 'configurable',
|
|
57
|
-
// 'enumerable', and 'writable' default to 'false' when
|
|
58
|
-
// using Object.defineProperty() but to 'true' when using
|
|
59
|
-
// the assignment operator. The code below ensures that
|
|
60
|
-
// descriptors returned from callbacks are treated as if
|
|
61
|
-
// they were assigned using the assignment operator unless
|
|
62
|
-
// those properties are explicitly defined in the
|
|
63
|
-
// descriptor. This allow merging properties that may
|
|
64
|
-
// otherwise fail due to 'configurable' or 'writable' being
|
|
65
|
-
// set to 'false'.
|
|
66
58
|
// Accessor and data descriptor
|
|
67
59
|
mergedObject.configurable = !("configurable" in mergedObject)
|
|
68
60
|
? true
|
package/package.json
CHANGED