@arrai-innovations/reactive-helpers 8.2.0 → 8.3.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/.husky/commit-msg CHANGED
@@ -1,2 +1,4 @@
1
1
  #!/usr/bin/env bash
2
+ . "$(dirname "$0")/_/husky.sh"
3
+
2
4
  npx --no-install commitlint --edit "$1"
package/.husky/pre-commit CHANGED
@@ -1,2 +1,5 @@
1
1
  #!/usr/bin/env bash
2
+ . "$(dirname "$0")/_/husky.sh"
3
+
2
4
  npx --no-install lint-staged
5
+ npm run docs
package/docs.md CHANGED
@@ -11,12 +11,15 @@
11
11
 
12
12
  ## Constants
13
13
 
14
- | Name | Description |
15
- | ------------------ | ----------- |
16
- | [objectifyClasses] |
17
- | [combineClasses] |
18
- | [stringifyClass] |
19
- | [stringifyClasses] |
14
+ | Name | Description |
15
+ | ------------------------------------------ | ------------------------------------------------------------------------------------------------------- |
16
+ | [objectifyClasses] |
17
+ | [combineClasses] |
18
+ | [stringifyClass] |
19
+ | [stringifyClasses] |
20
+ | [removeEmptyObjects] | Remove empty objects from a mixed object array tree. Mutates the object. |
21
+ | [compactSparseArrays] | Remove undefined values from arrays in a mixed object array tree. Mutates the object. |
22
+ | [removeEmptyObjectsAndCompactSparseArrays] | Remove empty objects and undefined values from arrays in a mixed object array tree. Mutates the object. |
20
23
 
21
24
  ## Functions
22
25
 
@@ -385,6 +388,36 @@ const transformed = transformWalk(obj, (key, value, path) => {
385
388
  | ---------- | -------------- | ------------------------------------------------------------------------------ |
386
389
  | ...classes | [`CSSClasses`] | Handles as arguments the multiple ways of specifying CSS class related values. |
387
390
 
391
+ ## removeEmptyObjects
392
+
393
+ Remove empty objects from a mixed object array tree. Mutates the object.
394
+
395
+ **Kind**: global constant
396
+
397
+ | Param | Type | Description |
398
+ | ----- | ------------------- | ------------------------------------------------- |
399
+ | obj | `object` \| `Array` | The object or array to remove empty objects from. |
400
+
401
+ ## compactSparseArrays
402
+
403
+ Remove undefined values from arrays in a mixed object array tree. Mutates the object.
404
+
405
+ **Kind**: global constant
406
+
407
+ | Param | Type | Description |
408
+ | ----- | ------------------- | ------------------------------------------------ |
409
+ | obj | `object` \| `Array` | The object or array to compact sparse arrays in. |
410
+
411
+ ## removeEmptyObjectsAndCompactSparseArrays
412
+
413
+ Remove empty objects and undefined values from arrays in a mixed object array tree. Mutates the object.
414
+
415
+ **Kind**: global constant
416
+
417
+ | Param | Type | Description |
418
+ | ----- | ------------------- | ------------------------------------------------------------------------------ |
419
+ | obj | `object` \| `Array` | The object or array to remove empty objects from and compact sparse arrays in. |
420
+
388
421
  ## useCombineClasses(classes)
389
422
 
390
423
  **Kind**: global function
@@ -460,6 +493,9 @@ A CSS object or a space-separated list of CSS classes.
460
493
  [combineclasses]: #combineclasses
461
494
  [stringifyclass]: #stringifyclass
462
495
  [stringifyclasses]: #stringifyclasses
496
+ [removeemptyobjects]: #removeemptyobjects
497
+ [compactsparsearrays]: #compactsparsearrays
498
+ [removeemptyobjectsandcompactsparsearrays]: #removeemptyobjectsandcompactsparsearrays
463
499
  [cssvalue]: #cssvalue
464
500
  [cssobject]: #cssobject
465
501
  [cssclasses]: #cssclasses
@@ -1,9 +1,5 @@
1
1
  export default {
2
- "**/*.{js,cjs,mjs,ts,jsx,tsx}": [
3
- "npx --no-install eslint --fix",
4
- "npx --no-install prettier --write",
5
- () => "npm run docs",
6
- ],
2
+ "**/*.{js,cjs,mjs,ts,jsx,tsx}": ["npx --no-install eslint --fix", "npx --no-install prettier --write"],
7
3
  "**/*.{markdown,md}": ["npx --no-install doctoc --github -u ."],
8
4
  "**/*.{less,scss,css,vue,markdown,json,md,yml,yaml,html}": ["npx --no-install prettier --write"],
9
5
  ".circleci/config.yml": ["circleci config validate"],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arrai-innovations/reactive-helpers",
3
- "version": "8.2.0",
3
+ "version": "8.3.0",
4
4
  "description": "VueJS 3 utility composition functions to help manipulate objects and lists.",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -0,0 +1,86 @@
1
+ const removeEmptyObjectsRecursive = (obj) => {
2
+ if (typeof obj !== "object" || obj === null) {
3
+ return;
4
+ }
5
+ if (Array.isArray(obj)) {
6
+ for (const item of obj) {
7
+ removeEmptyObjectsRecursive(item);
8
+ }
9
+ return;
10
+ }
11
+ for (const [key, value] of Object.entries(obj)) {
12
+ if (typeof value === "object" && value !== null) {
13
+ if (Object.keys(value).length === 0) {
14
+ delete obj[key];
15
+ continue;
16
+ }
17
+ removeEmptyObjectsRecursive(value);
18
+ }
19
+ }
20
+ };
21
+
22
+ /**
23
+ * Remove empty objects from a mixed object array tree. Mutates the object.
24
+ * @param {object | Array} obj - The object or array to remove empty objects from.
25
+ */
26
+ export const removeEmptyObjects = (obj) => {
27
+ removeEmptyObjectsRecursive(obj);
28
+ };
29
+
30
+ const compactSparseArraysRecursive = (obj) => {
31
+ if (typeof obj !== "object" || obj === null) {
32
+ return;
33
+ }
34
+ if (Array.isArray(obj)) {
35
+ for (let i = 0; i < obj.length; i++) {
36
+ if (obj[i] === undefined) {
37
+ obj.splice(i, 1);
38
+ i--;
39
+ }
40
+ }
41
+ }
42
+ for (const value of Object.values(obj)) {
43
+ if (typeof value === "object" && value !== null) {
44
+ compactSparseArraysRecursive(value);
45
+ }
46
+ }
47
+ };
48
+
49
+ /**
50
+ * Remove undefined values from arrays in a mixed object array tree. Mutates the object.
51
+ * @param {object | Array} obj - The object or array to compact sparse arrays in.
52
+ */
53
+ export const compactSparseArrays = (obj) => {
54
+ compactSparseArraysRecursive(obj);
55
+ };
56
+
57
+ const removeEmptyObjectsAndCompactSparseArraysRecursive = (obj) => {
58
+ if (typeof obj !== "object" || obj === null) {
59
+ return;
60
+ }
61
+ if (Array.isArray(obj)) {
62
+ for (let i = 0; i < obj.length; i++) {
63
+ if (obj[i] === undefined) {
64
+ obj.splice(i, 1);
65
+ i--;
66
+ }
67
+ }
68
+ }
69
+ for (const [key, value] of Object.entries(obj)) {
70
+ if (typeof value === "object" && value !== null) {
71
+ if (Object.keys(value).length === 0) {
72
+ delete obj[key];
73
+ continue;
74
+ }
75
+ removeEmptyObjectsAndCompactSparseArraysRecursive(value);
76
+ }
77
+ }
78
+ };
79
+
80
+ /**
81
+ * Remove empty objects and undefined values from arrays in a mixed object array tree. Mutates the object.
82
+ * @param {object | Array} obj - The object or array to remove empty objects from and compact sparse arrays in.
83
+ */
84
+ export const removeEmptyObjectsAndCompactSparseArrays = (obj) => {
85
+ removeEmptyObjectsAndCompactSparseArraysRecursive(obj);
86
+ };
package/utils/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./assignReactiveObject.js";
2
2
  export * from "./classes.js";
3
+ export * from "./compact.js";
3
4
  export * from "./debugMessage.js";
4
5
  export * from "./debugWatch.js";
5
6
  export * from "./flattenPaths.js";