@arrai-innovations/reactive-helpers 2.3.4 → 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 +1 -1
- package/tests/unit/utils/flattenPaths.spec.js +118 -0
- package/utils/flattenPaths.js +28 -0
- package/utils/getFakeId.js +10 -5
- package/utils/index.js +1 -0
package/package.json
CHANGED
|
@@ -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/getFakeId.js
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
import { isArray } from "lodash";
|
|
1
|
+
import { isArray, isSet } from "lodash";
|
|
2
2
|
|
|
3
|
-
export function getFakeId(
|
|
3
|
+
export function getFakeId(arraySetOrObject, key = "id") {
|
|
4
|
+
// sets are assumed to be of ids
|
|
5
|
+
// arrays are assumed to be objects with an property matching the passed key
|
|
6
|
+
// objects are assumed to have keys that are ids
|
|
4
7
|
let fakeId;
|
|
5
8
|
let test;
|
|
6
|
-
if (
|
|
7
|
-
test = () =>
|
|
9
|
+
if (isSet(arraySetOrObject)) {
|
|
10
|
+
test = () => arraySetOrObject.has(fakeId);
|
|
11
|
+
} else if (isArray(arraySetOrObject)) {
|
|
12
|
+
test = () => arraySetOrObject.some((item) => item[key] === fakeId);
|
|
8
13
|
} else {
|
|
9
|
-
test = () => fakeId in
|
|
14
|
+
test = () => fakeId in arraySetOrObject;
|
|
10
15
|
}
|
|
11
16
|
do {
|
|
12
17
|
fakeId = Math.floor(Math.random() * Number.MIN_SAFE_INTEGER);
|