@fgv/ts-utils 5.1.0-16 → 5.1.0-18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/packlets/collections/collector.js +23 -9
- package/dist/packlets/collections/collectorValidator.js +6 -1
- package/dist/packlets/conversion/baseConverter.js +102 -16
- package/dist/packlets/logging/logger.js +33 -8
- package/dist/ts-utils.d.ts +257 -55
- package/lib/packlets/collections/collector.d.ts +23 -9
- package/lib/packlets/collections/collector.js +23 -9
- package/lib/packlets/collections/collectorValidator.d.ts +29 -5
- package/lib/packlets/collections/collectorValidator.js +6 -1
- package/lib/packlets/collections/convertingCollector.d.ts +21 -3
- package/lib/packlets/collections/convertingCollectorValidator.d.ts +22 -3
- package/lib/packlets/collections/readonlyResultMap.d.ts +18 -7
- package/lib/packlets/collections/validatingCollector.d.ts +7 -3
- package/lib/packlets/collections/validatingResultMap.d.ts +2 -1
- package/lib/packlets/conversion/baseConverter.d.ts +102 -16
- package/lib/packlets/conversion/baseConverter.js +102 -16
- package/lib/packlets/logging/logger.d.ts +33 -8
- package/lib/packlets/logging/logger.js +33 -8
- package/lib/packlets/validation/validatorBase.d.ts +2 -1
- package/package.json +3 -3
|
@@ -47,25 +47,45 @@ class BaseConverter {
|
|
|
47
47
|
this._brand = traits === null || traits === void 0 ? void 0 : traits.brand;
|
|
48
48
|
}
|
|
49
49
|
/**
|
|
50
|
-
*
|
|
50
|
+
* Indicates whether this element is explicitly optional.
|
|
51
51
|
*/
|
|
52
52
|
get isOptional() {
|
|
53
53
|
return this._isOptional;
|
|
54
54
|
}
|
|
55
55
|
/**
|
|
56
|
-
*
|
|
56
|
+
* Returns the brand for a branded type.
|
|
57
57
|
*/
|
|
58
58
|
get brand() {
|
|
59
59
|
return this._brand;
|
|
60
60
|
}
|
|
61
61
|
/**
|
|
62
|
-
*
|
|
62
|
+
* Converts from `unknown` to `<T>`. For objects and arrays, is guaranteed
|
|
63
|
+
* to return a new entity, with any unrecognized properties removed.
|
|
64
|
+
* @param from - The `unknown` to be converted
|
|
65
|
+
* @param context - An optional conversion context of type `<TC>` to be used in
|
|
66
|
+
* the conversion.
|
|
67
|
+
* @returns A {@link Result} with a {@link Success} and a value on success or an
|
|
68
|
+
* {@link Failure} with a a message on failure.
|
|
63
69
|
*/
|
|
64
70
|
convert(from, context) {
|
|
65
71
|
return this._converter(from, this, context !== null && context !== void 0 ? context : this._defaultContext);
|
|
66
72
|
}
|
|
67
73
|
/**
|
|
68
|
-
*
|
|
74
|
+
* Converts from `unknown` to `<T>` or `undefined`, as appropriate.
|
|
75
|
+
*
|
|
76
|
+
* @remarks
|
|
77
|
+
* If `onError` is `failOnError`, the converter succeeds for
|
|
78
|
+
* `undefined` or any convertible value, but reports an error
|
|
79
|
+
* if it encounters a value that cannot be converted.
|
|
80
|
+
*
|
|
81
|
+
* If `onError` is `ignoreErrors` (default) then values that
|
|
82
|
+
* cannot be converted result in a successful return of `undefined`.
|
|
83
|
+
* @param from - The `unknown` to be converted
|
|
84
|
+
* @param context - An optional conversion context of type `<TC>` to be used in
|
|
85
|
+
* the conversion.
|
|
86
|
+
* @param onError - Specifies handling of values that cannot be converted (default `ignoreErrors`).
|
|
87
|
+
* @returns A {@link Result} with a {@link Success} and a value on success or an
|
|
88
|
+
* {@link Failure} with a a message on failure.
|
|
69
89
|
*/
|
|
70
90
|
convertOptional(from, context, onError) {
|
|
71
91
|
const result = this._converter(from, this, this._context(context));
|
|
@@ -76,7 +96,18 @@ class BaseConverter {
|
|
|
76
96
|
return result;
|
|
77
97
|
}
|
|
78
98
|
/**
|
|
79
|
-
* {@
|
|
99
|
+
* Creates a {@link Converter} for an optional value.
|
|
100
|
+
*
|
|
101
|
+
* @remarks
|
|
102
|
+
* If `onError` is `failOnError`, the resulting converter will accept `undefined`
|
|
103
|
+
* or a convertible value, but report an error if it encounters a value that cannot be
|
|
104
|
+
* converted.
|
|
105
|
+
*
|
|
106
|
+
* If `onError` is `ignoreErrors` (default) then values that cannot be converted will
|
|
107
|
+
* result in a successful return of `undefined`.
|
|
108
|
+
*
|
|
109
|
+
* @param onError - Specifies handling of values that cannot be converted (default `ignoreErrors`).
|
|
110
|
+
* @returns A new {@link Converter} returning `<T|undefined>`.
|
|
80
111
|
*/
|
|
81
112
|
optional(onError) {
|
|
82
113
|
return new BaseConverter((from, __self, context) => {
|
|
@@ -85,7 +116,11 @@ class BaseConverter {
|
|
|
85
116
|
})._with(this._traits({ isOptional: true }));
|
|
86
117
|
}
|
|
87
118
|
/**
|
|
88
|
-
* {@
|
|
119
|
+
* Creates a {@link Converter} which applies a (possibly) mapping conversion to
|
|
120
|
+
* the converted value of this {@link Converter}.
|
|
121
|
+
* @param mapper - A function which maps from the the result type `<T>` of this
|
|
122
|
+
* converter to a new result type `<T2>`.
|
|
123
|
+
* @returns A new {@link Converter} returning `<T2>`.
|
|
89
124
|
*/
|
|
90
125
|
map(mapper) {
|
|
91
126
|
return new BaseConverter((from, __self, context) => {
|
|
@@ -97,7 +132,12 @@ class BaseConverter {
|
|
|
97
132
|
})._with(this._traits());
|
|
98
133
|
}
|
|
99
134
|
/**
|
|
100
|
-
* {@
|
|
135
|
+
* Creates a {@link Converter} which applies an additional supplied
|
|
136
|
+
* converter to the result of this converter.
|
|
137
|
+
*
|
|
138
|
+
* @param mapConverter - The {@link Converter} to be applied to the
|
|
139
|
+
* converted result from this {@link Converter}.
|
|
140
|
+
* @returns A new {@link Converter} returning `<T2>`.
|
|
101
141
|
*/
|
|
102
142
|
mapConvert(mapConverter) {
|
|
103
143
|
return new BaseConverter((from, __self, context) => {
|
|
@@ -109,7 +149,15 @@ class BaseConverter {
|
|
|
109
149
|
})._with(this._traits());
|
|
110
150
|
}
|
|
111
151
|
/**
|
|
112
|
-
* {@
|
|
152
|
+
* Creates a {@link Converter} which maps the individual items of a collection
|
|
153
|
+
* resulting from this {@link Converter} using the supplied map function.
|
|
154
|
+
*
|
|
155
|
+
* @remarks
|
|
156
|
+
* Fails if `from` is not an array.
|
|
157
|
+
*
|
|
158
|
+
* @param mapper - The map function to be applied to each element of the
|
|
159
|
+
* result of this {@link Converter}.
|
|
160
|
+
* @returns A new {@link Converter} returning `<TI[]>`.
|
|
113
161
|
*/
|
|
114
162
|
mapItems(mapper) {
|
|
115
163
|
return new BaseConverter((from, __self, context) => {
|
|
@@ -122,7 +170,15 @@ class BaseConverter {
|
|
|
122
170
|
});
|
|
123
171
|
}
|
|
124
172
|
/**
|
|
125
|
-
* {@
|
|
173
|
+
* Creates a {@link Converter} which maps the individual items of a collection
|
|
174
|
+
* resulting from this {@link Converter} using the supplied {@link Converter}.
|
|
175
|
+
*
|
|
176
|
+
* @remarks
|
|
177
|
+
* Fails if `from` is not an array.
|
|
178
|
+
*
|
|
179
|
+
* @param mapConverter - The {@link Converter} to be applied to each element of the
|
|
180
|
+
* result of this {@link Converter}.
|
|
181
|
+
* @returns A new {@link Converter} returning `<TI[]>`.
|
|
126
182
|
*/
|
|
127
183
|
mapConvertItems(mapConverter) {
|
|
128
184
|
return new BaseConverter((from, __self, context) => {
|
|
@@ -135,7 +191,10 @@ class BaseConverter {
|
|
|
135
191
|
});
|
|
136
192
|
}
|
|
137
193
|
/**
|
|
138
|
-
* {@
|
|
194
|
+
* Creates a {@link Converter | Converter} which applies a supplied action after
|
|
195
|
+
* conversion. The supplied action is always called regardless of success or failure
|
|
196
|
+
* of the base conversion and is allowed to mutate the return type.
|
|
197
|
+
* @param action - The action to be applied.
|
|
139
198
|
*/
|
|
140
199
|
withAction(action) {
|
|
141
200
|
return new BaseConverter((from, __self, context) => {
|
|
@@ -143,7 +202,11 @@ class BaseConverter {
|
|
|
143
202
|
})._with(this._traits());
|
|
144
203
|
}
|
|
145
204
|
/**
|
|
146
|
-
* {@
|
|
205
|
+
* Creates a {@link Converter} which applies a supplied type guard to the conversion
|
|
206
|
+
* result.
|
|
207
|
+
* @param guard - The type guard function to apply.
|
|
208
|
+
* @param message - Optional message to be reported if the type guard fails.
|
|
209
|
+
* @returns A new {@link Converter} returning `<TI>`.
|
|
147
210
|
*/
|
|
148
211
|
withTypeGuard(guard, message) {
|
|
149
212
|
return new BaseConverter((from, __self, context) => {
|
|
@@ -156,7 +219,15 @@ class BaseConverter {
|
|
|
156
219
|
})._with(this._traits());
|
|
157
220
|
}
|
|
158
221
|
/**
|
|
159
|
-
* {@
|
|
222
|
+
* Creates a {@link Converter} which applies a supplied type guard to each member of
|
|
223
|
+
* the conversion result from this converter.
|
|
224
|
+
*
|
|
225
|
+
* @remarks
|
|
226
|
+
* Fails if the conversion result is not an array or if any member fails the
|
|
227
|
+
* type guard.
|
|
228
|
+
* @param guard - The type guard function to apply to each element.
|
|
229
|
+
* @param message - Optional message to be reported if the type guard fails.
|
|
230
|
+
* @returns A new {@link Converter} returning `<TI>`.
|
|
160
231
|
*/
|
|
161
232
|
withItemTypeGuard(guard, message) {
|
|
162
233
|
return new BaseConverter((from, __self, context) => {
|
|
@@ -174,7 +245,15 @@ class BaseConverter {
|
|
|
174
245
|
})._with(this._traits());
|
|
175
246
|
}
|
|
176
247
|
/**
|
|
177
|
-
* {@
|
|
248
|
+
* Creates a {@link Converter} which applies an optional constraint to the result
|
|
249
|
+
* of this conversion. If this {@link Converter} (the base converter) succeeds, the new
|
|
250
|
+
* converter calls a supplied constraint evaluation function with the conversion, which
|
|
251
|
+
* fails the entire conversion if the constraint function returns either `false` or
|
|
252
|
+
* {@link Failure | Failure<T>}.
|
|
253
|
+
*
|
|
254
|
+
* @param constraint - Constraint evaluation function.
|
|
255
|
+
* @param options - {@link Conversion.ConstraintOptions | Options} for constraint evaluation.
|
|
256
|
+
* @returns A new {@link Converter} returning `<T>`.
|
|
178
257
|
*/
|
|
179
258
|
withConstraint(constraint, options) {
|
|
180
259
|
return new BaseConverter((from, __self, context) => {
|
|
@@ -193,7 +272,10 @@ class BaseConverter {
|
|
|
193
272
|
})._with(this._traits());
|
|
194
273
|
}
|
|
195
274
|
/**
|
|
196
|
-
*
|
|
275
|
+
* Returns a converter which adds a brand to the type to prevent mismatched usage
|
|
276
|
+
* of simple types.
|
|
277
|
+
* @param brand - The brand to be applied to the result value.
|
|
278
|
+
* @returns A {@link Converter} returning `Brand<T, B>`.
|
|
197
279
|
*/
|
|
198
280
|
withBrand(brand) {
|
|
199
281
|
if (this._brand) {
|
|
@@ -206,7 +288,8 @@ class BaseConverter {
|
|
|
206
288
|
})._with(this._traits({ brand }));
|
|
207
289
|
}
|
|
208
290
|
/**
|
|
209
|
-
*
|
|
291
|
+
* Returns a Converter which always succeeds with a default value rather than failing.
|
|
292
|
+
* @param defaultValue - The default value to use if conversion fails.
|
|
210
293
|
*/
|
|
211
294
|
withDefault(defaultValue) {
|
|
212
295
|
return new defaultingConverter_1.GenericDefaultingConverter(this, defaultValue);
|
|
@@ -225,7 +308,10 @@ class BaseConverter {
|
|
|
225
308
|
return supplied !== null && supplied !== void 0 ? supplied : this._defaultContext;
|
|
226
309
|
}
|
|
227
310
|
/**
|
|
228
|
-
* {@
|
|
311
|
+
* Creates a new {@link Converter} which is derived from this one but which returns an
|
|
312
|
+
* error message formatted by the supplied formatter if the conversion fails.
|
|
313
|
+
* @param formatter - The formatter to be applied.
|
|
314
|
+
* @returns A new {@link Converter} returning `<T>`.
|
|
229
315
|
*/
|
|
230
316
|
withFormattedError(formatter) {
|
|
231
317
|
return new BaseConverter((from, __self, context) => {
|
|
@@ -106,36 +106,61 @@ export declare function isDetailLogger(logger: ILogger): logger is IDetailLogger
|
|
|
106
106
|
*/
|
|
107
107
|
export declare abstract class LoggerBase implements IDetailLogger {
|
|
108
108
|
/**
|
|
109
|
-
*
|
|
109
|
+
* The level of logging to be used.
|
|
110
110
|
*/
|
|
111
111
|
logLevel: ReporterLogLevel;
|
|
112
112
|
protected constructor(logLevel?: ReporterLogLevel);
|
|
113
113
|
/**
|
|
114
|
-
*
|
|
114
|
+
* Logs a detail message.
|
|
115
|
+
* @param message - The message to log.
|
|
116
|
+
* @param parameters - The parameters to log.
|
|
117
|
+
* @returns `Success` with the logged message if the level is enabled, or
|
|
118
|
+
* `Success` with `undefined` if the message is suppressed.
|
|
115
119
|
*/
|
|
116
120
|
detail(message?: unknown, ...parameters: unknown[]): Success<string | undefined>;
|
|
117
121
|
/**
|
|
118
|
-
*
|
|
122
|
+
* Logs an info message.
|
|
123
|
+
* @param message - The message to log.
|
|
124
|
+
* @param parameters - The parameters to log.
|
|
125
|
+
* @returns `Success` with the logged message if the level is enabled, or
|
|
126
|
+
* `Success` with `undefined` if the message is suppressed.
|
|
119
127
|
*/
|
|
120
128
|
info(message?: unknown, ...parameters: unknown[]): Success<string | undefined>;
|
|
121
129
|
/**
|
|
122
|
-
*
|
|
130
|
+
* Logs a warning message.
|
|
131
|
+
* @param message - The message to log.
|
|
132
|
+
* @param parameters - The parameters to log.
|
|
133
|
+
* @returns `Success` with the logged message if the level is enabled, or
|
|
134
|
+
* `Success` with `undefined` if the message is suppressed.
|
|
123
135
|
*/
|
|
124
136
|
warn(message?: unknown, ...parameters: unknown[]): Success<string | undefined>;
|
|
125
137
|
/**
|
|
126
|
-
*
|
|
138
|
+
* Logs an error message.
|
|
139
|
+
* @param message - The message to log.
|
|
140
|
+
* @param parameters - The parameters to log.
|
|
141
|
+
* @returns `Success` with the logged message if the level is enabled, or
|
|
142
|
+
* `Success` with `undefined` if the message is suppressed.
|
|
127
143
|
*/
|
|
128
144
|
error(message?: unknown, ...parameters: unknown[]): Success<string | undefined>;
|
|
129
145
|
/**
|
|
130
|
-
*
|
|
146
|
+
* Logs a short error summary at `error` level, then emits `detail` at `detail` level.
|
|
147
|
+
* @param message - Short human-readable summary.
|
|
148
|
+
* @param detail - Full detail (e.g. raw converter error) logged at `detail` level.
|
|
131
149
|
*/
|
|
132
150
|
errorWithDetail(message: string, detail: unknown): Success<string | undefined>;
|
|
133
151
|
/**
|
|
134
|
-
*
|
|
152
|
+
* Logs a short warning summary at `warning` level, then emits `detail` at `detail` level.
|
|
153
|
+
* @param message - Short human-readable summary.
|
|
154
|
+
* @param detail - Full detail logged at `detail` level.
|
|
135
155
|
*/
|
|
136
156
|
warnWithDetail(message: string, detail: unknown): Success<string | undefined>;
|
|
137
157
|
/**
|
|
138
|
-
*
|
|
158
|
+
* Logs a message at the given level.
|
|
159
|
+
* @param level - The level of the message.
|
|
160
|
+
* @param message - The message to log.
|
|
161
|
+
* @param parameters - The parameters to log.
|
|
162
|
+
* @returns `Success` with the logged message if the level is enabled, or
|
|
163
|
+
* `Success` with `undefined` if the message is suppressed.
|
|
139
164
|
*/
|
|
140
165
|
log(level: MessageLogLevel, message?: unknown, ...parameters: unknown[]): Success<string | undefined>;
|
|
141
166
|
/**
|
|
@@ -92,51 +92,76 @@ function isDetailLogger(logger) {
|
|
|
92
92
|
class LoggerBase {
|
|
93
93
|
constructor(logLevel) {
|
|
94
94
|
/**
|
|
95
|
-
*
|
|
95
|
+
* The level of logging to be used.
|
|
96
96
|
*/
|
|
97
97
|
this.logLevel = 'info';
|
|
98
98
|
this.logLevel = logLevel !== null && logLevel !== void 0 ? logLevel : 'info';
|
|
99
99
|
}
|
|
100
100
|
/**
|
|
101
|
-
*
|
|
101
|
+
* Logs a detail message.
|
|
102
|
+
* @param message - The message to log.
|
|
103
|
+
* @param parameters - The parameters to log.
|
|
104
|
+
* @returns `Success` with the logged message if the level is enabled, or
|
|
105
|
+
* `Success` with `undefined` if the message is suppressed.
|
|
102
106
|
*/
|
|
103
107
|
detail(message, ...parameters) {
|
|
104
108
|
return this.log('detail', message, ...parameters);
|
|
105
109
|
}
|
|
106
110
|
/**
|
|
107
|
-
*
|
|
111
|
+
* Logs an info message.
|
|
112
|
+
* @param message - The message to log.
|
|
113
|
+
* @param parameters - The parameters to log.
|
|
114
|
+
* @returns `Success` with the logged message if the level is enabled, or
|
|
115
|
+
* `Success` with `undefined` if the message is suppressed.
|
|
108
116
|
*/
|
|
109
117
|
info(message, ...parameters) {
|
|
110
118
|
return this.log('info', message, ...parameters);
|
|
111
119
|
}
|
|
112
120
|
/**
|
|
113
|
-
*
|
|
121
|
+
* Logs a warning message.
|
|
122
|
+
* @param message - The message to log.
|
|
123
|
+
* @param parameters - The parameters to log.
|
|
124
|
+
* @returns `Success` with the logged message if the level is enabled, or
|
|
125
|
+
* `Success` with `undefined` if the message is suppressed.
|
|
114
126
|
*/
|
|
115
127
|
warn(message, ...parameters) {
|
|
116
128
|
return this.log('warning', message, ...parameters);
|
|
117
129
|
}
|
|
118
130
|
/**
|
|
119
|
-
*
|
|
131
|
+
* Logs an error message.
|
|
132
|
+
* @param message - The message to log.
|
|
133
|
+
* @param parameters - The parameters to log.
|
|
134
|
+
* @returns `Success` with the logged message if the level is enabled, or
|
|
135
|
+
* `Success` with `undefined` if the message is suppressed.
|
|
120
136
|
*/
|
|
121
137
|
error(message, ...parameters) {
|
|
122
138
|
return this.log('error', message, ...parameters);
|
|
123
139
|
}
|
|
124
140
|
/**
|
|
125
|
-
*
|
|
141
|
+
* Logs a short error summary at `error` level, then emits `detail` at `detail` level.
|
|
142
|
+
* @param message - Short human-readable summary.
|
|
143
|
+
* @param detail - Full detail (e.g. raw converter error) logged at `detail` level.
|
|
126
144
|
*/
|
|
127
145
|
errorWithDetail(message, detail) {
|
|
128
146
|
this.detail(detail);
|
|
129
147
|
return this.error(message);
|
|
130
148
|
}
|
|
131
149
|
/**
|
|
132
|
-
*
|
|
150
|
+
* Logs a short warning summary at `warning` level, then emits `detail` at `detail` level.
|
|
151
|
+
* @param message - Short human-readable summary.
|
|
152
|
+
* @param detail - Full detail logged at `detail` level.
|
|
133
153
|
*/
|
|
134
154
|
warnWithDetail(message, detail) {
|
|
135
155
|
this.detail(detail);
|
|
136
156
|
return this.warn(message);
|
|
137
157
|
}
|
|
138
158
|
/**
|
|
139
|
-
*
|
|
159
|
+
* Logs a message at the given level.
|
|
160
|
+
* @param level - The level of the message.
|
|
161
|
+
* @param message - The message to log.
|
|
162
|
+
* @param parameters - The parameters to log.
|
|
163
|
+
* @returns `Success` with the logged message if the level is enabled, or
|
|
164
|
+
* `Success` with `undefined` if the message is suppressed.
|
|
140
165
|
*/
|
|
141
166
|
log(level, message, ...parameters) {
|
|
142
167
|
if (shouldLog(level, this.logLevel)) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { GenericValidator, GenericValidatorConstructorParams } from './genericValidator';
|
|
2
|
+
import { Validator } from './validator';
|
|
2
3
|
import { Failure } from '../base';
|
|
3
4
|
/**
|
|
4
5
|
* @internal
|
|
@@ -24,6 +25,6 @@ export declare abstract class ValidatorBase<T, TC = unknown> extends GenericVali
|
|
|
24
25
|
* with an error message if `from` is invalid.
|
|
25
26
|
* @internal
|
|
26
27
|
*/
|
|
27
|
-
protected abstract _validate(from: unknown, context?: TC, self?:
|
|
28
|
+
protected abstract _validate(from: unknown, context?: TC, self?: Validator<T, TC>): boolean | Failure<T>;
|
|
28
29
|
}
|
|
29
30
|
//# sourceMappingURL=validatorBase.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fgv/ts-utils",
|
|
3
|
-
"version": "5.1.0-
|
|
3
|
+
"version": "5.1.0-18",
|
|
4
4
|
"description": "Assorted Typescript Utilities",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -54,8 +54,8 @@
|
|
|
54
54
|
"@rushstack/heft-jest-plugin": "1.2.6",
|
|
55
55
|
"eslint-plugin-tsdoc": "~0.5.2",
|
|
56
56
|
"typedoc": "~0.28.16",
|
|
57
|
-
"@fgv/typedoc-compact-theme": "5.1.0-
|
|
58
|
-
"@fgv/heft-dual-rig": "5.1.0-
|
|
57
|
+
"@fgv/typedoc-compact-theme": "5.1.0-18",
|
|
58
|
+
"@fgv/heft-dual-rig": "5.1.0-18"
|
|
59
59
|
},
|
|
60
60
|
"repository": {
|
|
61
61
|
"type": "git",
|