relation-matcher 1.0.10 → 1.0.12

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.
@@ -1,71 +1,67 @@
1
- import type { TInputBase, TJoinedFromBase, TOutputBase } from "./generic-bases";
2
- import type { RelationMapRoot } from "./inputs";
3
- import type { ExtendsNull, NoUnderscore, Simplify } from "./utils";
4
-
5
- export type RelationMapperReturnRoot<
6
- TInput extends TInputBase,
7
- TOutput extends RelationMapRoot<TInput>,
8
- > = Simplify<
9
- {
10
- [inputKey in keyof TInput]: ExtendsNull<
11
- TInput[TOutput["base"]]
12
- > extends true
13
- ? Simplify<
14
- TInput[TOutput["base"]] &
15
- RelationMapperReturnJoinRoot<TInput, TOutput, inputKey>
16
- > | null
17
- : Simplify<
18
- TInput[TOutput["base"]] &
19
- RelationMapperReturnJoinRoot<TInput, TOutput, inputKey>
20
- >;
21
- }[keyof TInput]
22
- >;
23
-
24
- export type RelationMapperReturnJoinRoot<
25
- TInput extends TInputBase,
26
- TOutput extends RelationMapRoot<TInput>,
27
- TInputKey extends keyof TInput,
28
- > = {
29
- [outputKey in NoUnderscore<keyof TOutput>]: Simplify<
30
- RelationMapperReturn<
31
- TInput,
32
- TInput[TInputKey],
33
- TOutput[`_${outputKey}`]
34
- >
35
- >;
36
- };
37
-
38
- /** Fill current object with value specified in base */
39
- export type RelationMapperReturn<
40
- TInput extends TInputBase,
41
- TJoinedFrom extends TJoinedFromBase,
42
- TOutput extends TOutputBase<TInput, TJoinedFrom>,
43
- > = TOutput["joinType"] extends "array"
44
- ? Array<
45
- Simplify<
46
- TInput[TOutput["base"]] &
47
- RelationMapperReturnJoin<TInput, TJoinedFrom, TOutput>
48
- >
49
- >
50
- : ExtendsNull<TInput[TOutput["base"]]> extends true
51
- ? Simplify<
52
- TInput[TOutput["base"]] &
53
- RelationMapperReturnJoin<TInput, TJoinedFrom, TOutput>
54
- > | null
55
- : Simplify<
56
- TInput[TOutput["base"]] &
57
- RelationMapperReturnJoin<TInput, TJoinedFrom, TOutput>
58
- >;
59
-
60
- /** Takes underscored values (joins) and replaces their values with the actual output. */
61
- export type RelationMapperReturnJoin<
62
- TInput extends TInputBase,
63
- TJoinedFrom extends TJoinedFromBase,
64
- TOutput extends TOutputBase<TInput, TJoinedFrom>,
65
- > = {
66
- [k in NoUnderscore<keyof TOutput>]: RelationMapperReturn<
67
- TInput,
68
- TJoinedFrom,
69
- TOutput[`_${k}`]
70
- >;
71
- };
1
+ import type { TInputBase, TJoinedFromBase, TOutputBase } from "./generic-bases";
2
+ import type { RelationMapRoot } from "./inputs";
3
+ import type { ExtendsNull, NoUnderscore, Simplify } from "./utils";
4
+
5
+ export type RelationMapperReturnRoot<
6
+ TInput extends TInputBase,
7
+ TOutput extends RelationMapRoot<TInput>,
8
+ > = Simplify<
9
+ ExtendsNull<TInput[TOutput["base"]]> extends true
10
+ ?
11
+ | (TInput[TOutput["base"]] &
12
+ RelationMapperReturnJoinRoot<
13
+ TInput,
14
+ TOutput,
15
+ TOutput["base"]
16
+ >)
17
+ | null
18
+ : TInput[TOutput["base"]] &
19
+ RelationMapperReturnJoinRoot<TInput, TOutput, TOutput["base"]>
20
+ >;
21
+
22
+ export type RelationMapperReturnJoinRoot<
23
+ TInput extends TInputBase,
24
+ TOutput extends RelationMapRoot<TInput>,
25
+ TInputKey extends keyof TInput,
26
+ > = {
27
+ [outputKey in NoUnderscore<keyof TOutput>]: RelationMapperReturn<
28
+ TInput,
29
+ TInput[TInputKey],
30
+ TOutput[`_${outputKey}`]
31
+ >;
32
+ };
33
+
34
+ /** Fill current object with value specified in base */
35
+ export type RelationMapperReturn<
36
+ TInput extends TInputBase,
37
+ TJoinedFrom extends TJoinedFromBase,
38
+ TOutput extends TOutputBase<TInput, TJoinedFrom>,
39
+ > = TOutput["joinType"] extends "array"
40
+ ? Array<
41
+ Simplify<
42
+ TInput[TOutput["base"]] &
43
+ RelationMapperReturnJoin<TInput, TJoinedFrom, TOutput>
44
+ >
45
+ >
46
+ : ExtendsNull<TInput[TOutput["base"]]> extends true
47
+ ? Simplify<
48
+ TInput[TOutput["base"]] &
49
+ RelationMapperReturnJoin<TInput, TJoinedFrom, TOutput>
50
+ > | null
51
+ : Simplify<
52
+ TInput[TOutput["base"]] &
53
+ RelationMapperReturnJoin<TInput, TJoinedFrom, TOutput>
54
+ >;
55
+
56
+ /** Takes underscored values (joins) and replaces their values with the actual output. */
57
+ export type RelationMapperReturnJoin<
58
+ TInput extends TInputBase,
59
+ TJoinedFrom extends TJoinedFromBase,
60
+ TOutput extends TOutputBase<TInput, TJoinedFrom>,
61
+ > = {
62
+ [k in NoUnderscore<keyof TOutput>]: RelationMapperReturn<
63
+ TInput,
64
+ TJoinedFrom,
65
+ TOutput[`_${k}`]
66
+ >;
67
+ };
@@ -63,6 +63,14 @@ type B = RelationMapperReturnRoot<
63
63
  joinsFrom: "clerk_id";
64
64
  joinsTo: "user_clerk_id";
65
65
  joinType: "single";
66
+
67
+ _user: {
68
+ base: "user";
69
+ id: "clerk_id";
70
+ joinsFrom: "user_clerk_id";
71
+ joinsTo: "clerk_id";
72
+ joinType: "array";
73
+ };
66
74
  };
67
75
  }
68
76
  >;
@@ -1,28 +1,28 @@
1
- export type Simplify<T> = {
2
- [k in keyof T]: T[k];
3
- } & {};
4
-
5
- export type DeepSimplify<TInput, TDontSimplify> = {
6
- [k in keyof TInput]: TInput[k] extends TDontSimplify
7
- ? TInput[k]
8
- : DeepSimplify<TInput[k], TDontSimplify>;
9
- } & {};
10
-
11
- export type NoUnderscore<T extends string | number | symbol> =
12
- T extends `_${infer Result}` ? Result : never;
13
-
14
- export type DeepNonNullable<T extends object> = {
15
- [k in keyof T]: T[k] extends infer NonNull | null ? NonNull : T[k];
16
- };
17
-
18
- export type ArrayOrSingle<
19
- T,
20
- TType extends "single" | "array",
21
- > = TType extends "array" ? Array<T> : T;
22
-
23
- export type IsNullIfNull<
24
- TIsNull extends object | null,
25
- TReturn extends object | never = never,
26
- > = TIsNull extends infer T ? (T & TReturn) | null : TIsNull & TReturn;
27
-
28
- export type ExtendsNull<T> = null extends T ? true : false;
1
+ export type Simplify<T> = {
2
+ [k in keyof T]: T[k];
3
+ } & {};
4
+
5
+ export type DeepSimplify<TInput, TDontSimplify> = {
6
+ [k in keyof TInput]: TInput[k] extends TDontSimplify
7
+ ? TInput[k]
8
+ : DeepSimplify<TInput[k], TDontSimplify>;
9
+ } & {};
10
+
11
+ export type NoUnderscore<T extends string | number | symbol> =
12
+ T extends `_${infer Result}` ? Result : never;
13
+
14
+ export type DeepNonNullable<T extends object> = {
15
+ [k in keyof T]: T[k] extends infer NonNull | null ? NonNull : T[k];
16
+ };
17
+
18
+ export type ArrayOrSingle<
19
+ T,
20
+ TType extends "single" | "array",
21
+ > = TType extends "array" ? Array<T> : T;
22
+
23
+ export type IsNullIfNull<
24
+ TIsNull extends object | null,
25
+ TReturn extends object | never = never,
26
+ > = TIsNull extends infer T ? (T & TReturn) | null : TIsNull & TReturn;
27
+
28
+ export type ExtendsNull<T> = null extends T ? true : false;
@@ -1,28 +1,28 @@
1
- import type { TInputBase } from "~/types/generic-bases";
2
-
3
- export type InvertedInput<TInput extends TInputBase = TInputBase> = Record<
4
- keyof TInput,
5
- TInput[keyof TInput][]
6
- >;
7
-
8
- const invertInput = <TInput extends TInputBase>(
9
- inputs: TInputBase[],
10
- ): InvertedInput<TInput> => {
11
- const invertedInput = inputs.reduce<InvertedInput<TInput>>((final, row) => {
12
- Object.entries(row).forEach(([header, value]) => {
13
- if (final[header]) {
14
- if (value) final[header].push(value as TInput[keyof TInput]);
15
- } else {
16
- final[header as keyof TInput] = value
17
- ? [value as TInput[keyof TInput]]
18
- : [];
19
- }
20
- });
21
-
22
- return final;
23
- }, {} as InvertedInput<TInput>);
24
-
25
- return invertedInput;
26
- };
27
-
28
- export default invertInput;
1
+ import type { TInputBase } from "~/types/generic-bases";
2
+
3
+ export type InvertedInput<TInput extends TInputBase = TInputBase> = Record<
4
+ keyof TInput,
5
+ TInput[keyof TInput][]
6
+ >;
7
+
8
+ const invertInput = <TInput extends TInputBase>(
9
+ inputs: TInputBase[],
10
+ ): InvertedInput<TInput> => {
11
+ const invertedInput = inputs.reduce<InvertedInput<TInput>>((final, row) => {
12
+ Object.entries(row).forEach(([header, value]) => {
13
+ if (final[header]) {
14
+ if (value) final[header].push(value as TInput[keyof TInput]);
15
+ } else {
16
+ final[header as keyof TInput] = value
17
+ ? [value as TInput[keyof TInput]]
18
+ : [];
19
+ }
20
+ });
21
+
22
+ return final;
23
+ }, {} as InvertedInput<TInput>);
24
+
25
+ return invertedInput;
26
+ };
27
+
28
+ export default invertInput;
package/src/utils/keys.ts CHANGED
@@ -1 +1 @@
1
- export const getJoinFinalKey = (key: string) => key.substring(1);
1
+ export const getJoinFinalKey = (key: string) => key.substring(1);
@@ -1,8 +1,8 @@
1
- import type { TInputBase } from "~/types/generic-bases";
2
- import type { InvertedInput } from "./invertInput";
3
- import type { Output } from "~/types/generic-less";
4
-
5
- export const matcherFunc =
6
- <TInput extends TInputBase>(row: TInput[keyof TInput], value: Output) =>
7
- (item: InvertedInput[keyof InvertedInput][number]) =>
8
- row?.[value.joinsFrom as string] === item?.[value.joinsTo as string];
1
+ import type { TInputBase } from "~/types/generic-bases";
2
+ import type { InvertedInput } from "./invertInput";
3
+ import type { Output } from "~/types/generic-less";
4
+
5
+ export const matcherFunc =
6
+ <TInput extends TInputBase>(row: TInput[keyof TInput], value: Output) =>
7
+ (item: InvertedInput[keyof InvertedInput][number]) =>
8
+ row?.[value.joinsFrom as string] === item?.[value.joinsTo as string];