@vertexvis/utils 0.24.5-canary.6 → 1.0.0-testing.1
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/async.d.ts +79 -79
- package/dist/binaryReader.d.ts +58 -58
- package/dist/browser.cjs.js +1202 -1351
- package/dist/browser.cjs.js.map +1 -1
- package/dist/browser.esm.js +1197 -1351
- package/dist/browser.esm.js.map +1 -1
- package/dist/bundle.cjs.js +1199 -1344
- package/dist/bundle.cjs.js.map +1 -1
- package/dist/bundle.esm.js +1197 -1343
- package/dist/bundle.esm.js.map +1 -1
- package/dist/color.d.ts +62 -62
- package/dist/disposable.d.ts +10 -10
- package/dist/eventDispatcher.d.ts +16 -16
- package/dist/eventTargets.d.ts +10 -10
- package/dist/index.d.ts +19 -19
- package/dist/mappedTypes.d.ts +49 -49
- package/dist/mapper.d.ts +270 -270
- package/dist/objects.d.ts +72 -72
- package/dist/predicate.d.ts +5 -5
- package/dist/range.d.ts +86 -86
- package/dist/sets.d.ts +1 -1
- package/dist/sort.d.ts +21 -21
- package/dist/strings.d.ts +3 -3
- package/dist/uri.d.ts +56 -56
- package/dist/uuid.d.ts +8 -8
- package/package.json +11 -11
package/dist/mapper.d.ts
CHANGED
|
@@ -1,270 +1,270 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A module for defining functional schemas to map between different types. This
|
|
3
|
-
* module is useful for parsing to or from JSON/protobufs to domain types.
|
|
4
|
-
*
|
|
5
|
-
* Mappers support greedy validation, so all validation errors are aggregated
|
|
6
|
-
* and reported vs failing on the first invalid input.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
*
|
|
10
|
-
* ```ts
|
|
11
|
-
* import { Mapper as M } from '@vertexvis/utils';
|
|
12
|
-
*
|
|
13
|
-
* interface Address {
|
|
14
|
-
* address: string;
|
|
15
|
-
* city: string;
|
|
16
|
-
* state: string;
|
|
17
|
-
* zip: string;
|
|
18
|
-
* }
|
|
19
|
-
*
|
|
20
|
-
* interface Person {
|
|
21
|
-
* name: string;
|
|
22
|
-
* addresses: Address[];
|
|
23
|
-
* }
|
|
24
|
-
*
|
|
25
|
-
* type AddressJson = Partial<Address>;
|
|
26
|
-
* type PersonJson = {
|
|
27
|
-
* name?: string;
|
|
28
|
-
* addresses?: AddressJson[];
|
|
29
|
-
* }
|
|
30
|
-
*
|
|
31
|
-
* const mapAddress: M.Func<AddressJson, Address> = M.defineMapper(
|
|
32
|
-
* M.read(
|
|
33
|
-
* M.requireProp('address'),
|
|
34
|
-
* M.requireProp('city'),
|
|
35
|
-
* M.requireProp('state'),
|
|
36
|
-
* M.requireProp('zip')
|
|
37
|
-
* ),
|
|
38
|
-
* ([address, city, state, zip]) => ({
|
|
39
|
-
* address, city, state, zip
|
|
40
|
-
* })
|
|
41
|
-
* );
|
|
42
|
-
*
|
|
43
|
-
* const mapPerson: M.Func<PersonJson, Person> = M.defineMapper(
|
|
44
|
-
* M.read(
|
|
45
|
-
* M.requireProp('name'),
|
|
46
|
-
* M.mapProp(
|
|
47
|
-
* 'addresses',
|
|
48
|
-
* M.compose(M.required('addresses'), M.mapArray(mapAddress))
|
|
49
|
-
* )
|
|
50
|
-
* ),
|
|
51
|
-
* ([name, addresses]) => ({ name, addresses })
|
|
52
|
-
* );
|
|
53
|
-
*
|
|
54
|
-
* const person = mapPerson({
|
|
55
|
-
* name: 'John',
|
|
56
|
-
* addresses: [{ address: '123', city: 'Ames', state: 'IA', zip: '50010' }]
|
|
57
|
-
* });
|
|
58
|
-
*
|
|
59
|
-
* const invalidPerson = mapPerson({
|
|
60
|
-
* addresses: [{ city: 'Ames', state: 'IA', zip: '50010' }]
|
|
61
|
-
* });
|
|
62
|
-
* ```
|
|
63
|
-
* // {
|
|
64
|
-
* // errors: ["Name is required.", "Address is required."]
|
|
65
|
-
* // }
|
|
66
|
-
*
|
|
67
|
-
* @module
|
|
68
|
-
*/
|
|
69
|
-
/**
|
|
70
|
-
* An error that is thrown when validation of a schema fails.
|
|
71
|
-
*
|
|
72
|
-
* @see {@link ifInvalidThrow} - for throwing errors on invalid input.
|
|
73
|
-
*/
|
|
74
|
-
export declare class MapperValidationError extends Error {
|
|
75
|
-
readonly errors: string[];
|
|
76
|
-
constructor(errors: string[]);
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* A type that captures all errors on invalid input.
|
|
80
|
-
*/
|
|
81
|
-
export interface Invalid {
|
|
82
|
-
/**
|
|
83
|
-
* A list of errors in the input.
|
|
84
|
-
*/
|
|
85
|
-
errors: string[];
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* A type that represents either a valid or invalid input.
|
|
89
|
-
*/
|
|
90
|
-
export
|
|
91
|
-
/**
|
|
92
|
-
* A function that transforms an input into another type, or an invalid result
|
|
93
|
-
* if the input violates the schema.
|
|
94
|
-
*/
|
|
95
|
-
export
|
|
96
|
-
/**
|
|
97
|
-
* A function that transforms an input into another type, or throws if the input
|
|
98
|
-
* is invalid.
|
|
99
|
-
*/
|
|
100
|
-
export
|
|
101
|
-
/**
|
|
102
|
-
* Returns a mapper that asserts the input is not null or not undefined.
|
|
103
|
-
*
|
|
104
|
-
* @param name A name to report when invalid.
|
|
105
|
-
*/
|
|
106
|
-
export declare function required<T>(name: string): Func<T | null | undefined, NonNullable<T>>;
|
|
107
|
-
/**
|
|
108
|
-
* Returns a mapper that asserts a property on the input is not null or not
|
|
109
|
-
* defined.
|
|
110
|
-
*
|
|
111
|
-
* @param prop The prop to assert.
|
|
112
|
-
* @returns A mapper that returns the property's value.
|
|
113
|
-
*/
|
|
114
|
-
export declare function requiredProp<T, P extends keyof T>(prop: P): Func<T, NonNullable<T[P]>>;
|
|
115
|
-
/**
|
|
116
|
-
* Returns a mapper that invokes a function if the input is not null or not
|
|
117
|
-
* undefined.
|
|
118
|
-
*
|
|
119
|
-
* @param mapper A mapping function.
|
|
120
|
-
*/
|
|
121
|
-
export declare function ifDefined<T, R>(mapper: Func<T, R | null | undefined>): Func<T | null | undefined, R | null | undefined>;
|
|
122
|
-
/**
|
|
123
|
-
* Returns a mapper that extracts a property's value.
|
|
124
|
-
*
|
|
125
|
-
* @param prop The property to extract.
|
|
126
|
-
*/
|
|
127
|
-
export declare function getProp<T, P extends keyof T>(prop: P): Func<T, T[P]>;
|
|
128
|
-
/**
|
|
129
|
-
* Returns a mapper that will invoke a mapping function on an input's property.
|
|
130
|
-
*
|
|
131
|
-
* @param prop The name of the property to map over.
|
|
132
|
-
* @param mapper A function that will be invoked with the property's value.
|
|
133
|
-
*/
|
|
134
|
-
export declare function mapProp<T, P extends keyof T, R>(prop: P, mapper: Func<T[P], R>): Func<T, R>;
|
|
135
|
-
/**
|
|
136
|
-
* Returns a mapper that will check if the given property is defined, and if so
|
|
137
|
-
* invoke the given mapping function.
|
|
138
|
-
*
|
|
139
|
-
* @param prop The name of the property to map over.
|
|
140
|
-
* @param mapper A function that will be invoked with the property's value if
|
|
141
|
-
* the property is defined.
|
|
142
|
-
*/
|
|
143
|
-
export declare function mapRequiredProp<T, P extends keyof T, R>(prop: P, mapper: Func<NonNullable<T[P]>, R>): Func<T, R>;
|
|
144
|
-
/**
|
|
145
|
-
* Returns a mapper that will invoke a mapper over each value in the input
|
|
146
|
-
* array. Returns `Invalid` containing errors for all invalid values in the
|
|
147
|
-
* array.
|
|
148
|
-
*
|
|
149
|
-
* @param mapper A function that will be invoked with each array value.
|
|
150
|
-
* @returns
|
|
151
|
-
*/
|
|
152
|
-
export declare function mapArray<T, R>(mapper: Func<T, R>): Func<T[], R[]>;
|
|
153
|
-
/**
|
|
154
|
-
* A type guard that checks if the object is an `Invalid` type.
|
|
155
|
-
*/
|
|
156
|
-
export declare function isInvalid(obj: unknown): obj is Invalid;
|
|
157
|
-
/**
|
|
158
|
-
* Returns a function that throws an error if the input is invalid. Otherwise,
|
|
159
|
-
* returns the result.
|
|
160
|
-
*
|
|
161
|
-
* @param mapper A mapper that will be invoked with the input.
|
|
162
|
-
* @throws {@link MapperValidationError} If the input is invalid.
|
|
163
|
-
*/
|
|
164
|
-
export declare function ifInvalidThrow<T, R>(mapper: Func<T, R>): ThrowIfInvalidFunc<T, R>;
|
|
165
|
-
/**
|
|
166
|
-
* Accumulates the results of mappers into an array.
|
|
167
|
-
*
|
|
168
|
-
* @param mappers A sequence of mappers that will be invoked for the input.
|
|
169
|
-
* @see {@link defineMapper} - This function is normally used with
|
|
170
|
-
* `defineMapper`.
|
|
171
|
-
*/
|
|
172
|
-
export declare function read<T, R1>(a: Func<T, R1>): Func<T, [R1]>;
|
|
173
|
-
export declare function read<T, R1, R2>(a: Func<T, R1>, b: Func<T, R2>): Func<T, [R1, R2]>;
|
|
174
|
-
export declare function read<T, R1, R2, R3>(a: Func<T, R1>, b: Func<T, R2>, c: Func<T, R3>): Func<T, [R1, R2, R3]>;
|
|
175
|
-
export declare function read<T, R1, R2, R3, R4>(a: Func<T, R1>, b: Func<T, R2>, c: Func<T, R3>, d: Func<T, R4>): Func<T, [R1, R2, R3, R4]>;
|
|
176
|
-
export declare function read<T, R1, R2, R3, R4, R5>(a: Func<T, R1>, b: Func<T, R2>, c: Func<T, R3>, d: Func<T, R4>, e: Func<T, R5>): Func<T, [R1, R2, R3, R4, R5]>;
|
|
177
|
-
export declare function read<T, R1, R2, R3, R4, R5, R6>(a: Func<T, R1>, b: Func<T, R2>, c: Func<T, R3>, d: Func<T, R4>, e: Func<T, R5>, f: Func<T, R6>): Func<T, [R1, R2, R3, R4, R5, R6]>;
|
|
178
|
-
export declare function read<T, R1, R2, R3, R4, R5, R6, R7>(a: Func<T, R1>, b: Func<T, R2>, c: Func<T, R3>, d: Func<T, R4>, e: Func<T, R5>, f: Func<T, R6>, g: Func<T, R7>): Func<T, [R1, R2, R3, R4, R5, R6, R7]>;
|
|
179
|
-
export declare function read<T, R1, R2, R3, R4, R5, R6, R7, R8>(a: Func<T, R1>, b: Func<T, R2>, c: Func<T, R3>, d: Func<T, R4>, e: Func<T, R5>, f: Func<T, R6>, g: Func<T, R7>, h: Func<T, R8>): Func<T, [R1, R2, R3, R4, R5, R6, R7, R8]>;
|
|
180
|
-
export declare function read<T, R1, R2, R3, R4, R5, R6, R7, R8, R9>(a: Func<T, R1>, b: Func<T, R2>, c: Func<T, R3>, d: Func<T, R4>, e: Func<T, R5>, f: Func<T, R6>, g: Func<T, R7>, h: Func<T, R8>, i: Func<T, R9>): Func<T, [R1, R2, R3, R4, R5, R6, R7, R8, R9]>;
|
|
181
|
-
export declare function read<T, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10>(a: Func<T, R1>, b: Func<T, R2>, c: Func<T, R3>, d: Func<T, R4>, e: Func<T, R5>, f: Func<T, R6>, g: Func<T, R7>, h: Func<T, R8>, i: Func<T, R9>, j: Func<T, R10>): Func<T, [R1, R2, R3, R4, R5, R6, R7, R8, R9, R10]>;
|
|
182
|
-
export declare function read<T, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11>(a: Func<T, R1>, b: Func<T, R2>, c: Func<T, R3>, d: Func<T, R4>, e: Func<T, R5>, f: Func<T, R6>, g: Func<T, R7>, h: Func<T, R8>, i: Func<T, R9>, j: Func<T, R10>, k: Func<T, R11>): Func<T, [R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11]>;
|
|
183
|
-
/**
|
|
184
|
-
* Defines a mapper that reads the values from an input and invokes a builder to
|
|
185
|
-
* transform data from one schema to another.
|
|
186
|
-
*
|
|
187
|
-
* @example
|
|
188
|
-
*
|
|
189
|
-
* ```ts
|
|
190
|
-
* import { Mapper as M } from '@vertexvis/utils';
|
|
191
|
-
*
|
|
192
|
-
* interface Address {
|
|
193
|
-
* address: string;
|
|
194
|
-
* city: string;
|
|
195
|
-
* state: string;
|
|
196
|
-
* zip: string;
|
|
197
|
-
* }
|
|
198
|
-
*
|
|
199
|
-
* interface Person {
|
|
200
|
-
* name: string;
|
|
201
|
-
* addresses: Address[];
|
|
202
|
-
* }
|
|
203
|
-
*
|
|
204
|
-
* type AddressJson = Partial<Address>;
|
|
205
|
-
* type PersonJson = {
|
|
206
|
-
* name?: string;
|
|
207
|
-
* addresses?: AddressJson[];
|
|
208
|
-
* }
|
|
209
|
-
*
|
|
210
|
-
* const mapAddress: M.Func<AddressJson, Address> = M.defineMapper(
|
|
211
|
-
* M.read(
|
|
212
|
-
* M.requireProp('address'),
|
|
213
|
-
* M.requireProp('city'),
|
|
214
|
-
* M.requireProp('state'),
|
|
215
|
-
* M.requireProp('zip')
|
|
216
|
-
* ),
|
|
217
|
-
* ([address, city, state, zip]) => ({
|
|
218
|
-
* address, city, state, zip
|
|
219
|
-
* })
|
|
220
|
-
* );
|
|
221
|
-
*
|
|
222
|
-
* const mapPerson: M.Func<PersonJson, Person> = M.defineMapper(
|
|
223
|
-
* M.read(
|
|
224
|
-
* M.requireProp('name'),
|
|
225
|
-
* M.mapProp(
|
|
226
|
-
* 'addresses',
|
|
227
|
-
* M.compose(M.required('addresses'), M.mapArray(mapAddress))
|
|
228
|
-
* )
|
|
229
|
-
* ),
|
|
230
|
-
* ([name, addresses]) => ({ name, addresses })
|
|
231
|
-
* )
|
|
232
|
-
*
|
|
233
|
-
* const person = mapPerson({
|
|
234
|
-
* name: 'John',
|
|
235
|
-
* addresses: [{ address: '123', city: 'Ames', state: 'IA', zip: '50010' }]
|
|
236
|
-
* })
|
|
237
|
-
* ```
|
|
238
|
-
*
|
|
239
|
-
* @param reader The mapper that reads values from the input an creates an
|
|
240
|
-
* intermediate format that will be passed to the `builder`.
|
|
241
|
-
* @param builder A mapper that takes the output of `reader` and constructs the
|
|
242
|
-
* output format.
|
|
243
|
-
* @see {@link read} - a helper function to read and validate input values.
|
|
244
|
-
*/
|
|
245
|
-
export declare function defineMapper<T, V, R>(reader: Func<T, V>, builder: Func<V, R>): Func<T, R>;
|
|
246
|
-
/**
|
|
247
|
-
* Returns a mapper that passes the output of each mapper to the next mapper.
|
|
248
|
-
*/
|
|
249
|
-
export declare function compose<T, A, R>(a: Func<T, A>, b: Func<A, R>): Func<T, R>;
|
|
250
|
-
export declare function compose<T, A, B, R>(a: Func<T, A>, b: Func<A, B>, c: Func<B, R>): Func<T, R>;
|
|
251
|
-
export declare function compose<T, A, B, C, R>(a: Func<T, A>, b: Func<A, B>, c: Func<B, C>, d: Func<C, R>): Func<T, R>;
|
|
252
|
-
export declare function compose<T, A, B, C, D, R>(a: Func<T, A>, b: Func<A, B>, c: Func<B, C>, d: Func<C, D>, e: Func<D, R>): Func<T, R>;
|
|
253
|
-
export declare function compose<T, A, B, C, D, E, R>(a: Func<T, A>, b: Func<A, B>, c: Func<B, C>, d: Func<C, D>, e: Func<D, E>, f: Func<E, R>): Func<T, R>;
|
|
254
|
-
export declare function compose<T, A, B, C, D, E, F, R>(a: Func<T, A>, b: Func<A, B>, c: Func<B, C>, d: Func<C, D>, e: Func<D, E>, f: Func<E, F>, g: Func<F, R>): Func<T, R>;
|
|
255
|
-
export declare function compose<T, A, B, C, D, E, F, G, R>(a: Func<T, A>, b: Func<A, B>, c: Func<B, C>, d: Func<C, D>, e: Func<D, E>, f: Func<E, F>, g: Func<F, G>, h: Func<G, R>): Func<T, R>;
|
|
256
|
-
export declare function compose<T, A, B, C, D, E, F, G, H, R>(a: Func<T, A>, b: Func<A, B>, c: Func<B, C>, d: Func<C, D>, e: Func<D, E>, f: Func<E, F>, g: Func<F, G>, h: Func<G, H>, i: Func<H, R>): Func<T, R>;
|
|
257
|
-
export declare function compose<T, A, B, C, D, E, F, G, H, I, R>(a: Func<T, A>, b: Func<A, B>, c: Func<B, C>, d: Func<C, D>, e: Func<D, E>, f: Func<E, F>, g: Func<F, G>, h: Func<G, H>, i: Func<H, I>, j: Func<I, R>): Func<T, R>;
|
|
258
|
-
/**
|
|
259
|
-
* Returns a mapper that returns the first defined result of a mapper. If all
|
|
260
|
-
* mappers return `undefined`, then `undefined` is returned.
|
|
261
|
-
*/
|
|
262
|
-
export declare function pickFirst<T, A, B>(a: Func<T, A | undefined>, b: Func<T, B | undefined>): Func<T, A | B | undefined>;
|
|
263
|
-
export declare function pickFirst<T, A, B, C>(a: Func<T, A | undefined>, b: Func<T, B | undefined>, c: Func<T, C | undefined>): Func<T, A | B | C | undefined>;
|
|
264
|
-
export declare function pickFirst<T, A, B, C, D>(a: Func<T, A | undefined>, b: Func<T, B | undefined>, c: Func<T, C | undefined>, d: Func<T, D | undefined>): Func<T, A | B | C | undefined>;
|
|
265
|
-
export declare function pickFirst<T, A, B, C, D, E>(a: Func<T, A | undefined>, b: Func<T, B | undefined>, c: Func<T, C | undefined>, d: Func<T, D | undefined>, e: Func<T, E | undefined>): Func<T, A | B | C | D | E | undefined>;
|
|
266
|
-
export declare function pickFirst<T, A, B, C, D, E, F>(a: Func<T, A | undefined>, b: Func<T, B | undefined>, c: Func<T, C | undefined>, d: Func<T, D | undefined>, e: Func<T, E | undefined>, f: Func<T, F | undefined>): Func<T, A | B | C | D | E | F | undefined>;
|
|
267
|
-
export declare function pickFirst<T, A, B, C, D, E, F, G>(a: Func<T, A | undefined>, b: Func<T, B | undefined>, c: Func<T, C | undefined>, d: Func<T, D | undefined>, e: Func<T, E | undefined>, f: Func<T, F | undefined>, g: Func<T, G | undefined>): Func<T, A | B | C | D | E | F | G | undefined>;
|
|
268
|
-
export declare function pickFirst<T, A, B, C, D, E, F, G, H>(a: Func<T, A | undefined>, b: Func<T, B | undefined>, c: Func<T, C | undefined>, d: Func<T, D | undefined>, e: Func<T, E | undefined>, f: Func<T, F | undefined>, g: Func<T, G | undefined>, h: Func<T, H | undefined>): Func<T, A | B | C | D | E | F | G | H | undefined>;
|
|
269
|
-
export declare function pickFirst<T, A, B, C, D, E, F, G, H, I>(a: Func<T, A | undefined>, b: Func<T, B | undefined>, c: Func<T, C | undefined>, d: Func<T, D | undefined>, e: Func<T, E | undefined>, f: Func<T, F | undefined>, g: Func<T, G | undefined>, h: Func<T, H | undefined>, i: Func<T, I | undefined>): Func<T, A | B | C | D | E | F | G | H | I | undefined>;
|
|
270
|
-
export declare function pickFirst<T, A, B, C, D, E, F, G, H, I, J>(a: Func<T, A | undefined>, b: Func<T, B | undefined>, c: Func<T, C | undefined>, d: Func<T, D | undefined>, e: Func<T, E | undefined>, f: Func<T, F | undefined>, g: Func<T, G | undefined>, h: Func<T, H | undefined>, i: Func<T, I | undefined>, j: Func<T, J | undefined>): Func<T, A | B | C | D | E | F | G | H | I | J | undefined>;
|
|
1
|
+
/**
|
|
2
|
+
* A module for defining functional schemas to map between different types. This
|
|
3
|
+
* module is useful for parsing to or from JSON/protobufs to domain types.
|
|
4
|
+
*
|
|
5
|
+
* Mappers support greedy validation, so all validation errors are aggregated
|
|
6
|
+
* and reported vs failing on the first invalid input.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { Mapper as M } from '@vertexvis/utils';
|
|
12
|
+
*
|
|
13
|
+
* interface Address {
|
|
14
|
+
* address: string;
|
|
15
|
+
* city: string;
|
|
16
|
+
* state: string;
|
|
17
|
+
* zip: string;
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* interface Person {
|
|
21
|
+
* name: string;
|
|
22
|
+
* addresses: Address[];
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* type AddressJson = Partial<Address>;
|
|
26
|
+
* type PersonJson = {
|
|
27
|
+
* name?: string;
|
|
28
|
+
* addresses?: AddressJson[];
|
|
29
|
+
* }
|
|
30
|
+
*
|
|
31
|
+
* const mapAddress: M.Func<AddressJson, Address> = M.defineMapper(
|
|
32
|
+
* M.read(
|
|
33
|
+
* M.requireProp('address'),
|
|
34
|
+
* M.requireProp('city'),
|
|
35
|
+
* M.requireProp('state'),
|
|
36
|
+
* M.requireProp('zip')
|
|
37
|
+
* ),
|
|
38
|
+
* ([address, city, state, zip]) => ({
|
|
39
|
+
* address, city, state, zip
|
|
40
|
+
* })
|
|
41
|
+
* );
|
|
42
|
+
*
|
|
43
|
+
* const mapPerson: M.Func<PersonJson, Person> = M.defineMapper(
|
|
44
|
+
* M.read(
|
|
45
|
+
* M.requireProp('name'),
|
|
46
|
+
* M.mapProp(
|
|
47
|
+
* 'addresses',
|
|
48
|
+
* M.compose(M.required('addresses'), M.mapArray(mapAddress))
|
|
49
|
+
* )
|
|
50
|
+
* ),
|
|
51
|
+
* ([name, addresses]) => ({ name, addresses })
|
|
52
|
+
* );
|
|
53
|
+
*
|
|
54
|
+
* const person = mapPerson({
|
|
55
|
+
* name: 'John',
|
|
56
|
+
* addresses: [{ address: '123', city: 'Ames', state: 'IA', zip: '50010' }]
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* const invalidPerson = mapPerson({
|
|
60
|
+
* addresses: [{ city: 'Ames', state: 'IA', zip: '50010' }]
|
|
61
|
+
* });
|
|
62
|
+
* ```
|
|
63
|
+
* // {
|
|
64
|
+
* // errors: ["Name is required.", "Address is required."]
|
|
65
|
+
* // }
|
|
66
|
+
*
|
|
67
|
+
* @module
|
|
68
|
+
*/
|
|
69
|
+
/**
|
|
70
|
+
* An error that is thrown when validation of a schema fails.
|
|
71
|
+
*
|
|
72
|
+
* @see {@link ifInvalidThrow} - for throwing errors on invalid input.
|
|
73
|
+
*/
|
|
74
|
+
export declare class MapperValidationError extends Error {
|
|
75
|
+
readonly errors: string[];
|
|
76
|
+
constructor(errors: string[]);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* A type that captures all errors on invalid input.
|
|
80
|
+
*/
|
|
81
|
+
export interface Invalid {
|
|
82
|
+
/**
|
|
83
|
+
* A list of errors in the input.
|
|
84
|
+
*/
|
|
85
|
+
errors: string[];
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* A type that represents either a valid or invalid input.
|
|
89
|
+
*/
|
|
90
|
+
export type Validated<T> = Invalid | T;
|
|
91
|
+
/**
|
|
92
|
+
* A function that transforms an input into another type, or an invalid result
|
|
93
|
+
* if the input violates the schema.
|
|
94
|
+
*/
|
|
95
|
+
export type Func<T, R> = (input: T) => Validated<R>;
|
|
96
|
+
/**
|
|
97
|
+
* A function that transforms an input into another type, or throws if the input
|
|
98
|
+
* is invalid.
|
|
99
|
+
*/
|
|
100
|
+
export type ThrowIfInvalidFunc<T, R> = (input: T) => R;
|
|
101
|
+
/**
|
|
102
|
+
* Returns a mapper that asserts the input is not null or not undefined.
|
|
103
|
+
*
|
|
104
|
+
* @param name A name to report when invalid.
|
|
105
|
+
*/
|
|
106
|
+
export declare function required<T>(name: string): Func<T | null | undefined, NonNullable<T>>;
|
|
107
|
+
/**
|
|
108
|
+
* Returns a mapper that asserts a property on the input is not null or not
|
|
109
|
+
* defined.
|
|
110
|
+
*
|
|
111
|
+
* @param prop The prop to assert.
|
|
112
|
+
* @returns A mapper that returns the property's value.
|
|
113
|
+
*/
|
|
114
|
+
export declare function requiredProp<T, P extends keyof T>(prop: P): Func<T, NonNullable<T[P]>>;
|
|
115
|
+
/**
|
|
116
|
+
* Returns a mapper that invokes a function if the input is not null or not
|
|
117
|
+
* undefined.
|
|
118
|
+
*
|
|
119
|
+
* @param mapper A mapping function.
|
|
120
|
+
*/
|
|
121
|
+
export declare function ifDefined<T, R>(mapper: Func<T, R | null | undefined>): Func<T | null | undefined, R | null | undefined>;
|
|
122
|
+
/**
|
|
123
|
+
* Returns a mapper that extracts a property's value.
|
|
124
|
+
*
|
|
125
|
+
* @param prop The property to extract.
|
|
126
|
+
*/
|
|
127
|
+
export declare function getProp<T, P extends keyof T>(prop: P): Func<T, T[P]>;
|
|
128
|
+
/**
|
|
129
|
+
* Returns a mapper that will invoke a mapping function on an input's property.
|
|
130
|
+
*
|
|
131
|
+
* @param prop The name of the property to map over.
|
|
132
|
+
* @param mapper A function that will be invoked with the property's value.
|
|
133
|
+
*/
|
|
134
|
+
export declare function mapProp<T, P extends keyof T, R>(prop: P, mapper: Func<T[P], R>): Func<T, R>;
|
|
135
|
+
/**
|
|
136
|
+
* Returns a mapper that will check if the given property is defined, and if so
|
|
137
|
+
* invoke the given mapping function.
|
|
138
|
+
*
|
|
139
|
+
* @param prop The name of the property to map over.
|
|
140
|
+
* @param mapper A function that will be invoked with the property's value if
|
|
141
|
+
* the property is defined.
|
|
142
|
+
*/
|
|
143
|
+
export declare function mapRequiredProp<T, P extends keyof T, R>(prop: P, mapper: Func<NonNullable<T[P]>, R>): Func<T, R>;
|
|
144
|
+
/**
|
|
145
|
+
* Returns a mapper that will invoke a mapper over each value in the input
|
|
146
|
+
* array. Returns `Invalid` containing errors for all invalid values in the
|
|
147
|
+
* array.
|
|
148
|
+
*
|
|
149
|
+
* @param mapper A function that will be invoked with each array value.
|
|
150
|
+
* @returns
|
|
151
|
+
*/
|
|
152
|
+
export declare function mapArray<T, R>(mapper: Func<T, R>): Func<T[], R[]>;
|
|
153
|
+
/**
|
|
154
|
+
* A type guard that checks if the object is an `Invalid` type.
|
|
155
|
+
*/
|
|
156
|
+
export declare function isInvalid(obj: unknown): obj is Invalid;
|
|
157
|
+
/**
|
|
158
|
+
* Returns a function that throws an error if the input is invalid. Otherwise,
|
|
159
|
+
* returns the result.
|
|
160
|
+
*
|
|
161
|
+
* @param mapper A mapper that will be invoked with the input.
|
|
162
|
+
* @throws {@link MapperValidationError} If the input is invalid.
|
|
163
|
+
*/
|
|
164
|
+
export declare function ifInvalidThrow<T, R>(mapper: Func<T, R>): ThrowIfInvalidFunc<T, R>;
|
|
165
|
+
/**
|
|
166
|
+
* Accumulates the results of mappers into an array.
|
|
167
|
+
*
|
|
168
|
+
* @param mappers A sequence of mappers that will be invoked for the input.
|
|
169
|
+
* @see {@link defineMapper} - This function is normally used with
|
|
170
|
+
* `defineMapper`.
|
|
171
|
+
*/
|
|
172
|
+
export declare function read<T, R1>(a: Func<T, R1>): Func<T, [R1]>;
|
|
173
|
+
export declare function read<T, R1, R2>(a: Func<T, R1>, b: Func<T, R2>): Func<T, [R1, R2]>;
|
|
174
|
+
export declare function read<T, R1, R2, R3>(a: Func<T, R1>, b: Func<T, R2>, c: Func<T, R3>): Func<T, [R1, R2, R3]>;
|
|
175
|
+
export declare function read<T, R1, R2, R3, R4>(a: Func<T, R1>, b: Func<T, R2>, c: Func<T, R3>, d: Func<T, R4>): Func<T, [R1, R2, R3, R4]>;
|
|
176
|
+
export declare function read<T, R1, R2, R3, R4, R5>(a: Func<T, R1>, b: Func<T, R2>, c: Func<T, R3>, d: Func<T, R4>, e: Func<T, R5>): Func<T, [R1, R2, R3, R4, R5]>;
|
|
177
|
+
export declare function read<T, R1, R2, R3, R4, R5, R6>(a: Func<T, R1>, b: Func<T, R2>, c: Func<T, R3>, d: Func<T, R4>, e: Func<T, R5>, f: Func<T, R6>): Func<T, [R1, R2, R3, R4, R5, R6]>;
|
|
178
|
+
export declare function read<T, R1, R2, R3, R4, R5, R6, R7>(a: Func<T, R1>, b: Func<T, R2>, c: Func<T, R3>, d: Func<T, R4>, e: Func<T, R5>, f: Func<T, R6>, g: Func<T, R7>): Func<T, [R1, R2, R3, R4, R5, R6, R7]>;
|
|
179
|
+
export declare function read<T, R1, R2, R3, R4, R5, R6, R7, R8>(a: Func<T, R1>, b: Func<T, R2>, c: Func<T, R3>, d: Func<T, R4>, e: Func<T, R5>, f: Func<T, R6>, g: Func<T, R7>, h: Func<T, R8>): Func<T, [R1, R2, R3, R4, R5, R6, R7, R8]>;
|
|
180
|
+
export declare function read<T, R1, R2, R3, R4, R5, R6, R7, R8, R9>(a: Func<T, R1>, b: Func<T, R2>, c: Func<T, R3>, d: Func<T, R4>, e: Func<T, R5>, f: Func<T, R6>, g: Func<T, R7>, h: Func<T, R8>, i: Func<T, R9>): Func<T, [R1, R2, R3, R4, R5, R6, R7, R8, R9]>;
|
|
181
|
+
export declare function read<T, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10>(a: Func<T, R1>, b: Func<T, R2>, c: Func<T, R3>, d: Func<T, R4>, e: Func<T, R5>, f: Func<T, R6>, g: Func<T, R7>, h: Func<T, R8>, i: Func<T, R9>, j: Func<T, R10>): Func<T, [R1, R2, R3, R4, R5, R6, R7, R8, R9, R10]>;
|
|
182
|
+
export declare function read<T, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11>(a: Func<T, R1>, b: Func<T, R2>, c: Func<T, R3>, d: Func<T, R4>, e: Func<T, R5>, f: Func<T, R6>, g: Func<T, R7>, h: Func<T, R8>, i: Func<T, R9>, j: Func<T, R10>, k: Func<T, R11>): Func<T, [R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11]>;
|
|
183
|
+
/**
|
|
184
|
+
* Defines a mapper that reads the values from an input and invokes a builder to
|
|
185
|
+
* transform data from one schema to another.
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
*
|
|
189
|
+
* ```ts
|
|
190
|
+
* import { Mapper as M } from '@vertexvis/utils';
|
|
191
|
+
*
|
|
192
|
+
* interface Address {
|
|
193
|
+
* address: string;
|
|
194
|
+
* city: string;
|
|
195
|
+
* state: string;
|
|
196
|
+
* zip: string;
|
|
197
|
+
* }
|
|
198
|
+
*
|
|
199
|
+
* interface Person {
|
|
200
|
+
* name: string;
|
|
201
|
+
* addresses: Address[];
|
|
202
|
+
* }
|
|
203
|
+
*
|
|
204
|
+
* type AddressJson = Partial<Address>;
|
|
205
|
+
* type PersonJson = {
|
|
206
|
+
* name?: string;
|
|
207
|
+
* addresses?: AddressJson[];
|
|
208
|
+
* }
|
|
209
|
+
*
|
|
210
|
+
* const mapAddress: M.Func<AddressJson, Address> = M.defineMapper(
|
|
211
|
+
* M.read(
|
|
212
|
+
* M.requireProp('address'),
|
|
213
|
+
* M.requireProp('city'),
|
|
214
|
+
* M.requireProp('state'),
|
|
215
|
+
* M.requireProp('zip')
|
|
216
|
+
* ),
|
|
217
|
+
* ([address, city, state, zip]) => ({
|
|
218
|
+
* address, city, state, zip
|
|
219
|
+
* })
|
|
220
|
+
* );
|
|
221
|
+
*
|
|
222
|
+
* const mapPerson: M.Func<PersonJson, Person> = M.defineMapper(
|
|
223
|
+
* M.read(
|
|
224
|
+
* M.requireProp('name'),
|
|
225
|
+
* M.mapProp(
|
|
226
|
+
* 'addresses',
|
|
227
|
+
* M.compose(M.required('addresses'), M.mapArray(mapAddress))
|
|
228
|
+
* )
|
|
229
|
+
* ),
|
|
230
|
+
* ([name, addresses]) => ({ name, addresses })
|
|
231
|
+
* )
|
|
232
|
+
*
|
|
233
|
+
* const person = mapPerson({
|
|
234
|
+
* name: 'John',
|
|
235
|
+
* addresses: [{ address: '123', city: 'Ames', state: 'IA', zip: '50010' }]
|
|
236
|
+
* })
|
|
237
|
+
* ```
|
|
238
|
+
*
|
|
239
|
+
* @param reader The mapper that reads values from the input an creates an
|
|
240
|
+
* intermediate format that will be passed to the `builder`.
|
|
241
|
+
* @param builder A mapper that takes the output of `reader` and constructs the
|
|
242
|
+
* output format.
|
|
243
|
+
* @see {@link read} - a helper function to read and validate input values.
|
|
244
|
+
*/
|
|
245
|
+
export declare function defineMapper<T, V, R>(reader: Func<T, V>, builder: Func<V, R>): Func<T, R>;
|
|
246
|
+
/**
|
|
247
|
+
* Returns a mapper that passes the output of each mapper to the next mapper.
|
|
248
|
+
*/
|
|
249
|
+
export declare function compose<T, A, R>(a: Func<T, A>, b: Func<A, R>): Func<T, R>;
|
|
250
|
+
export declare function compose<T, A, B, R>(a: Func<T, A>, b: Func<A, B>, c: Func<B, R>): Func<T, R>;
|
|
251
|
+
export declare function compose<T, A, B, C, R>(a: Func<T, A>, b: Func<A, B>, c: Func<B, C>, d: Func<C, R>): Func<T, R>;
|
|
252
|
+
export declare function compose<T, A, B, C, D, R>(a: Func<T, A>, b: Func<A, B>, c: Func<B, C>, d: Func<C, D>, e: Func<D, R>): Func<T, R>;
|
|
253
|
+
export declare function compose<T, A, B, C, D, E, R>(a: Func<T, A>, b: Func<A, B>, c: Func<B, C>, d: Func<C, D>, e: Func<D, E>, f: Func<E, R>): Func<T, R>;
|
|
254
|
+
export declare function compose<T, A, B, C, D, E, F, R>(a: Func<T, A>, b: Func<A, B>, c: Func<B, C>, d: Func<C, D>, e: Func<D, E>, f: Func<E, F>, g: Func<F, R>): Func<T, R>;
|
|
255
|
+
export declare function compose<T, A, B, C, D, E, F, G, R>(a: Func<T, A>, b: Func<A, B>, c: Func<B, C>, d: Func<C, D>, e: Func<D, E>, f: Func<E, F>, g: Func<F, G>, h: Func<G, R>): Func<T, R>;
|
|
256
|
+
export declare function compose<T, A, B, C, D, E, F, G, H, R>(a: Func<T, A>, b: Func<A, B>, c: Func<B, C>, d: Func<C, D>, e: Func<D, E>, f: Func<E, F>, g: Func<F, G>, h: Func<G, H>, i: Func<H, R>): Func<T, R>;
|
|
257
|
+
export declare function compose<T, A, B, C, D, E, F, G, H, I, R>(a: Func<T, A>, b: Func<A, B>, c: Func<B, C>, d: Func<C, D>, e: Func<D, E>, f: Func<E, F>, g: Func<F, G>, h: Func<G, H>, i: Func<H, I>, j: Func<I, R>): Func<T, R>;
|
|
258
|
+
/**
|
|
259
|
+
* Returns a mapper that returns the first defined result of a mapper. If all
|
|
260
|
+
* mappers return `undefined`, then `undefined` is returned.
|
|
261
|
+
*/
|
|
262
|
+
export declare function pickFirst<T, A, B>(a: Func<T, A | undefined>, b: Func<T, B | undefined>): Func<T, A | B | undefined>;
|
|
263
|
+
export declare function pickFirst<T, A, B, C>(a: Func<T, A | undefined>, b: Func<T, B | undefined>, c: Func<T, C | undefined>): Func<T, A | B | C | undefined>;
|
|
264
|
+
export declare function pickFirst<T, A, B, C, D>(a: Func<T, A | undefined>, b: Func<T, B | undefined>, c: Func<T, C | undefined>, d: Func<T, D | undefined>): Func<T, A | B | C | undefined>;
|
|
265
|
+
export declare function pickFirst<T, A, B, C, D, E>(a: Func<T, A | undefined>, b: Func<T, B | undefined>, c: Func<T, C | undefined>, d: Func<T, D | undefined>, e: Func<T, E | undefined>): Func<T, A | B | C | D | E | undefined>;
|
|
266
|
+
export declare function pickFirst<T, A, B, C, D, E, F>(a: Func<T, A | undefined>, b: Func<T, B | undefined>, c: Func<T, C | undefined>, d: Func<T, D | undefined>, e: Func<T, E | undefined>, f: Func<T, F | undefined>): Func<T, A | B | C | D | E | F | undefined>;
|
|
267
|
+
export declare function pickFirst<T, A, B, C, D, E, F, G>(a: Func<T, A | undefined>, b: Func<T, B | undefined>, c: Func<T, C | undefined>, d: Func<T, D | undefined>, e: Func<T, E | undefined>, f: Func<T, F | undefined>, g: Func<T, G | undefined>): Func<T, A | B | C | D | E | F | G | undefined>;
|
|
268
|
+
export declare function pickFirst<T, A, B, C, D, E, F, G, H>(a: Func<T, A | undefined>, b: Func<T, B | undefined>, c: Func<T, C | undefined>, d: Func<T, D | undefined>, e: Func<T, E | undefined>, f: Func<T, F | undefined>, g: Func<T, G | undefined>, h: Func<T, H | undefined>): Func<T, A | B | C | D | E | F | G | H | undefined>;
|
|
269
|
+
export declare function pickFirst<T, A, B, C, D, E, F, G, H, I>(a: Func<T, A | undefined>, b: Func<T, B | undefined>, c: Func<T, C | undefined>, d: Func<T, D | undefined>, e: Func<T, E | undefined>, f: Func<T, F | undefined>, g: Func<T, G | undefined>, h: Func<T, H | undefined>, i: Func<T, I | undefined>): Func<T, A | B | C | D | E | F | G | H | I | undefined>;
|
|
270
|
+
export declare function pickFirst<T, A, B, C, D, E, F, G, H, I, J>(a: Func<T, A | undefined>, b: Func<T, B | undefined>, c: Func<T, C | undefined>, d: Func<T, D | undefined>, e: Func<T, E | undefined>, f: Func<T, F | undefined>, g: Func<T, G | undefined>, h: Func<T, H | undefined>, i: Func<T, I | undefined>, j: Func<T, J | undefined>): Func<T, A | B | C | D | E | F | G | H | I | J | undefined>;
|