relation-matcher 1.0.8 → 1.0.10
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/dist/jest.config.d.ts +1 -0
- package/dist/jest.config.js +1 -0
- package/dist/src/index.js +6 -32
- package/dist/src/index.test.js +7 -0
- package/dist/src/utils/invertInput.js +5 -4
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/jest.config.js +1 -0
- package/package.json +3 -3
- package/src/index.test.ts +17 -0
- package/src/index.ts +7 -40
- package/src/utils/invertInput.ts +4 -4
- package/tsconfig.json +1 -1
- package/dist/dist/jest.config.js +0 -10
- package/dist/dist/src/index.js +0 -107
- package/dist/dist/src/index.test.js +0 -81
- package/dist/dist/src/testing/file-output.js +0 -9
- package/dist/dist/src/testing/test-data.js +0 -164
- package/dist/dist/src/types/generic-bases.js +0 -1
- package/dist/dist/src/types/generic-less.js +0 -1
- package/dist/dist/src/types/inputs.js +0 -1
- package/dist/dist/src/types/return.js +0 -1
- package/dist/dist/src/types/typetest.js +0 -6
- package/dist/dist/src/types/utils.js +0 -1
- package/dist/dist/src/utils/invertInput.js +0 -17
- package/dist/dist/src/utils/keys.js +0 -1
- package/dist/dist/src/utils/matcher.js +0 -1
package/dist/jest.config.d.ts
CHANGED
package/dist/jest.config.js
CHANGED
package/dist/src/index.js
CHANGED
|
@@ -2,29 +2,24 @@ import invertInput, {} from "./utils/invertInput";
|
|
|
2
2
|
import { getJoinFinalKey } from "./utils/keys";
|
|
3
3
|
import { matcherFunc } from "./utils/matcher";
|
|
4
4
|
export const relationMatcherRoot = (inputs, output) => {
|
|
5
|
+
if (!inputs.length)
|
|
6
|
+
return {};
|
|
7
|
+
if (!Array.isArray(inputs)) {
|
|
8
|
+
throw new Error("Input must be an array.");
|
|
9
|
+
}
|
|
5
10
|
const invertedInput = invertInput(inputs);
|
|
6
11
|
const baseItems = invertedInput[output.base].reduce((final, row) => {
|
|
7
12
|
if (!row) {
|
|
8
13
|
return final;
|
|
9
14
|
}
|
|
10
|
-
// if (final[row[output.id as string] as string]) {
|
|
11
|
-
// return final;
|
|
12
|
-
// }
|
|
13
15
|
const relations = Object.entries(output).reduce((final, [key, value]) => {
|
|
14
|
-
// Filters out base parameters so value is only joins;
|
|
15
|
-
// value will be similar to { base: "team_to_user"; joinsTo: "team_id"; joinsFrom: "id"; joinType: "single"; }
|
|
16
16
|
if (typeof value === "object") {
|
|
17
17
|
const finalKey = getJoinFinalKey(key);
|
|
18
18
|
if (value.joinType === "array") {
|
|
19
19
|
const baseObjArr = invertedInput[value.base].filter(matcherFunc(row, value));
|
|
20
20
|
final[finalKey] = Object.values(baseObjArr.reduce((final, item) => {
|
|
21
|
-
if (!item
|
|
22
|
-
// final[item[value.id as string] as string]
|
|
23
|
-
)
|
|
21
|
+
if (!item)
|
|
24
22
|
return final;
|
|
25
|
-
// if (!item[value.joinsFrom as string]) {
|
|
26
|
-
console.log(item, value.joinsFrom);
|
|
27
|
-
// }
|
|
28
23
|
final[item[value.id]] = {
|
|
29
24
|
...item,
|
|
30
25
|
...relationMatcherJoiner(invertedInput, value, item),
|
|
@@ -41,12 +36,6 @@ export const relationMatcherRoot = (inputs, output) => {
|
|
|
41
36
|
};
|
|
42
37
|
}
|
|
43
38
|
}
|
|
44
|
-
// if (value.joinType === 'array')
|
|
45
|
-
// final[finalKey] = relationMatcherJoiner(
|
|
46
|
-
// invertedInput,
|
|
47
|
-
// value as Output,
|
|
48
|
-
// row[value.joinsFrom as string] as string,
|
|
49
|
-
// );
|
|
50
39
|
}
|
|
51
40
|
return final;
|
|
52
41
|
}, {});
|
|
@@ -60,17 +49,10 @@ export default relationMatcherRoot;
|
|
|
60
49
|
const relationMatcherJoiner = (input, output, joiningFrom) => {
|
|
61
50
|
return Object.entries(output).reduce((final, [key, value]) => {
|
|
62
51
|
if (typeof value === "object") {
|
|
63
|
-
// console.log(value.base);
|
|
64
52
|
const finalKey = key.replace(/^_/, "");
|
|
65
53
|
const matcherFunc = (item) => item?.[value.joinsTo] === joiningFrom[value.joinsFrom];
|
|
66
54
|
if (value.joinType === "array") {
|
|
67
55
|
const baseObjArr = input[value.base].filter(matcherFunc);
|
|
68
|
-
// console.log("BaseObjArr:", baseObjArr);
|
|
69
|
-
// console.log(
|
|
70
|
-
// "input[value.base]:",
|
|
71
|
-
// input[value.base]?.[0]?.[value.joinsTo],
|
|
72
|
-
// );
|
|
73
|
-
// console.log("joiningId", joiningId);
|
|
74
56
|
final[finalKey] = Object.values(baseObjArr.reduce((final, item) => {
|
|
75
57
|
if (!item)
|
|
76
58
|
return final;
|
|
@@ -83,17 +65,9 @@ const relationMatcherJoiner = (input, output, joiningFrom) => {
|
|
|
83
65
|
};
|
|
84
66
|
return final;
|
|
85
67
|
}, {}));
|
|
86
|
-
// baseObjArr.map(item => (item ? {
|
|
87
|
-
// ...item,
|
|
88
|
-
// ...relationMatcherJoiner(input, value, item[value.joinsFrom as keyof TInputBase])
|
|
89
|
-
// } : null))
|
|
90
68
|
}
|
|
91
69
|
else {
|
|
92
70
|
const item = input[value.base].find(matcherFunc) ?? null;
|
|
93
|
-
// console.log("item:", item);
|
|
94
|
-
// console.log("input[value.base]:", input[value.base]);
|
|
95
|
-
// console.log("joiningId:", joiningId);
|
|
96
|
-
// console.log("value:", value);
|
|
97
71
|
if (item) {
|
|
98
72
|
final[finalKey] = {
|
|
99
73
|
...item,
|
package/dist/src/index.test.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
const invertInput = (inputs) => {
|
|
2
2
|
const invertedInput = inputs.reduce((final, row) => {
|
|
3
3
|
Object.entries(row).forEach(([header, value]) => {
|
|
4
|
-
if (!value)
|
|
5
|
-
return;
|
|
6
4
|
if (final[header]) {
|
|
7
|
-
|
|
5
|
+
if (value)
|
|
6
|
+
final[header].push(value);
|
|
8
7
|
}
|
|
9
8
|
else {
|
|
10
|
-
final[header] =
|
|
9
|
+
final[header] = value
|
|
10
|
+
? [value]
|
|
11
|
+
: [];
|
|
11
12
|
}
|
|
12
13
|
});
|
|
13
14
|
return final;
|