@arrai-innovations/reactive-helpers 2.4.0 → 2.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arrai-innovations/reactive-helpers",
3
- "version": "2.4.0",
3
+ "version": "2.5.0",
4
4
  "description": "VueJS 3 utility composition functions to help manipulate objects and lists.",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -0,0 +1,118 @@
1
+ import { flattenPaths } from "../../../utils/flattenPaths";
2
+
3
+ describe("utils/flattenPaths", () => {
4
+ describe("flattenPaths", () => {
5
+ describe("should work on objects as the base", () => {
6
+ it("with a single level", () => {
7
+ expect(flattenPaths({ a: 1, b: 2 })).toEqual(["a", "b"]);
8
+ });
9
+ it("with a single level and a nested object", () => {
10
+ expect(flattenPaths({ a: 1, b: { c: 2 } })).toEqual(["a", "b.c"]);
11
+ });
12
+ it("with a single level and a nested array", () => {
13
+ expect(flattenPaths({ a: 1, b: [2] })).toEqual(["a", "b[0]"]);
14
+ });
15
+ it("with a single level and a nested object and array", () => {
16
+ expect(flattenPaths({ a: 1, b: { c: [2] } })).toEqual(["a", "b.c[0]"]);
17
+ });
18
+ });
19
+ describe("should work on arrays as the base", () => {
20
+ it("with a single level", () => {
21
+ expect(flattenPaths([1, 2])).toEqual(["[0]", "[1]"]);
22
+ });
23
+ it("with a single level and a nested object", () => {
24
+ expect(flattenPaths([1, { c: 2 }])).toEqual(["[0]", "[1].c"]);
25
+ });
26
+ it("with a single level and a nested array", () => {
27
+ expect(flattenPaths([1, [2]])).toEqual(["[0]", "[1][0]"]);
28
+ });
29
+ it("with a single level and a nested object and array", () => {
30
+ expect(flattenPaths([1, { c: [2] }])).toEqual(["[0]", "[1].c[0]"]);
31
+ });
32
+ });
33
+ it("should work on a real world example", () => {
34
+ const toBeFlattened = {
35
+ id: 1,
36
+ name: "test",
37
+ description: "test",
38
+ type: "test",
39
+ status: {
40
+ id: 1,
41
+ code: "test",
42
+ name: "test",
43
+ },
44
+ products: [
45
+ {
46
+ id: 1,
47
+ name: "test",
48
+ description: "test",
49
+ categories: [
50
+ {
51
+ id: 1,
52
+ name: "test",
53
+ description: "test",
54
+ },
55
+ {
56
+ id: 2,
57
+ name: "test2",
58
+ description: "test2",
59
+ },
60
+ ],
61
+ price: "$1.00",
62
+ regularPrice: "$1.00",
63
+ },
64
+ {
65
+ id: 2,
66
+ name: "test2",
67
+ description: "test2",
68
+ categories: [
69
+ {
70
+ id: 3,
71
+ name: "test3",
72
+ description: "test3",
73
+ },
74
+ {
75
+ id: 4,
76
+ name: "test4",
77
+ description: "test4",
78
+ },
79
+ ],
80
+ price: "$2.00",
81
+ regularPrice: "$2.00",
82
+ },
83
+ ],
84
+ };
85
+ expect(flattenPaths(toBeFlattened)).toEqual([
86
+ "id",
87
+ "name",
88
+ "description",
89
+ "type",
90
+ "status.id",
91
+ "status.code",
92
+ "status.name",
93
+ "products[0].id",
94
+ "products[0].name",
95
+ "products[0].description",
96
+ "products[0].categories[0].id",
97
+ "products[0].categories[0].name",
98
+ "products[0].categories[0].description",
99
+ "products[0].categories[1].id",
100
+ "products[0].categories[1].name",
101
+ "products[0].categories[1].description",
102
+ "products[0].price",
103
+ "products[0].regularPrice",
104
+ "products[1].id",
105
+ "products[1].name",
106
+ "products[1].description",
107
+ "products[1].categories[0].id",
108
+ "products[1].categories[0].name",
109
+ "products[1].categories[0].description",
110
+ "products[1].categories[1].id",
111
+ "products[1].categories[1].name",
112
+ "products[1].categories[1].description",
113
+ "products[1].price",
114
+ "products[1].regularPrice",
115
+ ]);
116
+ });
117
+ });
118
+ });
@@ -0,0 +1,28 @@
1
+ import { isArray, isObject } from "lodash";
2
+
3
+ export function flattenPaths(arrayOrObject, currentPath = "") {
4
+ // arrayOrObject keys or indexes values can be objects or arrays.
5
+ // find all paths you could use lodash to "get()" to.
6
+ // indexes use `[${index}]`, keys use `.${key}`
7
+ // first level should not include a leading ".", `[${index}]` or `${key}` is fine.
8
+ const paths = [];
9
+ const keysOrIndexes = isArray(arrayOrObject);
10
+ const dotOrNot = currentPath ? "." : "";
11
+ if (isObject(arrayOrObject)) {
12
+ Object.keys(arrayOrObject).forEach((key) => {
13
+ const value = arrayOrObject[key];
14
+ const keyPath = keysOrIndexes ? `[${key}]` : `${dotOrNot}${key}`;
15
+ if (isObject(value) || isArray(value)) {
16
+ paths.push(...flattenPaths(value, `${currentPath}${keyPath}`));
17
+ } else {
18
+ paths.push(`${currentPath}${keyPath}`);
19
+ }
20
+ });
21
+ } else {
22
+ // values
23
+ if (currentPath) {
24
+ paths.push(currentPath);
25
+ }
26
+ }
27
+ return paths;
28
+ }
package/utils/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./assignReactiveObject";
2
2
  export * from "./cancellableIntent";
3
+ export * from "./flattenPaths";
3
4
  export * from "./getFakeId";
4
5
  export * from "./keyDiff";
5
6
  export * from "./set";