@mongez/reinforcements 2.3.2 → 2.3.3

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 CHANGED
@@ -2057,6 +2057,8 @@ To run tests run `npm run test` or `yarn test`
2057
2057
 
2058
2058
  ## Change Log
2059
2059
 
2060
+ - 2.3.3 (06 May 2023)
2061
+ - Fixed clone function array cloning does not lose reference for nested objects.
2060
2062
  - 2.3.2 (06 May 2023)
2061
2063
  - Fixed clone function array cloning does not lose reference for nested objects.
2062
2064
  - 2.3.0 (25 Mar 2023)
@@ -1,5 +1,2 @@
1
- /**
2
- * Make a deep copy for the given object/array and its nested objects/arrays as well
3
- */
4
- export default function clone(value: any): any;
1
+ export default function clone<T>(value: T): T;
5
2
  //# sourceMappingURL=clone.d.ts.map
@@ -1,12 +1,24 @@
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 (value && (Array.isArray(value) || typeof value === "object")) {
8
- return merge({}, value);
3
+ if (typeof value !== "object" || value === null) {
4
+ // if value is not an object or is null, return the value as is
5
+ return value;
9
6
  }
10
- return value;
7
+ if (Array.isArray(value)) {
8
+ // if value is an array, create a new array and clone its elements recursively
9
+ const clonedArray = [];
10
+ for (const element of value) {
11
+ clonedArray.push(clone(element));
12
+ }
13
+ return clonedArray;
14
+ }
15
+ // if value is an object, create a new object and clone its properties recursively
16
+ const clonedObject = Object.create(Object.getPrototypeOf(value));
17
+ for (const property in value) {
18
+ if (Object.prototype.hasOwnProperty.call(value, property)) {
19
+ clonedObject[property] = clone(value[property]);
20
+ }
21
+ }
22
+ return clonedObject;
11
23
  }
12
24
  module.exports = clone;
@@ -1,2 +1,5 @@
1
+ /**
2
+ * Merge deep objects or arrays
3
+ */
1
4
  export default function merge(...objects: any[]): any;
2
5
  //# sourceMappingURL=merge.d.ts.map
@@ -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
31
  if (objects.length === 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 (Array.isArray(mergedObject)) {
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
@@ -1,5 +1,2 @@
1
- /**
2
- * Make a deep copy for the given object/array and its nested objects/arrays as well
3
- */
4
- export default function clone(value: any): any;
1
+ export default function clone<T>(value: T): T;
5
2
  //# sourceMappingURL=clone.d.ts.map
@@ -1,11 +1,23 @@
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 (value && (Array.isArray(value) || typeof value === "object")) {
7
- return merge({}, value);
2
+ if (typeof value !== "object" || value === null) {
3
+ // if value is not an object or is null, return the value as is
4
+ return value;
8
5
  }
9
- return value;
6
+ if (Array.isArray(value)) {
7
+ // if value is an array, create a new array and clone its elements recursively
8
+ const clonedArray = [];
9
+ for (const element of value) {
10
+ clonedArray.push(clone(element));
11
+ }
12
+ return clonedArray;
13
+ }
14
+ // if value is an object, create a new object and clone its properties recursively
15
+ const clonedObject = Object.create(Object.getPrototypeOf(value));
16
+ for (const property in value) {
17
+ if (Object.prototype.hasOwnProperty.call(value, property)) {
18
+ clonedObject[property] = clone(value[property]);
19
+ }
20
+ }
21
+ return clonedObject;
10
22
  }
11
23
  export { clone as default };
@@ -1,2 +1,5 @@
1
+ /**
2
+ * Merge deep objects or arrays
3
+ */
1
4
  export default function merge(...objects: any[]): any;
2
5
  //# sourceMappingURL=merge.d.ts.map
@@ -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
26
  if (objects.length === 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 (Array.isArray(mergedObject)) {
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mongez/reinforcements",
3
- "version": "2.3.2",
3
+ "version": "2.3.3",
4
4
  "description": "A lightweight package to give a massive reinforcements to variant types of data in Nodejs/Javascript",
5
5
  "main": "./cjs/index.js",
6
6
  "dependencies": {