@fgv/ts-utils 1.9.4 → 2.0.0

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.
Files changed (211) hide show
  1. package/CHANGELOG.json +26 -0
  2. package/CHANGELOG.md +14 -0
  3. package/dist/ts-utils.d.ts +3007 -0
  4. package/dist/tsdoc-metadata.json +11 -0
  5. package/lib/index.d.ts +12 -0
  6. package/lib/index.d.ts.map +1 -0
  7. package/lib/index.js +68 -0
  8. package/lib/index.js.map +1 -0
  9. package/lib/packlets/base/brand.d.ts.map +1 -0
  10. package/lib/packlets/base/brand.js +24 -0
  11. package/lib/packlets/base/brand.js.map +1 -0
  12. package/lib/packlets/base/index.d.ts +7 -0
  13. package/lib/packlets/base/index.d.ts.map +1 -0
  14. package/lib/packlets/base/index.js +58 -0
  15. package/lib/packlets/base/index.js.map +1 -0
  16. package/{logger.d.ts → lib/packlets/base/logger.d.ts} +16 -1
  17. package/lib/packlets/base/logger.d.ts.map +1 -0
  18. package/lib/packlets/base/logger.js +128 -0
  19. package/lib/packlets/base/logger.js.map +1 -0
  20. package/{normalize.d.ts → lib/packlets/base/normalize.d.ts} +6 -0
  21. package/lib/packlets/base/normalize.d.ts.map +1 -0
  22. package/lib/packlets/base/normalize.js +139 -0
  23. package/lib/packlets/base/normalize.js.map +1 -0
  24. package/{result.d.ts → lib/packlets/base/result.d.ts} +16 -15
  25. package/lib/packlets/base/result.d.ts.map +1 -0
  26. package/lib/packlets/base/result.js +571 -0
  27. package/lib/packlets/base/result.js.map +1 -0
  28. package/lib/packlets/base/utils.d.ts.map +1 -0
  29. package/lib/packlets/base/utils.js +200 -0
  30. package/lib/packlets/base/utils.js.map +1 -0
  31. package/{converter.d.ts → lib/packlets/conversion/converter.d.ts} +11 -12
  32. package/lib/packlets/conversion/converter.d.ts.map +1 -0
  33. package/lib/packlets/conversion/converter.js +218 -0
  34. package/lib/packlets/conversion/converter.js.map +1 -0
  35. package/{converters.d.ts → lib/packlets/conversion/converters.d.ts} +52 -216
  36. package/lib/packlets/conversion/converters.d.ts.map +1 -0
  37. package/lib/packlets/conversion/converters.js +739 -0
  38. package/lib/packlets/conversion/converters.js.map +1 -0
  39. package/lib/packlets/conversion/index.d.ts +6 -0
  40. package/lib/packlets/conversion/index.d.ts.map +1 -0
  41. package/lib/packlets/conversion/index.js +56 -0
  42. package/lib/packlets/conversion/index.js.map +1 -0
  43. package/lib/packlets/conversion/objectConverter.d.ts +94 -0
  44. package/lib/packlets/conversion/objectConverter.d.ts.map +1 -0
  45. package/lib/packlets/conversion/objectConverter.js +110 -0
  46. package/lib/packlets/conversion/objectConverter.js.map +1 -0
  47. package/lib/packlets/conversion/stringConverter.d.ts +78 -0
  48. package/lib/packlets/conversion/stringConverter.d.ts.map +1 -0
  49. package/lib/packlets/conversion/stringConverter.js +97 -0
  50. package/lib/packlets/conversion/stringConverter.js.map +1 -0
  51. package/{csvHelpers.d.ts → lib/packlets/csv/csvHelpers.d.ts} +1 -1
  52. package/lib/packlets/csv/csvHelpers.d.ts.map +1 -0
  53. package/lib/packlets/csv/csvHelpers.js +69 -0
  54. package/lib/packlets/csv/csvHelpers.js.map +1 -0
  55. package/lib/packlets/csv/index.d.ts +2 -0
  56. package/lib/packlets/csv/index.d.ts.map +1 -0
  57. package/lib/packlets/csv/index.js +39 -0
  58. package/lib/packlets/csv/index.js.map +1 -0
  59. package/{extendedArray.d.ts → lib/packlets/experimental/extendedArray.d.ts} +7 -7
  60. package/lib/packlets/experimental/extendedArray.d.ts.map +1 -0
  61. package/lib/packlets/experimental/extendedArray.js +106 -0
  62. package/lib/packlets/experimental/extendedArray.js.map +1 -0
  63. package/{formatter.d.ts → lib/packlets/experimental/formatter.d.ts} +6 -6
  64. package/lib/packlets/experimental/formatter.d.ts.map +1 -0
  65. package/lib/packlets/experimental/formatter.js +75 -0
  66. package/lib/packlets/experimental/formatter.js.map +1 -0
  67. package/lib/packlets/experimental/index.d.ts +4 -0
  68. package/lib/packlets/experimental/index.d.ts.map +1 -0
  69. package/lib/packlets/experimental/index.js +41 -0
  70. package/lib/packlets/experimental/index.js.map +1 -0
  71. package/{rangeOf.d.ts → lib/packlets/experimental/rangeOf.d.ts} +21 -24
  72. package/lib/packlets/experimental/rangeOf.d.ts.map +1 -0
  73. package/lib/packlets/experimental/rangeOf.js +185 -0
  74. package/lib/packlets/experimental/rangeOf.js.map +1 -0
  75. package/{hash.d.ts → lib/packlets/hash/hash.d.ts} +1 -1
  76. package/lib/packlets/hash/hash.d.ts.map +1 -0
  77. package/lib/packlets/hash/hash.js +168 -0
  78. package/lib/packlets/hash/hash.js.map +1 -0
  79. package/lib/packlets/hash/index.d.ts +2 -0
  80. package/lib/packlets/hash/index.d.ts.map +1 -0
  81. package/lib/packlets/hash/index.js +39 -0
  82. package/lib/packlets/hash/index.js.map +1 -0
  83. package/lib/packlets/record-jar/index.d.ts +2 -0
  84. package/lib/packlets/record-jar/index.d.ts.map +1 -0
  85. package/lib/packlets/record-jar/index.js +39 -0
  86. package/lib/packlets/record-jar/index.js.map +1 -0
  87. package/{recordJarHelpers.d.ts → lib/packlets/record-jar/recordJarHelpers.d.ts} +1 -1
  88. package/lib/packlets/record-jar/recordJarHelpers.d.ts.map +1 -0
  89. package/lib/packlets/record-jar/recordJarHelpers.js +263 -0
  90. package/lib/packlets/record-jar/recordJarHelpers.js.map +1 -0
  91. package/{validation → lib/packlets/validation}/array.d.ts +2 -2
  92. package/lib/packlets/validation/array.d.ts.map +1 -0
  93. package/lib/packlets/validation/array.js +67 -0
  94. package/lib/packlets/validation/array.js.map +1 -0
  95. package/{validation → lib/packlets/validation}/boolean.d.ts +1 -1
  96. package/lib/packlets/validation/boolean.d.ts.map +1 -0
  97. package/lib/packlets/validation/boolean.js +56 -0
  98. package/lib/packlets/validation/boolean.js.map +1 -0
  99. package/lib/packlets/validation/classes.d.ts.map +1 -0
  100. package/lib/packlets/validation/classes.js +38 -0
  101. package/lib/packlets/validation/classes.js.map +1 -0
  102. package/lib/packlets/validation/common.d.ts.map +1 -0
  103. package/lib/packlets/validation/common.js +24 -0
  104. package/lib/packlets/validation/common.js.map +1 -0
  105. package/{validation → lib/packlets/validation}/field.d.ts +1 -1
  106. package/lib/packlets/validation/field.d.ts.map +1 -0
  107. package/lib/packlets/validation/field.js +71 -0
  108. package/lib/packlets/validation/field.js.map +1 -0
  109. package/{validation → lib/packlets/validation}/genericValidator.d.ts +2 -3
  110. package/lib/packlets/validation/genericValidator.d.ts.map +1 -0
  111. package/lib/packlets/validation/genericValidator.js +137 -0
  112. package/lib/packlets/validation/genericValidator.js.map +1 -0
  113. package/lib/packlets/validation/index.d.ts.map +1 -0
  114. package/{validation → lib/packlets/validation}/index.js +1 -1
  115. package/lib/packlets/validation/index.js.map +1 -0
  116. package/{validation → lib/packlets/validation}/number.d.ts +1 -1
  117. package/lib/packlets/validation/number.d.ts.map +1 -0
  118. package/lib/packlets/validation/number.js +54 -0
  119. package/lib/packlets/validation/number.js.map +1 -0
  120. package/{validation → lib/packlets/validation}/object.d.ts +1 -1
  121. package/lib/packlets/validation/object.d.ts.map +1 -0
  122. package/lib/packlets/validation/object.js +139 -0
  123. package/lib/packlets/validation/object.js.map +1 -0
  124. package/{validation → lib/packlets/validation}/string.d.ts +1 -1
  125. package/lib/packlets/validation/string.d.ts.map +1 -0
  126. package/lib/packlets/validation/string.js +54 -0
  127. package/lib/packlets/validation/string.js.map +1 -0
  128. package/lib/packlets/validation/traits.d.ts.map +1 -0
  129. package/lib/packlets/validation/traits.js +58 -0
  130. package/lib/packlets/validation/traits.js.map +1 -0
  131. package/{validation → lib/packlets/validation}/typeGuard.d.ts +1 -1
  132. package/lib/packlets/validation/typeGuard.d.ts.map +1 -0
  133. package/lib/packlets/validation/typeGuard.js +62 -0
  134. package/lib/packlets/validation/typeGuard.js.map +1 -0
  135. package/{validation → lib/packlets/validation}/validator.d.ts +15 -16
  136. package/lib/packlets/validation/validator.d.ts.map +1 -0
  137. package/lib/packlets/validation/validator.js +24 -0
  138. package/lib/packlets/validation/validator.js.map +1 -0
  139. package/{validation → lib/packlets/validation}/validatorBase.d.ts +1 -1
  140. package/lib/packlets/validation/validatorBase.d.ts.map +1 -0
  141. package/lib/packlets/validation/validatorBase.js +41 -0
  142. package/lib/packlets/validation/validatorBase.js.map +1 -0
  143. package/{validation → lib/packlets/validation}/validators.d.ts +3 -6
  144. package/lib/packlets/validation/validators.d.ts.map +1 -0
  145. package/lib/packlets/validation/validators.js +121 -0
  146. package/lib/packlets/validation/validators.js.map +1 -0
  147. package/package.json +21 -20
  148. package/brand.d.ts.map +0 -1
  149. package/brand.js +0 -24
  150. package/converter.d.ts.map +0 -1
  151. package/converter.js +0 -222
  152. package/converters.d.ts.map +0 -1
  153. package/converters.js +0 -896
  154. package/csvHelpers.d.ts.map +0 -1
  155. package/csvHelpers.js +0 -69
  156. package/extendedArray.d.ts.map +0 -1
  157. package/extendedArray.js +0 -106
  158. package/formatter.d.ts.map +0 -1
  159. package/formatter.js +0 -75
  160. package/hash.d.ts.map +0 -1
  161. package/hash.js +0 -168
  162. package/index.d.ts +0 -15
  163. package/index.d.ts.map +0 -1
  164. package/index.js +0 -70
  165. package/logger.d.ts.map +0 -1
  166. package/logger.js +0 -115
  167. package/normalize.d.ts.map +0 -1
  168. package/normalize.js +0 -139
  169. package/rangeOf.d.ts.map +0 -1
  170. package/rangeOf.js +0 -185
  171. package/recordJarHelpers.d.ts.map +0 -1
  172. package/recordJarHelpers.js +0 -262
  173. package/result.d.ts.map +0 -1
  174. package/result.js +0 -569
  175. package/utils.d.ts.map +0 -1
  176. package/utils.js +0 -200
  177. package/validation/array.d.ts.map +0 -1
  178. package/validation/array.js +0 -67
  179. package/validation/boolean.d.ts.map +0 -1
  180. package/validation/boolean.js +0 -59
  181. package/validation/classes.d.ts.map +0 -1
  182. package/validation/classes.js +0 -38
  183. package/validation/common.d.ts.map +0 -1
  184. package/validation/common.js +0 -24
  185. package/validation/field.d.ts.map +0 -1
  186. package/validation/field.js +0 -72
  187. package/validation/genericValidator.d.ts.map +0 -1
  188. package/validation/genericValidator.js +0 -138
  189. package/validation/index.d.ts.map +0 -1
  190. package/validation/number.d.ts.map +0 -1
  191. package/validation/number.js +0 -57
  192. package/validation/object.d.ts.map +0 -1
  193. package/validation/object.js +0 -143
  194. package/validation/string.d.ts.map +0 -1
  195. package/validation/string.js +0 -57
  196. package/validation/traits.d.ts.map +0 -1
  197. package/validation/traits.js +0 -58
  198. package/validation/typeGuard.d.ts.map +0 -1
  199. package/validation/typeGuard.js +0 -62
  200. package/validation/validator.d.ts.map +0 -1
  201. package/validation/validator.js +0 -24
  202. package/validation/validatorBase.d.ts.map +0 -1
  203. package/validation/validatorBase.js +0 -44
  204. package/validation/validators.d.ts.map +0 -1
  205. package/validation/validators.js +0 -119
  206. /package/{brand.d.ts → lib/packlets/base/brand.d.ts} +0 -0
  207. /package/{utils.d.ts → lib/packlets/base/utils.d.ts} +0 -0
  208. /package/{validation → lib/packlets/validation}/classes.d.ts +0 -0
  209. /package/{validation → lib/packlets/validation}/common.d.ts +0 -0
  210. /package/{validation → lib/packlets/validation}/index.d.ts +0 -0
  211. /package/{validation → lib/packlets/validation}/traits.d.ts +0 -0
@@ -0,0 +1,571 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2020 Erik Fortune
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all
13
+ * copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.populateObject = exports.allSucceed = exports.mapFailures = exports.mapSuccess = exports.mapDetailedResults = exports.mapResults = exports.captureResult = exports.propagateWithDetail = exports.failWithDetail = exports.succeedWithDetail = exports.DetailedFailure = exports.DetailedSuccess = exports.fail = exports.succeed = exports.Failure = exports.Success = void 0;
25
+ /**
26
+ * Reports a successful {@link IResult | result} from some operation and the
27
+ * corresponding value.
28
+ * @public
29
+ */
30
+ class Success {
31
+ /**
32
+ * Constructs a {@link Success} with the supplied value.
33
+ * @param value - The value to be returned.
34
+ */
35
+ constructor(value) {
36
+ /**
37
+ * {@inheritdoc IResult.success}
38
+ */
39
+ this.success = true;
40
+ this._value = value;
41
+ }
42
+ /**
43
+ * The result value returned by the successful operation.
44
+ */
45
+ get value() {
46
+ return this._value;
47
+ }
48
+ /**
49
+ * {@inheritdoc IResult.isSuccess}
50
+ */
51
+ isSuccess() {
52
+ return true;
53
+ }
54
+ /**
55
+ * {@inheritdoc IResult.isFailure}
56
+ */
57
+ isFailure() {
58
+ return false;
59
+ }
60
+ /**
61
+ * {@inheritdoc IResult.orThrow}
62
+ */
63
+ orThrow(__logger) {
64
+ return this._value;
65
+ }
66
+ /**
67
+ * {@inheritdoc IResult.orDefault}
68
+ */
69
+ orDefault(dflt) {
70
+ var _a;
71
+ return (_a = this._value) !== null && _a !== void 0 ? _a : dflt;
72
+ }
73
+ /**
74
+ * {@inheritdoc IResult.getValueOrThrow}
75
+ */
76
+ getValueOrThrow(__logger) {
77
+ return this._value;
78
+ }
79
+ /**
80
+ * {@inheritdoc IResult.getValueOrDefault}
81
+ */
82
+ getValueOrDefault(dflt) {
83
+ var _a;
84
+ return (_a = this._value) !== null && _a !== void 0 ? _a : dflt;
85
+ }
86
+ /**
87
+ * {@inheritdoc IResult.onSuccess}
88
+ */
89
+ onSuccess(cb) {
90
+ return cb(this.value);
91
+ }
92
+ /**
93
+ * {@inheritdoc IResult.onFailure}
94
+ */
95
+ onFailure(__) {
96
+ return this;
97
+ }
98
+ /**
99
+ * {@inheritdoc IResult.withFailureDetail}
100
+ */
101
+ withFailureDetail(__detail) {
102
+ return succeedWithDetail(this.value);
103
+ }
104
+ /**
105
+ * {@inheritdoc IResult.withDetail}
106
+ */
107
+ withDetail(detail, successDetail) {
108
+ return succeedWithDetail(this.value, successDetail !== null && successDetail !== void 0 ? successDetail : detail);
109
+ }
110
+ }
111
+ exports.Success = Success;
112
+ /**
113
+ * Reports a failed {@link IResult | result} from some operation, with an error message.
114
+ * @public
115
+ */
116
+ class Failure {
117
+ /**
118
+ * Constructs a {@link Failure} with the supplied message.
119
+ * @param message - Error message to be reported.
120
+ */
121
+ constructor(message) {
122
+ /**
123
+ * {@inheritdoc IResult.success}
124
+ */
125
+ this.success = false;
126
+ this._message = message;
127
+ }
128
+ /**
129
+ * Gets the error message associated with this error.
130
+ */
131
+ get message() {
132
+ return this._message;
133
+ }
134
+ /**
135
+ * {@inheritdoc IResult.isSuccess}
136
+ */
137
+ isSuccess() {
138
+ return false;
139
+ }
140
+ /**
141
+ * {@inheritdoc IResult.isFailure}
142
+ */
143
+ isFailure() {
144
+ return true;
145
+ }
146
+ /**
147
+ * {@inheritdoc IResult.orThrow}
148
+ */
149
+ orThrow(logger) {
150
+ if (logger !== undefined) {
151
+ logger.error(this._message);
152
+ }
153
+ throw new Error(this._message);
154
+ }
155
+ /**
156
+ * {@inheritdoc IResult.orDefault}
157
+ */
158
+ orDefault(dflt) {
159
+ return dflt;
160
+ }
161
+ /**
162
+ * {@inheritdoc IResult.getValueOrThrow}
163
+ */
164
+ getValueOrThrow(logger) {
165
+ if (logger !== undefined) {
166
+ logger.error(this._message);
167
+ }
168
+ throw new Error(this._message);
169
+ }
170
+ /**
171
+ * {@inheritdoc IResult.getValueOrDefault}
172
+ */
173
+ getValueOrDefault(dflt) {
174
+ return dflt;
175
+ }
176
+ /**
177
+ * {@inheritdoc IResult.onSuccess}
178
+ */
179
+ onSuccess(__) {
180
+ return new Failure(this.message);
181
+ }
182
+ /**
183
+ * {@inheritdoc IResult.onFailure}
184
+ */
185
+ onFailure(cb) {
186
+ return cb(this.message);
187
+ }
188
+ /**
189
+ * {@inheritdoc IResult.withFailureDetail}
190
+ */
191
+ withFailureDetail(detail) {
192
+ return failWithDetail(this.message, detail);
193
+ }
194
+ /**
195
+ * {@inheritdoc IResult.withDetail}
196
+ */
197
+ withDetail(detail, __successDetail) {
198
+ return failWithDetail(this.message, detail);
199
+ }
200
+ /**
201
+ * Get a 'friendly' string representation of this object.
202
+ * @remarks
203
+ * The string representation of a {@link Failure} value is the error message.
204
+ * @returns A string representing this object.
205
+ */
206
+ toString() {
207
+ return this.message;
208
+ }
209
+ }
210
+ exports.Failure = Failure;
211
+ /**
212
+ * Returns {@link Success | Success<T>} with the supplied result value.
213
+ * @param value - The successful result value to be returned
214
+ * @public
215
+ */
216
+ function succeed(value) {
217
+ return new Success(value);
218
+ }
219
+ exports.succeed = succeed;
220
+ /**
221
+ * Returns {@link Failure | Failure<T>} with the supplied error message.
222
+ * @param message - Error message to be returned.
223
+ * @public
224
+ */
225
+ function fail(message) {
226
+ return new Failure(message);
227
+ }
228
+ exports.fail = fail;
229
+ /**
230
+ * A {@link DetailedSuccess} extends {@link Success} to report optional success details in
231
+ * addition to the error message.
232
+ * @public
233
+ */
234
+ class DetailedSuccess extends Success {
235
+ /**
236
+ * Constructs a new {@link DetailedSuccess | DetailedSuccess<T, TD>} with the supplied
237
+ * value and detail.
238
+ * @param value - The value to be returned.
239
+ * @param detail - An optional successful detail to be returned. If omitted, detail
240
+ * will be `undefined`.
241
+ */
242
+ constructor(value, detail) {
243
+ super(value);
244
+ this._detail = detail;
245
+ }
246
+ /**
247
+ * The success detail associated with this {@link DetailedSuccess}, or `undefined` if
248
+ * no detail was supplied.
249
+ */
250
+ get detail() {
251
+ return this._detail;
252
+ }
253
+ /**
254
+ * Reports that this {@link DetailedSuccess} is a success.
255
+ * @remarks
256
+ * Always true for {@link DetailedSuccess} but can be used as type guard
257
+ * to discriminate {@link DetailedSuccess} from {@link DetailedFailure} in
258
+ * a {@link DetailedResult}.
259
+ * @returns `true`
260
+ */
261
+ isSuccess() {
262
+ return true;
263
+ }
264
+ /**
265
+ * Invokes the supplied {@link DetailedSuccessContinuation | success callback} and propagates
266
+ * its returned {@link DetailedResult | DetailedResult<TN, TD>}.
267
+ * @remarks
268
+ * The success callback mutates the return type from `<T>` to `<TN>`.
269
+ * @param cb - The {@link DetailedSuccessContinuation | success callback} to be invoked.
270
+ * @returns The {@link DetailedResult | DetailedResult<T, TD>} returned by the success callback.
271
+ */
272
+ onSuccess(cb) {
273
+ return cb(this.value, this._detail);
274
+ }
275
+ /**
276
+ * Propagates this {@link DetailedSuccess}.
277
+ * @remarks
278
+ * Failure does not mutate return type so we can return this event directly.
279
+ * @param _cb - {@link DetailedFailureContinuation | Failure callback} to be called
280
+ * on a {@link DetailedResult} in case of failure (ignored).
281
+ * @returns `this`
282
+ */
283
+ onFailure(__cb) {
284
+ return this;
285
+ }
286
+ }
287
+ exports.DetailedSuccess = DetailedSuccess;
288
+ /**
289
+ * A {@link DetailedFailure} extends {@link Failure} to report optional failure details in
290
+ * addition to the error message.
291
+ * @public
292
+ */
293
+ class DetailedFailure extends Failure {
294
+ /**
295
+ * Constructs a new {@link DetailedFailure | DetailedFailure<T, TD>} with the supplied
296
+ * message and detail.
297
+ * @param message - The message to be returned.
298
+ * @param detail - The error detail to be returned.
299
+ */
300
+ constructor(message, detail) {
301
+ super(message);
302
+ this._detail = detail;
303
+ }
304
+ /**
305
+ * The error detail associated with this {@link DetailedFailure}.
306
+ */
307
+ get detail() {
308
+ return this._detail;
309
+ }
310
+ /**
311
+ * Reports that this {@link DetailedFailure} is a failure.
312
+ * @remarks
313
+ * Always true for {@link DetailedFailure} but can be used as type guard
314
+ * to discriminate {@link DetailedSuccess} from {@link DetailedFailure} in
315
+ * a {@link DetailedResult}.
316
+ * @returns `true`
317
+ */
318
+ isFailure() {
319
+ return true;
320
+ }
321
+ /**
322
+ * Propagates the error message and detail from this result.
323
+ * @remarks
324
+ * Mutates the success type as the success callback would have, but does not
325
+ * call the success callback.
326
+ * @param _cb - {@link DetailedSuccessContinuation | Success callback} to be called
327
+ * on a {@link DetailedResult} in case of success (ignored).
328
+ * @returns A new {@link DetailedFailure | DetailedFailure<TN, TD>} which contains
329
+ * the error message and detail from this one.
330
+ */
331
+ onSuccess(__cb) {
332
+ return new DetailedFailure(this.message, this._detail);
333
+ }
334
+ /**
335
+ * Invokes the supplied {@link DetailedFailureContinuation | failure callback} and propagates
336
+ * its returned {@link DetailedResult | DetailedResult<T, TD>}.
337
+ * @param cb - The {@link DetailedFailureContinuation | failure callback} to be invoked.
338
+ * @returns The {@link DetailedResult | DetailedResult<T, TD>} returned by the failure callback.
339
+ */
340
+ onFailure(cb) {
341
+ return cb(this.message, this._detail);
342
+ }
343
+ }
344
+ exports.DetailedFailure = DetailedFailure;
345
+ /**
346
+ * Returns {@link DetailedSuccess | DetailedSuccess<T, TD>} with a supplied value and optional
347
+ * detail.
348
+ * @param value - The value of type `<T>` to be returned.
349
+ * @param detail - An optional detail of type `<TD>` to be returned.
350
+ * @returns A {@link DetailedSuccess | DetailedSuccess<T, TD>} with the supplied value
351
+ * and detail, if supplied.
352
+ * @public
353
+ */
354
+ function succeedWithDetail(value, detail) {
355
+ return new DetailedSuccess(value, detail);
356
+ }
357
+ exports.succeedWithDetail = succeedWithDetail;
358
+ /**
359
+ * Returns {@link DetailedFailure | DetailedFailure<T, TD>} with a supplied error message and detail.
360
+ * @param message - The error message to be returned.
361
+ * @param detail - The event detail to be returned.
362
+ * @returns An {@link DetailedFailure | DetailedFailure<T, TD>} with the supplied error
363
+ * message and detail.
364
+ * @public
365
+ */
366
+ function failWithDetail(message, detail) {
367
+ return new DetailedFailure(message, detail);
368
+ }
369
+ exports.failWithDetail = failWithDetail;
370
+ /**
371
+ * Propagates a {@link Success} or {@link Failure} {@link Result}, adding supplied
372
+ * event details as appropriate.
373
+ * @param result - The {@link Result} to be propagated.
374
+ * @param detail - The event detail (type `<TD>`) to be added to the {@link Result | result}.
375
+ * @param successDetail - An optional distinct event detail to be added to {@link Success} results. If `successDetail`
376
+ * is omitted or `undefined`, then `detail` will be applied to {@link Success} results.
377
+ * @returns A new {@link DetailedResult | DetailedResult<T, TD>} with the success value or error
378
+ * message from the original `result` but with the specified detail added.
379
+ * @public
380
+ */
381
+ function propagateWithDetail(result, detail, successDetail) {
382
+ return result.isSuccess()
383
+ ? succeedWithDetail(result.value, successDetail !== null && successDetail !== void 0 ? successDetail : detail)
384
+ : failWithDetail(result.message, detail);
385
+ }
386
+ exports.propagateWithDetail = propagateWithDetail;
387
+ /**
388
+ * Wraps a function which might throw to convert exception results
389
+ * to {@link Failure}.
390
+ * @param func - The function to be captured.
391
+ * @returns Returns {@link Success} with a value of type `<T>` on
392
+ * success , or {@link Failure} with the thrown error message if
393
+ * `func` throws an `Error`.
394
+ * @public
395
+ */
396
+ function captureResult(func) {
397
+ try {
398
+ return succeed(func());
399
+ }
400
+ catch (err) {
401
+ return fail(err.message);
402
+ }
403
+ }
404
+ exports.captureResult = captureResult;
405
+ /**
406
+ * Aggregates successful result values from a collection of {@link Result | Result<T>}.
407
+ * @param results - The collection of {@link Result | Result<T>} to be mapped.
408
+ * @returns If all {@link Result | results} are successful, returns {@link Success} with an
409
+ * array containing all returned values. If any {@link Result | results} failed, returns
410
+ * {@link Failure} with a concatenated summary of all error messages.
411
+ * @public
412
+ */
413
+ function mapResults(results) {
414
+ const errors = [];
415
+ const elements = [];
416
+ for (const result of results) {
417
+ if (result.isSuccess()) {
418
+ elements.push(result.value);
419
+ }
420
+ else {
421
+ errors.push(result.message);
422
+ }
423
+ }
424
+ if (errors.length > 0) {
425
+ return fail(errors.join('\n'));
426
+ }
427
+ return succeed(elements);
428
+ }
429
+ exports.mapResults = mapResults;
430
+ /**
431
+ * Aggregates successful results from a collection of {@link DetailedResult | DetailedResult<T, TD>},
432
+ * optionally ignoring certain error details.
433
+ * @param results - The collection of {@link DetailedResult | DetailedResult<T, TD>} to be mapped.
434
+ * @param ignore - An array of error detail values (of type `<TD>`) that should be ignored.
435
+ * @returns {@link Success} with an array containing all successful results if all results either
436
+ * succeeded or returned error details listed in `ignore`. If any results failed with details
437
+ * that cannot be ignored, returns {@link Failure} with an concatenated summary of all non-ignorable
438
+ * error messages.
439
+ * @public
440
+ */
441
+ function mapDetailedResults(results, ignore) {
442
+ const errors = [];
443
+ const elements = [];
444
+ for (const result of results) {
445
+ if (result.isSuccess()) {
446
+ elements.push(result.value);
447
+ }
448
+ else if (!ignore.includes(result.detail)) {
449
+ errors.push(result.message);
450
+ }
451
+ }
452
+ if (errors.length > 0) {
453
+ return fail(errors.join('\n'));
454
+ }
455
+ return succeed(elements);
456
+ }
457
+ exports.mapDetailedResults = mapDetailedResults;
458
+ /**
459
+ * Aggregates successful results from a a collection of {@link Result | Result<T>}.
460
+ * @param results - An `Iterable` of {@link Result | Result<T>} from which success
461
+ * results are to be aggregated.
462
+ * @returns {@link Success} with an array of `<T>` if any results were successful. If
463
+ * all {@link Result | results} failed, returns {@link Failure} with a concatenated
464
+ * summary of all error messages.
465
+ * @public
466
+ */
467
+ function mapSuccess(results) {
468
+ const errors = [];
469
+ const elements = [];
470
+ for (const result of results) {
471
+ if (result.isSuccess()) {
472
+ elements.push(result.value);
473
+ }
474
+ else {
475
+ errors.push(result.message);
476
+ }
477
+ }
478
+ if (elements.length === 0 && errors.length > 0) {
479
+ return fail(errors.join('\n'));
480
+ }
481
+ return succeed(elements);
482
+ }
483
+ exports.mapSuccess = mapSuccess;
484
+ /**
485
+ * Aggregates error messages from a collection of {@link Result | Result<T>}.
486
+ * @param results - An iterable collection of {@link Result | Result<T>} for which
487
+ * error messages are aggregated.
488
+ * @returns An array of strings consisting of all error messages returned by
489
+ * {@link Result | results} in the source collection. Ignores {@link Success}
490
+ * results and returns an empty array if there were no errors.
491
+ * @public
492
+ */
493
+ function mapFailures(results) {
494
+ const errors = [];
495
+ for (const result of results) {
496
+ if (result.isFailure()) {
497
+ errors.push(result.message);
498
+ }
499
+ }
500
+ return errors;
501
+ }
502
+ exports.mapFailures = mapFailures;
503
+ /**
504
+ * Determines if an iterable collection of {@link Result | Result<T>} were all successful.
505
+ * @param results - The collection of {@link Result | Result<T>} to be tested.
506
+ * @returns Returns {@link Success} with `true` if all {@link Result | results} are successful.
507
+ * If any are unsuccessful, returns {@link Failure} with a concatenated summary the error
508
+ * messages from all failed elements.
509
+ * @public
510
+ */
511
+ function allSucceed(results, successValue) {
512
+ const errors = [];
513
+ // istanbul ignore else
514
+ if (results !== undefined) {
515
+ for (const result of results) {
516
+ if (result.isFailure()) {
517
+ errors.push(result.message);
518
+ }
519
+ }
520
+ }
521
+ if (errors.length > 0) {
522
+ return fail(errors.join('\n'));
523
+ }
524
+ return succeed(successValue);
525
+ }
526
+ exports.allSucceed = allSucceed;
527
+ function populateObject(initializers, optionsOrOrder) {
528
+ var _a;
529
+ const options = optionsOrOrder
530
+ ? Array.isArray(optionsOrOrder)
531
+ ? { order: optionsOrOrder }
532
+ : optionsOrOrder
533
+ : {};
534
+ const state = {};
535
+ const errors = [];
536
+ const keys = Array.from((_a = options.order) !== null && _a !== void 0 ? _a : []);
537
+ const foundKeys = new Set(options.order);
538
+ // start with the supplied order then append anything else we find
539
+ for (const key in initializers) {
540
+ if (!foundKeys.has(key)) {
541
+ keys.push(key);
542
+ foundKeys.add(key);
543
+ }
544
+ }
545
+ for (const key of keys) {
546
+ if (initializers[key]) {
547
+ const result = initializers[key](state);
548
+ if (result.isSuccess()) {
549
+ if (result.value === undefined) {
550
+ if (options.suppressUndefined === true ||
551
+ (Array.isArray(options.suppressUndefined) && options.suppressUndefined.includes(key))) {
552
+ continue;
553
+ }
554
+ }
555
+ state[key] = result.value;
556
+ }
557
+ else {
558
+ errors.push(result.message);
559
+ }
560
+ }
561
+ else {
562
+ errors.push(`populateObject: Key ${String(key)} is present but has no initializer`);
563
+ }
564
+ }
565
+ if (errors.length > 0) {
566
+ return fail(errors.join('\n'));
567
+ }
568
+ return succeed(state);
569
+ }
570
+ exports.populateObject = populateObject;
571
+ //# sourceMappingURL=result.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.js","sourceRoot":"","sources":["../../../src/packlets/base/result.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAoKH;;;;GAIG;AACH,MAAa,OAAO;IAUlB;;;OAGG;IACH,YAAmB,KAAQ;QAb3B;;WAEG;QACa,YAAO,GAAS,IAAI,CAAC;QAWnC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAED;;OAEG;IACH,IAAW,KAAK;QACd,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED;;OAEG;IACI,SAAS;QACd,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACI,SAAS;QACd,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;OAEG;IACI,OAAO,CAAC,QAAwB;QACrC,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED;;OAEG;IACI,SAAS,CAAC,IAAQ;;QACvB,OAAO,MAAA,IAAI,CAAC,MAAM,mCAAI,IAAI,CAAC;IAC7B,CAAC;IAED;;OAEG;IACI,eAAe,CAAC,QAAwB;QAC7C,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED;;OAEG;IACI,iBAAiB,CAAC,IAAQ;;QAC/B,OAAO,MAAA,IAAI,CAAC,MAAM,mCAAI,IAAI,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AC,GAAG,CAAC,EAAE;YACvB,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YACf,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;SACpB;KACF;IAED,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE;QACtB,IAAI,YAAY,CAAC,GAAG,CAAC,EAAE;YACrB,MAAM,MAAM,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;YACxC,IAAI,MAAM,CAAC,SAAS,EAAE,EAAE;gBACtB,IAAI,MAAM,CAAC,KAAK,KAAK,SAAS,EAAE;oBAC9B,IACE,OAAO,CAAC,iBAAiB,KAAK,IAAI;wBAClC,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,iBAAiB,CAAC,IAAI,OAAO,CAAC,iBAAiB,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,EACrF;wBACA,SAAS;qBACV;iBACF;gBACD,KAAK,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC;aAC3B;iBAAM;gBACL,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;aAC7B;SACF;aAAM;YACL,MAAM,CAAC,IAAI,CAAC,uBAAuB,MAAM,CAAC,GAAG,CAAC,oCAAoC,CAAC,CAAC;SACrF;KACF;IAED,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;QACrB,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;KAChC;IACD,OAAO,OAAO,CAAC,KAAU,CAAC,CAAC;AAC7B,CAAC;AA/CD,wCA+CC","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\n/* eslint-disable no-use-before-define */\n/**\n * Represents the {@link IResult | result} of some operation or sequence of operations.\n * @remarks\n * {@link Success | Success<T>} and {@link Failure | Failure<T>} share the common\n * contract {@link IResult}, enabling commingled discriminated usage.\n * @public\n */\nexport type Result<T> = Success<T> | Failure<T>;\n/**\n * Continuation callback to be called in the event that an\n * {@link Result} is successful.\n * @public\n */\nexport type SuccessContinuation<T, TN> = (value: T) => Result<TN>;\n/**\n * Continuation callback to be called in the event that an\n * {@link Result} fails.\n * @public\n */\nexport type FailureContinuation<T> = (message: string) => Result<T>;\n/**\n * Type inference to determine the result type of an {@link Result}.\n * @beta\n */\nexport type ResultValueType<T> = T extends Result<infer TV> ? TV : never;\n\n/**\n * Simple logger interface used by {@link IResult.orThrow}.\n * @public\n */\nexport interface IResultLogger {\n /**\n * Log an error message.\n * @param message - The message to be logged.\n */\n error(message: string): void;\n}\n\n/**\n * Represents the result of some operation of sequence of operations.\n * @remarks\n * This common contract enables commingled discriminated usage of {@link Success | Success<T>}\n * and {@link Failure | Failure<T>}.\n * @public\n */\nexport interface IResult<T> {\n /**\n * Indicates whether the operation was successful.\n */\n readonly success: boolean;\n\n /**\n * Indicates whether this operation was successful. Functions\n * as a type guard for {@link Success | Success<T>}.\n */\n isSuccess(): this is Success<T>;\n\n /**\n * Indicates whether this operation failed. Functions\n * as a type guard for {@link Failure | Failure<T>}.\n */\n isFailure(): this is Failure<T>;\n\n /**\n * Gets the value associated with a successful {@link IResult | result},\n * or throws the error message if the corresponding operation failed.\n *\n * Note that `getValueOrThrow` is being superseded by `orThrow` and\n * will eventually be deprecated. Please use orDefault instead.\n *\n * @param logger - An optional {@link IResultLogger | logger} to which the\n * error will also be reported.\n * @returns The return value, if the operation was successful.\n * @throws The error message if the operation failed.\n */\n getValueOrThrow(logger?: IResultLogger): T;\n\n /**\n * Gets the value associated with a successful {@link IResult | result},\n * or a default value if the corresponding operation failed.\n * @param dflt - The value to be returned if the operation failed (default is\n * `undefined`).\n *\n * Note that `getValueOrDefault` is being superseded by `orDefault` and\n * will eventually be deprecated. Please use orDefault instead.\n *\n * @returns The return value, if the operation was successful. Returns\n * the supplied default value or `undefined` if no default is supplied.\n */\n getValueOrDefault(dflt?: T): T | undefined;\n\n /**\n * Gets the value associated with a successful {@link IResult | result},\n * or throws the error message if the corresponding operation failed.\n * @param logger - An optional {@link IResultLogger | logger} to which the\n * error will also be reported.\n * @returns The return value, if the operation was successful.\n * @throws The error message if the operation failed.\n */\n orThrow(logger?: IResultLogger): T;\n\n /**\n * Gets the value associated with a successful {@link IResult | result},\n * or a default value if the corresponding operation failed.\n * @param dflt - The value to be returned if the operation failed (default is\n * `undefined`).\n * @returns The return value, if the operation was successful. Returns\n * the supplied default value or `undefined` if no default is supplied.\n */\n orDefault(dflt?: T): T | undefined;\n\n /**\n * Calls a supplied {@link SuccessContinuation | success continuation} if\n * the operation was a success.\n * @remarks\n * The {@link SuccessContinuation | success continuation} might return a\n * different result type than {@link IResult} on which it is invoked. This\n * enables chaining of operations with heterogenous return types.\n *\n * @param cb - The {@link SuccessContinuation | success continuation} to\n * be called in the event of success.\n * @returns If this operation was successful, returns the value returned\n * by the {@link SuccessContinuation | success continuation}. If this result\n * failed, propagates the error message from this failure.\n */\n onSuccess<TN>(cb: SuccessContinuation<T, TN>): Result<TN>;\n\n /**\n * Calls a supplied {@link FailureContinuation | failed continuation} if\n * the operation failed.\n * @param cb - The {@link FailureContinuation | failure continuation} to\n * be called in the event of failure.\n * @returns If this operation failed, returns the value returned by the\n * {@link FailureContinuation | failure continuation}. If this result\n * was successful, propagates the result value from the successful event.\n */\n onFailure(cb: FailureContinuation<T>): Result<T>;\n\n /**\n * Converts a {@link IResult | IResult<T>} to a {@link DetailedResult | DetailedResult<T, TD>},\n * adding a supplied detail if the operation failed.\n * @param detail - The detail to be added if this operation failed.\n * @returns A new {@link DetailedResult | DetailedResult<T, TD>} with either\n * the success result or the error message from this {@link IResult}, with\n * the supplied detail (if this event failed) or detail `undefined` (if\n * this result succeeded).\n */\n withFailureDetail<TD>(detail: TD): DetailedResult<T, TD>;\n\n /**\n * Converts a {@link IResult | IResult<T>} to a {@link DetailedResult | DetailedResult<T, TD>},\n * adding supplied details.\n * @param detail - The default detail to be added to the new {@link DetailedResult}.\n * @param successDetail - An optional detail to be added if this result was successful.\n * @returns A new {@link DetailedResult | DetailedResult<T, TD>} with either\n * the success result or the error message from this {@link IResult} and the\n * appropriate added detail.\n */\n withDetail<TD>(detail: TD, successDetail?: TD): DetailedResult<T, TD>;\n}\n\n/**\n * Reports a successful {@link IResult | result} from some operation and the\n * corresponding value.\n * @public\n */\nexport class Success<T> implements IResult<T> {\n /**\n * {@inheritdoc IResult.success}\n */\n public readonly success: true = true;\n /**\n * @internal\n */\n private readonly _value: T;\n\n /**\n * Constructs a {@link Success} with the supplied value.\n * @param value - The value to be returned.\n */\n public constructor(value: T) {\n this._value = value;\n }\n\n /**\n * The result value returned by the successful operation.\n */\n public get value(): T {\n return this._value;\n }\n\n /**\n * {@inheritdoc IResult.isSuccess}\n */\n public isSuccess(): this is Success<T> {\n return true;\n }\n\n /**\n * {@inheritdoc IResult.isFailure}\n */\n public isFailure(): this is Failure<T> {\n return false;\n }\n\n /**\n * {@inheritdoc IResult.orThrow}\n */\n public orThrow(__logger?: IResultLogger): T {\n return this._value;\n }\n\n /**\n * {@inheritdoc IResult.orDefault}\n */\n public orDefault(dflt?: T): T | undefined {\n return this._value ?? dflt;\n }\n\n /**\n * {@inheritdoc IResult.getValueOrThrow}\n */\n public getValueOrThrow(__logger?: IResultLogger): T {\n return this._value;\n }\n\n /**\n * {@inheritdoc IResult.getValueOrDefault}\n */\n public getValueOrDefault(dflt?: T): T | undefined {\n return this._value ?? dflt;\n }\n\n /**\n * {@inheritdoc IResult.onSuccess}\n */\n public onSuccess<TN>(cb: SuccessContinuation<T, TN>): Result<TN> {\n return cb(this.value);\n }\n\n /**\n * {@inheritdoc IResult.onFailure}\n */\n public onFailure(__: FailureContinuation<T>): Result<T> {\n return this;\n }\n\n /**\n * {@inheritdoc IResult.withFailureDetail}\n */\n public withFailureDetail<TD>(__detail: TD): DetailedResult<T, TD> {\n return succeedWithDetail(this.value);\n }\n\n /**\n * {@inheritdoc IResult.withDetail}\n */\n public withDetail<TD>(detail: TD, successDetail?: TD): DetailedResult<T, TD> {\n return succeedWithDetail(this.value, successDetail ?? detail);\n }\n}\n\n/**\n * Reports a failed {@link IResult | result} from some operation, with an error message.\n * @public\n */\nexport class Failure<T> implements IResult<T> {\n /**\n * {@inheritdoc IResult.success}\n */\n public readonly success: false = false;\n\n /**\n * @internal\n */\n private readonly _message: string;\n\n /**\n * Constructs a {@link Failure} with the supplied message.\n * @param message - Error message to be reported.\n */\n public constructor(message: string) {\n this._message = message;\n }\n\n /**\n * Gets the error message associated with this error.\n */\n public get message(): string {\n return this._message;\n }\n\n /**\n * {@inheritdoc IResult.isSuccess}\n */\n public isSuccess(): this is Success<T> {\n return false;\n }\n\n /**\n * {@inheritdoc IResult.isFailure}\n */\n public isFailure(): this is Failure<T> {\n return true;\n }\n\n /**\n * {@inheritdoc IResult.orThrow}\n */\n public orThrow(logger?: IResultLogger): never {\n if (logger !== undefined) {\n logger.error(this._message);\n }\n throw new Error(this._message);\n }\n\n /**\n * {@inheritdoc IResult.orDefault}\n */\n public orDefault(dflt?: T): T | undefined {\n return dflt;\n }\n\n /**\n * {@inheritdoc IResult.getValueOrThrow}\n */\n public getValueOrThrow(logger?: IResultLogger): never {\n if (logger !== undefined) {\n logger.error(this._message);\n }\n throw new Error(this._message);\n }\n\n /**\n * {@inheritdoc IResult.getValueOrDefault}\n */\n public getValueOrDefault(dflt?: T): T | undefined {\n return dflt;\n }\n\n /**\n * {@inheritdoc IResult.onSuccess}\n */\n public onSuccess<TN>(__: SuccessContinuation<T, TN>): Result<TN> {\n return new Failure(this.message);\n }\n\n /**\n * {@inheritdoc IResult.onFailure}\n */\n public onFailure(cb: FailureContinuation<T>): Result<T> {\n return cb(this.message);\n }\n\n /**\n * {@inheritdoc IResult.withFailureDetail}\n */\n public withFailureDetail<TD>(detail: TD): DetailedResult<T, TD> {\n return failWithDetail(this.message, detail);\n }\n\n /**\n * {@inheritdoc IResult.withDetail}\n */\n public withDetail<TD>(detail: TD, __successDetail?: TD): DetailedResult<T, TD> {\n return failWithDetail(this.message, detail);\n }\n\n /**\n * Get a 'friendly' string representation of this object.\n * @remarks\n * The string representation of a {@link Failure} value is the error message.\n * @returns A string representing this object.\n */\n public toString(): string {\n return this.message;\n }\n}\n\n/**\n * Returns {@link Success | Success<T>} with the supplied result value.\n * @param value - The successful result value to be returned\n * @public\n */\nexport function succeed<T>(value: T): Success<T> {\n return new Success<T>(value);\n}\n\n/**\n * Returns {@link Failure | Failure<T>} with the supplied error message.\n * @param message - Error message to be returned.\n * @public\n */\nexport function fail<T>(message: string): Failure<T> {\n return new Failure<T>(message);\n}\n\n/**\n * Callback to be called when a {@link DetailedResult} encounters success.\n * @remarks\n * A success callback can return a different result type than it receives, allowing\n * success results to chain through intermediate result types.\n * @public\n */\nexport type DetailedSuccessContinuation<T, TD, TN> = (value: T, detail?: TD) => DetailedResult<TN, TD>;\n\n/**\n * Callback to be called when a {@link DetailedResult} encounters a failure.\n * @remarks\n * A failure callback can change {@link Failure} to {@link Success} (e.g. by returning a default value)\n * or it can change or embellish the error message, but it cannot change the success return type.\n * @public\n */\nexport type DetailedFailureContinuation<T, TD> = (message: string, detail: TD) => DetailedResult<T, TD>;\n\n/**\n * A {@link DetailedSuccess} extends {@link Success} to report optional success details in\n * addition to the error message.\n * @public\n */\nexport class DetailedSuccess<T, TD> extends Success<T> {\n /**\n * @internal\n */\n protected _detail?: TD;\n\n /**\n * Constructs a new {@link DetailedSuccess | DetailedSuccess<T, TD>} with the supplied\n * value and detail.\n * @param value - The value to be returned.\n * @param detail - An optional successful detail to be returned. If omitted, detail\n * will be `undefined`.\n */\n public constructor(value: T, detail?: TD) {\n super(value);\n this._detail = detail;\n }\n\n /**\n * The success detail associated with this {@link DetailedSuccess}, or `undefined` if\n * no detail was supplied.\n */\n public get detail(): TD | undefined {\n return this._detail;\n }\n\n /**\n * Reports that this {@link DetailedSuccess} is a success.\n * @remarks\n * Always true for {@link DetailedSuccess} but can be used as type guard\n * to discriminate {@link DetailedSuccess} from {@link DetailedFailure} in\n * a {@link DetailedResult}.\n * @returns `true`\n */\n public isSuccess(): this is DetailedSuccess<T, TD> {\n return true;\n }\n\n /**\n * Invokes the supplied {@link DetailedSuccessContinuation | success callback} and propagates\n * its returned {@link DetailedResult | DetailedResult<TN, TD>}.\n * @remarks\n * The success callback mutates the return type from `<T>` to `<TN>`.\n * @param cb - The {@link DetailedSuccessContinuation | success callback} to be invoked.\n * @returns The {@link DetailedResult | DetailedResult<T, TD>} returned by the success callback.\n */\n public onSuccess<TN>(cb: DetailedSuccessContinuation<T, TD, TN>): DetailedResult<TN, TD> {\n return cb(this.value, this._detail);\n }\n\n /**\n * Propagates this {@link DetailedSuccess}.\n * @remarks\n * Failure does not mutate return type so we can return this event directly.\n * @param _cb - {@link DetailedFailureContinuation | Failure callback} to be called\n * on a {@link DetailedResult} in case of failure (ignored).\n * @returns `this`\n */\n public onFailure(__cb: DetailedFailureContinuation<T, TD>): DetailedResult<T, TD> {\n return this;\n }\n}\n\n/**\n * A {@link DetailedFailure} extends {@link Failure} to report optional failure details in\n * addition to the error message.\n * @public\n */\nexport class DetailedFailure<T, TD> extends Failure<T> {\n /**\n * @internal\n */\n protected _detail: TD;\n\n /**\n * Constructs a new {@link DetailedFailure | DetailedFailure<T, TD>} with the supplied\n * message and detail.\n * @param message - The message to be returned.\n * @param detail - The error detail to be returned.\n */\n public constructor(message: string, detail: TD) {\n super(message);\n this._detail = detail;\n }\n\n /**\n * The error detail associated with this {@link DetailedFailure}.\n */\n public get detail(): TD {\n return this._detail;\n }\n\n /**\n * Reports that this {@link DetailedFailure} is a failure.\n * @remarks\n * Always true for {@link DetailedFailure} but can be used as type guard\n * to discriminate {@link DetailedSuccess} from {@link DetailedFailure} in\n * a {@link DetailedResult}.\n * @returns `true`\n */\n public isFailure(): this is DetailedFailure<T, TD> {\n return true;\n }\n\n /**\n * Propagates the error message and detail from this result.\n * @remarks\n * Mutates the success type as the success callback would have, but does not\n * call the success callback.\n * @param _cb - {@link DetailedSuccessContinuation | Success callback} to be called\n * on a {@link DetailedResult} in case of success (ignored).\n * @returns A new {@link DetailedFailure | DetailedFailure<TN, TD>} which contains\n * the error message and detail from this one.\n */\n public onSuccess<TN>(__cb: DetailedSuccessContinuation<T, TD, TN>): DetailedResult<TN, TD> {\n return new DetailedFailure<TN, TD>(this.message, this._detail);\n }\n\n /**\n * Invokes the supplied {@link DetailedFailureContinuation | failure callback} and propagates\n * its returned {@link DetailedResult | DetailedResult<T, TD>}.\n * @param cb - The {@link DetailedFailureContinuation | failure callback} to be invoked.\n * @returns The {@link DetailedResult | DetailedResult<T, TD>} returned by the failure callback.\n */\n public onFailure(cb: DetailedFailureContinuation<T, TD>): DetailedResult<T, TD> {\n return cb(this.message, this._detail);\n }\n}\n\n/**\n * Type inference to determine the result type `T` of a {@link DetailedResult | DetailedResult<T, TD>}.\n * @beta\n */\nexport type DetailedResult<T, TD> = DetailedSuccess<T, TD> | DetailedFailure<T, TD>;\n\n/**\n * Type inference to determine the detail type `TD` of a {@link DetailedResult | DetailedResult<T, TD>}.\n * @beta\n */\nexport type ResultDetailType<T> = T extends DetailedResult<unknown, infer TD> ? TD : never;\n\n/**\n * Returns {@link DetailedSuccess | DetailedSuccess<T, TD>} with a supplied value and optional\n * detail.\n * @param value - The value of type `<T>` to be returned.\n * @param detail - An optional detail of type `<TD>` to be returned.\n * @returns A {@link DetailedSuccess | DetailedSuccess<T, TD>} with the supplied value\n * and detail, if supplied.\n * @public\n */\nexport function succeedWithDetail<T, TD>(value: T, detail?: TD): DetailedSuccess<T, TD> {\n return new DetailedSuccess<T, TD>(value, detail);\n}\n\n/**\n * Returns {@link DetailedFailure | DetailedFailure<T, TD>} with a supplied error message and detail.\n * @param message - The error message to be returned.\n * @param detail - The event detail to be returned.\n * @returns An {@link DetailedFailure | DetailedFailure<T, TD>} with the supplied error\n * message and detail.\n * @public\n */\nexport function failWithDetail<T, TD>(message: string, detail: TD): DetailedFailure<T, TD> {\n return new DetailedFailure<T, TD>(message, detail);\n}\n\n/**\n * Propagates a {@link Success} or {@link Failure} {@link Result}, adding supplied\n * event details as appropriate.\n * @param result - The {@link Result} to be propagated.\n * @param detail - The event detail (type `<TD>`) to be added to the {@link Result | result}.\n * @param successDetail - An optional distinct event detail to be added to {@link Success} results. If `successDetail`\n * is omitted or `undefined`, then `detail` will be applied to {@link Success} results.\n * @returns A new {@link DetailedResult | DetailedResult<T, TD>} with the success value or error\n * message from the original `result` but with the specified detail added.\n * @public\n */\nexport function propagateWithDetail<T, TD>(\n result: Result<T>,\n detail: TD,\n successDetail?: TD\n): DetailedResult<T, TD> {\n return result.isSuccess()\n ? succeedWithDetail(result.value, successDetail ?? detail)\n : failWithDetail(result.message, detail);\n}\n\n/**\n * Wraps a function which might throw to convert exception results\n * to {@link Failure}.\n * @param func - The function to be captured.\n * @returns Returns {@link Success} with a value of type `<T>` on\n * success , or {@link Failure} with the thrown error message if\n * `func` throws an `Error`.\n * @public\n */\nexport function captureResult<T>(func: () => T): Result<T> {\n try {\n return succeed(func());\n } catch (err) {\n return fail((err as Error).message);\n }\n}\n\n/**\n * Aggregates successful result values from a collection of {@link Result | Result<T>}.\n * @param results - The collection of {@link Result | Result<T>} to be mapped.\n * @returns If all {@link Result | results} are successful, returns {@link Success} with an\n * array containing all returned values. If any {@link Result | results} failed, returns\n * {@link Failure} with a concatenated summary of all error messages.\n * @public\n */\nexport function mapResults<T>(results: Iterable<Result<T>>): Result<T[]> {\n const errors: string[] = [];\n const elements: T[] = [];\n\n for (const result of results) {\n if (result.isSuccess()) {\n elements.push(result.value);\n } else {\n errors.push(result.message);\n }\n }\n\n if (errors.length > 0) {\n return fail(errors.join('\\n'));\n }\n return succeed(elements);\n}\n\n/**\n * Aggregates successful results from a collection of {@link DetailedResult | DetailedResult<T, TD>},\n * optionally ignoring certain error details.\n * @param results - The collection of {@link DetailedResult | DetailedResult<T, TD>} to be mapped.\n * @param ignore - An array of error detail values (of type `<TD>`) that should be ignored.\n * @returns {@link Success} with an array containing all successful results if all results either\n * succeeded or returned error details listed in `ignore`. If any results failed with details\n * that cannot be ignored, returns {@link Failure} with an concatenated summary of all non-ignorable\n * error messages.\n * @public\n */\nexport function mapDetailedResults<T, TD>(\n results: Iterable<DetailedResult<T, TD>>,\n ignore: TD[]\n): Result<T[]> {\n const errors: string[] = [];\n const elements: T[] = [];\n\n for (const result of results) {\n if (result.isSuccess()) {\n elements.push(result.value);\n } else if (!ignore.includes(result.detail)) {\n errors.push(result.message);\n }\n }\n\n if (errors.length > 0) {\n return fail(errors.join('\\n'));\n }\n return succeed(elements);\n}\n\n/**\n * Aggregates successful results from a a collection of {@link Result | Result<T>}.\n * @param results - An `Iterable` of {@link Result | Result<T>} from which success\n * results are to be aggregated.\n * @returns {@link Success} with an array of `<T>` if any results were successful. If\n * all {@link Result | results} failed, returns {@link Failure} with a concatenated\n * summary of all error messages.\n * @public\n */\nexport function mapSuccess<T>(results: Iterable<Result<T>>): Result<T[]> {\n const errors: string[] = [];\n const elements: T[] = [];\n\n for (const result of results) {\n if (result.isSuccess()) {\n elements.push(result.value);\n } else {\n errors.push(result.message);\n }\n }\n\n if (elements.length === 0 && errors.length > 0) {\n return fail(errors.join('\\n'));\n }\n return succeed(elements);\n}\n\n/**\n * Aggregates error messages from a collection of {@link Result | Result<T>}.\n * @param results - An iterable collection of {@link Result | Result<T>} for which\n * error messages are aggregated.\n * @returns An array of strings consisting of all error messages returned by\n * {@link Result | results} in the source collection. Ignores {@link Success}\n * results and returns an empty array if there were no errors.\n * @public\n */\nexport function mapFailures<T>(results: Iterable<Result<T>>): string[] {\n const errors: string[] = [];\n for (const result of results) {\n if (result.isFailure()) {\n errors.push(result.message);\n }\n }\n return errors;\n}\n\n/**\n * Determines if an iterable collection of {@link Result | Result<T>} were all successful.\n * @param results - The collection of {@link Result | Result<T>} to be tested.\n * @returns Returns {@link Success} with `true` if all {@link Result | results} are successful.\n * If any are unsuccessful, returns {@link Failure} with a concatenated summary the error\n * messages from all failed elements.\n * @public\n */\nexport function allSucceed<T>(results: Iterable<Result<unknown>>, successValue: T): Result<T> {\n const errors: string[] = [];\n\n // istanbul ignore else\n if (results !== undefined) {\n for (const result of results) {\n if (result.isFailure()) {\n errors.push(result.message);\n }\n }\n }\n\n if (errors.length > 0) {\n return fail(errors.join('\\n'));\n }\n return succeed(successValue);\n}\n\n/**\n * String-keyed record of initialization functions to be passed to {@link (populateObject:1)}\n * or {@link (populateObject:2)}.\n * @public\n */\nexport type FieldInitializers<T> = { [key in keyof T]: (state: Partial<T>) => Result<T[key]> };\n\n/**\n * Options for the {@link (populateObject:1)} function.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface PopulateObjectOptions<T> {\n /**\n * If present, specifies the order in which property values should\n * be evaluated. Any keys not listed are evaluated after all listed\n * keys in indeterminate order. If 'order' is not present, keys\n * are evaluated in indeterminate order.\n */\n order?: (keyof T)[];\n\n /**\n * Specify handling of `undefined` values. By default, successful\n * `undefined` results are written to the result object. If this value\n * is `true` then `undefined` results are suppressed for all properties.\n * If this value is an array of property keys then `undefined` results\n * are suppressed for those properties only.\n */\n suppressUndefined?: boolean | (keyof T)[];\n}\n\n/**\n * Populates an an object based on a prototype full of field initializers that return {@link Result | Result<T[key]>}.\n * Returns {@link Success} with the populated object if all initializers succeed, or {@link Failure} with a\n * concatenated list of all error messages.\n * @param initializers - An object with the shape of the target but with initializer functions for\n * each property.\n * @param options - An optional {@link PopulateObjectOptions | set of options} which\n * modify the behavior of this call.\n * {@label WITH_OPTIONS}\n * @public\n */\nexport function populateObject<T>(\n initializers: FieldInitializers<T>,\n options?: PopulateObjectOptions<T>\n): Result<T>;\n\n/**\n * Populates an an object based on a prototype full of field initializers that return {@link Result | Result<T[key]>}.\n * Returns {@link Success} with the populated object if all initializers succeed, or {@link Failure} with a\n * concatenated list of all error messages.\n * @param initializers - An object with the shape of the target but with initializer functions for\n * each property.\n * @param order - Optional order in which keys should be written.\n * @public\n * {@label WITH_ORDER}\n * @deprecated Pass {@link PopulateObjectOptions} instead.\n */\nexport function populateObject<T>(initializers: FieldInitializers<T>, order: (keyof T)[]): Result<T>;\nexport function populateObject<T>(\n initializers: FieldInitializers<T>,\n optionsOrOrder?: PopulateObjectOptions<T> | (keyof T)[]\n): Result<T> {\n const options: PopulateObjectOptions<T> = optionsOrOrder\n ? Array.isArray(optionsOrOrder)\n ? { order: optionsOrOrder }\n : optionsOrOrder\n : {};\n const state = {} as { [key in keyof T]: T[key] };\n const errors: string[] = [];\n const keys: (keyof T)[] = Array.from(options.order ?? []);\n const foundKeys = new Set<keyof T>(options.order);\n\n // start with the supplied order then append anything else we find\n for (const key in initializers) {\n if (!foundKeys.has(key)) {\n keys.push(key);\n foundKeys.add(key);\n }\n }\n\n for (const key of keys) {\n if (initializers[key]) {\n const result = initializers[key](state);\n if (result.isSuccess()) {\n if (result.value === undefined) {\n if (\n options.suppressUndefined === true ||\n (Array.isArray(options.suppressUndefined) && options.suppressUndefined.includes(key))\n ) {\n continue;\n }\n }\n state[key] = result.value;\n } else {\n errors.push(result.message);\n }\n } else {\n errors.push(`populateObject: Key ${String(key)} is present but has no initializer`);\n }\n }\n\n if (errors.length > 0) {\n return fail(errors.join('\\n'));\n }\n return succeed(state as T);\n}\n"]}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/packlets/base/utils.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,MAAM,EAAiB,MAAM,UAAU,CAAC;AAEjD;;;;;;;GAOG;AAEH,wBAAgB,OAAO,CAAC,CAAC,SAAS,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,IAAI,EAAE,CAAC,GAAG,GAAG,IAAI,MAAM,CAAC,CAEhG;AAED;;;;;;;GAOG;AACH,wBAAgB,IAAI,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,GAAG,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAQ3F;AAED;;;;;;;;GAQG;AACH,wBAAgB,IAAI,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,GAAG,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAO3F;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,2BAA2B,CAAC,CAAC,SAAS,MAAM,EAC1D,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAC7B,IAAI,EAAE,CAAC,EACP,YAAY,CAAC,EAAE,OAAO,GACrB,OAAO,GAAG,SAAS,CAErB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,MAAM,EAChD,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAC7B,IAAI,EAAE,CAAC,GAEL,QAAQ,GACR,QAAQ,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