@fgv/ts-utils 1.2.0-alpha.2 → 1.3.0
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/LICENSE +0 -0
- package/README.md +4 -0
- package/brand.d.ts +2 -0
- package/brand.d.ts.map +1 -0
- package/brand.js +1 -1
- package/converter.d.ts +185 -73
- package/converter.d.ts.map +1 -0
- package/converter.js +104 -51
- package/converters.d.ts +440 -182
- package/converters.d.ts.map +1 -0
- package/converters.js +358 -209
- package/csvHelpers.d.ts +7 -0
- package/csvHelpers.d.ts.map +1 -0
- package/csvHelpers.js +14 -4
- package/extendedArray.d.ts +46 -0
- package/extendedArray.d.ts.map +1 -0
- package/extendedArray.js +53 -8
- package/formatter.d.ts +53 -0
- package/formatter.d.ts.map +1 -0
- package/formatter.js +27 -4
- package/hash.d.ts +51 -0
- package/hash.d.ts.map +1 -0
- package/hash.js +168 -0
- package/index.d.ts +6 -2
- package/index.d.ts.map +1 -0
- package/index.js +15 -5
- package/logger.d.ts +3 -2
- package/logger.d.ts.map +1 -0
- package/logger.js +12 -12
- package/package.json +24 -20
- package/rangeOf.d.ts +96 -0
- package/rangeOf.d.ts.map +1 -0
- package/rangeOf.js +81 -2
- package/result.d.ts +391 -39
- package/result.d.ts.map +1 -0
- package/result.js +257 -49
- package/ts-utils.d.ts +2451 -0
- package/tsdoc-metadata.json +11 -0
- package/utils.d.ts +49 -27
- package/utils.d.ts.map +1 -0
- package/utils.js +53 -36
- package/validation/boolean.d.ts +27 -0
- package/validation/boolean.d.ts.map +1 -0
- package/validation/boolean.js +59 -0
- package/validation/classes.d.ts +5 -1
- package/validation/classes.d.ts.map +1 -0
- package/validation/classes.js +9 -2
- package/validation/field.d.ts +30 -0
- package/validation/field.d.ts.map +1 -0
- package/validation/field.js +23 -5
- package/validation/genericValidator.d.ts +41 -22
- package/validation/genericValidator.d.ts.map +1 -0
- package/validation/genericValidator.js +25 -26
- package/validation/index.d.ts +5 -3
- package/validation/index.d.ts.map +1 -0
- package/validation/index.js +15 -8
- package/validation/number.d.ts +20 -0
- package/validation/number.d.ts.map +1 -0
- package/validation/number.js +17 -2
- package/validation/object.d.ts +76 -11
- package/validation/object.d.ts.map +1 -0
- package/validation/object.js +52 -14
- package/validation/string.d.ts +20 -0
- package/validation/string.d.ts.map +1 -0
- package/validation/string.js +17 -2
- package/validation/traits.d.ts +50 -1
- package/validation/traits.d.ts.map +1 -0
- package/validation/traits.js +15 -2
- package/validation/validator.d.ts +44 -23
- package/validation/validator.d.ts.map +1 -0
- package/validation/validator.js +1 -1
- package/validation/validatorBase.d.ts +16 -1
- package/validation/validatorBase.d.ts.map +1 -0
- package/validation/validatorBase.js +7 -1
- package/validation/validators.d.ts +26 -2
- package/validation/validators.d.ts.map +1 -0
- package/validation/validators.js +26 -8
package/converters.d.ts
CHANGED
|
@@ -4,7 +4,9 @@ import { Result } from './result';
|
|
|
4
4
|
import { ExtendedArray } from './extendedArray';
|
|
5
5
|
declare type OnError = 'failOnError' | 'ignoreErrors';
|
|
6
6
|
/**
|
|
7
|
-
* Options for @
|
|
7
|
+
* Options for {@link Converters.StringConverter | StringConverter}
|
|
8
|
+
* matching method
|
|
9
|
+
* @public
|
|
8
10
|
*/
|
|
9
11
|
export interface StringMatchOptions {
|
|
10
12
|
/**
|
|
@@ -14,255 +16,392 @@ export interface StringMatchOptions {
|
|
|
14
16
|
message?: string;
|
|
15
17
|
}
|
|
16
18
|
/**
|
|
17
|
-
* The @
|
|
18
|
-
*
|
|
19
|
+
* The {@link Converters.StringConverter | StringConverter} class extends {@link BaseConverter}
|
|
20
|
+
* to provide string-specific helper methods.
|
|
21
|
+
* @public
|
|
19
22
|
*/
|
|
20
23
|
export declare class StringConverter<T extends string = string, TC = unknown> extends BaseConverter<T, TC> {
|
|
21
24
|
/**
|
|
22
|
-
* Construct a new @
|
|
23
|
-
* @param defaultContext Optional context used by the conversion
|
|
24
|
-
* @param traits Optional traits to be applied to the conversion
|
|
25
|
-
* @param converter Optional
|
|
25
|
+
* Construct a new {@link Converters.StringConverter | StringConverter}.
|
|
26
|
+
* @param defaultContext - Optional context used by the conversion.
|
|
27
|
+
* @param traits - Optional traits to be applied to the conversion.
|
|
28
|
+
* @param converter - Optional conversion function to be used for the conversion.
|
|
26
29
|
*/
|
|
27
30
|
constructor(defaultContext?: TC, traits?: ConverterTraits, converter?: (from: unknown, self: Converter<T, TC>, context?: TC) => Result<T>);
|
|
31
|
+
/**
|
|
32
|
+
* @internal
|
|
33
|
+
*/
|
|
28
34
|
protected static _convert<T extends string>(from: unknown): Result<T>;
|
|
35
|
+
/**
|
|
36
|
+
* @internal
|
|
37
|
+
*/
|
|
29
38
|
protected static _wrap<T extends string, TC>(wrapped: StringConverter<T, TC>, converter: (from: T) => Result<T>, traits?: ConverterTraits): StringConverter<T, TC>;
|
|
30
39
|
/**
|
|
31
|
-
* Returns a @
|
|
32
|
-
* string.
|
|
33
|
-
* @param match The string to be matched
|
|
34
|
-
* @param options Optional @
|
|
35
|
-
* @returns @
|
|
40
|
+
* Returns a {@link Converters.StringConverter | StringConverter} which constrains the result to match
|
|
41
|
+
* a supplied string.
|
|
42
|
+
* @param match - The string to be matched
|
|
43
|
+
* @param options - Optional {@link Converters.StringMatchOptions} for this conversion.
|
|
44
|
+
* @returns {@link Success} with a matching string or {@link Failure} with an informative
|
|
36
45
|
* error if the string does not match.
|
|
46
|
+
* {@label string}
|
|
37
47
|
*/
|
|
38
48
|
matching(match: string, options?: Partial<StringMatchOptions>): StringConverter<T, TC>;
|
|
39
49
|
/**
|
|
40
|
-
* Returns a @
|
|
41
|
-
* array of strings.
|
|
42
|
-
* @param match The array
|
|
43
|
-
* @param options Optional @
|
|
44
|
-
* @returns @
|
|
50
|
+
* Returns a {@link Converters.StringConverter | StringConverter} which constrains the result to match
|
|
51
|
+
* one of a supplied array of strings.
|
|
52
|
+
* @param match - The array of allowed strings.
|
|
53
|
+
* @param options - Optional {@link Converters.StringMatchOptions} for this conversion.
|
|
54
|
+
* @returns {@link Success} with a matching string or {@link Failure} with an informative
|
|
45
55
|
* error if the string does not match.
|
|
56
|
+
* {@label array}
|
|
46
57
|
*/
|
|
47
58
|
matching(match: string[], options?: Partial<StringMatchOptions>): StringConverter<T, TC>;
|
|
48
59
|
/**
|
|
49
|
-
* Returns a @
|
|
50
|
-
* Set of strings.
|
|
51
|
-
* @param match The Set
|
|
52
|
-
* @param options Optional @
|
|
53
|
-
* @returns @
|
|
60
|
+
* Returns a {@link Converters.StringConverter | StringConverter} which constrains the result to match
|
|
61
|
+
* one of a supplied `Set` of strings.
|
|
62
|
+
* @param match - The `Set` of allowed strings.
|
|
63
|
+
* @param options - Optional {@link Converters.StringMatchOptions} for this conversion.
|
|
64
|
+
* @returns {@link Success} with a matching string or {@link Failure} with an informative
|
|
54
65
|
* error if the string does not match.
|
|
66
|
+
* {@label set}
|
|
55
67
|
*/
|
|
56
68
|
matching(match: Set<T>, options?: Partial<StringMatchOptions>): StringConverter<T, TC>;
|
|
57
69
|
/**
|
|
58
|
-
* Returns a @
|
|
59
|
-
* expression.
|
|
60
|
-
* @param match The
|
|
61
|
-
* @param options Optional @
|
|
62
|
-
* @returns @
|
|
70
|
+
* Returns a {@link Converters.StringConverter | StringConverter} which constrains the result to match
|
|
71
|
+
* a supplied regular expression.
|
|
72
|
+
* @param match - The regular expression to be used as a constraint.
|
|
73
|
+
* @param options - Optional {@link Converters.StringMatchOptions} for this conversion
|
|
74
|
+
* @returns {@link Success} with a matching string or {@link Failure} with an informative
|
|
63
75
|
* error if the string does not match.
|
|
76
|
+
* {@label regexp}
|
|
64
77
|
*/
|
|
65
78
|
matching(match: RegExp, options?: Partial<StringMatchOptions>): StringConverter<T, TC>;
|
|
66
79
|
}
|
|
67
80
|
/**
|
|
68
81
|
* A converter to convert unknown to string. Values of type
|
|
69
82
|
* string succeed. Anything else fails.
|
|
83
|
+
* @public
|
|
70
84
|
*/
|
|
71
85
|
export declare const string: StringConverter<string, unknown>;
|
|
72
86
|
/**
|
|
73
|
-
* Helper function to create a
|
|
74
|
-
* template conversions supplied at construction time or at
|
|
75
|
-
*
|
|
87
|
+
* Helper function to create a {@link Converters.StringConverter | StringConverter} which converts
|
|
88
|
+
* `unknown` to `string`, applying template conversions supplied at construction time or at
|
|
89
|
+
* runtime as context.
|
|
90
|
+
* @remarks
|
|
91
|
+
* Template conversions are applied using `mustache` syntax.
|
|
92
|
+
* @param defaultContext - Optional default context to use for template values.
|
|
93
|
+
* @returns A new {@link Converter} returning `string`.
|
|
94
|
+
* @public
|
|
76
95
|
*/
|
|
77
96
|
export declare function templateString(defaultContext?: unknown): StringConverter<string, unknown>;
|
|
78
97
|
/**
|
|
79
|
-
*
|
|
98
|
+
* Helper function to create a {@link Converter} which converts `unknown` to one of a set of supplied
|
|
99
|
+
* enumerated values. Anything else fails.
|
|
100
|
+
*
|
|
101
|
+
* @remarks
|
|
80
102
|
* Allowed enumerated values can also be supplied as context at conversion time.
|
|
81
|
-
* @param values Array of allowed values
|
|
103
|
+
* @param values - Array of allowed values.
|
|
104
|
+
* @returns A new {@link Converter} returning `<T>`.
|
|
105
|
+
* @public
|
|
82
106
|
*/
|
|
83
107
|
export declare function enumeratedValue<T>(values: T[]): Converter<T, T[]>;
|
|
84
108
|
/**
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
* @
|
|
109
|
+
* Helper function to create a {@link Converter} which converts `unknown` to one of a set of supplied enumerated
|
|
110
|
+
* values, mapping any of multiple supplied values to the enumeration.
|
|
111
|
+
* @remarks
|
|
112
|
+
* Enables mapping of multiple input values to a consistent internal representation (so e.g. `'y'`, `'yes'`,
|
|
113
|
+
* `'true'`, `1` and `true` can all map to boolean `true`)
|
|
114
|
+
* @param map - An array of tuples describing the mapping. The first element of each tuple is the result
|
|
115
|
+
* value, the second is the set of values that map to the result. Tuples are evaluated in the order
|
|
116
|
+
* supplied and are not checked for duplicates.
|
|
117
|
+
* @param message - An optional error message.
|
|
118
|
+
* @returns A {@link Converter} which applies the mapping and yields `<T>` on success.
|
|
119
|
+
* @public
|
|
120
|
+
*/
|
|
121
|
+
export declare function mappedEnumeratedValue<T>(map: [T, unknown[]][], message?: string): Converter<T, undefined>;
|
|
122
|
+
/**
|
|
123
|
+
* Helper function to create a {@link Converter} which converts `unknown` to some supplied literal value. Succeeds with
|
|
124
|
+
* the supplied value if an identity comparison succeeds, fails otherwise.
|
|
125
|
+
* @param value - The value to be compared.
|
|
126
|
+
* @returns A {@link Converter} which returns the supplied value on success.
|
|
127
|
+
* @public
|
|
88
128
|
*/
|
|
89
129
|
export declare function literal<T>(value: T): Converter<T, unknown>;
|
|
90
130
|
/**
|
|
91
131
|
* Deprecated alias for @see literal
|
|
92
|
-
* @param value The value to be compared
|
|
93
|
-
* @deprecated
|
|
132
|
+
* @param value - The value to be compared.
|
|
133
|
+
* @deprecated Use {@link Converters.literal} instead.
|
|
134
|
+
* @internal
|
|
94
135
|
*/
|
|
95
136
|
export declare const value: typeof literal;
|
|
96
137
|
/**
|
|
97
|
-
* A
|
|
98
|
-
*
|
|
138
|
+
* A {@link Converter} which converts `unknown` to a `number`.
|
|
139
|
+
* @remarks
|
|
140
|
+
* Numbers and strings with a numeric format succeed. Anything else fails.
|
|
141
|
+
* @public
|
|
99
142
|
*/
|
|
100
143
|
export declare const number: BaseConverter<number, undefined>;
|
|
101
144
|
/**
|
|
102
|
-
* A
|
|
103
|
-
*
|
|
104
|
-
*
|
|
145
|
+
* A {@link Converter} which converts `unknown` to `boolean`.
|
|
146
|
+
* @remarks
|
|
147
|
+
* Boolean values or the case-insensitive strings `'true'` and `'false'` succeed.
|
|
148
|
+
* Anything else fails.
|
|
149
|
+
* @public
|
|
105
150
|
*/
|
|
106
151
|
export declare const boolean: BaseConverter<boolean, undefined>;
|
|
107
152
|
/**
|
|
108
|
-
* A
|
|
109
|
-
* are returned. Anything else returns
|
|
153
|
+
* A {@link Converter} which converts an optional `string` value. Values of type
|
|
154
|
+
* `string` are returned. Anything else returns {@link Success} with value `undefined`.
|
|
155
|
+
* @public
|
|
110
156
|
*/
|
|
111
157
|
export declare const optionalString: Converter<string | undefined, unknown>;
|
|
112
158
|
/**
|
|
113
|
-
*
|
|
114
|
-
* by separating at a supplied delimiter.
|
|
115
|
-
*
|
|
116
|
-
*
|
|
159
|
+
* Helper function to create a {@link Converter} which converts any `string` into an
|
|
160
|
+
* array of `string`, by separating at a supplied delimiter.
|
|
161
|
+
* @remarks
|
|
162
|
+
* Delimeter may also be supplied as context at conversion time.
|
|
163
|
+
* @param delimiter - The delimiter at which to split.
|
|
164
|
+
* @returns A new {@link Converter} returning `string[]`.
|
|
165
|
+
* @public
|
|
117
166
|
*/
|
|
118
167
|
export declare function delimitedString(delimiter: string, options?: 'filtered' | 'all'): Converter<string[], string>;
|
|
119
168
|
/**
|
|
120
|
-
* A
|
|
169
|
+
* A {@link Converter} which converts an iso formatted string, a number or a `Date` object to
|
|
170
|
+
* a `Date` object.
|
|
171
|
+
* @public
|
|
121
172
|
*/
|
|
122
173
|
export declare const isoDate: BaseConverter<Date, undefined>;
|
|
123
174
|
/**
|
|
124
|
-
* A
|
|
125
|
-
*
|
|
126
|
-
*
|
|
175
|
+
* A {@link Converter} which converts an optional `number` value.
|
|
176
|
+
* @remarks
|
|
177
|
+
* Values of type `number` or numeric strings are converted and returned.
|
|
178
|
+
* Anything else returns {@link Success} with value `undefined`.
|
|
179
|
+
* @public
|
|
127
180
|
*/
|
|
128
181
|
export declare const optionalNumber: Converter<number | undefined, undefined>;
|
|
129
182
|
/**
|
|
130
|
-
* A
|
|
131
|
-
*
|
|
132
|
-
*
|
|
183
|
+
* A {@link Converter} to convert an optional `boolean` value.
|
|
184
|
+
* @remarks
|
|
185
|
+
* Values of type `boolean` or strings that match (case-insensitive) `'true'`
|
|
186
|
+
* or `'false'` are converted and returned. Anything else returns {@link Success}
|
|
187
|
+
* with value `undefined`.
|
|
188
|
+
* @public
|
|
133
189
|
*/
|
|
134
190
|
export declare const optionalBoolean: Converter<boolean | undefined, undefined>;
|
|
135
191
|
/**
|
|
136
|
-
* A helper
|
|
137
|
-
* in sequence, returning the first successful
|
|
138
|
-
* if none of the supplied converters can convert the value.
|
|
139
|
-
*
|
|
140
|
-
* If onError is
|
|
192
|
+
* A helper function to create a {@link Converter} for polymorphic values. Returns a
|
|
193
|
+
* converter which Invokes the wrapped converters in sequence, returning the first successful
|
|
194
|
+
* result. Returns an error if none of the supplied converters can convert the value.
|
|
195
|
+
* @remarks
|
|
196
|
+
* If `onError` is `ignoreErrors` (default), then errors from any of the
|
|
141
197
|
* converters are ignored provided that some converter succeeds. If
|
|
142
|
-
* onError is
|
|
198
|
+
* onError is `failOnError`, then an error from any converter fails the entire
|
|
143
199
|
* conversion.
|
|
144
200
|
*
|
|
145
|
-
* @param converters An ordered list of converters to be considered
|
|
146
|
-
* @param onError Specifies treatment of unconvertable elements
|
|
201
|
+
* @param converters - An ordered list of {@link Converter | converters} to be considered.
|
|
202
|
+
* @param onError - Specifies treatment of unconvertable elements.
|
|
203
|
+
* @returns A new {@link Converter} which yields a value from the union of the types returned
|
|
204
|
+
* by the wrapped converters.
|
|
205
|
+
* @public
|
|
147
206
|
*/
|
|
148
207
|
export declare function oneOf<T, TC = unknown>(converters: Array<Converter<T, TC>>, onError?: OnError): Converter<T, TC>;
|
|
149
208
|
/**
|
|
150
|
-
* A helper
|
|
151
|
-
*
|
|
152
|
-
* is '
|
|
153
|
-
*
|
|
154
|
-
* @param
|
|
209
|
+
* A helper function to create a {@link Converter} which converts `unknown` to an array of `<T>`.
|
|
210
|
+
* @remarks
|
|
211
|
+
* If `onError` is `'failOnError'` (default), then the entire conversion fails if any element cannot
|
|
212
|
+
* be converted. If `onError` is `'ignoreErrors'`, then failing elements are silently ignored.
|
|
213
|
+
* @param converter - {@link Converter} used to convert each item in the array.
|
|
214
|
+
* @param ignoreErrors - Specifies treatment of unconvertable elements.
|
|
215
|
+
* @returns A {@link Converter} which returns an array of `<T>`.
|
|
216
|
+
* @public
|
|
155
217
|
*/
|
|
156
218
|
export declare function arrayOf<T, TC = undefined>(converter: Converter<T, TC>, onError?: OnError): Converter<T[], TC>;
|
|
157
219
|
/**
|
|
158
|
-
* A helper
|
|
159
|
-
*
|
|
160
|
-
* is '
|
|
161
|
-
*
|
|
162
|
-
* @param
|
|
220
|
+
* A helper function to create a {@link Converter} which converts `unknown` to {@link ExtendedArray | ExtendedArray<T>}.
|
|
221
|
+
* @remarks
|
|
222
|
+
* If `onError` is `'failOnError'` (default), then the entire conversion fails if any element cannot
|
|
223
|
+
* be converted. If `onError` is `'ignoreErrors'`, then failing elements are silently ignored.
|
|
224
|
+
* @param converter - {@link Converter} used to convert each item in the array
|
|
225
|
+
* @param ignoreErrors - Specifies treatment of unconvertable elements
|
|
226
|
+
* @beta
|
|
163
227
|
*/
|
|
164
228
|
export declare function extendedArrayOf<T, TC = undefined>(label: string, converter: Converter<T, TC>, onError?: OnError): Converter<ExtendedArray<T>, TC>;
|
|
165
229
|
/**
|
|
166
|
-
* Converter to convert an unknown to an array of
|
|
167
|
-
*
|
|
230
|
+
* {@link Converter} to convert an `unknown` to an array of `string`.
|
|
231
|
+
* @remarks
|
|
232
|
+
* Returns {@link Success} with the the supplied value if it as an array
|
|
233
|
+
* of strings, returns {@link Failure} with an error message otherwise.
|
|
234
|
+
* @public
|
|
168
235
|
*/
|
|
169
236
|
export declare const stringArray: Converter<string[], unknown>;
|
|
170
237
|
/**
|
|
171
|
-
* Converter to convert an unknown to an array of
|
|
172
|
-
*
|
|
238
|
+
* {@link Converter} to convert an `unknown` to an array of `number`.
|
|
239
|
+
* @remarks
|
|
240
|
+
* Returns {@link Success} with the the supplied value if it as an array
|
|
241
|
+
* of numbers, returns {@link Failure} with an error message otherwise.
|
|
242
|
+
* @public
|
|
173
243
|
*/
|
|
174
244
|
export declare const numberArray: Converter<number[], undefined>;
|
|
175
245
|
/**
|
|
176
|
-
* Options for
|
|
246
|
+
* Options for {@link Converters.(recordOf:withOptions)} and {@link Converters.(mapOf:withOptions)}
|
|
247
|
+
* helper functions.
|
|
248
|
+
* @public
|
|
177
249
|
*/
|
|
178
250
|
export interface KeyedConverterOptions<T extends string = string, TC = undefined> {
|
|
251
|
+
/**
|
|
252
|
+
* if `onError` is `'fail'` (default), then the entire conversion fails if any key or element
|
|
253
|
+
* cannot be converted. If `onError` is `'ignore'`, failing elements are silently ignored.
|
|
254
|
+
*/
|
|
179
255
|
onError?: 'fail' | 'ignore';
|
|
256
|
+
/**
|
|
257
|
+
* If present, `keyConverter` is used to convert the source object property names to
|
|
258
|
+
* keys in the resulting map or record.
|
|
259
|
+
* @remarks
|
|
260
|
+
* Can be used to coerce key names to supported values and/or strong types.
|
|
261
|
+
*/
|
|
180
262
|
keyConverter?: Converter<T, TC>;
|
|
181
263
|
}
|
|
182
264
|
/**
|
|
183
|
-
* A helper
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
265
|
+
* A helper function to create a {@link Converter} which converts the `string`-keyed properties
|
|
266
|
+
* using a supplied {@link Converter | Converter<T>} to produce a `Record<string, T>`.
|
|
267
|
+
* @remarks
|
|
268
|
+
* The resulting converter fails conversion if any element cannot be converted.
|
|
269
|
+
* @param converter - {@link Converter} used to convert each item in the source object.
|
|
270
|
+
* @returns A {@link Converter} which returns `Record<string, T>`.
|
|
271
|
+
* {@label default}
|
|
272
|
+
* @public
|
|
187
273
|
*/
|
|
188
274
|
export declare function recordOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>): Converter<Record<TK, T>, TC>;
|
|
189
275
|
/**
|
|
190
|
-
* A helper
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
* @
|
|
194
|
-
*
|
|
276
|
+
* A helper function to create a {@link Converter} which converts the `string`-keyed properties
|
|
277
|
+
* using a supplied {@link Converter | Converter<T>} to produce a `Record<string, T>` and optionally
|
|
278
|
+
* specified handling of elements that cannot be converted.
|
|
279
|
+
* @remarks
|
|
280
|
+
* if `onError` is `'fail'` (default), then the entire conversion fails if any key or element
|
|
281
|
+
* cannot be converted. If `onError` is `'ignore'`, failing elements are silently ignored.
|
|
282
|
+
* @param converter - {@link Converter} used to convert each item in the source object.
|
|
283
|
+
* @returns A {@link Converter} which returns `Record<string, T>`.
|
|
284
|
+
* {@label withOnError}
|
|
285
|
+
* @public
|
|
195
286
|
*/
|
|
196
287
|
export declare function recordOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>, onError: 'fail' | 'ignore'): Converter<Record<TK, T>, TC>;
|
|
197
288
|
/**
|
|
198
|
-
* A helper
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
* @param
|
|
289
|
+
* A helper function to create a {@link Converter} which converts the `string`-keyed properties
|
|
290
|
+
* using a supplied {@link Converter | Converter<T>} to produce a `Record<TK, T>`.
|
|
291
|
+
* @remarks
|
|
292
|
+
* If present, the supplied {@link Converters.KeyedConverterOptions | options} can provide a strongly-typed
|
|
293
|
+
* converter for keys and/or control the handling of elements that fail conversion.
|
|
294
|
+
* @param converter - {@link Converter} used to convert each item in the source object.
|
|
295
|
+
* @param options - Optional {@link Converters.KeyedConverterOptions | KeyedConverterOptions<TK, TC>} which
|
|
296
|
+
* supplies a key converter and/or error-handling options.
|
|
297
|
+
* @returns A {@link Converter} which returns `Record<TK, T>`.
|
|
298
|
+
* {@label withOptions}
|
|
299
|
+
* @public
|
|
204
300
|
*/
|
|
205
301
|
export declare function recordOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>, options: KeyedConverterOptions<TK, TC>): Converter<Record<TK, T>, TC>;
|
|
206
302
|
/**
|
|
207
|
-
* A helper
|
|
208
|
-
*
|
|
209
|
-
* @
|
|
303
|
+
* A helper function to create a {@link Converter} which converts the `string`-keyed properties
|
|
304
|
+
* using a supplied {@link Converter | Converter<T>} to produce a `Map<string, T>`.
|
|
305
|
+
* @remarks
|
|
306
|
+
* The resulting converter fails conversion if any element cannot be converted.
|
|
307
|
+
* @param converter - {@link Converter} used to convert each item in the source object.
|
|
308
|
+
* @returns A {@link Converter} which returns `Map<string, T>`.
|
|
309
|
+
* {@label default}
|
|
310
|
+
* @public
|
|
210
311
|
*/
|
|
211
312
|
export declare function mapOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>): Converter<Map<TK, T>, TC>;
|
|
212
313
|
/**
|
|
213
|
-
* A helper
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
* @
|
|
217
|
-
*
|
|
314
|
+
* A helper function to create a {@link Converter} which converts the `string`-keyed properties
|
|
315
|
+
* using a supplied {@link Converter | Converter<T>} to produce a `Map<string, T>` and optionally
|
|
316
|
+
* specified handling of elements that cannot be converted.
|
|
317
|
+
* @remarks
|
|
318
|
+
* if `onError` is `'fail'` (default), then the entire conversion fails if any key or element
|
|
319
|
+
* cannot be converted. If `onError` is `'ignore'`, failing elements are silently ignored.
|
|
320
|
+
* @param converter - {@link Converter} used to convert each item in the source object.
|
|
321
|
+
* @returns A {@link Converter} which returns `Map<string, T>`.
|
|
322
|
+
* {@label withOnError}
|
|
323
|
+
* @public
|
|
218
324
|
*/
|
|
219
325
|
export declare function mapOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>, onError: 'fail' | 'ignore'): Converter<Map<TK, T>, TC>;
|
|
220
326
|
/**
|
|
221
|
-
* A helper
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
* @param
|
|
327
|
+
* A helper function to create a {@link Converter} which converts the `string`-keyed properties
|
|
328
|
+
* using a supplied {@link Converter | Converter<T>} to produce a `Map<TK, T>`.
|
|
329
|
+
* @remarks
|
|
330
|
+
* If present, the supplied {@link Converters.KeyedConverterOptions | options} can provide a strongly-typed
|
|
331
|
+
* converter for keys and/or control the handling of elements that fail conversion.
|
|
332
|
+
* @param converter - {@link Converter} used to convert each item in the source object.
|
|
333
|
+
* @param options - Optional {@link Converters.KeyedConverterOptions | KeyedConverterOptions<TK, TC>} which
|
|
334
|
+
* supplies a key converter and/or error-handling options.
|
|
335
|
+
* @returns A {@link Converter} which returns `Map<TK, T>`.
|
|
336
|
+
* {@label withOptions}
|
|
337
|
+
* @public
|
|
227
338
|
*/
|
|
228
339
|
export declare function mapOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>, options: KeyedConverterOptions<TK, TC>): Converter<Map<TK, T>, TC>;
|
|
229
340
|
/**
|
|
230
|
-
*
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
341
|
+
* Helper function to create a {@link Converter} which validates that a supplied value is
|
|
342
|
+
* of a type validated by a supplied validator function and returns it.
|
|
343
|
+
* @remarks
|
|
344
|
+
* If `validator` succeeds, this {@link Converter} returns {@link Success} with the supplied
|
|
345
|
+
* value of `from` coerced to type `<T>`. Returns a {@link Failure} with additional
|
|
346
|
+
* information otherwise.
|
|
347
|
+
* @param validator - A validator function to determine if the converted value is valid.
|
|
348
|
+
* @param description - A description of the validated type for use in error messages.
|
|
349
|
+
* @returns A new {@link Converter | Converter<T>} which applies the supplied validation.
|
|
350
|
+
* @public
|
|
351
|
+
*/
|
|
352
|
+
export declare function validateWith<T, TC = undefined>(validator: (from: unknown) => from is T, description?: string): Converter<T, TC>;
|
|
353
|
+
/**
|
|
354
|
+
* A helper function to create a {@link Converter} which extracts and converts an element from an array.
|
|
355
|
+
* @remarks
|
|
356
|
+
* The returned {@link Converter} returns {@link Success} with the converted value if the element exists
|
|
357
|
+
* in the supplied array and can be converted. Returns {@link Failure} with an error message otherwise.
|
|
358
|
+
* @param index - The index of the element to be extracted.
|
|
359
|
+
* @param converter - A {@link Converter} used to convert the extracted element.
|
|
360
|
+
* @returns A {@link Converter | Converter<T>} which extracts the specified element from an array.
|
|
361
|
+
* @public
|
|
235
362
|
*/
|
|
236
363
|
export declare function element<T, TC = undefined>(index: number, converter: Converter<T, TC>): Converter<T, TC>;
|
|
237
364
|
/**
|
|
238
|
-
* A helper function to
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
* @param
|
|
365
|
+
* A helper function to create a {@link Converter} which extracts and converts an optional element from an array.
|
|
366
|
+
* @remarks
|
|
367
|
+
* The resulting {@link Converter} returns {@link Success} with the converted value if the element exsist
|
|
368
|
+
* in the supplied array and can be converted. Returns {@link Success} with value `undefined` if the parameter
|
|
369
|
+
* is an array but the index is out of range. Returns {@link Failure} with a message if the supplied parameter
|
|
370
|
+
* is not an array, if the requested index is negative, or if the element cannot be converted.
|
|
371
|
+
* @param index - The index of the element to be extracted.
|
|
372
|
+
* @param converter - A {@link Converter} used to convert the extracted element.
|
|
373
|
+
* @returns A {@link Converter | Converter<T>} which extracts the specified element from an array.
|
|
374
|
+
* @public
|
|
245
375
|
*/
|
|
246
376
|
export declare function optionalElement<T, TC = undefined>(index: number, converter: Converter<T, TC>): Converter<T | undefined, TC>;
|
|
247
377
|
/**
|
|
248
|
-
* A helper function to
|
|
249
|
-
*
|
|
250
|
-
*
|
|
251
|
-
* @
|
|
252
|
-
*
|
|
378
|
+
* A helper function to create a {@link Converter} which extracts and convert a property specified
|
|
379
|
+
* by name from an object.
|
|
380
|
+
* @remarks
|
|
381
|
+
* The resulting {@link Converter} returns {@link Success} with the converted value of the correpsonding
|
|
382
|
+
* object property if the field exists and can be converted. Returns {@link Failure} with an error message
|
|
383
|
+
* otherwise.
|
|
384
|
+
* @param name - The name of the field to be extracted.
|
|
385
|
+
* @param converter - {@link Converter} used to convert the extracted field.
|
|
386
|
+
* @public
|
|
253
387
|
*/
|
|
254
388
|
export declare function field<T, TC = undefined>(name: string, converter: Converter<T, TC>): Converter<T, TC>;
|
|
255
389
|
/**
|
|
256
|
-
* A helper function to
|
|
257
|
-
*
|
|
258
|
-
*
|
|
259
|
-
*
|
|
260
|
-
*
|
|
261
|
-
*
|
|
390
|
+
* A helper function to create a {@link Converter} which extracts and convert a property specified
|
|
391
|
+
* by name from an object.
|
|
392
|
+
* @remarks
|
|
393
|
+
* The resulting {@link Converter} returns {@link Success} with the converted value of the correpsonding
|
|
394
|
+
* object property if the field exists and can be converted. Returns {@link Success} with value `undefined`
|
|
395
|
+
* if the supplied parametr is an object but the named field is not present. Returns {@link Failure} with
|
|
396
|
+
* an error message otherwise.
|
|
397
|
+
* @param name - The name of the field to be extracted.
|
|
398
|
+
* @param converter - {@link Converter} used to convert the extracted field.
|
|
399
|
+
* @public
|
|
262
400
|
*/
|
|
263
401
|
export declare function optionalField<T, TC = undefined>(name: string, converter: Converter<T, TC>): Converter<T | undefined, TC>;
|
|
264
402
|
/**
|
|
265
|
-
* Options for an @
|
|
403
|
+
* Options for an {@link Converters.ObjectConverter | ObjectConverter}.
|
|
404
|
+
* @public
|
|
266
405
|
*/
|
|
267
406
|
export interface ObjectConverterOptions<T> {
|
|
268
407
|
/**
|
|
@@ -277,101 +416,220 @@ export interface ObjectConverterOptions<T> {
|
|
|
277
416
|
}
|
|
278
417
|
/**
|
|
279
418
|
* Per-property converters for each of the properties in type T.
|
|
419
|
+
* @remarks
|
|
420
|
+
* Used to construct a {@link Converters.ObjectConverter | ObjectConverter}
|
|
421
|
+
* @public
|
|
280
422
|
*/
|
|
281
423
|
export declare type FieldConverters<T, TC = unknown> = {
|
|
282
424
|
[key in keyof T]: Converter<T[key], TC>;
|
|
283
425
|
};
|
|
284
426
|
/**
|
|
285
|
-
* Converter
|
|
286
|
-
*
|
|
287
|
-
*
|
|
288
|
-
* the
|
|
427
|
+
* A {@link Converter} which converts an object of type `<T>` without changing shape, given
|
|
428
|
+
* a {@link Converters.FieldConverters | FieldConverters<T>} for the fields in the object.
|
|
429
|
+
* @remarks
|
|
430
|
+
* By default, if all of the required fields exist and can be converted, returns a new object with
|
|
431
|
+
* the converted values under the original key names. If any required fields do not exist or cannot
|
|
432
|
+
* be converted, the entire conversion fails. See {@link Converters.ObjectConverterOptions | ObjectConverterOptions}
|
|
433
|
+
* for other conversion options.
|
|
434
|
+
* @public
|
|
289
435
|
*/
|
|
290
436
|
export declare class ObjectConverter<T, TC = unknown> extends BaseConverter<T, TC> {
|
|
437
|
+
/**
|
|
438
|
+
* Fields converted by this {@link Converters.ObjectConverter | ObjectConverter}.
|
|
439
|
+
*/
|
|
291
440
|
readonly fields: FieldConverters<T>;
|
|
441
|
+
/**
|
|
442
|
+
* Options used to initialize this {@link Converters.ObjectConverter | ObjectConverter}.
|
|
443
|
+
*/
|
|
292
444
|
readonly options: ObjectConverterOptions<T>;
|
|
293
445
|
/**
|
|
294
|
-
* Constructs a new @
|
|
295
|
-
*
|
|
296
|
-
* @param fields A @
|
|
297
|
-
*
|
|
446
|
+
* Constructs a new {@link Converters.ObjectConverter | ObjectConverter<T>} using options
|
|
447
|
+
* supplied in a {@link Converters.ObjectConverterOptions | ObjectConverterOptions<T>}.
|
|
448
|
+
* @param fields - A {@link Converters.FieldConverters | FieldConverters<T>} containing
|
|
449
|
+
* a {@link Converter} for each field
|
|
450
|
+
* @param options - An optional @see ObjectConverterOptions to configure the conversion
|
|
451
|
+
* {@label withOptions}
|
|
298
452
|
*/
|
|
299
453
|
constructor(fields: FieldConverters<T, TC>, options?: ObjectConverterOptions<T>);
|
|
300
454
|
/**
|
|
301
|
-
* Constructs a new @
|
|
302
|
-
*
|
|
303
|
-
* @param fields A @
|
|
304
|
-
* @
|
|
455
|
+
* Constructs a new {@link Converters.ObjectConverter | ObjectConverter<T>} with optional
|
|
456
|
+
* properties specified as an array of `keyof T`.
|
|
457
|
+
* @param fields - A {@link Converters.FieldConverters | FieldConverters<T>} containing
|
|
458
|
+
* a {@link Converter} for each field.
|
|
459
|
+
* @param optional - An array of `keyof T` listing fields that are not required.
|
|
460
|
+
* {@label withKeys}
|
|
305
461
|
*/
|
|
306
462
|
constructor(fields: FieldConverters<T, TC>, optional?: (keyof T)[]);
|
|
463
|
+
/**
|
|
464
|
+
* Creates a new {@link Converters.ObjectConverter | ObjectConverter} derived from this one but with
|
|
465
|
+
* new optional properties as specified by a supplied {@link Converters.ObjectConverterOptions | ObjectConverterOptions<T>}.
|
|
466
|
+
* @param options - The {@link Converters.ObjectConverterOptions | options} to be applied to the new
|
|
467
|
+
* converter.
|
|
468
|
+
* @returns A new {@link Converters.ObjectConverter | ObjectConverter} with the additional optional source properties.
|
|
469
|
+
* {@label withOptions}
|
|
470
|
+
*/
|
|
307
471
|
partial(options: ObjectConverterOptions<T>): ObjectConverter<Partial<T>, TC>;
|
|
472
|
+
/**
|
|
473
|
+
* Creates a new {@link Converters.ObjectConverter | ObjectConverter} derived from this one but with
|
|
474
|
+
* new optional properties as specified by a supplied array of `keyof T`.
|
|
475
|
+
* @param optional - The keys of the source object properties to be made optional.
|
|
476
|
+
* @returns A new {@link Converters.ObjectConverter | ObjectConverter} with the additional optional source
|
|
477
|
+
* properties.
|
|
478
|
+
* {@label withKeys}
|
|
479
|
+
*/
|
|
308
480
|
partial(optional?: (keyof T)[]): ObjectConverter<Partial<T>, TC>;
|
|
309
|
-
|
|
481
|
+
/**
|
|
482
|
+
* Creates a new {@link Converters.ObjectConverter | ObjectConverter} derived from this one but with
|
|
483
|
+
* new optional properties as specified by a supplied array of `keyof T`.
|
|
484
|
+
* @param addOptionalProperties - The keys to be made optional.
|
|
485
|
+
* @returns A new {@link Converters.ObjectConverter | ObjectConverter} with the additional optional source
|
|
486
|
+
* properties.
|
|
487
|
+
*/
|
|
488
|
+
addPartial(addOptionalProperties: (keyof T)[]): ObjectConverter<Partial<T>, TC>;
|
|
310
489
|
}
|
|
311
490
|
/**
|
|
312
|
-
* Helper to
|
|
313
|
-
*
|
|
314
|
-
*
|
|
315
|
-
*
|
|
316
|
-
*
|
|
491
|
+
* Helper function to create a {@link Converters.ObjectConverter | ObjectConverter<T>} which converts an object
|
|
492
|
+
* without changing shape, given a {@link Converters.FieldConverters | FieldConverters<T>} and an optional
|
|
493
|
+
* {@link Converters.ObjectConverterOptions | ObjectConverterOptions<T>} to further refine conversion beavior.
|
|
494
|
+
* @remarks
|
|
495
|
+
* By default, if all of the requested fields exist and can be converted, returns {@link Success}
|
|
496
|
+
* with a new object that contains the converted values under the original key names. If any requried properties
|
|
497
|
+
* do not exist or cannot be converted, the entire conversion fails, returning {@link Failure} with additional
|
|
498
|
+
* error information.
|
|
499
|
+
*
|
|
500
|
+
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
501
|
+
* fail the conversion.
|
|
502
|
+
* @param properties - An {@link Converters.FieldConverters | FieldConverters<T>} defining the shape of the
|
|
503
|
+
* source object and {@link Converter | converters} to be applied to each properties.
|
|
504
|
+
* @param options - An {@link Converters.ObjectConverterOptions | ObjectConverterOptions<T>} containing options
|
|
505
|
+
* for the object converter.
|
|
506
|
+
* @returns A new {@link Converters.ObjectConverter | ObjectConverter} which applies the specified conversions.
|
|
507
|
+
* {@label withOptions}
|
|
508
|
+
* @public
|
|
509
|
+
*/
|
|
510
|
+
export declare function object<T>(properties: FieldConverters<T>, options?: ObjectConverterOptions<T>): ObjectConverter<T>;
|
|
511
|
+
/**
|
|
512
|
+
* Helper function to create a {@link Converters.ObjectConverter | ObjectConverter<T>} which converts an object
|
|
513
|
+
* without changing shape, given a {@link Converters.FieldConverters | FieldConverters<T>} and a set of
|
|
514
|
+
* optional properties.
|
|
515
|
+
* @remarks
|
|
516
|
+
* By default, if all of the requested fields exist and can be converted, returns {@link Success}
|
|
517
|
+
* with a new object that contains the converted values under the original key names. If any requried properties
|
|
518
|
+
* do not exist or cannot be converted, the entire conversion fails, returning {@link Failure} with additional
|
|
519
|
+
* error information.
|
|
317
520
|
*
|
|
318
521
|
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
319
522
|
* fail the conversion.
|
|
320
|
-
* @param
|
|
321
|
-
*
|
|
322
|
-
*
|
|
523
|
+
* @param properties - An {@link Converters.FieldConverters | FieldConverters<T>} defining the shape of the
|
|
524
|
+
* source object and {@link Converter | converters} to be applied to each properties.
|
|
525
|
+
* @param optional - An array of `(keyof T)` listing the keys to be considered optional.
|
|
526
|
+
* {@label withKeys}
|
|
527
|
+
* @returns A new {@link Converters.ObjectConverter | ObjectConverter} which applies the specified conversions.
|
|
528
|
+
* @public
|
|
529
|
+
* @deprecated Use {@link Converters.(object:withOptions) | Converters.object(fields, options)} instead.
|
|
323
530
|
*/
|
|
324
|
-
export declare function object<T>(
|
|
531
|
+
export declare function object<T>(properties: FieldConverters<T>, optional: (keyof T)[]): ObjectConverter<T>;
|
|
325
532
|
/**
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
533
|
+
* Options for the {@link Converters.(strictObject:withOptions)} helper function.
|
|
534
|
+
* @public
|
|
535
|
+
*/
|
|
536
|
+
export declare type StrictObjectConverterOptions<T> = Omit<ObjectConverterOptions<T>, 'strict'>;
|
|
537
|
+
/**
|
|
538
|
+
* Helper function to create a {@link Converters.ObjectConverter | ObjectConverter} which converts an object
|
|
539
|
+
* without changing shape, a {@link Converters.FieldConverters | FieldConverters<T>} and an optional
|
|
540
|
+
* {@link Converters.StrictObjectConverterOptions | StrictObjectConverterOptions<T>} to further refine
|
|
541
|
+
* conversion behavior.
|
|
542
|
+
*
|
|
543
|
+
* @remarks
|
|
544
|
+
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
545
|
+
* fail the conversion.
|
|
546
|
+
*
|
|
547
|
+
* The conversion fails if any unexpected fields are encountered.
|
|
331
548
|
*
|
|
549
|
+
* @param properties - An object containing defining the shape and converters to be applied.
|
|
550
|
+
* @param options - An optional @see StrictObjectConverterOptions<T> containing options for the object converter.
|
|
551
|
+
* @returns A new {@link Converters.ObjectConverter | ObjectConverter} which applies the specified conversions.
|
|
552
|
+
* {@label withOptions}
|
|
553
|
+
* @public
|
|
554
|
+
*/
|
|
555
|
+
export declare function strictObject<T>(properties: FieldConverters<T>, options?: StrictObjectConverterOptions<T>): ObjectConverter<T>;
|
|
556
|
+
/**
|
|
557
|
+
* Helper function to create a {@link Converters.ObjectConverter | ObjectConverter} which converts an object
|
|
558
|
+
* without changing shape, a {@link Converters.FieldConverters | FieldConverters<T>} and an optional
|
|
559
|
+
* {@link Converters.StrictObjectConverterOptions | StrictObjectConverterOptions<T>} to further refine
|
|
560
|
+
* conversion behavior.
|
|
561
|
+
*
|
|
562
|
+
* @remarks
|
|
332
563
|
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
333
564
|
* fail the conversion.
|
|
334
565
|
*
|
|
335
566
|
* The conversion fails if any unexpected fields are encountered.
|
|
336
567
|
*
|
|
337
|
-
* @param
|
|
338
|
-
* @param
|
|
339
|
-
*
|
|
568
|
+
* @param properties - An object containing defining the shape and converters to be applied.
|
|
569
|
+
* @param optional - An array of `keyof T` containing keys to be considered optional.
|
|
570
|
+
* @returns A new {@link Converters.ObjectConverter | ObjectConverter} which applies the specified conversions.
|
|
571
|
+
* {@label withKeys}
|
|
572
|
+
* @deprecated Use {@link Converters.(strictObject:withOptions) | Converters.strictObject(options)} instead.
|
|
573
|
+
* @public
|
|
574
|
+
*/
|
|
575
|
+
export declare function strictObject<T>(properties: FieldConverters<T>, optional: (keyof T)[]): ObjectConverter<T>;
|
|
576
|
+
/**
|
|
577
|
+
* A string-keyed `Record<string, Converter>` which maps specific {@link Converter | converters} to the
|
|
578
|
+
* value of a discriminator property.
|
|
579
|
+
* @public
|
|
340
580
|
*/
|
|
341
|
-
export declare function strictObject<T>(fields: FieldConverters<T>, opt?: (keyof T)[] | Omit<ObjectConverterOptions<T>, 'strict'>): ObjectConverter<T>;
|
|
342
581
|
export declare type DiscriminatedObjectConverters<T, TD extends string = string, TC = unknown> = Record<TD, Converter<T, TC>>;
|
|
343
582
|
/**
|
|
344
|
-
* Helper to
|
|
345
|
-
*
|
|
346
|
-
*
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
*
|
|
351
|
-
* discriminator property
|
|
583
|
+
* Helper to create a {@link Converter} whhich converts a discriminated object without changing shape.
|
|
584
|
+
* @remarks
|
|
585
|
+
* Takes the name of the discriminator property and a
|
|
586
|
+
* {@link Converters.DiscriminatedObjectConverters | string-keyed Record of converters}. During conversion,
|
|
587
|
+
* the resulting {@link Converter} invokes the converter from `converters` that corresponds to the value of
|
|
588
|
+
* the discriminator property in the source object.
|
|
589
|
+
*
|
|
590
|
+
* If the source is not an object, the discriminator property is missing, or the discriminator has
|
|
591
|
+
* a value not present in the converters, conversion fails and returns {@link Failure} with more information.
|
|
592
|
+
* @param discriminatorProp - Name of the property used to discriminate types.
|
|
593
|
+
* @param converters - {@link Converters.DiscriminatedObjectConverters | String-keyed record of converters} to
|
|
594
|
+
* invoke, where each key corresponds to a value of the discriminator property.
|
|
595
|
+
* @returns A {@link Converter} which converts the corresponding discriminated object.
|
|
596
|
+
* @public
|
|
352
597
|
*/
|
|
353
598
|
export declare function discriminatedObject<T, TD extends string = string, TC = unknown>(discriminatorProp: string, converters: DiscriminatedObjectConverters<T, TD>): Converter<T, TC>;
|
|
354
599
|
/**
|
|
355
|
-
* Helper to
|
|
356
|
-
*
|
|
357
|
-
* converter that will extract and convert a single field from the source object.
|
|
600
|
+
* Helper to create a {@link Converter} which converts a source object to a new object with a
|
|
601
|
+
* different shape.
|
|
358
602
|
*
|
|
359
|
-
*
|
|
360
|
-
*
|
|
361
|
-
*
|
|
603
|
+
* @remarks
|
|
604
|
+
* On successful conversion, the resulting {@link Converter} returns {@link Success} with a new
|
|
605
|
+
* object, which contains the converted values under the key names specified at initialization time.
|
|
606
|
+
* It returns {@link Failure} with an error message if any fields to be extracted do not exist
|
|
607
|
+
* or cannot be converted.
|
|
362
608
|
*
|
|
363
609
|
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
364
610
|
* fail the conversion.
|
|
365
611
|
*
|
|
366
|
-
* @param
|
|
367
|
-
*
|
|
612
|
+
* @param properties - An object with key names that correspond to the target object and an
|
|
613
|
+
* appropriate {@link Converters.FieldConverters | FieldConverter} which extracts and converts
|
|
614
|
+
* a single filed from the source object.
|
|
615
|
+
* @returns A {@link Converter} with the specified conversion behavior.
|
|
616
|
+
* @public
|
|
368
617
|
*/
|
|
369
|
-
export declare function transform<T, TC = unknown>(
|
|
618
|
+
export declare function transform<T, TC = unknown>(properties: FieldConverters<T, TC>): Converter<T, TC>;
|
|
370
619
|
/**
|
|
371
|
-
* A helper wrapper to
|
|
372
|
-
*
|
|
373
|
-
* @param
|
|
620
|
+
* A helper wrapper to construct a {@link Converter} which converts to an arbitrary strongly-typed
|
|
621
|
+
* range of some comparable type.
|
|
622
|
+
* @param converter - {@link Converter} used to convert `min` and `max` extent of the range.
|
|
623
|
+
* @param constructor - Static constructor to instantiate the object.
|
|
624
|
+
* @public
|
|
374
625
|
*/
|
|
375
626
|
export declare function rangeTypeOf<T, RT extends RangeOf<T>, TC = unknown>(converter: Converter<T, TC>, constructor: (init: RangeOfProperties<T>) => Result<RT>): Converter<RT, TC>;
|
|
627
|
+
/**
|
|
628
|
+
* A helper wrapper to construct a {@link Converter} which converts to {@link RangeOf | RangeOf<T>}
|
|
629
|
+
* where `<T>` is some comparable type.
|
|
630
|
+
* @param converter - {@link Converter} used to convert `min` and `max` extent of the range.
|
|
631
|
+
* @public
|
|
632
|
+
*/
|
|
376
633
|
export declare function rangeOf<T, TC = unknown>(converter: Converter<T, TC>): Converter<RangeOf<T>, TC>;
|
|
377
634
|
export {};
|
|
635
|
+
//# sourceMappingURL=converters.d.ts.map
|