-
Notifications
You must be signed in to change notification settings - Fork 586
/
object-mapping.ts
201 lines (185 loc) · 5.9 KB
/
object-mapping.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* A set of instructions for multiple keys.
* The aim is to provide a concise yet readable way to map and filter values
* onto a target object.
*
* @example
* ```javascript
* const example: ObjectMappingInstructions = {
* lazyValue1: [, () => 1],
* lazyValue2: [, () => 2],
* lazyValue3: [, () => 3],
* lazyConditionalValue1: [() => true, () => 4],
* lazyConditionalValue2: [() => true, () => 5],
* lazyConditionalValue3: [true, () => 6],
* lazyConditionalValue4: [false, () => 44],
* lazyConditionalValue5: [() => false, () => 55],
* lazyConditionalValue6: ["", () => 66],
* simpleValue1: [, 7],
* simpleValue2: [, 8],
* simpleValue3: [, 9],
* conditionalValue1: [() => true, 10],
* conditionalValue2: [() => true, 11],
* conditionalValue3: [{}, 12],
* conditionalValue4: [false, 110],
* conditionalValue5: [() => false, 121],
* conditionalValue6: ["", 132],
* };
*
* const exampleResult: Record<string, any> = {
* lazyValue1: 1,
* lazyValue2: 2,
* lazyValue3: 3,
* lazyConditionalValue1: 4,
* lazyConditionalValue2: 5,
* lazyConditionalValue3: 6,
* simpleValue1: 7,
* simpleValue2: 8,
* simpleValue3: 9,
* conditionalValue1: 10,
* conditionalValue2: 11,
* conditionalValue3: 12,
* };
* ```
*/
export type ObjectMappingInstructions = Record<string, ObjectMappingInstruction>;
/**
* An instruction set for assigning a value to a target object.
*/
export type ObjectMappingInstruction =
| LazyValueInstruction
| ConditionalLazyValueInstruction
| SimpleValueInstruction
| ConditionalValueInstruction
| UnfilteredValue;
/**
* non-array
*/
export type UnfilteredValue = any;
export type LazyValueInstruction = [FilterStatus, ValueSupplier];
export type ConditionalLazyValueInstruction = [FilterStatusSupplier, ValueSupplier];
export type SimpleValueInstruction = [FilterStatus, Value];
export type ConditionalValueInstruction = [ValueFilteringFunction, Value];
/**
* Filter is considered passed if
* 1. It is a boolean true.
* 2. It is not undefined and is itself truthy.
* 3. It is undefined and the corresponding _value_ is neither null nor undefined.
*/
export type FilterStatus = boolean | unknown | void;
/**
* Supplies the filter check but not against any value as input.
*/
export type FilterStatusSupplier = () => boolean;
/**
* Filter check with the given value.
*/
export type ValueFilteringFunction = (value: any) => boolean;
/**
* Supplies the value for lazy evaluation.
*/
export type ValueSupplier = () => any;
/**
* A non-function value.
*/
export type Value = any;
/**
* Internal/Private, for codegen use only.
*
* Transfer a set of keys from [instructions] to [target].
*
* For each instruction in the record, the target key will be the instruction key.
* The target assignment will be conditional on the instruction's filter.
* The target assigned value will be supplied by the instructions as an evaluable function or non-function value.
*
* @see ObjectMappingInstructions for an example.
* @private
*/
export function map(
target: any,
filter: (value: any) => boolean,
instructions: Record<string, ValueSupplier | Value>
): typeof target;
export function map(instructions: Record<string, ObjectMappingInstruction>): any;
export function map(target: any, instructions: Record<string, ObjectMappingInstruction>): typeof target;
export function map(arg0: any, arg1?: any, arg2?: any): any {
let target: any;
let filter: (value?: any) => boolean;
let instructions: ObjectMappingInstructions;
if (typeof arg1 === "undefined" && typeof arg2 === "undefined") {
target = {};
instructions = arg0;
} else {
target = arg0;
if (typeof arg1 === "function") {
filter = arg1;
instructions = arg2;
return mapWithFilter(target, filter, instructions);
} else {
instructions = arg1;
}
}
for (const key of Object.keys(instructions)) {
if (!Array.isArray(instructions[key])) {
target[key] = instructions[key]; // unchecked value.
}
// eslint-disable-next-line prefer-const
let [filter, value]: [((_?: any) => boolean) | unknown, any] = instructions[key];
if (typeof value === "function") {
let _value: any;
const defaultFilterPassed = filter === undefined && (_value = value()) != null;
const customFilterPassed =
(typeof filter === "function" && !!filter(void 0)) || (typeof filter !== "function" && !!filter);
if (defaultFilterPassed) {
target[key] = _value;
} else if (customFilterPassed) {
target[key] = value();
}
} else {
const defaultFilterPassed = filter === undefined && value != null;
const customFilterPassed =
(typeof filter === "function" && !!filter(value)) || (typeof filter !== "function" && !!filter);
if (defaultFilterPassed || customFilterPassed) {
target[key] = value;
}
}
}
return target;
}
/**
* Private, for codegen use only.
*
* @param target - target object.
* @param filter - uniform filter function to apply to all values
* @param instructions - map of keys and values/suppliers (will be evaluated)
*
* @private
*/
const mapWithFilter = (
target: any,
filter: (value: any) => boolean,
instructions: Record<string, ValueSupplier | Value | ObjectMappingInstruction>
): typeof target => {
return map(
target,
Object.entries(instructions).reduce(
(
_instructions: ObjectMappingInstructions,
[key, value]: [string, ValueSupplier | Value | ObjectMappingInstruction]
) => {
if (Array.isArray(value)) {
// is custom instruction and not a value or value supplier
_instructions[key] = value as ObjectMappingInstruction;
} else {
if (typeof value === "function") {
_instructions[key] = [filter, value()];
} else {
_instructions[key] = [filter, value];
}
}
return _instructions;
},
{}
)
);
};