@fgv/ts-utils 5.0.1-9 → 5.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/dist/index.js +32 -0
- package/dist/packlets/base/brand.js +23 -0
- package/dist/packlets/base/index.js +28 -0
- package/dist/packlets/base/mapResults.js +217 -0
- package/dist/packlets/base/messageAggregator.js +110 -0
- package/dist/packlets/base/normalize.js +132 -0
- package/dist/packlets/base/result.js +611 -0
- package/dist/packlets/base/utils.js +185 -0
- package/dist/packlets/collections/collectible.js +82 -0
- package/dist/packlets/collections/collector.js +189 -0
- package/dist/packlets/collections/collectorValidator.js +94 -0
- package/dist/packlets/collections/common.js +23 -0
- package/dist/packlets/collections/convertingCollector.js +118 -0
- package/dist/packlets/collections/convertingCollectorValidator.js +95 -0
- package/dist/packlets/collections/index.js +37 -0
- package/dist/packlets/collections/keyValueConverters.js +100 -0
- package/dist/packlets/collections/readonlyResultMap.js +23 -0
- package/dist/packlets/collections/resultMap.js +214 -0
- package/dist/packlets/collections/resultMapValidator.js +122 -0
- package/dist/packlets/collections/utils.js +31 -0
- package/dist/packlets/collections/validatingCollector.js +63 -0
- package/dist/packlets/collections/validatingConvertingCollector.js +64 -0
- package/dist/packlets/collections/validatingResultMap.js +57 -0
- package/dist/packlets/conversion/baseConverter.js +242 -0
- package/dist/packlets/conversion/converter.js +2 -0
- package/dist/packlets/conversion/converters.js +637 -0
- package/dist/packlets/conversion/defaultingConverter.js +149 -0
- package/dist/packlets/conversion/index.js +29 -0
- package/dist/packlets/conversion/objectConverter.js +141 -0
- package/dist/packlets/conversion/stringConverter.js +93 -0
- package/dist/packlets/hash/crcNormalizer.js +114 -0
- package/dist/packlets/hash/hashingNormalizer.js +92 -0
- package/dist/packlets/hash/index.js +24 -0
- package/dist/packlets/logging/index.js +24 -0
- package/dist/packlets/logging/logReporter.js +114 -0
- package/dist/packlets/logging/logger.js +258 -0
- package/dist/packlets/validation/array.js +64 -0
- package/dist/packlets/validation/boolean.js +52 -0
- package/dist/packlets/validation/classes.js +31 -0
- package/dist/packlets/validation/common.js +23 -0
- package/dist/packlets/validation/field.js +67 -0
- package/dist/packlets/validation/genericValidator.js +160 -0
- package/dist/packlets/validation/index.js +29 -0
- package/dist/packlets/validation/number.js +52 -0
- package/dist/packlets/validation/object.js +134 -0
- package/dist/packlets/validation/oneOf.js +58 -0
- package/dist/packlets/validation/string.js +52 -0
- package/dist/packlets/validation/traits.js +56 -0
- package/dist/packlets/validation/typeGuard.js +59 -0
- package/dist/packlets/validation/validator.js +23 -0
- package/dist/packlets/validation/validatorBase.js +37 -0
- package/dist/packlets/validation/validators.js +191 -0
- package/dist/test/helpers/jest/helpers/fsHelpers.js +139 -0
- package/dist/test/helpers/jest/helpers/index.js +2 -0
- package/dist/test/helpers/jest/index.js +17 -0
- package/dist/test/helpers/jest/matchers/index.js +14 -0
- package/dist/test/helpers/jest/matchers/toFail/index.js +23 -0
- package/dist/test/helpers/jest/matchers/toFail/predicate.js +5 -0
- package/dist/test/helpers/jest/matchers/toFailTest/index.js +28 -0
- package/dist/test/helpers/jest/matchers/toFailTest/predicate.js +8 -0
- package/dist/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/index.js +24 -0
- package/dist/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/predicate.js +11 -0
- package/dist/test/helpers/jest/matchers/toFailTestWith/index.js +29 -0
- package/dist/test/helpers/jest/matchers/toFailTestWith/predicate.js +26 -0
- package/dist/test/helpers/jest/matchers/toFailWith/index.js +27 -0
- package/dist/test/helpers/jest/matchers/toFailWith/predicate.js +16 -0
- package/dist/test/helpers/jest/matchers/toFailWithDetail/index.js +27 -0
- package/dist/test/helpers/jest/matchers/toFailWithDetail/predicate.js +22 -0
- package/dist/test/helpers/jest/matchers/toSucceed/index.js +23 -0
- package/dist/test/helpers/jest/matchers/toSucceed/predicate.js +5 -0
- package/dist/test/helpers/jest/matchers/toSucceedAndMatchInlineSnapshot/index.js +24 -0
- package/dist/test/helpers/jest/matchers/toSucceedAndMatchSnapshot/index.js +24 -0
- package/dist/test/helpers/jest/matchers/toSucceedAndSatisfy/index.js +53 -0
- package/dist/test/helpers/jest/matchers/toSucceedAndSatisfy/predicate.js +17 -0
- package/dist/test/helpers/jest/matchers/toSucceedWith/index.js +27 -0
- package/dist/test/helpers/jest/matchers/toSucceedWith/predicate.js +12 -0
- package/dist/test/helpers/jest/matchers/toSucceedWithDetail/index.js +27 -0
- package/dist/test/helpers/jest/matchers/toSucceedWithDetail/predicate.js +17 -0
- package/dist/test/helpers/jest/resolvers/cli.js +10 -0
- package/dist/test/helpers/jest/resolvers/ide.js +10 -0
- package/dist/test/helpers/jest/ts-utils.js +2 -0
- package/dist/test/helpers/jest/types/index.js +3 -0
- package/dist/test/helpers/jest/utils/matcherHelpers.js +47 -0
- package/dist/test/helpers/jest/utils/snapshotResolver.js +11 -0
- package/dist/test/unit/collections/helpers.js +106 -0
- package/dist/test/unit/hashTextEncodeCompat.js +23 -0
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/packlets/hash/crcNormalizer.js +42 -2
- package/package.json +12 -2
|
@@ -0,0 +1,611 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2020 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Checks if a result is a deferred result.
|
|
24
|
+
* @param result - The result to check.
|
|
25
|
+
* @returns `true` if the result is a deferred result, `false` otherwise.
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
export function isDeferredResult(result) {
|
|
29
|
+
return typeof result === 'function';
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Reports a successful {@link IResult | result} from some operation and the
|
|
33
|
+
* corresponding value.
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
36
|
+
export class Success {
|
|
37
|
+
/**
|
|
38
|
+
* Constructs a {@link Success} with the supplied value.
|
|
39
|
+
* @param value - The value to be returned.
|
|
40
|
+
*/
|
|
41
|
+
constructor(value) {
|
|
42
|
+
/**
|
|
43
|
+
* {@inheritdoc IResult.success}
|
|
44
|
+
*/
|
|
45
|
+
this.success = true;
|
|
46
|
+
/**
|
|
47
|
+
* For a successful operation, the error message is always `undefined`.
|
|
48
|
+
*/
|
|
49
|
+
this.message = undefined;
|
|
50
|
+
this._value = value;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The result value returned by the successful operation.
|
|
54
|
+
*/
|
|
55
|
+
get value() {
|
|
56
|
+
return this._value;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* {@inheritdoc IResult.isSuccess}
|
|
60
|
+
*/
|
|
61
|
+
isSuccess() {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* {@inheritdoc IResult.isFailure}
|
|
66
|
+
*/
|
|
67
|
+
isFailure() {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
orThrow(__logger) {
|
|
71
|
+
return this._value;
|
|
72
|
+
}
|
|
73
|
+
orDefault(dflt) {
|
|
74
|
+
var _a;
|
|
75
|
+
return (_a = this._value) !== null && _a !== void 0 ? _a : dflt;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* {@inheritdoc IResult.getValueOrThrow}
|
|
79
|
+
* @deprecated Use {@link Success.(orThrow:1) | orThrow(logger)} or {@link Success.(orThrow:2) | orThrow(formatter)} instead.
|
|
80
|
+
*/
|
|
81
|
+
getValueOrThrow(__logger) {
|
|
82
|
+
return this._value;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* {@inheritdoc IResult.getValueOrDefault}
|
|
86
|
+
* @deprecated Use {@link Success.(orDefault:1) | orDefault(T)} or {@link Success.(orDefault:2) | orDefault()} instead.
|
|
87
|
+
*/
|
|
88
|
+
getValueOrDefault(dflt) {
|
|
89
|
+
var _a;
|
|
90
|
+
return (_a = this._value) !== null && _a !== void 0 ? _a : dflt;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* {@inheritdoc IResult.onSuccess}
|
|
94
|
+
*/
|
|
95
|
+
onSuccess(cb) {
|
|
96
|
+
return cb(this._value);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* {@inheritdoc IResult.onFailure}
|
|
100
|
+
*/
|
|
101
|
+
onFailure(__) {
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* {@inheritdoc IResult.withErrorFormat}
|
|
106
|
+
*/
|
|
107
|
+
withErrorFormat(__cb) {
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* {@inheritdoc IResult.withFailureDetail}
|
|
112
|
+
*/
|
|
113
|
+
withFailureDetail(__detail) {
|
|
114
|
+
return succeedWithDetail(this._value);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* {@inheritdoc IResult.withDetail}
|
|
118
|
+
*/
|
|
119
|
+
withDetail(detail, successDetail) {
|
|
120
|
+
return succeedWithDetail(this._value, successDetail !== null && successDetail !== void 0 ? successDetail : detail);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* {@inheritdoc IResult.aggregateError}
|
|
124
|
+
*/
|
|
125
|
+
aggregateError(__errors, __formatter) {
|
|
126
|
+
return this;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* {@inheritdoc IResult.report}
|
|
130
|
+
*/
|
|
131
|
+
report(reporter, options) {
|
|
132
|
+
var _a;
|
|
133
|
+
const successOptions = typeof (options === null || options === void 0 ? void 0 : options.success) === 'object' ? options.success : { level: options === null || options === void 0 ? void 0 : options.success };
|
|
134
|
+
const level = (_a = successOptions.level) !== null && _a !== void 0 ? _a : 'quiet';
|
|
135
|
+
reporter === null || reporter === void 0 ? void 0 : reporter.reportSuccess(level, this._value, undefined, successOptions.message);
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Creates a {@link Success | Success<T>} with the supplied value.
|
|
140
|
+
* @param value - The value to be returned.
|
|
141
|
+
* @returns The resulting {@link Success | Success<T>} with the supplied value.
|
|
142
|
+
* @public
|
|
143
|
+
*/
|
|
144
|
+
static with(value) {
|
|
145
|
+
return new Success(value);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Reports a failed {@link IResult | result} from some operation, with an error message.
|
|
150
|
+
* @public
|
|
151
|
+
*/
|
|
152
|
+
export class Failure {
|
|
153
|
+
/**
|
|
154
|
+
* Constructs a {@link Failure} with the supplied message.
|
|
155
|
+
* @param message - Error message to be reported.
|
|
156
|
+
*/
|
|
157
|
+
constructor(message) {
|
|
158
|
+
/**
|
|
159
|
+
* {@inheritdoc IResult.success}
|
|
160
|
+
*/
|
|
161
|
+
this.success = false;
|
|
162
|
+
/**
|
|
163
|
+
* Failed operation always returns undefined for value.
|
|
164
|
+
*/
|
|
165
|
+
this.value = undefined;
|
|
166
|
+
this._message = message;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Gets the error message associated with this error.
|
|
170
|
+
*/
|
|
171
|
+
get message() {
|
|
172
|
+
return this._message;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* {@inheritdoc IResult.isSuccess}
|
|
176
|
+
*/
|
|
177
|
+
isSuccess() {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* {@inheritdoc IResult.isFailure}
|
|
182
|
+
*/
|
|
183
|
+
isFailure() {
|
|
184
|
+
return true;
|
|
185
|
+
}
|
|
186
|
+
orThrow(logOrFormat) {
|
|
187
|
+
if (logOrFormat !== undefined) {
|
|
188
|
+
if (typeof logOrFormat === 'function') {
|
|
189
|
+
throw new Error(logOrFormat(this._message));
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
logOrFormat.error(this._message);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
throw new Error(this._message);
|
|
196
|
+
}
|
|
197
|
+
orDefault(dflt) {
|
|
198
|
+
return dflt;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* {@inheritdoc IResult.getValueOrThrow}
|
|
202
|
+
* @deprecated Use {@link Failure.(orThrow:1) | orThrow(logger)} or {@link Failure.(orThrow:2) | orThrow(formatter)} instead.
|
|
203
|
+
*/
|
|
204
|
+
getValueOrThrow(logger) {
|
|
205
|
+
if (logger !== undefined) {
|
|
206
|
+
logger.error(this._message);
|
|
207
|
+
}
|
|
208
|
+
throw new Error(this._message);
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* {@inheritdoc IResult.getValueOrDefault}
|
|
212
|
+
* @deprecated Use {@link Failure.(orDefault:1) | orDefault(T)} or {@link Failure.(orDefault:2) | orDefault()} instead.
|
|
213
|
+
*/
|
|
214
|
+
getValueOrDefault(dflt) {
|
|
215
|
+
return dflt;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* {@inheritdoc IResult.onSuccess}
|
|
219
|
+
*/
|
|
220
|
+
onSuccess(__) {
|
|
221
|
+
return new Failure(this._message);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* {@inheritdoc IResult.onFailure}
|
|
225
|
+
*/
|
|
226
|
+
onFailure(cb) {
|
|
227
|
+
return cb(this._message);
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* {@inheritdoc IResult.withErrorFormat}
|
|
231
|
+
*/
|
|
232
|
+
withErrorFormat(cb) {
|
|
233
|
+
return fail(cb(this._message));
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* {@inheritdoc IResult.withFailureDetail}
|
|
237
|
+
*/
|
|
238
|
+
withFailureDetail(detail) {
|
|
239
|
+
return failWithDetail(this._message, detail);
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* {@inheritdoc IResult.withDetail}
|
|
243
|
+
*/
|
|
244
|
+
withDetail(detail, __successDetail) {
|
|
245
|
+
return failWithDetail(this._message, detail);
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* {@inheritdoc IResult.aggregateError}
|
|
249
|
+
*/
|
|
250
|
+
aggregateError(errors, formatter) {
|
|
251
|
+
const message = formatter ? formatter(this._message) : this._message;
|
|
252
|
+
errors.addMessage(message);
|
|
253
|
+
return this;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* {@inheritdoc IResult.report}
|
|
257
|
+
*/
|
|
258
|
+
report(reporter, options) {
|
|
259
|
+
var _a, _b, _c;
|
|
260
|
+
const failureOptions = typeof (options === null || options === void 0 ? void 0 : options.failure) === 'object' ? options.failure : { level: options === null || options === void 0 ? void 0 : options.failure };
|
|
261
|
+
const level = (_a = failureOptions.level) !== null && _a !== void 0 ? _a : 'error';
|
|
262
|
+
const message = (_c = (_b = failureOptions.message) === null || _b === void 0 ? void 0 : _b.call(failureOptions, this._message)) !== null && _c !== void 0 ? _c : this._message;
|
|
263
|
+
reporter === null || reporter === void 0 ? void 0 : reporter.reportFailure(level, message);
|
|
264
|
+
return this;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Get a 'friendly' string representation of this object.
|
|
268
|
+
* @remarks
|
|
269
|
+
* The string representation of a {@link Failure} value is the error message.
|
|
270
|
+
* @returns A string representing this object.
|
|
271
|
+
*/
|
|
272
|
+
toString() {
|
|
273
|
+
return this._message;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Creates a {@link Failure | Failure<T>} with the supplied error message.
|
|
277
|
+
* @param message - The error message to be returned.
|
|
278
|
+
* @returns The resulting {@link Failure | Failure<T>} with the supplied error message.
|
|
279
|
+
*/
|
|
280
|
+
static with(message) {
|
|
281
|
+
return new Failure(message);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Returns {@link Success | Success<T>} with the supplied result value.
|
|
286
|
+
* @param value - The successful result value to be returned
|
|
287
|
+
* @remarks
|
|
288
|
+
* A `succeeds` alias was added in release 5.0 for
|
|
289
|
+
* naming consistency with {@link fails | fails}, which was added
|
|
290
|
+
* to avoid conflicts with test frameworks and libraries.
|
|
291
|
+
* @public
|
|
292
|
+
*/
|
|
293
|
+
export function succeed(value) {
|
|
294
|
+
return new Success(value);
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* {@inheritdoc succeed}
|
|
298
|
+
* @public
|
|
299
|
+
*/
|
|
300
|
+
export function succeeds(value) {
|
|
301
|
+
return new Success(value);
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Returns {@link Failure | Failure<T>} with the supplied error message.
|
|
305
|
+
* @param message - Error message to be returned.
|
|
306
|
+
* @remarks
|
|
307
|
+
* A `fails` alias was added in release 5.0 due to
|
|
308
|
+
* issues with the name `fail` being used test frameworks and libraries.
|
|
309
|
+
* @public
|
|
310
|
+
*/
|
|
311
|
+
export function fail(message) {
|
|
312
|
+
return new Failure(message);
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* {@inheritdoc fail}
|
|
316
|
+
* @public
|
|
317
|
+
*/
|
|
318
|
+
export function fails(message) {
|
|
319
|
+
return new Failure(message);
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Uses a value or calls a supplied initializer if the supplied value is undefined.
|
|
323
|
+
* @param value - the value
|
|
324
|
+
* @param initializer - a function that initializes the value if it is undefined
|
|
325
|
+
* @returns `Success` with the value if it is defined, or the result of calling the initializer function.
|
|
326
|
+
* @public
|
|
327
|
+
*/
|
|
328
|
+
export function useOrInitialize(value, initializer) {
|
|
329
|
+
return value !== undefined ? succeed(value) : initializer();
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* A {@link DetailedSuccess | DetailedSuccess} extends {@link Success | Success} to report optional success
|
|
333
|
+
* details in addition to the error message.
|
|
334
|
+
* @public
|
|
335
|
+
*/
|
|
336
|
+
export class DetailedSuccess extends Success {
|
|
337
|
+
/**
|
|
338
|
+
* Constructs a new {@link DetailedSuccess | DetailedSuccess<T, TD>} with the supplied
|
|
339
|
+
* value and detail.
|
|
340
|
+
* @param value - The value to be returned.
|
|
341
|
+
* @param detail - An optional successful detail to be returned. If omitted, detail
|
|
342
|
+
* will be `undefined`.
|
|
343
|
+
*/
|
|
344
|
+
constructor(value, detail) {
|
|
345
|
+
super(value);
|
|
346
|
+
this._detail = detail;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* The success detail associated with this {@link DetailedSuccess}, or `undefined` if
|
|
350
|
+
* no detail was supplied.
|
|
351
|
+
*/
|
|
352
|
+
get detail() {
|
|
353
|
+
return this._detail;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Reports that this {@link DetailedSuccess} is a success.
|
|
357
|
+
* @remarks
|
|
358
|
+
* Always true for {@link DetailedSuccess} but can be used as type guard
|
|
359
|
+
* to discriminate {@link DetailedSuccess} from {@link DetailedFailure} in
|
|
360
|
+
* a {@link DetailedResult}.
|
|
361
|
+
* @returns `true`
|
|
362
|
+
*/
|
|
363
|
+
isSuccess() {
|
|
364
|
+
return true;
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Invokes the supplied {@link DetailedSuccessContinuation | success callback} and propagates
|
|
368
|
+
* its returned {@link DetailedResult | DetailedResult<TN, TD>}.
|
|
369
|
+
* @remarks
|
|
370
|
+
* The success callback mutates the return type from `<T>` to `<TN>`.
|
|
371
|
+
* @param cb - The {@link DetailedSuccessContinuation | success callback} to be invoked.
|
|
372
|
+
* @returns The {@link DetailedResult | DetailedResult<T, TD>} returned by the success callback.
|
|
373
|
+
*/
|
|
374
|
+
onSuccess(cb) {
|
|
375
|
+
return cb(this._value, this._detail);
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Propagates this {@link DetailedSuccess}.
|
|
379
|
+
* @remarks
|
|
380
|
+
* Failure does not mutate return type so we can return this event directly.
|
|
381
|
+
* @param _cb - {@link DetailedFailureContinuation | Failure callback} to be called
|
|
382
|
+
* on a {@link DetailedResult} in case of failure (ignored).
|
|
383
|
+
* @returns `this`
|
|
384
|
+
*/
|
|
385
|
+
onFailure(__cb) {
|
|
386
|
+
return this;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* {@inheritdoc Success.withErrorFormat}
|
|
390
|
+
*/
|
|
391
|
+
withErrorFormat(cb) {
|
|
392
|
+
return this;
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* {@inheritdoc IResult.report}
|
|
396
|
+
*/
|
|
397
|
+
report(reporter, options) {
|
|
398
|
+
var _a;
|
|
399
|
+
const successOptions = typeof (options === null || options === void 0 ? void 0 : options.success) === 'object' ? options.success : { level: options === null || options === void 0 ? void 0 : options.success };
|
|
400
|
+
const level = (_a = successOptions.level) !== null && _a !== void 0 ? _a : 'quiet';
|
|
401
|
+
// Cast reporter to preserve detail type when calling reportSuccess
|
|
402
|
+
const detailedReporter = reporter;
|
|
403
|
+
detailedReporter === null || detailedReporter === void 0 ? void 0 : detailedReporter.reportSuccess(level, this._value, this._detail, successOptions.message);
|
|
404
|
+
return this;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Creates a {@link DetailedSuccess | DetailedSuccess<T, TD>} with the supplied value and
|
|
408
|
+
* optional detail.
|
|
409
|
+
*/
|
|
410
|
+
static with(value, detail) {
|
|
411
|
+
return new DetailedSuccess(value, detail);
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Returns this {@link DetailedSuccess} as a {@link Result}.
|
|
415
|
+
*/
|
|
416
|
+
get asResult() {
|
|
417
|
+
return this;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* A {@link DetailedFailure | DetailedFailure<T, TD>} extends {@link Failure | Failure<T>} to report optional
|
|
422
|
+
* failure details in addition to the error message.
|
|
423
|
+
* @public
|
|
424
|
+
*/
|
|
425
|
+
export class DetailedFailure extends Failure {
|
|
426
|
+
/**
|
|
427
|
+
* Constructs a new {@link DetailedFailure | DetailedFailure<T, TD>} with the supplied
|
|
428
|
+
* message and detail.
|
|
429
|
+
* @param message - The message to be returned.
|
|
430
|
+
* @param detail - The error detail to be returned.
|
|
431
|
+
*/
|
|
432
|
+
constructor(message, detail) {
|
|
433
|
+
super(message);
|
|
434
|
+
this._detail = detail;
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* The error detail associated with this {@link DetailedFailure}.
|
|
438
|
+
*/
|
|
439
|
+
get detail() {
|
|
440
|
+
return this._detail;
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Reports that this {@link DetailedFailure} is a failure.
|
|
444
|
+
* @remarks
|
|
445
|
+
* Always true for {@link DetailedFailure} but can be used as type guard
|
|
446
|
+
* to discriminate {@link DetailedSuccess} from {@link DetailedFailure} in
|
|
447
|
+
* a {@link DetailedResult}.
|
|
448
|
+
* @returns `true`
|
|
449
|
+
*/
|
|
450
|
+
isFailure() {
|
|
451
|
+
return true;
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Propagates the error message and detail from this result.
|
|
455
|
+
* @remarks
|
|
456
|
+
* Mutates the success type as the success callback would have, but does not
|
|
457
|
+
* call the success callback.
|
|
458
|
+
* @param _cb - {@link DetailedSuccessContinuation | Success callback} to be called
|
|
459
|
+
* on a {@link DetailedResult} in case of success (ignored).
|
|
460
|
+
* @returns A new {@link DetailedFailure | DetailedFailure<TN, TD>} which contains
|
|
461
|
+
* the error message and detail from this one.
|
|
462
|
+
*/
|
|
463
|
+
onSuccess(__cb) {
|
|
464
|
+
return new DetailedFailure(this._message, this._detail);
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Invokes the supplied {@link DetailedFailureContinuation | failure callback} and propagates
|
|
468
|
+
* its returned {@link DetailedResult | DetailedResult<T, TD>}.
|
|
469
|
+
* @param cb - The {@link DetailedFailureContinuation | failure callback} to be invoked.
|
|
470
|
+
* @returns The {@link DetailedResult | DetailedResult<T, TD>} returned by the failure callback.
|
|
471
|
+
*/
|
|
472
|
+
onFailure(cb) {
|
|
473
|
+
return cb(this._message, this._detail);
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* {@inheritdoc IResult.withErrorFormat}
|
|
477
|
+
*/
|
|
478
|
+
withErrorFormat(cb) {
|
|
479
|
+
return failWithDetail(cb(this._message, this._detail), this._detail);
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* {@inheritdoc IResult.aggregateError}
|
|
483
|
+
*/
|
|
484
|
+
aggregateError(errors, formatter) {
|
|
485
|
+
const message = formatter ? formatter(this._message, this._detail) : this._message;
|
|
486
|
+
errors.addMessage(message);
|
|
487
|
+
return this;
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* {@inheritdoc IResult.report}
|
|
491
|
+
*/
|
|
492
|
+
report(reporter, options) {
|
|
493
|
+
var _a, _b;
|
|
494
|
+
const failureOptions = typeof (options === null || options === void 0 ? void 0 : options.failure) === 'object' ? options.failure : { level: options === null || options === void 0 ? void 0 : options.failure };
|
|
495
|
+
const level = (_a = failureOptions.level) !== null && _a !== void 0 ? _a : 'error';
|
|
496
|
+
// Cast formatter to handle detail type properly
|
|
497
|
+
const formatter = failureOptions.message;
|
|
498
|
+
const message = (_b = formatter === null || formatter === void 0 ? void 0 : formatter(this._message, this._detail)) !== null && _b !== void 0 ? _b : this._message;
|
|
499
|
+
// Cast reporter to preserve detail type when calling reportFailure
|
|
500
|
+
const detailedReporter = reporter;
|
|
501
|
+
detailedReporter === null || detailedReporter === void 0 ? void 0 : detailedReporter.reportFailure(level, message, this._detail);
|
|
502
|
+
return this;
|
|
503
|
+
}
|
|
504
|
+
orThrow(logOrFormat) {
|
|
505
|
+
if (logOrFormat !== undefined) {
|
|
506
|
+
if (typeof logOrFormat === 'function') {
|
|
507
|
+
throw new Error(logOrFormat(this._message, this._detail));
|
|
508
|
+
}
|
|
509
|
+
else {
|
|
510
|
+
logOrFormat.error(this._message, this._detail);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
throw new Error(this._message);
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Returns this {@link DetailedFailure} as a {@link Result}.
|
|
517
|
+
*/
|
|
518
|
+
get asResult() {
|
|
519
|
+
return this;
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Creates a {@link DetailedFailure | DetailedFailure<T, TD>} with the supplied error message
|
|
523
|
+
* and optional detail.
|
|
524
|
+
* @param message - The error message to be returned.
|
|
525
|
+
* @param detail - The error detail to be returned.
|
|
526
|
+
* @returns The resulting {@link DetailedFailure | DetailedFailure<T, TD>} with the supplied
|
|
527
|
+
* error message and detail.
|
|
528
|
+
* @public
|
|
529
|
+
*/
|
|
530
|
+
static with(message, detail) {
|
|
531
|
+
return new DetailedFailure(message, detail);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Returns {@link DetailedSuccess | DetailedSuccess<T, TD>} with a supplied value and optional
|
|
536
|
+
* detail.
|
|
537
|
+
* @param value - The value of type `<T>` to be returned.
|
|
538
|
+
* @param detail - An optional detail of type `<TD>` to be returned.
|
|
539
|
+
* @returns A {@link DetailedSuccess | DetailedSuccess<T, TD>} with the supplied value
|
|
540
|
+
* and detail, if supplied.
|
|
541
|
+
* @remarks
|
|
542
|
+
* The `succeedsWithDetail` alias was added in release 5.0 for
|
|
543
|
+
* naming consistency with {@link fails | fails}, which was added to avoid conflicts
|
|
544
|
+
* with test frameworks and libraries.
|
|
545
|
+
* @public
|
|
546
|
+
*/
|
|
547
|
+
export function succeedWithDetail(value, detail) {
|
|
548
|
+
return new DetailedSuccess(value, detail);
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* {@inheritdoc succeedWithDetail}
|
|
552
|
+
* @public
|
|
553
|
+
*/
|
|
554
|
+
export function succeedsWithDetail(value, detail) {
|
|
555
|
+
return new DetailedSuccess(value, detail);
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Returns {@link DetailedFailure | DetailedFailure<T, TD>} with a supplied error message and detail.
|
|
559
|
+
* @param message - The error message to be returned.
|
|
560
|
+
* @param detail - The event detail to be returned.
|
|
561
|
+
* @returns An {@link DetailedFailure | DetailedFailure<T, TD>} with the supplied error
|
|
562
|
+
* message and detail.
|
|
563
|
+
* @remarks
|
|
564
|
+
* The `failsWithDetail` alias was added in release 5.0 for naming consistency
|
|
565
|
+
* with {@link fails | fails}, which was added to avoid conflicts with test frameworks and libraries.
|
|
566
|
+
* @public
|
|
567
|
+
*/
|
|
568
|
+
export function failWithDetail(message, detail) {
|
|
569
|
+
return new DetailedFailure(message, detail);
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* {@inheritdoc failWithDetail}
|
|
573
|
+
* @public
|
|
574
|
+
*/
|
|
575
|
+
export function failsWithDetail(message, detail) {
|
|
576
|
+
return new DetailedFailure(message, detail);
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* Propagates a {@link Success} or {@link Failure} {@link Result}, adding supplied
|
|
580
|
+
* event details as appropriate.
|
|
581
|
+
* @param result - The {@link Result} to be propagated.
|
|
582
|
+
* @param detail - The event detail (type `<TD>`) to be added to the {@link Result | result}.
|
|
583
|
+
* @param successDetail - An optional distinct event detail to be added to {@link Success} results. If `successDetail`
|
|
584
|
+
* is omitted or `undefined`, then `detail` will be applied to {@link Success} results.
|
|
585
|
+
* @returns A new {@link DetailedResult | DetailedResult<T, TD>} with the success value or error
|
|
586
|
+
* message from the original `result` but with the specified detail added.
|
|
587
|
+
* @public
|
|
588
|
+
*/
|
|
589
|
+
export function propagateWithDetail(result, detail, successDetail) {
|
|
590
|
+
return result.isSuccess()
|
|
591
|
+
? succeedWithDetail(result.value, successDetail !== null && successDetail !== void 0 ? successDetail : detail)
|
|
592
|
+
: failWithDetail(result.message, detail);
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Wraps a function which might throw to convert exception results
|
|
596
|
+
* to {@link Failure}.
|
|
597
|
+
* @param func - The function to be captured.
|
|
598
|
+
* @returns Returns {@link Success} with a value of type `<T>` on
|
|
599
|
+
* success , or {@link Failure} with the thrown error message if
|
|
600
|
+
* `func` throws an `Error`.
|
|
601
|
+
* @public
|
|
602
|
+
*/
|
|
603
|
+
export function captureResult(func) {
|
|
604
|
+
try {
|
|
605
|
+
return succeed(func());
|
|
606
|
+
}
|
|
607
|
+
catch (err) {
|
|
608
|
+
return fail(err.message);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
//# sourceMappingURL=result.js.map
|