@fgv/ts-utils 5.0.2 → 5.1.0-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/README.md +152 -84
- package/dist/index.js +2 -2
- package/dist/packlets/base/messageAggregator.js +6 -6
- package/dist/packlets/base/result.js +39 -39
- package/dist/packlets/base/utils.js +47 -0
- package/dist/packlets/collections/aggregatedResultMap.js +722 -0
- package/dist/packlets/collections/collector.js +9 -9
- package/dist/packlets/collections/collectorValidator.js +4 -4
- package/dist/packlets/collections/converters.js +39 -0
- package/dist/packlets/collections/convertingCollectorValidator.js +3 -3
- package/dist/packlets/collections/convertingResultMap.js +208 -0
- package/dist/packlets/collections/index.js +4 -0
- package/dist/packlets/collections/keyValueConverters.js +1 -4
- package/dist/packlets/collections/readOnlyConvertingResultMap.js +193 -0
- package/dist/packlets/collections/resultMapValidator.js +50 -6
- package/dist/packlets/collections/validatingConvertingResultMap.js +78 -0
- package/dist/packlets/conversion/advancedConverters.js +249 -0
- package/dist/packlets/conversion/baseConverter.js +23 -16
- package/dist/packlets/conversion/basicConverters.js +571 -0
- package/dist/packlets/conversion/converters.js +3 -617
- package/dist/packlets/conversion/defaultingConverter.js +21 -15
- package/dist/packlets/logging/bootLogger.js +122 -0
- package/dist/packlets/logging/index.js +1 -0
- package/dist/packlets/logging/logReporter.js +32 -1
- package/dist/packlets/logging/logger.js +25 -5
- package/dist/packlets/validation/classes.js +1 -0
- package/dist/packlets/validation/compositeId.js +60 -0
- package/dist/packlets/validation/field.js +1 -1
- package/dist/packlets/validation/genericValidator.js +24 -10
- package/dist/packlets/validation/object.js +3 -6
- package/dist/packlets/validation/validators.js +12 -0
- package/dist/ts-utils.d.ts +1633 -200
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.js +2 -1
- package/lib/packlets/base/mapResults.d.ts +3 -3
- package/lib/packlets/base/messageAggregator.d.ts +6 -6
- package/lib/packlets/base/messageAggregator.js +6 -6
- package/lib/packlets/base/result.d.ts +74 -51
- package/lib/packlets/base/result.js +39 -39
- package/lib/packlets/base/utils.d.ts +42 -0
- package/lib/packlets/base/utils.js +52 -0
- package/lib/packlets/collections/aggregatedResultMap.d.ts +433 -0
- package/lib/packlets/collections/aggregatedResultMap.js +728 -0
- package/lib/packlets/collections/collector.d.ts +11 -10
- package/lib/packlets/collections/collector.js +9 -9
- package/lib/packlets/collections/collectorValidator.d.ts +12 -10
- package/lib/packlets/collections/collectorValidator.js +4 -4
- package/lib/packlets/collections/converters.d.ts +18 -0
- package/lib/packlets/collections/converters.js +42 -0
- package/lib/packlets/collections/convertingCollector.d.ts +4 -4
- package/lib/packlets/collections/convertingCollectorValidator.d.ts +10 -7
- package/lib/packlets/collections/convertingCollectorValidator.js +3 -3
- package/lib/packlets/collections/convertingResultMap.d.ts +176 -0
- package/lib/packlets/collections/convertingResultMap.js +213 -0
- package/lib/packlets/collections/index.d.ts +4 -0
- package/lib/packlets/collections/index.js +4 -0
- package/lib/packlets/collections/keyValueConverters.d.ts +1 -4
- package/lib/packlets/collections/keyValueConverters.js +1 -4
- package/lib/packlets/collections/readOnlyConvertingResultMap.d.ts +153 -0
- package/lib/packlets/collections/readOnlyConvertingResultMap.js +197 -0
- package/lib/packlets/collections/readonlyResultMap.d.ts +7 -7
- package/lib/packlets/collections/resultMap.d.ts +108 -2
- package/lib/packlets/collections/resultMapValidator.d.ts +45 -11
- package/lib/packlets/collections/resultMapValidator.js +52 -7
- package/lib/packlets/collections/validatingCollector.d.ts +5 -5
- package/lib/packlets/collections/validatingConvertingCollector.d.ts +3 -3
- package/lib/packlets/collections/validatingConvertingResultMap.d.ts +102 -0
- package/lib/packlets/collections/validatingConvertingResultMap.js +83 -0
- package/lib/packlets/collections/validatingResultMap.d.ts +1 -1
- package/lib/packlets/conversion/advancedConverters.d.ts +170 -0
- package/lib/packlets/conversion/advancedConverters.js +258 -0
- package/lib/packlets/conversion/baseConverter.d.ts +39 -23
- package/lib/packlets/conversion/baseConverter.js +23 -16
- package/lib/packlets/conversion/basicConverters.d.ts +470 -0
- package/lib/packlets/conversion/basicConverters.js +595 -0
- package/lib/packlets/conversion/converter.d.ts +7 -0
- package/lib/packlets/conversion/converters.d.ts +2 -535
- package/lib/packlets/conversion/converters.js +17 -639
- package/lib/packlets/conversion/defaultingConverter.d.ts +20 -16
- package/lib/packlets/conversion/defaultingConverter.js +21 -15
- package/lib/packlets/logging/bootLogger.d.ts +84 -0
- package/lib/packlets/logging/bootLogger.js +126 -0
- package/lib/packlets/logging/index.d.ts +1 -0
- package/lib/packlets/logging/index.js +1 -0
- package/lib/packlets/logging/logReporter.d.ts +18 -3
- package/lib/packlets/logging/logReporter.js +31 -0
- package/lib/packlets/logging/logger.d.ts +39 -6
- package/lib/packlets/logging/logger.js +26 -5
- package/lib/packlets/validation/classes.d.ts +1 -0
- package/lib/packlets/validation/classes.js +3 -1
- package/lib/packlets/validation/compositeId.d.ts +29 -0
- package/lib/packlets/validation/compositeId.js +64 -0
- package/lib/packlets/validation/field.d.ts +1 -1
- package/lib/packlets/validation/field.js +1 -1
- package/lib/packlets/validation/genericValidator.d.ts +15 -11
- package/lib/packlets/validation/genericValidator.js +24 -10
- package/lib/packlets/validation/object.d.ts +3 -6
- package/lib/packlets/validation/object.js +3 -6
- package/lib/packlets/validation/traits.d.ts +3 -3
- package/lib/packlets/validation/validator.d.ts +7 -0
- package/lib/packlets/validation/validators.d.ts +10 -0
- package/lib/packlets/validation/validators.js +13 -0
- package/package.json +27 -27
|
@@ -1,536 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { Converter, OnError } from './converter';
|
|
4
|
-
import { FieldConverters, ObjectConverter, ObjectConverterOptions } from './objectConverter';
|
|
5
|
-
import { StringConverter } from './stringConverter';
|
|
6
|
-
/**
|
|
7
|
-
* Action to take on conversion failures (deprecated - use Conversion.OnError)
|
|
8
|
-
* @public
|
|
9
|
-
* @deprecated use Conversion.OnError.
|
|
10
|
-
*/
|
|
11
|
-
export { OnError };
|
|
12
|
-
/**
|
|
13
|
-
* A converter to convert unknown to string. Values of type
|
|
14
|
-
* string succeed. Anything else fails.
|
|
15
|
-
* @public
|
|
16
|
-
*/
|
|
17
|
-
export declare const string: StringConverter;
|
|
18
|
-
/**
|
|
19
|
-
* Helper function to create a {@link Converter | Converter} which converts `unknown` to one of a set of supplied
|
|
20
|
-
* enumerated values. Anything else fails.
|
|
21
|
-
*
|
|
22
|
-
* @remarks
|
|
23
|
-
* Allowed enumerated values can also be supplied as context at conversion time.
|
|
24
|
-
* @param values - Array of allowed values.
|
|
25
|
-
* @returns A new {@link Converter | Converter} returning `<T>`.
|
|
26
|
-
* @public
|
|
27
|
-
*/
|
|
28
|
-
export declare function enumeratedValue<T>(values: ReadonlyArray<T>): Converter<T, ReadonlyArray<T>>;
|
|
29
|
-
/**
|
|
30
|
-
* Helper function to create a {@link Converter | Converter} which converts `unknown` to one of a set of supplied enumerated
|
|
31
|
-
* values, mapping any of multiple supplied values to the enumeration.
|
|
32
|
-
* @remarks
|
|
33
|
-
* Enables mapping of multiple input values to a consistent internal representation (so e.g. `'y'`, `'yes'`,
|
|
34
|
-
* `'true'`, `1` and `true` can all map to boolean `true`)
|
|
35
|
-
* @param map - An array of tuples describing the mapping. The first element of each tuple is the result
|
|
36
|
-
* value, the second is the set of values that map to the result. Tuples are evaluated in the order
|
|
37
|
-
* supplied and are not checked for duplicates.
|
|
38
|
-
* @param message - An optional error message.
|
|
39
|
-
* @returns A {@link Converter | Converter} which applies the mapping and yields `<T>` on success.
|
|
40
|
-
* @public
|
|
41
|
-
*/
|
|
42
|
-
export declare function mappedEnumeratedValue<T, TC = unknown>(map: ReadonlyArray<[T, ReadonlyArray<TC>]>, message?: string): Converter<T, ReadonlyArray<TC>>;
|
|
43
|
-
/**
|
|
44
|
-
* Helper function to create a {@link Converter | Converter} which converts `unknown` to some supplied literal value. Succeeds with
|
|
45
|
-
* the supplied value if an identity comparison succeeds, fails otherwise.
|
|
46
|
-
* @param value - The value to be compared.
|
|
47
|
-
* @returns A {@link Converter | Converter} which returns the supplied value on success.
|
|
48
|
-
* @public
|
|
49
|
-
*/
|
|
50
|
-
export declare function literal<T, TC = unknown>(value: T): Converter<T, TC>;
|
|
51
|
-
/**
|
|
52
|
-
* Deprecated alias for @see literal
|
|
53
|
-
* @param value - The value to be compared.
|
|
54
|
-
* @deprecated Use {@link Converters.literal} instead.
|
|
55
|
-
* @internal
|
|
56
|
-
*/
|
|
57
|
-
export declare const value: typeof literal;
|
|
58
|
-
/**
|
|
59
|
-
* A {@link Converter | Converter} which converts `unknown` to a `number`.
|
|
60
|
-
* @remarks
|
|
61
|
-
* Numbers and strings with a numeric format succeed. Anything else fails.
|
|
62
|
-
* @public
|
|
63
|
-
*/
|
|
64
|
-
export declare const number: Converter<number, unknown>;
|
|
65
|
-
/**
|
|
66
|
-
* A {@link Converter | Converter} which converts `unknown` to `boolean`.
|
|
67
|
-
* @remarks
|
|
68
|
-
* Boolean values or the case-insensitive strings `'true'` and `'false'` succeed.
|
|
69
|
-
* Anything else fails.
|
|
70
|
-
* @public
|
|
71
|
-
*/
|
|
72
|
-
export declare const boolean: Converter<boolean, unknown>;
|
|
73
|
-
/**
|
|
74
|
-
* A {@link Converter | Converter} which converts an optional `string` value. Values of type
|
|
75
|
-
* `string` are returned. Anything else returns {@link Success | Success} with value `undefined`.
|
|
76
|
-
* @public
|
|
77
|
-
*/
|
|
78
|
-
export declare const optionalString: Converter<string | undefined, unknown>;
|
|
79
|
-
/**
|
|
80
|
-
* Helper function to create a {@link Converter | Converter} which converts any `string` into an
|
|
81
|
-
* array of `string`, by separating at a supplied delimiter.
|
|
82
|
-
* @remarks
|
|
83
|
-
* Delimiter may also be supplied as context at conversion time.
|
|
84
|
-
* @param delimiter - The delimiter at which to split.
|
|
85
|
-
* @returns A new {@link Converter | Converter} returning `string[]`.
|
|
86
|
-
* @public
|
|
87
|
-
*/
|
|
88
|
-
export declare function delimitedString(delimiter: string, options?: 'filtered' | 'all'): Converter<string[], string>;
|
|
89
|
-
/**
|
|
90
|
-
* Helper function to create a {@link Converter | Converter} from any {@link Validation.Validator}
|
|
91
|
-
* @param validator - the validator to be wrapped
|
|
92
|
-
* @returns A {@link Converter | Converter} which uses the supplied validator.
|
|
93
|
-
* @public
|
|
94
|
-
*/
|
|
95
|
-
export declare function validated<T, TC = unknown>(validator: Validator<T, TC>): Converter<T, TC>;
|
|
96
|
-
/**
|
|
97
|
-
* Helper function to create a {@link Converter | Converter} from a supplied {@link Conversion.ConverterFunc | ConverterFunc}.
|
|
98
|
-
* @param convert - the function to be wrapped
|
|
99
|
-
* @returns A {@link Converter | Converter} which uses the supplied function.
|
|
100
|
-
* @public
|
|
101
|
-
*/
|
|
102
|
-
export declare function generic<T, TC = unknown>(convert: ConverterFunc<T, TC>): Converter<T, TC>;
|
|
103
|
-
/**
|
|
104
|
-
* Helper function to create a {@link Converter | Converter} from a supplied type guard function.
|
|
105
|
-
* @param description - a description of the thing to be validated for use in error messages
|
|
106
|
-
* @param guard - a {@link Validation.TypeGuardWithContext} which performs the validation.
|
|
107
|
-
* @returns A new {@link Converter | Converter} which validates the values using the supplied type guard
|
|
108
|
-
* and returns them in place.
|
|
109
|
-
* @public
|
|
110
|
-
*/
|
|
111
|
-
export declare function isA<T, TC = unknown>(description: string, guard: TypeGuardWithContext<T, TC>): Converter<T, TC>;
|
|
112
|
-
/**
|
|
113
|
-
* A {@link Converter | Converter} which converts an optional `number` value.
|
|
114
|
-
* @remarks
|
|
115
|
-
* Values of type `number` or numeric strings are converted and returned.
|
|
116
|
-
* Anything else returns {@link Success | Success} with value `undefined`.
|
|
117
|
-
* @public
|
|
118
|
-
*/
|
|
119
|
-
export declare const optionalNumber: Converter<number | undefined, unknown>;
|
|
120
|
-
/**
|
|
121
|
-
* A {@link Converter | Converter} to convert an optional `boolean` value.
|
|
122
|
-
* @remarks
|
|
123
|
-
* Values of type `boolean` or strings that match (case-insensitive) `'true'`
|
|
124
|
-
* or `'false'` are converted and returned. Anything else returns {@link Success | Success}
|
|
125
|
-
* with value `undefined`.
|
|
126
|
-
* @public
|
|
127
|
-
*/
|
|
128
|
-
export declare const optionalBoolean: Converter<boolean | undefined, unknown>;
|
|
129
|
-
/**
|
|
130
|
-
* A helper function to create a {@link Converter | Converter} for polymorphic values.
|
|
131
|
-
* Returns a converter which invokes the wrapped converters in sequence, returning the
|
|
132
|
-
* first successful result. Returns an error if none of the supplied converters can
|
|
133
|
-
* convert the value.
|
|
134
|
-
* @remarks
|
|
135
|
-
* If `onError` is `ignoreErrors` (default), then errors from any of the
|
|
136
|
-
* converters are ignored provided that some converter succeeds. If
|
|
137
|
-
* onError is `failOnError`, then an error from any converter fails the entire
|
|
138
|
-
* conversion.
|
|
139
|
-
*
|
|
140
|
-
* @param converters - An ordered list of {@link Converter | converters} or {@link Validator | validators}
|
|
141
|
-
* to be considered.
|
|
142
|
-
* @param onError - Specifies treatment of unconvertible elements.
|
|
143
|
-
* @returns A new {@link Converter | Converter} which yields a value from the union of the types returned
|
|
144
|
-
* by the wrapped converters.
|
|
145
|
-
* @public
|
|
146
|
-
*/
|
|
147
|
-
export declare function oneOf<T, TC = unknown>(converters: Array<Converter<T, TC> | Validator<T, TC>>, onError?: OnError): Converter<T, TC>;
|
|
148
|
-
/**
|
|
149
|
-
* A helper function to create a {@link Converter | Converter} which converts `unknown` to an array of `<T>`.
|
|
150
|
-
* @remarks
|
|
151
|
-
* If `onError` is `'failOnError'` (default), then the entire conversion fails if any element cannot
|
|
152
|
-
* be converted. If `onError` is `'ignoreErrors'`, then failing elements are silently ignored.
|
|
153
|
-
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} used to convert each
|
|
154
|
-
* item in the array.
|
|
155
|
-
* @param ignoreErrors - Specifies treatment of unconvertible elements.
|
|
156
|
-
* @returns A {@link Converter | Converter} which returns an array of `<T>`.
|
|
157
|
-
* @public
|
|
158
|
-
*/
|
|
159
|
-
export declare function arrayOf<T, TC = unknown>(converter: Converter<T, TC> | Validator<T, TC>, onError?: OnError): Converter<T[], TC>;
|
|
160
|
-
/**
|
|
161
|
-
* {@link Converter | Converter} to convert an `unknown` to an array of `string`.
|
|
162
|
-
* @remarks
|
|
163
|
-
* Returns {@link Success | Success} with the the supplied value if it as an array
|
|
164
|
-
* of strings, returns {@link Failure | Failure} with an error message otherwise.
|
|
165
|
-
* @public
|
|
166
|
-
*/
|
|
167
|
-
export declare const stringArray: Converter<string[], unknown>;
|
|
168
|
-
/**
|
|
169
|
-
* {@link Converter | Converter} to convert an `unknown` to an array of `number`.
|
|
170
|
-
* @remarks
|
|
171
|
-
* Returns {@link Success | Success} with the the supplied value if it as an array
|
|
172
|
-
* of numbers, returns {@link Failure | Failure} with an error message otherwise.
|
|
173
|
-
* @public
|
|
174
|
-
*/
|
|
175
|
-
export declare const numberArray: Converter<number[], unknown>;
|
|
176
|
-
/**
|
|
177
|
-
* Options for {@link Converters.(recordOf:3) | Converters.recordOf} and
|
|
178
|
-
* {@link Converters.(mapOf:3) | Converters.mapOf}
|
|
179
|
-
* helper functions.
|
|
180
|
-
* @public
|
|
181
|
-
*/
|
|
182
|
-
export interface KeyedConverterOptions<T extends string = string, TC = unknown> {
|
|
183
|
-
/**
|
|
184
|
-
* if `onError` is `'fail'` (default), then the entire conversion fails if any key or element
|
|
185
|
-
* cannot be converted. If `onError` is `'ignore'`, failing elements are silently ignored.
|
|
186
|
-
*/
|
|
187
|
-
onError?: 'fail' | 'ignore';
|
|
188
|
-
/**
|
|
189
|
-
* If present, `keyConverter` is used to convert the source object property names to
|
|
190
|
-
* keys in the resulting map or record.
|
|
191
|
-
* @remarks
|
|
192
|
-
* Can be used to coerce key names to supported values and/or strong types.
|
|
193
|
-
*/
|
|
194
|
-
keyConverter?: Converter<T, TC> | Validator<T, TC>;
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* A helper function to create a {@link Converter | Converter} which converts the `string`-keyed
|
|
198
|
-
* properties using a supplied {@link Converter | Converter<T, TC>} or {@link Validator | Validator<T>} to
|
|
199
|
-
* produce a `Record<string, T>`.
|
|
200
|
-
* @remarks
|
|
201
|
-
* The resulting converter fails conversion if any element cannot be converted.
|
|
202
|
-
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} used for each
|
|
203
|
-
* item in the source object.
|
|
204
|
-
* @returns A {@link Converter | Converter} which returns `Record<string, T>`.
|
|
205
|
-
* {@label WITH_DEFAULT}
|
|
206
|
-
* @public
|
|
207
|
-
*/
|
|
208
|
-
export declare function recordOf<T, TC = unknown, TK extends string = string>(converter: Converter<T, TC> | Validator<T, TC>): Converter<Record<TK, T>, TC>;
|
|
209
|
-
/**
|
|
210
|
-
* A helper function to create a {@link Converter | Converter} which converts the `string`-keyed properties
|
|
211
|
-
* using a supplied {@link Converter | Converter<T, TC>} or {@link Validator | Validator<T>} to produce a
|
|
212
|
-
* `Record<string, T>` and optionally specified handling of elements that cannot be converted.
|
|
213
|
-
* @remarks
|
|
214
|
-
* if `onError` is `'fail'` (default), then the entire conversion fails if any key or element
|
|
215
|
-
* cannot be converted. If `onError` is `'ignore'`, failing elements are silently ignored.
|
|
216
|
-
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} for each item in
|
|
217
|
-
* the source object.
|
|
218
|
-
* @returns A {@link Converter | Converter} which returns `Record<string, T>`.
|
|
219
|
-
* {@label WITH_ON_ERROR}
|
|
220
|
-
* @public
|
|
221
|
-
*/
|
|
222
|
-
export declare function recordOf<T, TC = unknown, TK extends string = string>(converter: Converter<T, TC> | Validator<T, TC>, onError: 'fail' | 'ignore'): Converter<Record<TK, T>, TC>;
|
|
223
|
-
/**
|
|
224
|
-
* A helper function to create a {@link Converter | Converter} or which converts the `string`-keyed properties
|
|
225
|
-
* using a supplied {@link Converter | Converter<T, TC>} or {@link Validator | Validator<T>} to produce a
|
|
226
|
-
* `Record<TK, T>`.
|
|
227
|
-
* @remarks
|
|
228
|
-
* If present, the supplied {@link Converters.KeyedConverterOptions | options} can provide a strongly-typed
|
|
229
|
-
* converter for keys and/or control the handling of elements that fail conversion.
|
|
230
|
-
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} used for each item in the source object.
|
|
231
|
-
* @param options - Optional {@link Converters.KeyedConverterOptions | KeyedConverterOptions<TK, TC>} which
|
|
232
|
-
* supplies a key converter and/or error-handling options.
|
|
233
|
-
* @returns A {@link Converter | Converter} which returns `Record<TK, T>`.
|
|
234
|
-
* {@label WITH_OPTIONS}
|
|
235
|
-
* @public
|
|
236
|
-
*/
|
|
237
|
-
export declare function recordOf<T, TC = unknown, TK extends string = string>(converter: Converter<T, TC> | Validator<T, TC>, options: KeyedConverterOptions<TK, TC>): Converter<Record<TK, T>, TC>;
|
|
238
|
-
/**
|
|
239
|
-
* A helper function to create a {@link Converter | Converter} which converts the `string`-keyed properties
|
|
240
|
-
* using a supplied {@link Converter | Converter<T, TC>} or {@link Validator | Validator<T>} to produce a
|
|
241
|
-
* `Map<string, T>`.
|
|
242
|
-
* @remarks
|
|
243
|
-
* The resulting converter fails conversion if any element cannot be converted.
|
|
244
|
-
* @param converter - {@link Converter | Converter} | {@link Validator | Validator} used for each item in
|
|
245
|
-
* the source object.
|
|
246
|
-
* @returns A {@link Converter | Converter} which returns `Map<string, T>`.
|
|
247
|
-
* {@label WITH_DEFAULT}
|
|
248
|
-
* @public
|
|
249
|
-
*/
|
|
250
|
-
export declare function mapOf<T, TC = unknown, TK extends string = string>(converter: Converter<T, TC> | Validator<T, TC>): Converter<Map<TK, T>, TC>;
|
|
251
|
-
/**
|
|
252
|
-
* A helper function to create a {@link Converter | Converter} which converts the `string`-keyed properties
|
|
253
|
-
* using a supplied {@link Converter | Converter<T, TC>} or {@link Validator | Validator<T>} to produce a
|
|
254
|
-
* `Map<string, T>` and specified handling of elements that cannot be converted.
|
|
255
|
-
* @remarks
|
|
256
|
-
* if `onError` is `'fail'` (default), then the entire conversion fails if any key or element
|
|
257
|
-
* cannot be converted. If `onError` is `'ignore'`, failing elements are silently ignored.
|
|
258
|
-
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} used for
|
|
259
|
-
* each item in the source object.
|
|
260
|
-
* @returns A {@link Converter | Converter} which returns `Map<string, T>`.
|
|
261
|
-
* {@label WITH_ON_ERROR}
|
|
262
|
-
* @public
|
|
263
|
-
*/
|
|
264
|
-
export declare function mapOf<T, TC = unknown, TK extends string = string>(converter: Converter<T, TC> | Validator<T, TC>, onError: 'fail' | 'ignore'): Converter<Map<TK, T>, TC>;
|
|
265
|
-
/**
|
|
266
|
-
* A helper function to create a {@link Converter | Converter} which converts the `string`-keyed properties
|
|
267
|
-
* using a supplied {@link Converter | Converter<T, TC>} or {@link Validator | Validator<T>} to produce
|
|
268
|
-
* a `Map<TK,T>`.
|
|
269
|
-
* @remarks
|
|
270
|
-
* If present, the supplied {@link Converters.KeyedConverterOptions | options} can provide a strongly-typed
|
|
271
|
-
* converter for keys and/or control the handling of elements that fail conversion.
|
|
272
|
-
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} used for each item
|
|
273
|
-
* in the source object.
|
|
274
|
-
* @param options - Optional {@link Converters.KeyedConverterOptions | KeyedConverterOptions<TK, TC>} which
|
|
275
|
-
* supplies a key converter and/or error-handling options.
|
|
276
|
-
* @returns A {@link Converter | Converter} which returns `Map<TK,T>`.
|
|
277
|
-
* {@label WITH_OPTIONS}
|
|
278
|
-
* @public
|
|
279
|
-
*/
|
|
280
|
-
export declare function mapOf<T, TC = unknown, TK extends string = string>(converter: Converter<T, TC> | Validator<T, TC>, options: KeyedConverterOptions<TK, TC>): Converter<Map<TK, T>, TC>;
|
|
281
|
-
/**
|
|
282
|
-
* Helper function to create a {@link Converter | Converter} which validates that a supplied value is
|
|
283
|
-
* of a type validated by a supplied validator function and returns it.
|
|
284
|
-
* @remarks
|
|
285
|
-
* If `validator` succeeds, this {@link Converter | Converter} returns {@link Success | Success} with the supplied
|
|
286
|
-
* value of `from` coerced to type `<T>`. Returns a {@link Failure | Failure} with additional
|
|
287
|
-
* information otherwise.
|
|
288
|
-
* @param validator - A validator function to determine if the converted value is valid.
|
|
289
|
-
* @param description - A description of the validated type for use in error messages.
|
|
290
|
-
* @returns A new {@link Converter | Converter<T, TC>} which applies the supplied validation.
|
|
291
|
-
* @public
|
|
292
|
-
*/
|
|
293
|
-
export declare function validateWith<T, TC = unknown>(validator: (from: unknown) => from is T, description?: string): Converter<T, TC>;
|
|
294
|
-
/**
|
|
295
|
-
* A helper function to create a {@link Converter | Converter} which extracts and converts an element from an array.
|
|
296
|
-
* @remarks
|
|
297
|
-
* The returned {@link Converter | Converter} returns {@link Success | Success} with the converted value if the element exists
|
|
298
|
-
* in the supplied array and can be converted. Returns {@link Failure | Failure} with an error message otherwise.
|
|
299
|
-
* @param index - The index of the element to be extracted.
|
|
300
|
-
* @param converter - A {@link Converter | Converter} or {@link Validator | Validator} for the extracted element.
|
|
301
|
-
* @returns A {@link Converter | Converter<T, TC>} which extracts the specified element from an array.
|
|
302
|
-
* @public
|
|
303
|
-
*/
|
|
304
|
-
export declare function element<T, TC = unknown>(index: number, converter: Converter<T, TC> | Validator<T, TC>): Converter<T, TC>;
|
|
305
|
-
/**
|
|
306
|
-
* A helper function to create a {@link Converter | Converter} which extracts and converts an optional element from an array.
|
|
307
|
-
* @remarks
|
|
308
|
-
* The resulting {@link Converter | Converter} returns {@link Success | Success} with the converted value if the element exists
|
|
309
|
-
* in the supplied array and can be converted. Returns {@link Success | Success} with value `undefined` if the parameter
|
|
310
|
-
* is an array but the index is out of range. Returns {@link Failure | Failure} with a message if the supplied parameter
|
|
311
|
-
* is not an array, if the requested index is negative, or if the element cannot be converted.
|
|
312
|
-
* @param index - The index of the element to be extracted.
|
|
313
|
-
* @param converter - A {@link Converter | Converter} or {@link Validator | Validator} used for the extracted element.
|
|
314
|
-
* @returns A {@link Converter | Converter<T, TC>} which extracts the specified element from an array.
|
|
315
|
-
* @public
|
|
316
|
-
*/
|
|
317
|
-
export declare function optionalElement<T, TC = unknown>(index: number, converter: Converter<T, TC> | Validator<T, TC>): Converter<T | undefined, TC>;
|
|
318
|
-
/**
|
|
319
|
-
* A helper function to create a {@link Converter | Converter} which extracts and convert a property specified
|
|
320
|
-
* by name from an object.
|
|
321
|
-
* @remarks
|
|
322
|
-
* The resulting {@link Converter | Converter} returns {@link Success | Success} with the converted value of the corresponding
|
|
323
|
-
* object property if the field exists and can be converted. Returns {@link Failure | Failure} with an error message
|
|
324
|
-
* otherwise.
|
|
325
|
-
* @param name - The name of the field to be extracted.
|
|
326
|
-
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} to use for the extracted
|
|
327
|
-
* field.
|
|
328
|
-
* @public
|
|
329
|
-
*/
|
|
330
|
-
export declare function field<T, TC = unknown>(name: string, converter: Converter<T, TC> | Validator<T, TC>): Converter<T, TC>;
|
|
331
|
-
/**
|
|
332
|
-
* A helper function to create a {@link Converter | Converter} which extracts and convert a property specified
|
|
333
|
-
* by name from an object.
|
|
334
|
-
* @remarks
|
|
335
|
-
* The resulting {@link Converter | Converter} returns {@link Success | Success} with the converted value of
|
|
336
|
-
* the corresponding object property if the field exists and can be converted. Returns {@link Success | Success}
|
|
337
|
-
* with `undefined` if the supplied parameter is an object but the named field is not present.
|
|
338
|
-
* Returns {@link Failure | Failure} with an error message otherwise.
|
|
339
|
-
* @param name - The name of the field to be extracted.
|
|
340
|
-
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} to use for the extracted field.
|
|
341
|
-
* @public
|
|
342
|
-
*/
|
|
343
|
-
export declare function optionalField<T, TC = unknown>(name: string, converter: Converter<T, TC> | Validator<T, TC>): Converter<T | undefined, TC>;
|
|
344
|
-
/**
|
|
345
|
-
* Helper function to create a {@link Conversion.ObjectConverter | ObjectConverter<T, TC>} which converts an object
|
|
346
|
-
* without changing shape, given a {@link Conversion.FieldConverters | FieldConverters<T, TC>} and an optional
|
|
347
|
-
* {@link Conversion.ObjectConverterOptions | ObjectConverterOptions<T>} to further refine conversion behavior.
|
|
348
|
-
* @remarks
|
|
349
|
-
* By default, if all of the requested fields exist and can be converted, returns {@link Success | Success}
|
|
350
|
-
* with a new object that contains the converted values under the original key names. If any required properties
|
|
351
|
-
* do not exist or cannot be converted, the entire conversion fails, returning {@link Failure | Failure} with additional
|
|
352
|
-
* error information.
|
|
353
|
-
*
|
|
354
|
-
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
355
|
-
* fail the conversion.
|
|
356
|
-
* @param properties - An {@link Conversion.FieldConverters | FieldConverters<T, TC>} defining the shape of the
|
|
357
|
-
* source object and {@link Converter | converters} to be applied to each properties.
|
|
358
|
-
* @param options - An {@link Conversion.ObjectConverterOptions | ObjectConverterOptions<T>} containing options
|
|
359
|
-
* for the object converter.
|
|
360
|
-
* @returns A new {@link Conversion.ObjectConverter | ObjectConverter} which applies the specified conversions.
|
|
361
|
-
* {@label WITH_OPTIONS}
|
|
362
|
-
* @public
|
|
363
|
-
*/
|
|
364
|
-
export declare function object<T, TC = unknown>(properties: FieldConverters<T, TC>, options?: ObjectConverterOptions<T>): ObjectConverter<T, TC>;
|
|
365
|
-
/**
|
|
366
|
-
* Helper function to create a {@link Conversion.ObjectConverter | ObjectConverter<T, TC>} which converts an object
|
|
367
|
-
* without changing shape, given a {@link Conversion.FieldConverters | FieldConverters<T, TC>} and a set of
|
|
368
|
-
* optional properties.
|
|
369
|
-
* @remarks
|
|
370
|
-
* By default, if all of the requested fields exist and can be converted, returns {@link Success | Success}
|
|
371
|
-
* with a new object that contains the converted values under the original key names. If any required properties
|
|
372
|
-
* do not exist or cannot be converted, the entire conversion fails, returning {@link Failure | Failure} with additional
|
|
373
|
-
* error information.
|
|
374
|
-
*
|
|
375
|
-
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
376
|
-
* fail the conversion.
|
|
377
|
-
* @param properties - An {@link Conversion.FieldConverters | FieldConverters<T, TC>} defining the shape of the
|
|
378
|
-
* source object and {@link Converter | converters} to be applied to each properties.
|
|
379
|
-
* @param optional - An array of `(keyof T)` listing the keys to be considered optional.
|
|
380
|
-
* {@label WITH_KEYS}
|
|
381
|
-
* @returns A new {@link Conversion.ObjectConverter | ObjectConverter} which applies the specified conversions.
|
|
382
|
-
* @public
|
|
383
|
-
* @deprecated Use {@link Converters.(object:1) | Converters.object(fields, options)} instead.
|
|
384
|
-
*/
|
|
385
|
-
export declare function object<T, TC = unknown>(properties: FieldConverters<T, TC>, optional: (keyof T)[]): ObjectConverter<T, TC>;
|
|
386
|
-
/**
|
|
387
|
-
* Options for the {@link Converters.(strictObject:1)} helper function.
|
|
388
|
-
* @public
|
|
389
|
-
*/
|
|
390
|
-
export type StrictObjectConverterOptions<T> = Omit<ObjectConverterOptions<T>, 'strict'>;
|
|
391
|
-
/**
|
|
392
|
-
* Helper function to create a {@link Conversion.ObjectConverter | ObjectConverter} which converts an object
|
|
393
|
-
* without changing shape, a {@link Conversion.FieldConverters | FieldConverters<T, TC>} and an optional
|
|
394
|
-
* {@link Converters.StrictObjectConverterOptions | StrictObjectConverterOptions<T>} to further refine
|
|
395
|
-
* conversion behavior.
|
|
396
|
-
*
|
|
397
|
-
* @remarks
|
|
398
|
-
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
399
|
-
* fail the conversion.
|
|
400
|
-
*
|
|
401
|
-
* The conversion fails if any unexpected fields are encountered.
|
|
402
|
-
*
|
|
403
|
-
* @param properties - An object containing defining the shape and converters to be applied.
|
|
404
|
-
* @param options - An optional @see StrictObjectConverterOptions<T> containing options for the object converter.
|
|
405
|
-
* @returns A new {@link Conversion.ObjectConverter | ObjectConverter} which applies the specified conversions.
|
|
406
|
-
* {@label WITH_OPTIONS}
|
|
407
|
-
* @public
|
|
408
|
-
*/
|
|
409
|
-
export declare function strictObject<T, TC = unknown>(properties: FieldConverters<T, TC>, options?: StrictObjectConverterOptions<T>): ObjectConverter<T, TC>;
|
|
410
|
-
/**
|
|
411
|
-
* Helper function to create a {@link Conversion.ObjectConverter | ObjectConverter} which converts an object
|
|
412
|
-
* without changing shape, a {@link Conversion.FieldConverters | FieldConverters<T, TC>} and an optional
|
|
413
|
-
* {@link Converters.StrictObjectConverterOptions | StrictObjectConverterOptions<T>} to further refine
|
|
414
|
-
* conversion behavior.
|
|
415
|
-
*
|
|
416
|
-
* @remarks
|
|
417
|
-
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
418
|
-
* fail the conversion.
|
|
419
|
-
*
|
|
420
|
-
* The conversion fails if any unexpected fields are encountered.
|
|
421
|
-
*
|
|
422
|
-
* @param properties - An object containing defining the shape and converters to be applied.
|
|
423
|
-
* @param optional - An array of `keyof T` containing keys to be considered optional.
|
|
424
|
-
* @returns A new {@link Conversion.ObjectConverter | ObjectConverter} which applies the specified conversions.
|
|
425
|
-
* {@label WITH_KEYS}
|
|
426
|
-
* @deprecated Use {@link Converters.(strictObject:1) | Converters.strictObject(options)} instead.
|
|
427
|
-
* @public
|
|
428
|
-
*/
|
|
429
|
-
export declare function strictObject<T, TC = unknown>(properties: FieldConverters<T, TC>, optional: (keyof T)[]): ObjectConverter<T, TC>;
|
|
430
|
-
/**
|
|
431
|
-
* A string-keyed `Record<string, Converter>` which maps specific {@link Converter | converters} or
|
|
432
|
-
* {@link Validator | Validators} to the value of a discriminator property.
|
|
433
|
-
* @public
|
|
434
|
-
*/
|
|
435
|
-
export type DiscriminatedObjectConverters<T, TD extends string = string, TC = unknown> = Record<TD, Converter<T, TC> | Validator<T, TC>>;
|
|
436
|
-
/**
|
|
437
|
-
* Helper to create a {@link Converter | Converter} which converts a discriminated object without changing shape.
|
|
438
|
-
* @remarks
|
|
439
|
-
* Takes the name of the discriminator property and a
|
|
440
|
-
* {@link Converters.DiscriminatedObjectConverters | string-keyed Record of converters and validators}. During conversion,
|
|
441
|
-
* the resulting {@link Converter | Converter} invokes the converter from `converters` that corresponds to the value of
|
|
442
|
-
* the discriminator property in the source object.
|
|
443
|
-
*
|
|
444
|
-
* If the source is not an object, the discriminator property is missing, or the discriminator has
|
|
445
|
-
* a value not present in the converters, conversion fails and returns {@link Failure | Failure} with more information.
|
|
446
|
-
* @param discriminatorProp - Name of the property used to discriminate types.
|
|
447
|
-
* @param converters - {@link Converters.DiscriminatedObjectConverters | String-keyed record of converters and validators}
|
|
448
|
-
* to invoke, where each key corresponds to a value of the discriminator property.
|
|
449
|
-
* @returns A {@link Converter | Converter} which converts the corresponding discriminated object.
|
|
450
|
-
* @public
|
|
451
|
-
*/
|
|
452
|
-
export declare function discriminatedObject<T, TD extends string = string, TC = unknown>(discriminatorProp: string, converters: DiscriminatedObjectConverters<T, TD>): Converter<T, TC>;
|
|
453
|
-
/**
|
|
454
|
-
* Helper to create a {@link Converter | Converter} which converts a source object to a new object with a
|
|
455
|
-
* different shape.
|
|
456
|
-
*
|
|
457
|
-
* @remarks
|
|
458
|
-
* On successful conversion, the resulting {@link Converter | Converter} returns {@link Success | Success} with a new
|
|
459
|
-
* object, which contains the converted values under the key names specified at initialization time.
|
|
460
|
-
* It returns {@link Failure | Failure} with an error message if any fields to be extracted do not exist
|
|
461
|
-
* or cannot be converted.
|
|
462
|
-
*
|
|
463
|
-
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
464
|
-
* fail the conversion.
|
|
465
|
-
*
|
|
466
|
-
* @param properties - An object with key names that correspond to the target object and an
|
|
467
|
-
* appropriate {@link Conversion.FieldConverters | FieldConverter} which extracts and converts
|
|
468
|
-
* a single filed from the source object.
|
|
469
|
-
* @returns A {@link Converter | Converter} with the specified conversion behavior.
|
|
470
|
-
* @public
|
|
471
|
-
*/
|
|
472
|
-
export declare function transform<T, TC = unknown>(properties: FieldConverters<T, TC>): Converter<T, TC>;
|
|
473
|
-
/**
|
|
474
|
-
* Per-property converters and configuration for each field in the destination object of
|
|
475
|
-
* a {@link Converters.transformObject} call.
|
|
476
|
-
* @public
|
|
477
|
-
*/
|
|
478
|
-
export type FieldTransformers<TSRC, TDEST, TC = unknown> = {
|
|
479
|
-
[key in keyof TDEST]: {
|
|
480
|
-
/**
|
|
481
|
-
* The name of the property in the source object to be converted.
|
|
482
|
-
*/
|
|
483
|
-
from: keyof TSRC;
|
|
484
|
-
/**
|
|
485
|
-
* The converter or validator used to convert the property.
|
|
486
|
-
*/
|
|
487
|
-
converter: Converter<TDEST[key], TC> | Validator<TDEST[key], TC>;
|
|
488
|
-
/**
|
|
489
|
-
* If `true` then a missing source property is ignored. If `false` or omitted
|
|
490
|
-
* then a missing source property causes an error.
|
|
491
|
-
*/
|
|
492
|
-
optional?: boolean;
|
|
493
|
-
};
|
|
494
|
-
};
|
|
495
|
-
/**
|
|
496
|
-
* Options for a {@link Converters.transformObject} call.
|
|
497
|
-
* @public
|
|
498
|
-
*/
|
|
499
|
-
export interface TransformObjectOptions<TSRC> {
|
|
500
|
-
/**
|
|
501
|
-
* If `strict` is `true` then unused properties in the source object cause
|
|
502
|
-
* an error, otherwise they are ignored.
|
|
503
|
-
*/
|
|
504
|
-
strict: true;
|
|
505
|
-
/**
|
|
506
|
-
* An optional list of source properties to be ignored when strict mode
|
|
507
|
-
* is enabled.
|
|
508
|
-
*/
|
|
509
|
-
ignore?: (keyof TSRC)[];
|
|
510
|
-
/**
|
|
511
|
-
* An optional description of this transform to be used for error messages.
|
|
512
|
-
*/
|
|
513
|
-
description?: string;
|
|
514
|
-
}
|
|
515
|
-
/**
|
|
516
|
-
* Helper to create a strongly-typed {@link Converter | Converter} which converts a source object to a
|
|
517
|
-
* new object with a different shape.
|
|
518
|
-
*
|
|
519
|
-
* @remarks
|
|
520
|
-
* On successful conversion, the resulting {@link Converter | Converter} returns {@link Success | Success} with a new
|
|
521
|
-
* object, which contains the converted values under the key names specified at initialization time.
|
|
522
|
-
*
|
|
523
|
-
* It returns {@link Failure | Failure} with an error message if any fields to be extracted do not exist
|
|
524
|
-
* or cannot be converted.
|
|
525
|
-
*
|
|
526
|
-
* @param destinationFields - An object with key names that correspond to the target object and an
|
|
527
|
-
* appropriate {@link Converters.FieldTransformers | FieldTransformers} which specifies the name
|
|
528
|
-
* of the corresponding property in the source object, the converter or validator used for each source
|
|
529
|
-
* property and any other configuration to guide the conversion.
|
|
530
|
-
* @param options - Options which affect the transformation.
|
|
531
|
-
*
|
|
532
|
-
* @returns A {@link Converter | Converter} with the specified conversion behavior.
|
|
533
|
-
* @public
|
|
534
|
-
*/
|
|
535
|
-
export declare function transformObject<TSRC, TDEST, TC = unknown>(destinationFields: FieldTransformers<TSRC, TDEST, TC>, options?: TransformObjectOptions<TSRC>): Converter<TDEST, TC>;
|
|
1
|
+
export * from './basicConverters';
|
|
2
|
+
export * from './advancedConverters';
|
|
536
3
|
//# sourceMappingURL=converters.d.ts.map
|