@fgv/ts-utils 1.2.0 → 1.3.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/LICENSE +0 -0
- package/README.md +4 -0
- package/brand.d.ts +8 -0
- package/brand.d.ts.map +1 -0
- package/brand.js +24 -0
- package/converter.d.ts +149 -105
- package/converter.d.ts.map +1 -0
- package/converter.js +59 -72
- package/converters.d.ts +432 -194
- package/converters.d.ts.map +1 -0
- package/converters.js +335 -224
- 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 +7 -2
- package/index.d.ts.map +1 -0
- package/index.js +16 -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 +393 -39
- package/result.d.ts.map +1 -0
- package/result.js +259 -49
- package/ts-utils.d.ts +2475 -0
- package/tsdoc-metadata.json +11 -0
- package/utils.d.ts +71 -27
- package/utils.d.ts.map +1 -0
- package/utils.js +82 -37
- 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 -0
- package/validation/classes.d.ts.map +1 -0
- package/validation/classes.js +34 -0
- package/validation/field.d.ts +43 -0
- package/validation/field.d.ts.map +1 -0
- package/validation/field.js +72 -0
- package/validation/genericValidator.d.ts +84 -0
- package/validation/genericValidator.d.ts.map +1 -0
- package/validation/genericValidator.js +138 -0
- package/validation/index.d.ts +7 -0
- package/validation/index.d.ts.map +1 -0
- package/validation/index.js +59 -0
- package/validation/number.d.ts +27 -0
- package/validation/number.d.ts.map +1 -0
- package/validation/number.js +57 -0
- package/validation/object.d.ts +115 -0
- package/validation/object.d.ts.map +1 -0
- package/validation/object.js +143 -0
- package/validation/string.d.ts +27 -0
- package/validation/string.d.ts.map +1 -0
- package/validation/string.js +57 -0
- package/validation/traits.d.ts +68 -0
- package/validation/traits.d.ts.map +1 -0
- package/validation/traits.js +58 -0
- package/validation/validator.d.ts +82 -0
- package/validation/validator.d.ts.map +1 -0
- package/validation/validator.js +24 -0
- package/validation/validatorBase.d.ts +25 -0
- package/validation/validatorBase.d.ts.map +1 -0
- package/validation/validatorBase.js +44 -0
- package/validation/validators.d.ts +32 -0
- package/validation/validators.d.ts.map +1 -0
- package/validation/validators.js +59 -0
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,275 +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
|
-
* values
|
|
87
|
-
*
|
|
88
|
-
*
|
|
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
|
|
89
115
|
* value, the second is the set of values that map to the result. Tuples are evaluated in the order
|
|
90
116
|
* supplied and are not checked for duplicates.
|
|
91
|
-
* @param message
|
|
92
|
-
* @returns
|
|
117
|
+
* @param message - An optional error message.
|
|
118
|
+
* @returns A {@link Converter} which applies the mapping and yields `<T>` on success.
|
|
119
|
+
* @public
|
|
93
120
|
*/
|
|
94
121
|
export declare function mappedEnumeratedValue<T>(map: [T, unknown[]][], message?: string): Converter<T, undefined>;
|
|
95
122
|
/**
|
|
96
|
-
*
|
|
97
|
-
* comparison succeeds, fails otherwise.
|
|
98
|
-
* @param value The value to be compared
|
|
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
|
|
99
128
|
*/
|
|
100
129
|
export declare function literal<T>(value: T): Converter<T, unknown>;
|
|
101
130
|
/**
|
|
102
131
|
* Deprecated alias for @see literal
|
|
103
|
-
* @param value The value to be compared
|
|
104
|
-
* @deprecated
|
|
132
|
+
* @param value - The value to be compared.
|
|
133
|
+
* @deprecated Use {@link Converters.literal} instead.
|
|
134
|
+
* @internal
|
|
105
135
|
*/
|
|
106
136
|
export declare const value: typeof literal;
|
|
107
137
|
/**
|
|
108
|
-
* A
|
|
109
|
-
*
|
|
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
|
|
110
142
|
*/
|
|
111
143
|
export declare const number: BaseConverter<number, undefined>;
|
|
112
144
|
/**
|
|
113
|
-
* A
|
|
114
|
-
*
|
|
115
|
-
*
|
|
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
|
|
116
150
|
*/
|
|
117
151
|
export declare const boolean: BaseConverter<boolean, undefined>;
|
|
118
152
|
/**
|
|
119
|
-
* A
|
|
120
|
-
* 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
|
|
121
156
|
*/
|
|
122
157
|
export declare const optionalString: Converter<string | undefined, unknown>;
|
|
123
158
|
/**
|
|
124
|
-
*
|
|
125
|
-
* by separating at a supplied delimiter.
|
|
126
|
-
*
|
|
127
|
-
*
|
|
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
|
|
128
166
|
*/
|
|
129
167
|
export declare function delimitedString(delimiter: string, options?: 'filtered' | 'all'): Converter<string[], string>;
|
|
130
168
|
/**
|
|
131
|
-
* A
|
|
169
|
+
* A {@link Converter} which converts an iso formatted string, a number or a `Date` object to
|
|
170
|
+
* a `Date` object.
|
|
171
|
+
* @public
|
|
132
172
|
*/
|
|
133
173
|
export declare const isoDate: BaseConverter<Date, undefined>;
|
|
134
174
|
/**
|
|
135
|
-
* A
|
|
136
|
-
*
|
|
137
|
-
*
|
|
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
|
|
138
180
|
*/
|
|
139
181
|
export declare const optionalNumber: Converter<number | undefined, undefined>;
|
|
140
182
|
/**
|
|
141
|
-
* A
|
|
142
|
-
*
|
|
143
|
-
*
|
|
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
|
|
144
189
|
*/
|
|
145
190
|
export declare const optionalBoolean: Converter<boolean | undefined, undefined>;
|
|
146
191
|
/**
|
|
147
|
-
* A helper
|
|
148
|
-
* in sequence, returning the first successful
|
|
149
|
-
* if none of the supplied converters can convert the value.
|
|
150
|
-
*
|
|
151
|
-
* 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
|
|
152
197
|
* converters are ignored provided that some converter succeeds. If
|
|
153
|
-
* onError is
|
|
198
|
+
* onError is `failOnError`, then an error from any converter fails the entire
|
|
154
199
|
* conversion.
|
|
155
200
|
*
|
|
156
|
-
* @param converters An ordered list of converters to be considered
|
|
157
|
-
* @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
|
|
158
206
|
*/
|
|
159
207
|
export declare function oneOf<T, TC = unknown>(converters: Array<Converter<T, TC>>, onError?: OnError): Converter<T, TC>;
|
|
160
208
|
/**
|
|
161
|
-
* A helper
|
|
162
|
-
*
|
|
163
|
-
* is '
|
|
164
|
-
*
|
|
165
|
-
* @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
|
|
166
217
|
*/
|
|
167
218
|
export declare function arrayOf<T, TC = undefined>(converter: Converter<T, TC>, onError?: OnError): Converter<T[], TC>;
|
|
168
219
|
/**
|
|
169
|
-
* A helper
|
|
170
|
-
*
|
|
171
|
-
* is '
|
|
172
|
-
*
|
|
173
|
-
* @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
|
|
174
227
|
*/
|
|
175
228
|
export declare function extendedArrayOf<T, TC = undefined>(label: string, converter: Converter<T, TC>, onError?: OnError): Converter<ExtendedArray<T>, TC>;
|
|
176
229
|
/**
|
|
177
|
-
* Converter to convert an unknown to an array of
|
|
178
|
-
*
|
|
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
|
|
179
235
|
*/
|
|
180
236
|
export declare const stringArray: Converter<string[], unknown>;
|
|
181
237
|
/**
|
|
182
|
-
* Converter to convert an unknown to an array of
|
|
183
|
-
*
|
|
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
|
|
184
243
|
*/
|
|
185
244
|
export declare const numberArray: Converter<number[], undefined>;
|
|
186
245
|
/**
|
|
187
|
-
* Options for
|
|
246
|
+
* Options for {@link Converters.(recordOf:withOptions)} and {@link Converters.(mapOf:withOptions)}
|
|
247
|
+
* helper functions.
|
|
248
|
+
* @public
|
|
188
249
|
*/
|
|
189
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
|
+
*/
|
|
190
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
|
+
*/
|
|
191
262
|
keyConverter?: Converter<T, TC>;
|
|
192
263
|
}
|
|
193
264
|
/**
|
|
194
|
-
* A helper
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
*
|
|
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
|
|
198
273
|
*/
|
|
199
274
|
export declare function recordOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>): Converter<Record<TK, T>, TC>;
|
|
200
275
|
/**
|
|
201
|
-
* A helper
|
|
202
|
-
*
|
|
203
|
-
*
|
|
204
|
-
* @
|
|
205
|
-
*
|
|
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
|
|
206
286
|
*/
|
|
207
287
|
export declare function recordOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>, onError: 'fail' | 'ignore'): Converter<Record<TK, T>, TC>;
|
|
208
288
|
/**
|
|
209
|
-
* A helper
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
* @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
|
|
215
300
|
*/
|
|
216
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>;
|
|
217
302
|
/**
|
|
218
|
-
* A helper
|
|
219
|
-
*
|
|
220
|
-
* @
|
|
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
|
|
221
311
|
*/
|
|
222
312
|
export declare function mapOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>): Converter<Map<TK, T>, TC>;
|
|
223
313
|
/**
|
|
224
|
-
* A helper
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
* @
|
|
228
|
-
*
|
|
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
|
|
229
324
|
*/
|
|
230
325
|
export declare function mapOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>, onError: 'fail' | 'ignore'): Converter<Map<TK, T>, TC>;
|
|
231
326
|
/**
|
|
232
|
-
* A helper
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
* @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
|
|
238
338
|
*/
|
|
239
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>;
|
|
240
340
|
/**
|
|
241
|
-
*
|
|
242
|
-
* a supplied validator and returns it.
|
|
243
|
-
* @
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
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
|
|
247
351
|
*/
|
|
248
352
|
export declare function validateWith<T, TC = undefined>(validator: (from: unknown) => from is T, description?: string): Converter<T, TC>;
|
|
249
353
|
/**
|
|
250
|
-
* A helper function to
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
* @param
|
|
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
|
|
255
362
|
*/
|
|
256
363
|
export declare function element<T, TC = undefined>(index: number, converter: Converter<T, TC>): Converter<T, TC>;
|
|
257
364
|
/**
|
|
258
|
-
* A helper function to
|
|
259
|
-
*
|
|
260
|
-
*
|
|
261
|
-
*
|
|
262
|
-
*
|
|
263
|
-
*
|
|
264
|
-
* @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
|
|
265
375
|
*/
|
|
266
376
|
export declare function optionalElement<T, TC = undefined>(index: number, converter: Converter<T, TC>): Converter<T | undefined, TC>;
|
|
267
377
|
/**
|
|
268
|
-
* A helper function to
|
|
269
|
-
*
|
|
270
|
-
*
|
|
271
|
-
* @
|
|
272
|
-
*
|
|
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
|
|
273
387
|
*/
|
|
274
388
|
export declare function field<T, TC = undefined>(name: string, converter: Converter<T, TC>): Converter<T, TC>;
|
|
275
389
|
/**
|
|
276
|
-
* A helper function to
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
*
|
|
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
|
|
282
400
|
*/
|
|
283
401
|
export declare function optionalField<T, TC = undefined>(name: string, converter: Converter<T, TC>): Converter<T | undefined, TC>;
|
|
284
402
|
/**
|
|
285
|
-
* Options for an @
|
|
403
|
+
* Options for an {@link Converters.ObjectConverter | ObjectConverter}.
|
|
404
|
+
* @public
|
|
286
405
|
*/
|
|
287
406
|
export interface ObjectConverterOptions<T> {
|
|
288
407
|
/**
|
|
@@ -297,101 +416,220 @@ export interface ObjectConverterOptions<T> {
|
|
|
297
416
|
}
|
|
298
417
|
/**
|
|
299
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
|
|
300
422
|
*/
|
|
301
423
|
export declare type FieldConverters<T, TC = unknown> = {
|
|
302
424
|
[key in keyof T]: Converter<T[key], TC>;
|
|
303
425
|
};
|
|
304
426
|
/**
|
|
305
|
-
* Converter
|
|
306
|
-
*
|
|
307
|
-
*
|
|
308
|
-
* 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
|
|
309
435
|
*/
|
|
310
436
|
export declare class ObjectConverter<T, TC = unknown> extends BaseConverter<T, TC> {
|
|
437
|
+
/**
|
|
438
|
+
* Fields converted by this {@link Converters.ObjectConverter | ObjectConverter}.
|
|
439
|
+
*/
|
|
311
440
|
readonly fields: FieldConverters<T>;
|
|
441
|
+
/**
|
|
442
|
+
* Options used to initialize this {@link Converters.ObjectConverter | ObjectConverter}.
|
|
443
|
+
*/
|
|
312
444
|
readonly options: ObjectConverterOptions<T>;
|
|
313
445
|
/**
|
|
314
|
-
* Constructs a new @
|
|
315
|
-
*
|
|
316
|
-
* @param fields A @
|
|
317
|
-
*
|
|
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}
|
|
318
452
|
*/
|
|
319
453
|
constructor(fields: FieldConverters<T, TC>, options?: ObjectConverterOptions<T>);
|
|
320
454
|
/**
|
|
321
|
-
* Constructs a new @
|
|
322
|
-
*
|
|
323
|
-
* @param fields A @
|
|
324
|
-
* @
|
|
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}
|
|
325
461
|
*/
|
|
326
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
|
+
*/
|
|
327
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
|
+
*/
|
|
328
480
|
partial(optional?: (keyof T)[]): ObjectConverter<Partial<T>, TC>;
|
|
329
|
-
|
|
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>;
|
|
330
489
|
}
|
|
331
490
|
/**
|
|
332
|
-
* Helper to
|
|
333
|
-
*
|
|
334
|
-
*
|
|
335
|
-
*
|
|
336
|
-
*
|
|
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.
|
|
337
520
|
*
|
|
338
521
|
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
339
522
|
* fail the conversion.
|
|
340
|
-
* @param
|
|
341
|
-
*
|
|
342
|
-
*
|
|
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.
|
|
343
530
|
*/
|
|
344
|
-
export declare function object<T>(
|
|
531
|
+
export declare function object<T>(properties: FieldConverters<T>, optional: (keyof T)[]): ObjectConverter<T>;
|
|
345
532
|
/**
|
|
346
|
-
*
|
|
347
|
-
*
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
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.
|
|
351
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
|
|
352
563
|
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
353
564
|
* fail the conversion.
|
|
354
565
|
*
|
|
355
566
|
* The conversion fails if any unexpected fields are encountered.
|
|
356
567
|
*
|
|
357
|
-
* @param
|
|
358
|
-
* @param
|
|
359
|
-
*
|
|
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
|
|
360
580
|
*/
|
|
361
|
-
export declare function strictObject<T>(fields: FieldConverters<T>, opt?: (keyof T)[] | Omit<ObjectConverterOptions<T>, 'strict'>): ObjectConverter<T>;
|
|
362
581
|
export declare type DiscriminatedObjectConverters<T, TD extends string = string, TC = unknown> = Record<TD, Converter<T, TC>>;
|
|
363
582
|
/**
|
|
364
|
-
* Helper to
|
|
365
|
-
*
|
|
366
|
-
*
|
|
367
|
-
*
|
|
368
|
-
*
|
|
369
|
-
*
|
|
370
|
-
*
|
|
371
|
-
* 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
|
|
372
597
|
*/
|
|
373
598
|
export declare function discriminatedObject<T, TD extends string = string, TC = unknown>(discriminatorProp: string, converters: DiscriminatedObjectConverters<T, TD>): Converter<T, TC>;
|
|
374
599
|
/**
|
|
375
|
-
* Helper to
|
|
376
|
-
*
|
|
377
|
-
* 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.
|
|
378
602
|
*
|
|
379
|
-
*
|
|
380
|
-
*
|
|
381
|
-
*
|
|
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.
|
|
382
608
|
*
|
|
383
609
|
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
384
610
|
* fail the conversion.
|
|
385
611
|
*
|
|
386
|
-
* @param
|
|
387
|
-
*
|
|
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
|
|
388
617
|
*/
|
|
389
|
-
export declare function transform<T, TC = unknown>(
|
|
618
|
+
export declare function transform<T, TC = unknown>(properties: FieldConverters<T, TC>): Converter<T, TC>;
|
|
390
619
|
/**
|
|
391
|
-
* A helper wrapper to
|
|
392
|
-
*
|
|
393
|
-
* @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
|
|
394
625
|
*/
|
|
395
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
|
+
*/
|
|
396
633
|
export declare function rangeOf<T, TC = unknown>(converter: Converter<T, TC>): Converter<RangeOf<T>, TC>;
|
|
397
634
|
export {};
|
|
635
|
+
//# sourceMappingURL=converters.d.ts.map
|