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.
@@ -4,5 +4,6 @@ declare const _default: {
4
4
  transform: {
5
5
  "^.+\\.tsx?$": ["ts-jest", import("ts-jest").DefaultTransformOptions];
6
6
  };
7
+ testPathIgnorePatterns: string[];
7
8
  };
8
9
  export default _default;
@@ -7,4 +7,5 @@ export default {
7
7
  transform: {
8
8
  ...tsJestTransformCfg,
9
9
  },
10
+ testPathIgnorePatterns: ["/node_modules/", "/dist/"],
10
11
  };
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,
@@ -79,3 +79,10 @@ test("Tests output of mapper.", () => {
79
79
  },
80
80
  });
81
81
  });
82
+ test("Tests empty input.", () => {
83
+ const input = [];
84
+ expect(relationMatcherRoot(input, {
85
+ base: "user",
86
+ id: "clerk_id",
87
+ })).toStrictEqual({});
88
+ });
@@ -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
- final[header].push(value);
5
+ if (value)
6
+ final[header].push(value);
8
7
  }
9
8
  else {
10
- final[header] = [value];
9
+ final[header] = value
10
+ ? [value]
11
+ : [];
11
12
  }
12
13
  });
13
14
  return final;