-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.ts
151 lines (134 loc) · 3.75 KB
/
extract.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/ban-types */
import {isFunction, pick} from 'lodash-es';
import {cast} from '../other/index.js';
export type ExtractFieldTransformer<
T,
TResult = any,
TExtraArgs = any,
TData = any,
> = (val: T, extraArgs: TExtraArgs, data: TData) => TResult;
export type ExtractFieldsDefaultScalarTypes =
| undefined
| boolean
| string
| number
| bigint
| symbol
| null
| Date;
export type ExtractFieldsFrom<
T extends object,
TResult extends Record<keyof T, any> = T,
TExtraArgs = undefined,
TScalarTypes = ExtractFieldsDefaultScalarTypes,
> = {
[Key in keyof Required<T>]: T[Key] extends TScalarTypes | TScalarTypes[]
? boolean | ExtractFieldTransformer<T[Key], TResult[Key], TExtraArgs, T>
: ExtractFieldTransformer<T[Key], TResult[Key], TExtraArgs, T>;
};
export interface ExtractObjectPaths<
T extends object,
TResult extends Record<keyof T, any> = T,
TExtraArgs = any,
> {
object: T;
extraArgs: TExtraArgs;
result: TResult;
scalarFields: string[];
scalarFieldsWithTransformers: Array<{
property: string;
transformer: ExtractFieldTransformer<any>;
}>;
finalize?: (
data: T,
currentResult: TResult,
extraArgs: TExtraArgs
) => TResult;
}
export function getExtractFields<
T extends object,
TResult extends Record<keyof T, any> = T,
TExtraArgs = any,
TScalarTypes = ExtractFieldsDefaultScalarTypes,
>(
data: ExtractFieldsFrom<T, TResult, TExtraArgs, TScalarTypes>,
finalizeFn?: (
data: T,
currentResult: TResult,
extraArgs: TExtraArgs
) => TResult
): ExtractObjectPaths<T, TResult, TExtraArgs> {
const keys = Object.keys(data) as Array<keyof typeof data>;
return keys.reduce(
(paths, key) => {
const value = data[key];
if (isFunction(value)) {
paths.scalarFieldsWithTransformers.push({
property: key as string,
transformer: value,
});
} else {
paths.scalarFields.push(key as string);
}
return paths;
},
{
scalarFields: [],
scalarFieldsWithTransformers: [],
object: cast<T>({}),
extraArgs: cast<TExtraArgs>({}),
result: cast<TResult>({}),
finalize: finalizeFn,
} as ExtractObjectPaths<T, TResult, TExtraArgs>
);
}
export function extractFields<
TObjectPaths extends ExtractObjectPaths<any>,
TData extends Partial<
Record<keyof TObjectPaths['object'], any>
> = TObjectPaths['object'],
>(
data: TData,
paths: TObjectPaths,
extraArgs?: TObjectPaths['extraArgs']
): TObjectPaths['result'] {
let result = pick(data, paths.scalarFields);
paths.scalarFieldsWithTransformers.forEach(({property, transformer}) => {
const propValue = data[property];
if (propValue === undefined) {
return;
}
result[property] =
propValue === null ? null : transformer(propValue, extraArgs, data);
});
if (paths.finalize) {
result = paths.finalize(data, result, extraArgs);
}
return result as unknown as TObjectPaths['result'];
}
// TODO: make extract simpler
export function makeExtract<T extends ExtractObjectPaths<any>>(fields: T) {
const fn = <T1 extends T['object']>(data: Partial<Record<keyof T1, any>>) => {
return extractFields(data, fields);
};
return fn;
}
export function makeExtractIfPresent<T extends ExtractObjectPaths<any>>(
fields: T
) {
const fn = <T1 extends T['object']>(
data?: Partial<Record<keyof T1, any>>
) => {
return data && extractFields(data, fields);
};
return fn;
}
export function makeListExtract<T extends ExtractObjectPaths<any>>(fields: T) {
const fn = <T1 extends T['object']>(
data: Partial<Record<keyof T1, any>>[]
) => {
return data.map(datum => extractFields(datum, fields));
};
return fn;
}