@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
|
@@ -47,25 +47,25 @@ class BaseConverter {
|
|
|
47
47
|
this._brand = traits === null || traits === void 0 ? void 0 : traits.brand;
|
|
48
48
|
}
|
|
49
49
|
/**
|
|
50
|
-
* {@
|
|
50
|
+
* {@inheritDoc Converter.isOptional}
|
|
51
51
|
*/
|
|
52
52
|
get isOptional() {
|
|
53
53
|
return this._isOptional;
|
|
54
54
|
}
|
|
55
55
|
/**
|
|
56
|
-
* {@
|
|
56
|
+
* {@inheritDoc Converter.brand}
|
|
57
57
|
*/
|
|
58
58
|
get brand() {
|
|
59
59
|
return this._brand;
|
|
60
60
|
}
|
|
61
61
|
/**
|
|
62
|
-
* {@
|
|
62
|
+
* {@inheritDoc Converter.convert}
|
|
63
63
|
*/
|
|
64
64
|
convert(from, context) {
|
|
65
65
|
return this._converter(from, this, context !== null && context !== void 0 ? context : this._defaultContext);
|
|
66
66
|
}
|
|
67
67
|
/**
|
|
68
|
-
* {@
|
|
68
|
+
* {@inheritDoc Converter.convertOptional}
|
|
69
69
|
*/
|
|
70
70
|
convertOptional(from, context, onError) {
|
|
71
71
|
const result = this._converter(from, this, this._context(context));
|
|
@@ -76,7 +76,7 @@ class BaseConverter {
|
|
|
76
76
|
return result;
|
|
77
77
|
}
|
|
78
78
|
/**
|
|
79
|
-
* {@
|
|
79
|
+
* {@inheritDoc Converter.optional}
|
|
80
80
|
*/
|
|
81
81
|
optional(onError) {
|
|
82
82
|
return new BaseConverter((from, __self, context) => {
|
|
@@ -85,7 +85,7 @@ class BaseConverter {
|
|
|
85
85
|
})._with(this._traits({ isOptional: true }));
|
|
86
86
|
}
|
|
87
87
|
/**
|
|
88
|
-
* {@
|
|
88
|
+
* {@inheritDoc Converter.map}
|
|
89
89
|
*/
|
|
90
90
|
map(mapper) {
|
|
91
91
|
return new BaseConverter((from, __self, context) => {
|
|
@@ -97,7 +97,7 @@ class BaseConverter {
|
|
|
97
97
|
})._with(this._traits());
|
|
98
98
|
}
|
|
99
99
|
/**
|
|
100
|
-
* {@
|
|
100
|
+
* {@inheritDoc Converter.mapConvert}
|
|
101
101
|
*/
|
|
102
102
|
mapConvert(mapConverter) {
|
|
103
103
|
return new BaseConverter((from, __self, context) => {
|
|
@@ -109,7 +109,7 @@ class BaseConverter {
|
|
|
109
109
|
})._with(this._traits());
|
|
110
110
|
}
|
|
111
111
|
/**
|
|
112
|
-
* {@
|
|
112
|
+
* {@inheritDoc Converter.mapItems}
|
|
113
113
|
*/
|
|
114
114
|
mapItems(mapper) {
|
|
115
115
|
return new BaseConverter((from, __self, context) => {
|
|
@@ -122,7 +122,7 @@ class BaseConverter {
|
|
|
122
122
|
});
|
|
123
123
|
}
|
|
124
124
|
/**
|
|
125
|
-
* {@
|
|
125
|
+
* {@inheritDoc Converter.mapConvertItems}
|
|
126
126
|
*/
|
|
127
127
|
mapConvertItems(mapConverter) {
|
|
128
128
|
return new BaseConverter((from, __self, context) => {
|
|
@@ -135,7 +135,7 @@ class BaseConverter {
|
|
|
135
135
|
});
|
|
136
136
|
}
|
|
137
137
|
/**
|
|
138
|
-
* {@
|
|
138
|
+
* {@inheritDoc Converter.withAction}
|
|
139
139
|
*/
|
|
140
140
|
withAction(action) {
|
|
141
141
|
return new BaseConverter((from, __self, context) => {
|
|
@@ -143,7 +143,7 @@ class BaseConverter {
|
|
|
143
143
|
})._with(this._traits());
|
|
144
144
|
}
|
|
145
145
|
/**
|
|
146
|
-
* {@
|
|
146
|
+
* {@inheritDoc Converter.withTypeGuard}
|
|
147
147
|
*/
|
|
148
148
|
withTypeGuard(guard, message) {
|
|
149
149
|
return new BaseConverter((from, __self, context) => {
|
|
@@ -156,7 +156,7 @@ class BaseConverter {
|
|
|
156
156
|
})._with(this._traits());
|
|
157
157
|
}
|
|
158
158
|
/**
|
|
159
|
-
* {@
|
|
159
|
+
* {@inheritDoc Converter.withItemTypeGuard}
|
|
160
160
|
*/
|
|
161
161
|
withItemTypeGuard(guard, message) {
|
|
162
162
|
return new BaseConverter((from, __self, context) => {
|
|
@@ -174,7 +174,7 @@ class BaseConverter {
|
|
|
174
174
|
})._with(this._traits());
|
|
175
175
|
}
|
|
176
176
|
/**
|
|
177
|
-
* {@
|
|
177
|
+
* {@inheritDoc Converter.withConstraint}
|
|
178
178
|
*/
|
|
179
179
|
withConstraint(constraint, options) {
|
|
180
180
|
return new BaseConverter((from, __self, context) => {
|
|
@@ -193,7 +193,7 @@ class BaseConverter {
|
|
|
193
193
|
})._with(this._traits());
|
|
194
194
|
}
|
|
195
195
|
/**
|
|
196
|
-
* {@
|
|
196
|
+
* {@inheritDoc Converter.withBrand}
|
|
197
197
|
*/
|
|
198
198
|
withBrand(brand) {
|
|
199
199
|
if (this._brand) {
|
|
@@ -206,11 +206,18 @@ class BaseConverter {
|
|
|
206
206
|
})._with(this._traits({ brand }));
|
|
207
207
|
}
|
|
208
208
|
/**
|
|
209
|
-
* {@
|
|
209
|
+
* {@inheritDoc Converter.withDefault}
|
|
210
210
|
*/
|
|
211
211
|
withDefault(defaultValue) {
|
|
212
212
|
return new defaultingConverter_1.GenericDefaultingConverter(this, defaultValue);
|
|
213
213
|
}
|
|
214
|
+
or(other) {
|
|
215
|
+
return new BaseConverter((from, __self, context) => {
|
|
216
|
+
return this._converter(from, this, this._context(context)).onFailure(() => {
|
|
217
|
+
return other.convert(from, this._context(context));
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
}
|
|
214
221
|
/**
|
|
215
222
|
* @internal
|
|
216
223
|
*/
|
|
@@ -218,7 +225,7 @@ class BaseConverter {
|
|
|
218
225
|
return supplied !== null && supplied !== void 0 ? supplied : this._defaultContext;
|
|
219
226
|
}
|
|
220
227
|
/**
|
|
221
|
-
* {@
|
|
228
|
+
* {@inheritDoc Converter.withFormattedError}
|
|
222
229
|
*/
|
|
223
230
|
withFormattedError(formatter) {
|
|
224
231
|
return new BaseConverter((from, __self, context) => {
|
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
import { TypeGuardWithContext, Validator } from '../validation';
|
|
2
|
+
import { ConverterFunc } from './baseConverter';
|
|
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
|
+
* Determines if a supplied {@link Conversion.Converter | Converter} or {@link Validation.Validator | Validator} is
|
|
91
|
+
* a {@link Validation.Validator | Validator}.
|
|
92
|
+
* @param converterOrValidator - The {@link Conversion.Converter | Converter} or {@link Validation.Validator | Validator} to be tested.
|
|
93
|
+
* @returns `true` if `converterOrValidator` is a {@link Validation.Validator | Validator}, `false` otherwise.
|
|
94
|
+
* @public
|
|
95
|
+
*/
|
|
96
|
+
export declare function isValidator<T, TC>(converterOrValidator: Converter<T, TC> | Validator<T, TC>): converterOrValidator is Validator<T, TC>;
|
|
97
|
+
/**
|
|
98
|
+
* Helper function to create a {@link Converter | Converter} from any {@link Validation.Validator}
|
|
99
|
+
* @param converterOrValidator - the {@link Validation.Validator} to be wrapped or {@link Converter | Converter}
|
|
100
|
+
* to be used directly.
|
|
101
|
+
* @returns A {@link Converter | Converter} which uses the supplied validator.
|
|
102
|
+
* @public
|
|
103
|
+
*/
|
|
104
|
+
export declare function validated<T, TC = unknown>(converterOrValidator: Validator<T, TC> | Converter<T, TC>): Converter<T, TC>;
|
|
105
|
+
/**
|
|
106
|
+
* Helper function to create a {@link Validation.Validator | Validator} from any {@link Conversion.Converter | Converter}
|
|
107
|
+
* or {@link Validation.Validator | Validator}.
|
|
108
|
+
* @param converterOrValidator - the {@link Conversion.Converter | Converter} to be wrappped or {@link Validation.Validator | Validator} to be used directly.
|
|
109
|
+
* @returns A {@link Validation.Validator | Validator} which uses the supplied converter or validator.
|
|
110
|
+
* @public
|
|
111
|
+
*/
|
|
112
|
+
export declare function asValidator<T, TC = unknown>(converterOrValidator: Converter<T, TC> | Validator<T, TC>): Validator<T, TC>;
|
|
113
|
+
/**
|
|
114
|
+
* Helper function to create a {@link Converter | Converter} from a supplied {@link Conversion.ConverterFunc | ConverterFunc}.
|
|
115
|
+
* @param convert - the function to be wrapped
|
|
116
|
+
* @returns A {@link Converter | Converter} which uses the supplied function.
|
|
117
|
+
* @public
|
|
118
|
+
*/
|
|
119
|
+
export declare function generic<T, TC = unknown>(convert: ConverterFunc<T, TC>): Converter<T, TC>;
|
|
120
|
+
/**
|
|
121
|
+
* Helper function to create a {@link Converter | Converter} from a supplied type guard function.
|
|
122
|
+
* @param description - a description of the thing to be validated for use in error messages
|
|
123
|
+
* @param guard - a {@link Validation.TypeGuardWithContext} which performs the validation.
|
|
124
|
+
* @returns A new {@link Converter | Converter} which validates the values using the supplied type guard
|
|
125
|
+
* and returns them in place.
|
|
126
|
+
* @public
|
|
127
|
+
*/
|
|
128
|
+
export declare function isA<T, TC = unknown>(description: string, guard: TypeGuardWithContext<T, TC>): Converter<T, TC>;
|
|
129
|
+
/**
|
|
130
|
+
* A {@link Converter | Converter} which converts an optional `number` value.
|
|
131
|
+
* @remarks
|
|
132
|
+
* Values of type `number` or numeric strings are converted and returned.
|
|
133
|
+
* Anything else returns {@link Success | Success} with value `undefined`.
|
|
134
|
+
* @public
|
|
135
|
+
*/
|
|
136
|
+
export declare const optionalNumber: Converter<number | undefined, unknown>;
|
|
137
|
+
/**
|
|
138
|
+
* A {@link Converter | Converter} to convert an optional `boolean` value.
|
|
139
|
+
* @remarks
|
|
140
|
+
* Values of type `boolean` or strings that match (case-insensitive) `'true'`
|
|
141
|
+
* or `'false'` are converted and returned. Anything else returns {@link Success | Success}
|
|
142
|
+
* with value `undefined`.
|
|
143
|
+
* @public
|
|
144
|
+
*/
|
|
145
|
+
export declare const optionalBoolean: Converter<boolean | undefined, unknown>;
|
|
146
|
+
/**
|
|
147
|
+
* A helper function to create a {@link Converter | Converter} for polymorphic values.
|
|
148
|
+
* Returns a converter which invokes the wrapped converters in sequence, returning the
|
|
149
|
+
* first successful result. Returns an error if none of the supplied converters can
|
|
150
|
+
* convert the value.
|
|
151
|
+
* @remarks
|
|
152
|
+
* If `onError` is `ignoreErrors` (default), then errors from any of the
|
|
153
|
+
* converters are ignored provided that some converter succeeds. If
|
|
154
|
+
* onError is `failOnError`, then an error from any converter fails the entire
|
|
155
|
+
* conversion.
|
|
156
|
+
*
|
|
157
|
+
* @param converters - An ordered list of {@link Converter | converters} or {@link Validator | validators}
|
|
158
|
+
* to be considered.
|
|
159
|
+
* @param onError - Specifies treatment of unconvertible elements.
|
|
160
|
+
* @returns A new {@link Converter | Converter} which yields a value from the union of the types returned
|
|
161
|
+
* by the wrapped converters.
|
|
162
|
+
* @public
|
|
163
|
+
*/
|
|
164
|
+
export declare function oneOf<T, TC = unknown>(converters: Array<Converter<T, TC> | Validator<T, TC>>, onError?: OnError): Converter<T, TC>;
|
|
165
|
+
/**
|
|
166
|
+
* A helper function to create a {@link Converter | Converter} which converts `unknown` to an array of `<T>`.
|
|
167
|
+
* @remarks
|
|
168
|
+
* If `onError` is `'failOnError'` (default), then the entire conversion fails if any element cannot
|
|
169
|
+
* be converted. If `onError` is `'ignoreErrors'`, then failing elements are silently ignored.
|
|
170
|
+
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} used to convert each
|
|
171
|
+
* item in the array.
|
|
172
|
+
* @param onError - Specifies treatment of unconvertible elements.
|
|
173
|
+
* @returns A {@link Converter | Converter} which returns an array of `<T>`.
|
|
174
|
+
* @public
|
|
175
|
+
*/
|
|
176
|
+
export declare function arrayOf<T, TC = unknown>(converter: Converter<T, TC> | Validator<T, TC>, onError?: OnError): Converter<T[], TC>;
|
|
177
|
+
/**
|
|
178
|
+
* {@link Converter | Converter} to convert an `unknown` to an array of `string`.
|
|
179
|
+
* @remarks
|
|
180
|
+
* Returns {@link Success | Success} with the the supplied value if it as an array
|
|
181
|
+
* of strings, returns {@link Failure | Failure} with an error message otherwise.
|
|
182
|
+
* @public
|
|
183
|
+
*/
|
|
184
|
+
export declare const stringArray: Converter<string[], unknown>;
|
|
185
|
+
/**
|
|
186
|
+
* {@link Converter | Converter} to convert an `unknown` to an array of `number`.
|
|
187
|
+
* @remarks
|
|
188
|
+
* Returns {@link Success | Success} with the the supplied value if it as an array
|
|
189
|
+
* of numbers, returns {@link Failure | Failure} with an error message otherwise.
|
|
190
|
+
* @public
|
|
191
|
+
*/
|
|
192
|
+
export declare const numberArray: Converter<number[], unknown>;
|
|
193
|
+
/**
|
|
194
|
+
* Options for {@link recordOf} and
|
|
195
|
+
* {@link mapOf}
|
|
196
|
+
* helper functions.
|
|
197
|
+
* @public
|
|
198
|
+
*/
|
|
199
|
+
export interface KeyedConverterOptions<T extends string = string, TC = unknown> {
|
|
200
|
+
/**
|
|
201
|
+
* if `onError` is `'fail'` (default), then the entire conversion fails if any key or element
|
|
202
|
+
* cannot be converted. If `onError` is `'ignore'`, failing elements are silently ignored.
|
|
203
|
+
*/
|
|
204
|
+
onError?: 'fail' | 'ignore';
|
|
205
|
+
/**
|
|
206
|
+
* If present, `keyConverter` is used to convert the source object property names to
|
|
207
|
+
* keys in the resulting map or record.
|
|
208
|
+
* @remarks
|
|
209
|
+
* Can be used to coerce key names to supported values and/or strong types.
|
|
210
|
+
*/
|
|
211
|
+
keyConverter?: Converter<T, TC> | Validator<T, TC>;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* A helper function to create a {@link Converter | Converter} which converts the `string`-keyed
|
|
215
|
+
* properties using a supplied {@link Converter | Converter<T, TC>} or {@link Validator | Validator<T>} to
|
|
216
|
+
* produce a `Record<string, T>`.
|
|
217
|
+
* @remarks
|
|
218
|
+
* The resulting converter fails conversion if any element cannot be converted.
|
|
219
|
+
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} used for each
|
|
220
|
+
* item in the source object.
|
|
221
|
+
* @returns A {@link Converter | Converter} which returns `Record<string, T>`.
|
|
222
|
+
* {@label WITH_DEFAULT}
|
|
223
|
+
* @public
|
|
224
|
+
*/
|
|
225
|
+
export declare function recordOf<T, TC = unknown, TK extends string = string>(converter: Converter<T, TC> | Validator<T, TC>): Converter<Record<TK, T>, TC>;
|
|
226
|
+
/**
|
|
227
|
+
* A helper function to create a {@link Converter | Converter} which converts the `string`-keyed properties
|
|
228
|
+
* using a supplied {@link Converter | Converter<T, TC>} or {@link Validator | Validator<T>} to produce a
|
|
229
|
+
* `Record<string, T>` and optionally specified handling of elements that cannot be converted.
|
|
230
|
+
* @remarks
|
|
231
|
+
* if `onError` is `'fail'` (default), then the entire conversion fails if any key or element
|
|
232
|
+
* cannot be converted. If `onError` is `'ignore'`, failing elements are silently ignored.
|
|
233
|
+
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} for each item in
|
|
234
|
+
* the source object.
|
|
235
|
+
* @returns A {@link Converter | Converter} which returns `Record<string, T>`.
|
|
236
|
+
* {@label WITH_ON_ERROR}
|
|
237
|
+
* @public
|
|
238
|
+
*/
|
|
239
|
+
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>;
|
|
240
|
+
/**
|
|
241
|
+
* A helper function to create a {@link Converter | Converter} or which converts the `string`-keyed properties
|
|
242
|
+
* using a supplied {@link Converter | Converter<T, TC>} or {@link Validator | Validator<T>} to produce a
|
|
243
|
+
* `Record<TK, T>`.
|
|
244
|
+
* @remarks
|
|
245
|
+
* If present, the supplied {@link Converters.KeyedConverterOptions | options} can provide a strongly-typed
|
|
246
|
+
* converter for keys and/or control the handling of elements that fail conversion.
|
|
247
|
+
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} used for each item in the source object.
|
|
248
|
+
* @param options - Optional {@link Converters.KeyedConverterOptions | KeyedConverterOptions<TK, TC>} which
|
|
249
|
+
* supplies a key converter and/or error-handling options.
|
|
250
|
+
* @returns A {@link Converter | Converter} which returns `Record<TK, T>`.
|
|
251
|
+
* {@label WITH_OPTIONS}
|
|
252
|
+
* @public
|
|
253
|
+
*/
|
|
254
|
+
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>;
|
|
255
|
+
/**
|
|
256
|
+
* A helper function to create a {@link Converter | Converter} which converts the `string`-keyed properties
|
|
257
|
+
* using a supplied {@link Converter | Converter<T, TC>} or {@link Validator | Validator<T>} to produce a
|
|
258
|
+
* `Map<string, T>`.
|
|
259
|
+
* @remarks
|
|
260
|
+
* The resulting converter fails conversion if any element cannot be converted.
|
|
261
|
+
* @param converter - {@link Converter | Converter} | {@link Validator | Validator} used for each item in
|
|
262
|
+
* the source object.
|
|
263
|
+
* @returns A {@link Converter | Converter} which returns `Map<string, T>`.
|
|
264
|
+
* {@label WITH_DEFAULT}
|
|
265
|
+
* @public
|
|
266
|
+
*/
|
|
267
|
+
export declare function mapOf<T, TC = unknown, TK extends string = string>(converter: Converter<T, TC> | Validator<T, TC>): Converter<Map<TK, T>, TC>;
|
|
268
|
+
/**
|
|
269
|
+
* A helper function to create a {@link Converter | Converter} which converts the `string`-keyed properties
|
|
270
|
+
* using a supplied {@link Converter | Converter<T, TC>} or {@link Validator | Validator<T>} to produce a
|
|
271
|
+
* `Map<string, T>` and specified handling of elements that cannot be converted.
|
|
272
|
+
* @remarks
|
|
273
|
+
* if `onError` is `'fail'` (default), then the entire conversion fails if any key or element
|
|
274
|
+
* cannot be converted. If `onError` is `'ignore'`, failing elements are silently ignored.
|
|
275
|
+
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} used for
|
|
276
|
+
* each item in the source object.
|
|
277
|
+
* @returns A {@link Converter | Converter} which returns `Map<string, T>`.
|
|
278
|
+
* {@label WITH_ON_ERROR}
|
|
279
|
+
* @public
|
|
280
|
+
*/
|
|
281
|
+
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>;
|
|
282
|
+
/**
|
|
283
|
+
* A helper function to create a {@link Converter | Converter} which converts the `string`-keyed properties
|
|
284
|
+
* using a supplied {@link Converter | Converter<T, TC>} or {@link Validator | Validator<T>} to produce
|
|
285
|
+
* a `Map<TK,T>`.
|
|
286
|
+
* @remarks
|
|
287
|
+
* If present, the supplied {@link Converters.KeyedConverterOptions | options} can provide a strongly-typed
|
|
288
|
+
* converter for keys and/or control the handling of elements that fail conversion.
|
|
289
|
+
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} used for each item
|
|
290
|
+
* in the source object.
|
|
291
|
+
* @param options - Optional {@link Converters.KeyedConverterOptions | KeyedConverterOptions<TK, TC>} which
|
|
292
|
+
* supplies a key converter and/or error-handling options.
|
|
293
|
+
* @returns A {@link Converter | Converter} which returns `Map<TK,T>`.
|
|
294
|
+
* {@label WITH_OPTIONS}
|
|
295
|
+
* @public
|
|
296
|
+
*/
|
|
297
|
+
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>;
|
|
298
|
+
/**
|
|
299
|
+
* Helper function to create a {@link Converter | Converter} which validates that a supplied value is
|
|
300
|
+
* of a type validated by a supplied validator function and returns it.
|
|
301
|
+
* @remarks
|
|
302
|
+
* If `validator` succeeds, this {@link Converter | Converter} returns {@link Success | Success} with the supplied
|
|
303
|
+
* value of `from` coerced to type `<T>`. Returns a {@link Failure | Failure} with additional
|
|
304
|
+
* information otherwise.
|
|
305
|
+
* @param validator - A validator function to determine if the converted value is valid.
|
|
306
|
+
* @param description - A description of the validated type for use in error messages.
|
|
307
|
+
* @returns A new {@link Converter | Converter<T, TC>} which applies the supplied validation.
|
|
308
|
+
* @public
|
|
309
|
+
*/
|
|
310
|
+
export declare function validateWith<T, TC = unknown>(validator: (from: unknown) => from is T, description?: string): Converter<T, TC>;
|
|
311
|
+
/**
|
|
312
|
+
* A helper function to create a {@link Converter | Converter} which extracts and converts an element from an array.
|
|
313
|
+
* @remarks
|
|
314
|
+
* The returned {@link Converter | Converter} returns {@link Success | Success} with the converted value if the element exists
|
|
315
|
+
* in the supplied array and can be converted. Returns {@link Failure | Failure} with an error message otherwise.
|
|
316
|
+
* @param index - The index of the element to be extracted.
|
|
317
|
+
* @param converter - A {@link Converter | Converter} or {@link Validator | Validator} for the extracted element.
|
|
318
|
+
* @returns A {@link Converter | Converter<T, TC>} which extracts the specified element from an array.
|
|
319
|
+
* @public
|
|
320
|
+
*/
|
|
321
|
+
export declare function element<T, TC = unknown>(index: number, converter: Converter<T, TC> | Validator<T, TC>): Converter<T, TC>;
|
|
322
|
+
/**
|
|
323
|
+
* A helper function to create a {@link Converter | Converter} which extracts and converts an optional element from an array.
|
|
324
|
+
* @remarks
|
|
325
|
+
* The resulting {@link Converter | Converter} returns {@link Success | Success} with the converted value if the element exists
|
|
326
|
+
* in the supplied array and can be converted. Returns {@link Success | Success} with value `undefined` if the parameter
|
|
327
|
+
* is an array but the index is out of range. Returns {@link Failure | Failure} with a message if the supplied parameter
|
|
328
|
+
* is not an array, if the requested index is negative, or if the element cannot be converted.
|
|
329
|
+
* @param index - The index of the element to be extracted.
|
|
330
|
+
* @param converter - A {@link Converter | Converter} or {@link Validator | Validator} used for the extracted element.
|
|
331
|
+
* @returns A {@link Converter | Converter<T, TC>} which extracts the specified element from an array.
|
|
332
|
+
* @public
|
|
333
|
+
*/
|
|
334
|
+
export declare function optionalElement<T, TC = unknown>(index: number, converter: Converter<T, TC> | Validator<T, TC>): Converter<T | undefined, TC>;
|
|
335
|
+
/**
|
|
336
|
+
* A helper function to create a {@link Converter | Converter} which extracts and convert a property specified
|
|
337
|
+
* by name from an object.
|
|
338
|
+
* @remarks
|
|
339
|
+
* The resulting {@link Converter | Converter} returns {@link Success | Success} with the converted value of the corresponding
|
|
340
|
+
* object property if the field exists and can be converted. Returns {@link Failure | Failure} with an error message
|
|
341
|
+
* otherwise.
|
|
342
|
+
* @param name - The name of the field to be extracted.
|
|
343
|
+
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} to use for the extracted
|
|
344
|
+
* field.
|
|
345
|
+
* @public
|
|
346
|
+
*/
|
|
347
|
+
export declare function field<T, TC = unknown>(name: string, converter: Converter<T, TC> | Validator<T, TC>): Converter<T, TC>;
|
|
348
|
+
/**
|
|
349
|
+
* A helper function to create a {@link Converter | Converter} which extracts and convert a property specified
|
|
350
|
+
* by name from an object.
|
|
351
|
+
* @remarks
|
|
352
|
+
* The resulting {@link Converter | Converter} returns {@link Success | Success} with the converted value of
|
|
353
|
+
* the corresponding object property if the field exists and can be converted. Returns {@link Success | Success}
|
|
354
|
+
* with `undefined` if the supplied parameter is an object but the named field is not present.
|
|
355
|
+
* Returns {@link Failure | Failure} with an error message otherwise.
|
|
356
|
+
* @param name - The name of the field to be extracted.
|
|
357
|
+
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} to use for the extracted field.
|
|
358
|
+
* @public
|
|
359
|
+
*/
|
|
360
|
+
export declare function optionalField<T, TC = unknown>(name: string, converter: Converter<T, TC> | Validator<T, TC>): Converter<T | undefined, TC>;
|
|
361
|
+
/**
|
|
362
|
+
* Helper function to create a {@link Conversion.ObjectConverter | ObjectConverter<T, TC>} which converts an object
|
|
363
|
+
* without changing shape, given a {@link Conversion.FieldConverters | FieldConverters<T, TC>} and an optional
|
|
364
|
+
* {@link Conversion.ObjectConverterOptions | ObjectConverterOptions<T>} to further refine conversion behavior.
|
|
365
|
+
* @remarks
|
|
366
|
+
* By default, if all of the requested fields exist and can be converted, returns {@link Success | Success}
|
|
367
|
+
* with a new object that contains the converted values under the original key names. If any required properties
|
|
368
|
+
* do not exist or cannot be converted, the entire conversion fails, returning {@link Failure | Failure} with additional
|
|
369
|
+
* error information.
|
|
370
|
+
*
|
|
371
|
+
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
372
|
+
* fail the conversion.
|
|
373
|
+
* @param properties - An {@link Conversion.FieldConverters | FieldConverters<T, TC>} defining the shape of the
|
|
374
|
+
* source object and {@link Converter | converters} to be applied to each properties.
|
|
375
|
+
* @param options - An {@link Conversion.ObjectConverterOptions | ObjectConverterOptions<T>} containing options
|
|
376
|
+
* for the object converter.
|
|
377
|
+
* @returns A new {@link Conversion.ObjectConverter | ObjectConverter} which applies the specified conversions.
|
|
378
|
+
* {@label WITH_OPTIONS}
|
|
379
|
+
* @public
|
|
380
|
+
*/
|
|
381
|
+
export declare function object<T, TC = unknown>(properties: FieldConverters<T, TC>, options?: ObjectConverterOptions<T>): ObjectConverter<T, TC>;
|
|
382
|
+
/**
|
|
383
|
+
* Helper function to create a {@link Conversion.ObjectConverter | ObjectConverter<T, TC>} which converts an object
|
|
384
|
+
* without changing shape, given a {@link Conversion.FieldConverters | FieldConverters<T, TC>} and a set of
|
|
385
|
+
* optional properties.
|
|
386
|
+
* @remarks
|
|
387
|
+
* By default, if all of the requested fields exist and can be converted, returns {@link Success | Success}
|
|
388
|
+
* with a new object that contains the converted values under the original key names. If any required properties
|
|
389
|
+
* do not exist or cannot be converted, the entire conversion fails, returning {@link Failure | Failure} with additional
|
|
390
|
+
* error information.
|
|
391
|
+
*
|
|
392
|
+
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
393
|
+
* fail the conversion.
|
|
394
|
+
* @param properties - An {@link Conversion.FieldConverters | FieldConverters<T, TC>} defining the shape of the
|
|
395
|
+
* source object and {@link Converter | converters} to be applied to each properties.
|
|
396
|
+
* @param optional - An array of `(keyof T)` listing the keys to be considered optional.
|
|
397
|
+
* {@label WITH_KEYS}
|
|
398
|
+
* @returns A new {@link Conversion.ObjectConverter | ObjectConverter} which applies the specified conversions.
|
|
399
|
+
* @public
|
|
400
|
+
* @deprecated Use {@link Converters.(object:1) | Converters.object(fields, options)} instead.
|
|
401
|
+
*/
|
|
402
|
+
export declare function object<T, TC = unknown>(properties: FieldConverters<T, TC>, optional: (keyof T)[]): ObjectConverter<T, TC>;
|
|
403
|
+
/**
|
|
404
|
+
* Options for the {@link strictObject} helper function.
|
|
405
|
+
* @public
|
|
406
|
+
*/
|
|
407
|
+
export type StrictObjectConverterOptions<T> = Omit<ObjectConverterOptions<T>, 'strict'>;
|
|
408
|
+
/**
|
|
409
|
+
* Helper function to create a {@link Conversion.ObjectConverter | ObjectConverter} which converts an object
|
|
410
|
+
* without changing shape, a {@link Conversion.FieldConverters | FieldConverters<T, TC>} and an optional
|
|
411
|
+
* {@link Converters.StrictObjectConverterOptions | StrictObjectConverterOptions<T>} to further refine
|
|
412
|
+
* conversion behavior.
|
|
413
|
+
*
|
|
414
|
+
* @remarks
|
|
415
|
+
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
416
|
+
* fail the conversion.
|
|
417
|
+
*
|
|
418
|
+
* The conversion fails if any unexpected fields are encountered.
|
|
419
|
+
*
|
|
420
|
+
* @param properties - An object containing defining the shape and converters to be applied.
|
|
421
|
+
* @param options - An optional @see StrictObjectConverterOptions<T> containing options for the object converter.
|
|
422
|
+
* @returns A new {@link Conversion.ObjectConverter | ObjectConverter} which applies the specified conversions.
|
|
423
|
+
* {@label WITH_OPTIONS}
|
|
424
|
+
* @public
|
|
425
|
+
*/
|
|
426
|
+
export declare function strictObject<T, TC = unknown>(properties: FieldConverters<T, TC>, options?: StrictObjectConverterOptions<T>): ObjectConverter<T, TC>;
|
|
427
|
+
/**
|
|
428
|
+
* Helper function to create a {@link Conversion.ObjectConverter | ObjectConverter} which converts an object
|
|
429
|
+
* without changing shape, a {@link Conversion.FieldConverters | FieldConverters<T, TC>} and an optional
|
|
430
|
+
* {@link Converters.StrictObjectConverterOptions | StrictObjectConverterOptions<T>} to further refine
|
|
431
|
+
* conversion behavior.
|
|
432
|
+
*
|
|
433
|
+
* @remarks
|
|
434
|
+
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
435
|
+
* fail the conversion.
|
|
436
|
+
*
|
|
437
|
+
* The conversion fails if any unexpected fields are encountered.
|
|
438
|
+
*
|
|
439
|
+
* @param properties - An object containing defining the shape and converters to be applied.
|
|
440
|
+
* @param optional - An array of `keyof T` containing keys to be considered optional.
|
|
441
|
+
* @returns A new {@link Conversion.ObjectConverter | ObjectConverter} which applies the specified conversions.
|
|
442
|
+
* {@label WITH_KEYS}
|
|
443
|
+
* @deprecated Use {@link Converters.strictObject | Converters.strictObject(options)} instead.
|
|
444
|
+
* @public
|
|
445
|
+
*/
|
|
446
|
+
export declare function strictObject<T, TC = unknown>(properties: FieldConverters<T, TC>, optional: (keyof T)[]): ObjectConverter<T, TC>;
|
|
447
|
+
/**
|
|
448
|
+
* A string-keyed `Record<string, Converter>` which maps specific {@link Converter | converters} or
|
|
449
|
+
* {@link Validator | Validators} to the value of a discriminator property.
|
|
450
|
+
* @public
|
|
451
|
+
*/
|
|
452
|
+
export type DiscriminatedObjectConverters<T, TD extends string = string, TC = unknown> = Record<TD, Converter<T, TC> | Validator<T, TC>>;
|
|
453
|
+
/**
|
|
454
|
+
* Helper to create a {@link Converter | Converter} which converts a discriminated object without changing shape.
|
|
455
|
+
* @remarks
|
|
456
|
+
* Takes the name of the discriminator property and a
|
|
457
|
+
* {@link Converters.DiscriminatedObjectConverters | string-keyed Record of converters and validators}. During conversion,
|
|
458
|
+
* the resulting {@link Converter | Converter} invokes the converter from `converters` that corresponds to the value of
|
|
459
|
+
* the discriminator property in the source object.
|
|
460
|
+
*
|
|
461
|
+
* If the source is not an object, the discriminator property is missing, or the discriminator has
|
|
462
|
+
* a value not present in the converters, conversion fails and returns {@link Failure | Failure} with more information.
|
|
463
|
+
* @param discriminatorProp - Name of the property used to discriminate types.
|
|
464
|
+
* @param converters - {@link Converters.DiscriminatedObjectConverters | String-keyed record of converters and validators}
|
|
465
|
+
* to invoke, where each key corresponds to a value of the discriminator property.
|
|
466
|
+
* @returns A {@link Converter | Converter} which converts the corresponding discriminated object.
|
|
467
|
+
* @public
|
|
468
|
+
*/
|
|
469
|
+
export declare function discriminatedObject<T, TD extends string = string, TC = unknown>(discriminatorProp: string, converters: DiscriminatedObjectConverters<T, TD>): Converter<T, TC>;
|
|
470
|
+
//# sourceMappingURL=basicConverters.d.ts.map
|