@arrai-innovations/reactive-helpers 8.3.1 → 8.3.2

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/utils/compact.js +23 -19
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arrai-innovations/reactive-helpers",
3
- "version": "8.3.1",
3
+ "version": "8.3.2",
4
4
  "description": "VueJS 3 utility composition functions to help manipulate objects and lists.",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/utils/compact.js CHANGED
@@ -4,12 +4,7 @@ const removeEmptyObjectsRecursive = (obj) => {
4
4
  if (typeof obj !== "object" || obj === null) {
5
5
  return;
6
6
  }
7
- if (Array.isArray(obj)) {
8
- for (const item of obj) {
9
- removeEmptyObjectsRecursive(item);
10
- }
11
- return;
12
- }
7
+ // we need to go deep first, so we so empty nested objects are removed before we check if the object is empty
13
8
  for (const [key, value] of Object.entries(obj)) {
14
9
  if (typeof value === "object" && value !== null) {
15
10
  if (Object.keys(value).length === 0) {
@@ -19,6 +14,12 @@ const removeEmptyObjectsRecursive = (obj) => {
19
14
  removeEmptyObjectsRecursive(value);
20
15
  }
21
16
  }
17
+ if (Array.isArray(obj)) {
18
+ for (const item of obj) {
19
+ removeEmptyObjectsRecursive(item);
20
+ }
21
+ return;
22
+ }
22
23
  };
23
24
 
24
25
  /**
@@ -33,6 +34,12 @@ const compactSparseArraysRecursive = (obj) => {
33
34
  if (typeof obj !== "object" || obj === null) {
34
35
  return;
35
36
  }
37
+ // we need to go deep first, so we so empty nested arrays are removed before we check if the array is empty
38
+ for (const value of Object.values(obj)) {
39
+ if (typeof value === "object" && value !== null) {
40
+ compactSparseArraysRecursive(value);
41
+ }
42
+ }
36
43
  if (Array.isArray(obj)) {
37
44
  for (let i = 0; i < obj.length; i++) {
38
45
  if (obj[i] === undefined) {
@@ -41,11 +48,6 @@ const compactSparseArraysRecursive = (obj) => {
41
48
  }
42
49
  }
43
50
  }
44
- for (const value of Object.values(obj)) {
45
- if (typeof value === "object" && value !== null) {
46
- compactSparseArraysRecursive(value);
47
- }
48
- }
49
51
  };
50
52
 
51
53
  /**
@@ -60,14 +62,8 @@ const removeEmptyObjectsAndCompactSparseArraysRecursive = (obj) => {
60
62
  if (typeof obj !== "object" || obj === null) {
61
63
  return;
62
64
  }
63
- if (Array.isArray(obj)) {
64
- for (let i = 0; i < obj.length; i++) {
65
- if (obj[i] === undefined || isEmpty(obj[i])) {
66
- obj.splice(i, 1);
67
- i--;
68
- }
69
- }
70
- }
65
+ // we need to go deep first, so we so empty properties of objects or items in arrays are removed
66
+ // before we check if the object or array is empty
71
67
  for (const [key, value] of Object.entries(obj)) {
72
68
  if (typeof value === "object" && value !== null) {
73
69
  if (Object.keys(value).length === 0) {
@@ -77,6 +73,14 @@ const removeEmptyObjectsAndCompactSparseArraysRecursive = (obj) => {
77
73
  removeEmptyObjectsAndCompactSparseArraysRecursive(value);
78
74
  }
79
75
  }
76
+ if (Array.isArray(obj)) {
77
+ for (let i = 0; i < obj.length; i++) {
78
+ if (obj[i] === undefined || isEmpty(obj[i])) {
79
+ obj.splice(i, 1);
80
+ i--;
81
+ }
82
+ }
83
+ }
80
84
  };
81
85
 
82
86
  /**