@fgv/ts-utils 5.0.0-12 → 5.0.0-15

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 (288) hide show
  1. package/.vscode/launch.json +16 -0
  2. package/.vscode/settings.json +36 -0
  3. package/config/api-extractor.json +343 -0
  4. package/config/rig.json +16 -0
  5. package/dist/tsdoc-metadata.json +1 -1
  6. package/lib/test/helpers/jest/helpers/fsHelpers.d.ts +52 -0
  7. package/lib/test/helpers/jest/helpers/fsHelpers.js +180 -0
  8. package/lib/test/helpers/jest/helpers/index.d.ts +2 -0
  9. package/lib/test/helpers/jest/helpers/index.js +18 -0
  10. package/lib/test/helpers/jest/index.d.ts +3 -0
  11. package/lib/test/helpers/jest/index.js +27 -0
  12. package/lib/test/helpers/jest/matchers/index.d.ts +16 -0
  13. package/lib/test/helpers/jest/matchers/index.js +19 -0
  14. package/lib/test/helpers/jest/matchers/toFail/index.d.ts +16 -0
  15. package/lib/test/helpers/jest/matchers/toFail/index.js +25 -0
  16. package/lib/test/helpers/jest/matchers/toFail/predicate.d.ts +4 -0
  17. package/lib/test/helpers/jest/matchers/toFail/predicate.js +9 -0
  18. package/lib/test/helpers/jest/matchers/toFailTest/index.d.ts +16 -0
  19. package/lib/test/helpers/jest/matchers/toFailTest/index.js +30 -0
  20. package/lib/test/helpers/jest/matchers/toFailTest/predicate.d.ts +3 -0
  21. package/lib/test/helpers/jest/matchers/toFailTest/predicate.js +12 -0
  22. package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/index.d.ts +17 -0
  23. package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/index.js +26 -0
  24. package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/predicate.d.ts +4 -0
  25. package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/predicate.js +15 -0
  26. package/lib/test/helpers/jest/matchers/toFailTestWith/index.d.ts +18 -0
  27. package/lib/test/helpers/jest/matchers/toFailTestWith/index.js +31 -0
  28. package/lib/test/helpers/jest/matchers/toFailTestWith/predicate.d.ts +4 -0
  29. package/lib/test/helpers/jest/matchers/toFailTestWith/predicate.js +30 -0
  30. package/lib/test/helpers/jest/matchers/toFailWith/index.d.ts +18 -0
  31. package/lib/test/helpers/jest/matchers/toFailWith/index.js +29 -0
  32. package/lib/test/helpers/jest/matchers/toFailWith/predicate.d.ts +4 -0
  33. package/lib/test/helpers/jest/matchers/toFailWith/predicate.js +20 -0
  34. package/lib/test/helpers/jest/matchers/toFailWithDetail/index.d.ts +19 -0
  35. package/lib/test/helpers/jest/matchers/toFailWithDetail/index.js +29 -0
  36. package/lib/test/helpers/jest/matchers/toFailWithDetail/predicate.d.ts +4 -0
  37. package/lib/test/helpers/jest/matchers/toFailWithDetail/predicate.js +26 -0
  38. package/lib/test/helpers/jest/matchers/toSucceed/index.d.ts +16 -0
  39. package/lib/test/helpers/jest/matchers/toSucceed/index.js +25 -0
  40. package/lib/test/helpers/jest/matchers/toSucceed/predicate.d.ts +4 -0
  41. package/lib/test/helpers/jest/matchers/toSucceed/predicate.js +9 -0
  42. package/lib/test/helpers/jest/matchers/toSucceedAndMatchInlineSnapshot/index.d.ts +17 -0
  43. package/lib/test/helpers/jest/matchers/toSucceedAndMatchInlineSnapshot/index.js +26 -0
  44. package/lib/test/helpers/jest/matchers/toSucceedAndMatchSnapshot/index.d.ts +17 -0
  45. package/lib/test/helpers/jest/matchers/toSucceedAndMatchSnapshot/index.js +26 -0
  46. package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/index.d.ts +19 -0
  47. package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/index.js +55 -0
  48. package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/predicate.d.ts +4 -0
  49. package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/predicate.js +21 -0
  50. package/lib/test/helpers/jest/matchers/toSucceedWith/index.d.ts +18 -0
  51. package/lib/test/helpers/jest/matchers/toSucceedWith/index.js +29 -0
  52. package/lib/test/helpers/jest/matchers/toSucceedWith/predicate.d.ts +4 -0
  53. package/lib/test/helpers/jest/matchers/toSucceedWith/predicate.js +16 -0
  54. package/lib/test/helpers/jest/matchers/toSucceedWithDetail/index.d.ts +18 -0
  55. package/lib/test/helpers/jest/matchers/toSucceedWithDetail/index.js +29 -0
  56. package/lib/test/helpers/jest/matchers/toSucceedWithDetail/predicate.d.ts +4 -0
  57. package/lib/test/helpers/jest/matchers/toSucceedWithDetail/predicate.js +21 -0
  58. package/lib/test/helpers/jest/resolvers/cli.d.ts +5 -0
  59. package/lib/test/helpers/jest/resolvers/cli.js +48 -0
  60. package/lib/test/helpers/jest/resolvers/ide.d.ts +5 -0
  61. package/lib/test/helpers/jest/resolvers/ide.js +48 -0
  62. package/lib/test/helpers/jest/ts-utils.d.ts +2 -0
  63. package/lib/test/helpers/jest/ts-utils.js +18 -0
  64. package/lib/test/helpers/jest/types/index.d.ts +79 -0
  65. package/lib/test/helpers/jest/types/index.js +4 -0
  66. package/lib/test/helpers/jest/utils/matcherHelpers.d.ts +6 -0
  67. package/lib/test/helpers/jest/utils/matcherHelpers.js +53 -0
  68. package/lib/test/helpers/jest/utils/snapshotResolver.d.ts +4 -0
  69. package/lib/test/helpers/jest/utils/snapshotResolver.js +19 -0
  70. package/lib/test/unit/collections/collectible.test.d.ts +2 -0
  71. package/lib/test/unit/collections/collector.test.d.ts +2 -0
  72. package/lib/test/unit/collections/collectors.test.d.ts +2 -0
  73. package/lib/test/unit/collections/convertingCollector.test.d.ts +2 -0
  74. package/lib/test/unit/collections/helpers.d.ts +44 -0
  75. package/lib/test/unit/collections/helpers.js +115 -0
  76. package/lib/test/unit/collections/resultMap.test.d.ts +2 -0
  77. package/lib/test/unit/collections/validatingCollector.test.d.ts +2 -0
  78. package/lib/test/unit/collections/validatingConvertingCollector.test.d.ts +2 -0
  79. package/lib/test/unit/collections/validatingResultMap.test.d.ts +2 -0
  80. package/lib/test/unit/converter.test.d.ts +2 -0
  81. package/lib/test/unit/converters.test.d.ts +2 -0
  82. package/lib/test/unit/defaultingConverter.test.d.ts +2 -0
  83. package/lib/test/unit/file-tree/fileItem.test.d.ts +2 -0
  84. package/lib/test/unit/file-tree/fileTree.test.d.ts +2 -0
  85. package/lib/test/unit/file-tree/fsTree.test.d.ts +2 -0
  86. package/lib/test/unit/file-tree/inMemoryTree.test.d.ts +2 -0
  87. package/lib/test/unit/file-tree/sample/rootFile.json +1 -0
  88. package/lib/test/unit/file-tree/sample/some/other/path/file2.json +1 -0
  89. package/lib/test/unit/file-tree/sample/some/path/below/file4.json +1 -0
  90. package/lib/test/unit/file-tree/sample/some/path/file1.json +1 -0
  91. package/lib/test/unit/file-tree/sample/some/path/file3.json +1 -0
  92. package/lib/test/unit/hash.test.d.ts +2 -0
  93. package/lib/test/unit/logger.test.d.ts +2 -0
  94. package/lib/test/unit/mapResults.test.d.ts +3 -0
  95. package/lib/test/unit/messageAggregator.test.d.ts +3 -0
  96. package/lib/test/unit/normalize.test.d.ts +2 -0
  97. package/lib/test/unit/result.test.d.ts +3 -0
  98. package/lib/test/unit/utils.test.d.ts +3 -0
  99. package/lib/test/unit/validation/array.test.d.ts +2 -0
  100. package/lib/test/unit/validation/boolean.test.d.ts +2 -0
  101. package/lib/test/unit/validation/genericValidator.test.d.ts +2 -0
  102. package/lib/test/unit/validation/number.test.d.ts +2 -0
  103. package/lib/test/unit/validation/object.test.d.ts +2 -0
  104. package/lib/test/unit/validation/oneOf.test.d.ts +2 -0
  105. package/lib/test/unit/validation/recordOf.test.d.ts +2 -0
  106. package/lib/test/unit/validation/string.test.d.ts +2 -0
  107. package/lib/test/unit/validation/traits.test.d.ts +2 -0
  108. package/lib/test/unit/validation/typeGuard.test.d.ts +2 -0
  109. package/lib/test/unit/validation/validators.test.d.ts +2 -0
  110. package/package.json +9 -9
  111. package/src/index.ts +63 -0
  112. package/src/packlets/base/brand.ts +28 -0
  113. package/src/packlets/base/index.ts +31 -0
  114. package/src/packlets/base/logger.ts +156 -0
  115. package/src/packlets/base/mapResults.ts +302 -0
  116. package/src/packlets/base/messageAggregator.ts +120 -0
  117. package/src/packlets/base/normalize.ts +144 -0
  118. package/src/packlets/base/result.ts +961 -0
  119. package/src/packlets/base/utils.ts +236 -0
  120. package/src/packlets/collections/collectible.ts +238 -0
  121. package/src/packlets/collections/collector.ts +273 -0
  122. package/src/packlets/collections/collectorValidator.ts +178 -0
  123. package/src/packlets/collections/common.ts +27 -0
  124. package/src/packlets/collections/convertingCollector.ts +223 -0
  125. package/src/packlets/collections/convertingCollectorValidator.ts +164 -0
  126. package/src/packlets/collections/index.ts +39 -0
  127. package/src/packlets/collections/keyValueConverters.ts +142 -0
  128. package/src/packlets/collections/readonlyResultMap.ts +99 -0
  129. package/src/packlets/collections/resultMap.ts +322 -0
  130. package/src/packlets/collections/resultMapValidator.ts +188 -0
  131. package/src/packlets/collections/utils.ts +33 -0
  132. package/src/packlets/collections/validatingCollector.ts +123 -0
  133. package/src/packlets/collections/validatingConvertingCollector.ts +113 -0
  134. package/src/packlets/collections/validatingResultMap.ts +94 -0
  135. package/src/packlets/conversion/baseConverter.ts +330 -0
  136. package/src/packlets/conversion/converter.ts +260 -0
  137. package/src/packlets/conversion/converters.ts +1039 -0
  138. package/src/packlets/conversion/defaultingConverter.ts +188 -0
  139. package/src/packlets/conversion/index.ts +30 -0
  140. package/src/packlets/conversion/objectConverter.ts +259 -0
  141. package/src/packlets/conversion/stringConverter.ts +165 -0
  142. package/src/packlets/file-tree/directoryItem.ts +84 -0
  143. package/src/packlets/file-tree/fileItem.ts +119 -0
  144. package/src/packlets/file-tree/fileTree.ts +152 -0
  145. package/src/packlets/file-tree/fileTreeAccessors.ts +176 -0
  146. package/src/packlets/file-tree/fsTree.ts +118 -0
  147. package/src/packlets/file-tree/in-memory/inMemoryTree.ts +171 -0
  148. package/src/packlets/file-tree/in-memory/index.ts +23 -0
  149. package/src/packlets/file-tree/in-memory/treeBuilder.ts +203 -0
  150. package/src/packlets/file-tree/index.ts +31 -0
  151. package/src/packlets/hash/crcNormalizer.ts +81 -0
  152. package/src/packlets/hash/hashingNormalizer.ts +102 -0
  153. package/src/packlets/hash/index.ts +24 -0
  154. package/src/packlets/validation/array.ts +83 -0
  155. package/src/packlets/validation/boolean.ts +63 -0
  156. package/src/packlets/validation/classes.ts +38 -0
  157. package/src/packlets/validation/common.ts +38 -0
  158. package/src/packlets/validation/field.ts +99 -0
  159. package/src/packlets/validation/genericValidator.ts +205 -0
  160. package/src/packlets/validation/index.ts +31 -0
  161. package/src/packlets/validation/number.ts +66 -0
  162. package/src/packlets/validation/object.ts +208 -0
  163. package/src/packlets/validation/oneOf.ts +77 -0
  164. package/src/packlets/validation/string.ts +66 -0
  165. package/src/packlets/validation/traits.ts +113 -0
  166. package/src/packlets/validation/typeGuard.ts +82 -0
  167. package/src/packlets/validation/validator.ts +145 -0
  168. package/src/packlets/validation/validatorBase.ts +61 -0
  169. package/src/packlets/validation/validators.ts +246 -0
  170. package/CHANGELOG.md +0 -135
  171. package/lib/index.d.ts.map +0 -1
  172. package/lib/index.js.map +0 -1
  173. package/lib/packlets/base/brand.d.ts.map +0 -1
  174. package/lib/packlets/base/brand.js.map +0 -1
  175. package/lib/packlets/base/index.d.ts.map +0 -1
  176. package/lib/packlets/base/index.js.map +0 -1
  177. package/lib/packlets/base/logger.d.ts.map +0 -1
  178. package/lib/packlets/base/logger.js.map +0 -1
  179. package/lib/packlets/base/mapResults.d.ts.map +0 -1
  180. package/lib/packlets/base/mapResults.js.map +0 -1
  181. package/lib/packlets/base/messageAggregator.d.ts.map +0 -1
  182. package/lib/packlets/base/messageAggregator.js.map +0 -1
  183. package/lib/packlets/base/normalize.d.ts.map +0 -1
  184. package/lib/packlets/base/normalize.js.map +0 -1
  185. package/lib/packlets/base/result.d.ts.map +0 -1
  186. package/lib/packlets/base/result.js.map +0 -1
  187. package/lib/packlets/base/utils.d.ts.map +0 -1
  188. package/lib/packlets/base/utils.js.map +0 -1
  189. package/lib/packlets/collections/collectible.d.ts.map +0 -1
  190. package/lib/packlets/collections/collectible.js.map +0 -1
  191. package/lib/packlets/collections/collector.d.ts.map +0 -1
  192. package/lib/packlets/collections/collector.js.map +0 -1
  193. package/lib/packlets/collections/collectorValidator.d.ts.map +0 -1
  194. package/lib/packlets/collections/collectorValidator.js.map +0 -1
  195. package/lib/packlets/collections/common.d.ts.map +0 -1
  196. package/lib/packlets/collections/common.js.map +0 -1
  197. package/lib/packlets/collections/convertingCollector.d.ts.map +0 -1
  198. package/lib/packlets/collections/convertingCollector.js.map +0 -1
  199. package/lib/packlets/collections/convertingCollectorValidator.d.ts.map +0 -1
  200. package/lib/packlets/collections/convertingCollectorValidator.js.map +0 -1
  201. package/lib/packlets/collections/index.d.ts.map +0 -1
  202. package/lib/packlets/collections/index.js.map +0 -1
  203. package/lib/packlets/collections/keyValueConverters.d.ts.map +0 -1
  204. package/lib/packlets/collections/keyValueConverters.js.map +0 -1
  205. package/lib/packlets/collections/readonlyResultMap.d.ts.map +0 -1
  206. package/lib/packlets/collections/readonlyResultMap.js.map +0 -1
  207. package/lib/packlets/collections/resultMap.d.ts.map +0 -1
  208. package/lib/packlets/collections/resultMap.js.map +0 -1
  209. package/lib/packlets/collections/resultMapValidator.d.ts.map +0 -1
  210. package/lib/packlets/collections/resultMapValidator.js.map +0 -1
  211. package/lib/packlets/collections/utils.d.ts.map +0 -1
  212. package/lib/packlets/collections/utils.js.map +0 -1
  213. package/lib/packlets/collections/validatingCollector.d.ts.map +0 -1
  214. package/lib/packlets/collections/validatingCollector.js.map +0 -1
  215. package/lib/packlets/collections/validatingConvertingCollector.d.ts.map +0 -1
  216. package/lib/packlets/collections/validatingConvertingCollector.js.map +0 -1
  217. package/lib/packlets/collections/validatingResultMap.d.ts.map +0 -1
  218. package/lib/packlets/collections/validatingResultMap.js.map +0 -1
  219. package/lib/packlets/conversion/baseConverter.d.ts.map +0 -1
  220. package/lib/packlets/conversion/baseConverter.js.map +0 -1
  221. package/lib/packlets/conversion/converter.d.ts.map +0 -1
  222. package/lib/packlets/conversion/converter.js.map +0 -1
  223. package/lib/packlets/conversion/converters.d.ts.map +0 -1
  224. package/lib/packlets/conversion/converters.js.map +0 -1
  225. package/lib/packlets/conversion/defaultingConverter.d.ts.map +0 -1
  226. package/lib/packlets/conversion/defaultingConverter.js.map +0 -1
  227. package/lib/packlets/conversion/index.d.ts.map +0 -1
  228. package/lib/packlets/conversion/index.js.map +0 -1
  229. package/lib/packlets/conversion/objectConverter.d.ts.map +0 -1
  230. package/lib/packlets/conversion/objectConverter.js.map +0 -1
  231. package/lib/packlets/conversion/stringConverter.d.ts.map +0 -1
  232. package/lib/packlets/conversion/stringConverter.js.map +0 -1
  233. package/lib/packlets/file-tree/directoryItem.d.ts.map +0 -1
  234. package/lib/packlets/file-tree/directoryItem.js.map +0 -1
  235. package/lib/packlets/file-tree/fileItem.d.ts.map +0 -1
  236. package/lib/packlets/file-tree/fileItem.js.map +0 -1
  237. package/lib/packlets/file-tree/fileTree.d.ts.map +0 -1
  238. package/lib/packlets/file-tree/fileTree.js.map +0 -1
  239. package/lib/packlets/file-tree/fileTreeAccessors.d.ts.map +0 -1
  240. package/lib/packlets/file-tree/fileTreeAccessors.js.map +0 -1
  241. package/lib/packlets/file-tree/fsTree.d.ts.map +0 -1
  242. package/lib/packlets/file-tree/fsTree.js.map +0 -1
  243. package/lib/packlets/file-tree/in-memory/inMemoryTree.d.ts.map +0 -1
  244. package/lib/packlets/file-tree/in-memory/inMemoryTree.js.map +0 -1
  245. package/lib/packlets/file-tree/in-memory/index.d.ts.map +0 -1
  246. package/lib/packlets/file-tree/in-memory/index.js.map +0 -1
  247. package/lib/packlets/file-tree/in-memory/treeBuilder.d.ts.map +0 -1
  248. package/lib/packlets/file-tree/in-memory/treeBuilder.js.map +0 -1
  249. package/lib/packlets/file-tree/index.d.ts.map +0 -1
  250. package/lib/packlets/file-tree/index.js.map +0 -1
  251. package/lib/packlets/hash/crcNormalizer.d.ts.map +0 -1
  252. package/lib/packlets/hash/crcNormalizer.js.map +0 -1
  253. package/lib/packlets/hash/hashingNormalizer.d.ts.map +0 -1
  254. package/lib/packlets/hash/hashingNormalizer.js.map +0 -1
  255. package/lib/packlets/hash/index.d.ts.map +0 -1
  256. package/lib/packlets/hash/index.js.map +0 -1
  257. package/lib/packlets/validation/array.d.ts.map +0 -1
  258. package/lib/packlets/validation/array.js.map +0 -1
  259. package/lib/packlets/validation/boolean.d.ts.map +0 -1
  260. package/lib/packlets/validation/boolean.js.map +0 -1
  261. package/lib/packlets/validation/classes.d.ts.map +0 -1
  262. package/lib/packlets/validation/classes.js.map +0 -1
  263. package/lib/packlets/validation/common.d.ts.map +0 -1
  264. package/lib/packlets/validation/common.js.map +0 -1
  265. package/lib/packlets/validation/field.d.ts.map +0 -1
  266. package/lib/packlets/validation/field.js.map +0 -1
  267. package/lib/packlets/validation/genericValidator.d.ts.map +0 -1
  268. package/lib/packlets/validation/genericValidator.js.map +0 -1
  269. package/lib/packlets/validation/index.d.ts.map +0 -1
  270. package/lib/packlets/validation/index.js.map +0 -1
  271. package/lib/packlets/validation/number.d.ts.map +0 -1
  272. package/lib/packlets/validation/number.js.map +0 -1
  273. package/lib/packlets/validation/object.d.ts.map +0 -1
  274. package/lib/packlets/validation/object.js.map +0 -1
  275. package/lib/packlets/validation/oneOf.d.ts.map +0 -1
  276. package/lib/packlets/validation/oneOf.js.map +0 -1
  277. package/lib/packlets/validation/string.d.ts.map +0 -1
  278. package/lib/packlets/validation/string.js.map +0 -1
  279. package/lib/packlets/validation/traits.d.ts.map +0 -1
  280. package/lib/packlets/validation/traits.js.map +0 -1
  281. package/lib/packlets/validation/typeGuard.d.ts.map +0 -1
  282. package/lib/packlets/validation/typeGuard.js.map +0 -1
  283. package/lib/packlets/validation/validator.d.ts.map +0 -1
  284. package/lib/packlets/validation/validator.js.map +0 -1
  285. package/lib/packlets/validation/validatorBase.d.ts.map +0 -1
  286. package/lib/packlets/validation/validatorBase.js.map +0 -1
  287. package/lib/packlets/validation/validators.d.ts.map +0 -1
  288. package/lib/packlets/validation/validators.js.map +0 -1
@@ -0,0 +1,961 @@
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
+ /* eslint-disable no-use-before-define */
24
+ /**
25
+ * Represents the {@link IResult | result} of some operation or sequence of operations.
26
+ * @remarks
27
+ * {@link Success | Success<T>} and {@link Failure | Failure<T>} share the common
28
+ * contract {@link IResult}, enabling commingled discriminated usage.
29
+ * @public
30
+ */
31
+ export type Result<T> = Success<T> | Failure<T>;
32
+ /**
33
+ * Continuation callback to be called in the event that an
34
+ * {@link Result} is successful.
35
+ * @public
36
+ */
37
+ export type SuccessContinuation<T, TN> = (value: T) => Result<TN>;
38
+
39
+ /**
40
+ * Continuation callback to be called in the event that an
41
+ * {@link Result} fails.
42
+ * @public
43
+ */
44
+ export type FailureContinuation<T> = (message: string) => Result<T>;
45
+
46
+ /**
47
+ * Type inference to determine the result type of an {@link Result}.
48
+ * @beta
49
+ */
50
+ export type ResultValueType<T> = T extends Result<infer TV> ? TV : never;
51
+
52
+ /**
53
+ * Formats an error message.
54
+ * @param message - The error message to be formatted.
55
+ * @param detail - An optional detail to be included in the formatted message.
56
+ * @public
57
+ */
58
+ export type ErrorFormatter<TD = unknown> = (message: string, detail?: TD) => string;
59
+
60
+ /**
61
+ * Simple logger interface used by {@link IResult.(orThrow:1) | orThrow(logger)} and {@link IResult.(orThrow:2) | orThrow(formatter)}.
62
+ * @public
63
+ */
64
+ export interface IResultLogger<TD = unknown> {
65
+ /**
66
+ * Log an error message.
67
+ * @param message - The message to be logged.
68
+ */
69
+ error(message: string, detail?: TD): void;
70
+ }
71
+
72
+ /**
73
+ * Simple error aggregator to simplify collecting all errors in
74
+ * a flow.
75
+ * @public
76
+ */
77
+ export interface IMessageAggregator {
78
+ /**
79
+ * Indicates whether any messages have been aggregated.
80
+ */
81
+ readonly hasMessages: boolean;
82
+
83
+ /**
84
+ * The number of messages aggregated.
85
+ */
86
+ readonly numMessages: number;
87
+
88
+ /**
89
+ * The aggregated messages.
90
+ */
91
+ readonly messages: ReadonlyArray<string>;
92
+
93
+ /**
94
+ * Adds a message to the aggregator, if defined.
95
+ * @param message - The message to add - pass `undefined`
96
+ * or the empty string to continue without adding a message.
97
+ */
98
+ addMessage(message: string | undefined): this;
99
+
100
+ /**
101
+ * Adds multiple messages to the aggregator.
102
+ * @param messages - the messages to add.
103
+ */
104
+ addMessages(messages: string[] | undefined): this;
105
+
106
+ /**
107
+ * Returns all messages as a single string joined
108
+ * using the optionally-supplied `separator`, or
109
+ * newline if no separator is specified.
110
+ * @param separator - The optional separator used
111
+ * to join strings.
112
+ */
113
+ toString(separator?: string): string;
114
+ }
115
+
116
+ /**
117
+ * Represents the result of some operation of sequence of operations.
118
+ * @remarks
119
+ * This common contract enables commingled discriminated usage of {@link Success | Success<T>}
120
+ * and {@link Failure | Failure<T>}.
121
+ * @public
122
+ */
123
+ export interface IResult<T> {
124
+ /**
125
+ * Indicates whether the operation was successful.
126
+ */
127
+ readonly success: boolean;
128
+
129
+ /**
130
+ * Value returned by a successful operation, undefined
131
+ * for a failed operation.
132
+ */
133
+ readonly value: T | undefined;
134
+
135
+ /**
136
+ * Error message returned by a failed operation, undefined
137
+ * for a successful operation.
138
+ */
139
+ readonly message: string | undefined;
140
+
141
+ /**
142
+ * Indicates whether this operation was successful. Functions
143
+ * as a type guard for {@link Success | Success<T>}.
144
+ */
145
+ isSuccess(): this is Success<T>;
146
+
147
+ /**
148
+ * Indicates whether this operation failed. Functions
149
+ * as a type guard for {@link Failure | Failure<T>}.
150
+ */
151
+ isFailure(): this is Failure<T>;
152
+
153
+ /**
154
+ * Gets the value associated with a successful {@link IResult | result},
155
+ * or throws the error message if the corresponding operation failed.
156
+ *
157
+ * Note that `getValueOrThrow` is being superseded by `orThrow` and
158
+ * will eventually be deprecated. Please use orDefault instead.
159
+ *
160
+ * @param logger - An optional {@link IResultLogger | logger} to which the
161
+ * error will also be reported.
162
+ * @returns The return value, if the operation was successful.
163
+ * @throws The error message if the operation failed.
164
+ * @deprecated Use {@link IResult.(orThrow:1) | orThrow(logger)} or {@link IResult.(orThrow:2) | orThrow(formatter)} instead.
165
+ */
166
+ getValueOrThrow(logger?: IResultLogger): T;
167
+
168
+ /**
169
+ * Gets the value associated with a successful {@link IResult | result},
170
+ * or a default value if the corresponding operation failed.
171
+ * @param dflt - The value to be returned if the operation failed (default is
172
+ * `undefined`).
173
+ *
174
+ * Note that `getValueOrDefault` is being superseded by `orDefault` and
175
+ * will eventually be deprecated. Please use orDefault instead.
176
+ *
177
+ * @returns The return value, if the operation was successful. Returns
178
+ * the supplied default value or `undefined` if no default is supplied.
179
+ * @deprecated Use {@link IResult.(orDefault:1) | orDefault(T)} or {@link IResult.(orDefault:2) | orDefault()} instead.
180
+ */
181
+ getValueOrDefault(dflt?: T): T | undefined;
182
+
183
+ /**
184
+ * Gets the value associated with a successful {@link IResult | result},
185
+ * or throws the error message if the corresponding operation failed.
186
+ * @param logger - An optional {@link IResultLogger | logger} to which the
187
+ * error will also be reported.
188
+ * @returns The return value, if the operation was successful.
189
+ * @throws The error message if the operation failed.
190
+ * {@label logger}
191
+ */
192
+ orThrow(logger?: IResultLogger): T;
193
+
194
+ /**
195
+ * Gets the value associated with a successful {@link IResult | result},
196
+ * or throws the error message if the corresponding operation failed.
197
+ * @param cb - The {@link ErrorFormatter | error formatter} to be called in the event of failure.
198
+ * @returns The return value, if the operation was successful.
199
+ * @throws The error message if the operation failed.
200
+ * {@label formatter}
201
+ */
202
+ orThrow(cb: ErrorFormatter): T;
203
+
204
+ /**
205
+ * Gets the value associated with a successful {@link IResult | result},
206
+ * or a default value if the corresponding operation failed.
207
+ * @param dflt - The value to be returned if the operation failed.
208
+ * @returns The return value, if the operation was successful. Returns
209
+ * the supplied default if an error occurred.
210
+ * {@label SUPPLIED}
211
+ */
212
+ orDefault(dflt: T): T;
213
+
214
+ /**
215
+ * Gets the value associated with a successful {@link IResult | result},
216
+ * or a default value if the corresponding operation failed.
217
+ * @returns The return value, if the operation was successful, or
218
+ * `undefined` if an error occurs.
219
+ * {@label MISSING}
220
+ */
221
+ orDefault(): T | undefined;
222
+
223
+ /**
224
+ * Calls a supplied {@link SuccessContinuation | success continuation} if
225
+ * the operation was a success.
226
+ * @remarks
227
+ * The {@link SuccessContinuation | success continuation} might return a
228
+ * different result type than {@link IResult} on which it is invoked. This
229
+ * enables chaining of operations with heterogenous return types.
230
+ *
231
+ * @param cb - The {@link SuccessContinuation | success continuation} to
232
+ * be called in the event of success.
233
+ * @returns If this operation was successful, returns the value returned
234
+ * by the {@link SuccessContinuation | success continuation}. If this result
235
+ * failed, propagates the error message from this failure.
236
+ */
237
+ onSuccess<TN>(cb: SuccessContinuation<T, TN>): Result<TN>;
238
+
239
+ /**
240
+ * Calls a supplied {@link FailureContinuation | failed continuation} if
241
+ * the operation failed.
242
+ * @param cb - The {@link FailureContinuation | failure continuation} to
243
+ * be called in the event of failure.
244
+ * @returns If this operation failed, returns the value returned by the
245
+ * {@link FailureContinuation | failure continuation}. If this result
246
+ * was successful, propagates the result value from the successful event.
247
+ */
248
+ onFailure(cb: FailureContinuation<T>): Result<T>;
249
+
250
+ /**
251
+ * Calls a supplied {@link ErrorFormatter | error formatter} if
252
+ * the operation failed.
253
+ * @param cb - The {@link ErrorFormatter | error formatter} to
254
+ * be called in the event of failure.
255
+ * @returns If this operation failed, returns the returns {@link Failure | Failure}
256
+ * with the message returned by the formatter. If this result
257
+ * was successful, propagates the result value from the successful event.
258
+ */
259
+ withErrorFormat(cb: ErrorFormatter): Result<T>;
260
+
261
+ /**
262
+ * Converts a {@link IResult | IResult<T>} to a {@link DetailedResult | DetailedResult<T, TD>},
263
+ * adding a supplied detail if the operation failed.
264
+ * @param detail - The detail to be added if this operation failed.
265
+ * @returns A new {@link DetailedResult | DetailedResult<T, TD>} with either
266
+ * the success result or the error message from this {@link IResult}, with
267
+ * the supplied detail (if this event failed) or detail `undefined` (if
268
+ * this result succeeded).
269
+ */
270
+ withFailureDetail<TD>(detail: TD): DetailedResult<T, TD>;
271
+
272
+ /**
273
+ * Converts a {@link IResult | IResult<T>} to a {@link DetailedResult | DetailedResult<T, TD>},
274
+ * adding supplied details.
275
+ * @param detail - The default detail to be added to the new {@link DetailedResult}.
276
+ * @param successDetail - An optional detail to be added if this result was successful.
277
+ * @returns A new {@link DetailedResult | DetailedResult<T, TD>} with either
278
+ * the success result or the error message from this {@link IResult} and the
279
+ * appropriate added detail.
280
+ */
281
+ withDetail<TD>(detail: TD, successDetail?: TD): DetailedResult<T, TD>;
282
+
283
+ /**
284
+ * Propagates interior result, appending any error message to the
285
+ * supplied errors array.
286
+ * @param errors - {@link IMessageAggregator | Error aggregator} in which
287
+ * errors will be aggregated.
288
+ */
289
+ aggregateError(errors: IMessageAggregator): this;
290
+ }
291
+
292
+ /**
293
+ * Reports a successful {@link IResult | result} from some operation and the
294
+ * corresponding value.
295
+ * @public
296
+ */
297
+ export class Success<T> implements IResult<T> {
298
+ /**
299
+ * {@inheritdoc IResult.success}
300
+ */
301
+ public readonly success: true = true;
302
+
303
+ /**
304
+ * For a successful operation, the error message is always `undefined`.
305
+ */
306
+ public readonly message: undefined = undefined;
307
+
308
+ /**
309
+ * @internal
310
+ */
311
+ protected readonly _value: T;
312
+
313
+ /**
314
+ * Constructs a {@link Success} with the supplied value.
315
+ * @param value - The value to be returned.
316
+ */
317
+ public constructor(value: T) {
318
+ this._value = value;
319
+ }
320
+
321
+ /**
322
+ * The result value returned by the successful operation.
323
+ */
324
+ public get value(): T {
325
+ return this._value;
326
+ }
327
+
328
+ /**
329
+ * {@inheritdoc IResult.isSuccess}
330
+ */
331
+ public isSuccess(): this is Success<T> {
332
+ return true;
333
+ }
334
+
335
+ /**
336
+ * {@inheritdoc IResult.isFailure}
337
+ */
338
+ public isFailure(): this is Failure<T> {
339
+ return false;
340
+ }
341
+
342
+ /**
343
+ * {@inheritdoc IResult.(orThrow:1)}
344
+ */
345
+ public orThrow(logger?: IResultLogger): T;
346
+
347
+ /**
348
+ * {@inheritdoc IResult.(orThrow:2)}
349
+ */
350
+ public orThrow(cb: ErrorFormatter): T;
351
+ public orThrow(__logger?: IResultLogger | ErrorFormatter): T {
352
+ return this._value;
353
+ }
354
+
355
+ /**
356
+ * {@inheritdoc IResult.(orDefault:1)}
357
+ */
358
+ public orDefault(dflt: T): T;
359
+ /**
360
+ * {@inheritdoc IResult.(orDefault:2)}
361
+ */
362
+ public orDefault(): T | undefined;
363
+ public orDefault(dflt?: T): T | undefined {
364
+ return this._value ?? dflt;
365
+ }
366
+
367
+ /**
368
+ * {@inheritdoc IResult.getValueOrThrow}
369
+ * @deprecated Use {@link Success.(orThrow:1) | orThrow(logger)} or {@link Success.(orThrow:2) | orThrow(formatter)} instead.
370
+ */
371
+ public getValueOrThrow(__logger?: IResultLogger): T {
372
+ return this._value;
373
+ }
374
+
375
+ /**
376
+ * {@inheritdoc IResult.getValueOrDefault}
377
+ * @deprecated Use {@link Success.(orDefault:1) | orDefault(T)} or {@link Success.(orDefault:2) | orDefault()} instead.
378
+ */
379
+ public getValueOrDefault(dflt?: T): T | undefined {
380
+ return this._value ?? dflt;
381
+ }
382
+
383
+ /**
384
+ * {@inheritdoc IResult.onSuccess}
385
+ */
386
+ public onSuccess<TN>(cb: SuccessContinuation<T, TN>): Result<TN> {
387
+ return cb(this._value);
388
+ }
389
+
390
+ /**
391
+ * {@inheritdoc IResult.onFailure}
392
+ */
393
+ public onFailure(__: FailureContinuation<T>): Result<T> {
394
+ return this;
395
+ }
396
+
397
+ /**
398
+ * {@inheritdoc IResult.withErrorFormat}
399
+ */
400
+ public withErrorFormat(__cb: ErrorFormatter): Result<T> {
401
+ return this;
402
+ }
403
+
404
+ /**
405
+ * {@inheritdoc IResult.withFailureDetail}
406
+ */
407
+ public withFailureDetail<TD>(__detail: TD): DetailedResult<T, TD> {
408
+ return succeedWithDetail(this._value);
409
+ }
410
+
411
+ /**
412
+ * {@inheritdoc IResult.withDetail}
413
+ */
414
+ public withDetail<TD>(detail: TD, successDetail?: TD): DetailedResult<T, TD> {
415
+ return succeedWithDetail(this._value, successDetail ?? detail);
416
+ }
417
+
418
+ /**
419
+ * {@inheritdoc IResult.aggregateError}
420
+ */
421
+ public aggregateError(__errors: IMessageAggregator): this {
422
+ return this;
423
+ }
424
+
425
+ /**
426
+ * Creates a {@link Success | Success<T>} with the supplied value.
427
+ * @param value - The value to be returned.
428
+ * @returns The resulting {@link Success | Success<T>} with the supplied value.
429
+ * @public
430
+ */
431
+ public static with<T>(value: T): Success<T> {
432
+ return new Success<T>(value);
433
+ }
434
+ }
435
+
436
+ /**
437
+ * Reports a failed {@link IResult | result} from some operation, with an error message.
438
+ * @public
439
+ */
440
+ export class Failure<T> implements IResult<T> {
441
+ /**
442
+ * {@inheritdoc IResult.success}
443
+ */
444
+ public readonly success: false = false;
445
+ /**
446
+ * Failed operation always returns undefined for value.
447
+ */
448
+ public readonly value: undefined = undefined;
449
+
450
+ /**
451
+ * @internal
452
+ */
453
+ protected readonly _message: string;
454
+
455
+ /**
456
+ * Constructs a {@link Failure} with the supplied message.
457
+ * @param message - Error message to be reported.
458
+ */
459
+ public constructor(message: string) {
460
+ this._message = message;
461
+ }
462
+
463
+ /**
464
+ * Gets the error message associated with this error.
465
+ */
466
+ public get message(): string {
467
+ return this._message;
468
+ }
469
+
470
+ /**
471
+ * {@inheritdoc IResult.isSuccess}
472
+ */
473
+ public isSuccess(): this is Success<T> {
474
+ return false;
475
+ }
476
+
477
+ /**
478
+ * {@inheritdoc IResult.isFailure}
479
+ */
480
+ public isFailure(): this is Failure<T> {
481
+ return true;
482
+ }
483
+
484
+ /**
485
+ * {@inheritdoc IResult.(orThrow:1)}
486
+ */
487
+ public orThrow(logger?: IResultLogger): never;
488
+
489
+ /**
490
+ * {@inheritdoc IResult.(orThrow:2)}
491
+ */
492
+ public orThrow(cb: ErrorFormatter): never;
493
+ public orThrow(logOrFormat?: IResultLogger | ErrorFormatter): never {
494
+ if (logOrFormat !== undefined) {
495
+ if (typeof logOrFormat === 'function') {
496
+ throw new Error(logOrFormat(this._message));
497
+ } else {
498
+ logOrFormat.error(this._message);
499
+ }
500
+ }
501
+ throw new Error(this._message);
502
+ }
503
+
504
+ /**
505
+ * {@inheritdoc IResult.(orDefault:1)}
506
+ */
507
+ public orDefault(dflt: T): T;
508
+ /**
509
+ * {@inheritdoc IResult.(orDefault:2)}
510
+ */
511
+ public orDefault(): T | undefined;
512
+ public orDefault(dflt?: T): T | undefined {
513
+ return dflt;
514
+ }
515
+
516
+ /**
517
+ * {@inheritdoc IResult.getValueOrThrow}
518
+ * @deprecated Use {@link Failure.(orThrow:1) | orThrow(logger)} or {@link Failure.(orThrow:2) | orThrow(formatter)} instead.
519
+ */
520
+ public getValueOrThrow(logger?: IResultLogger): never {
521
+ if (logger !== undefined) {
522
+ logger.error(this._message);
523
+ }
524
+ throw new Error(this._message);
525
+ }
526
+
527
+ /**
528
+ * {@inheritdoc IResult.getValueOrDefault}
529
+ * @deprecated Use {@link Failure.(orDefault:1) | orDefault(T)} or {@link Failure.(orDefault:2) | orDefault()} instead.
530
+ */
531
+ public getValueOrDefault(dflt?: T): T | undefined {
532
+ return dflt;
533
+ }
534
+
535
+ /**
536
+ * {@inheritdoc IResult.onSuccess}
537
+ */
538
+ public onSuccess<TN>(__: SuccessContinuation<T, TN>): Result<TN> {
539
+ return new Failure(this._message);
540
+ }
541
+
542
+ /**
543
+ * {@inheritdoc IResult.onFailure}
544
+ */
545
+ public onFailure(cb: FailureContinuation<T>): Result<T> {
546
+ return cb(this._message);
547
+ }
548
+
549
+ /**
550
+ * {@inheritdoc IResult.withErrorFormat}
551
+ */
552
+ public withErrorFormat(cb: ErrorFormatter): Result<T> {
553
+ return fail(cb(this._message));
554
+ }
555
+
556
+ /**
557
+ * {@inheritdoc IResult.withFailureDetail}
558
+ */
559
+ public withFailureDetail<TD>(detail: TD): DetailedResult<T, TD> {
560
+ return failWithDetail(this._message, detail);
561
+ }
562
+
563
+ /**
564
+ * {@inheritdoc IResult.withDetail}
565
+ */
566
+ public withDetail<TD>(detail: TD, __successDetail?: TD): DetailedResult<T, TD> {
567
+ return failWithDetail(this._message, detail);
568
+ }
569
+
570
+ /**
571
+ * {@inheritdoc IResult.aggregateError}
572
+ */
573
+ public aggregateError(errors: IMessageAggregator): this {
574
+ errors.addMessage(this._message);
575
+ return this;
576
+ }
577
+
578
+ /**
579
+ * Get a 'friendly' string representation of this object.
580
+ * @remarks
581
+ * The string representation of a {@link Failure} value is the error message.
582
+ * @returns A string representing this object.
583
+ */
584
+ public toString(): string {
585
+ return this._message;
586
+ }
587
+
588
+ /**
589
+ * Creates a {@link Failure | Failure<T>} with the supplied error message.
590
+ * @param message - The error message to be returned.
591
+ * @returns The resulting {@link Failure | Failure<T>} with the supplied error message.
592
+ */
593
+ public static with<T>(message: string): Failure<T> {
594
+ return new Failure<T>(message);
595
+ }
596
+ }
597
+
598
+ /**
599
+ * Returns {@link Success | Success<T>} with the supplied result value.
600
+ * @param value - The successful result value to be returned
601
+ * @remarks
602
+ * A `succeeds` alias was added in release 5.0 for
603
+ * naming consistency with {@link fails | fails}, which was added
604
+ * to avoid conflicts with test frameworks and libraries.
605
+ * @public
606
+ */
607
+ export function succeed<T>(value: T): Success<T> {
608
+ return new Success<T>(value);
609
+ }
610
+
611
+ /**
612
+ * {@inheritdoc succeed}
613
+ * @public
614
+ */
615
+ export function succeeds<T>(value: T): Success<T> {
616
+ return new Success<T>(value);
617
+ }
618
+
619
+ /**
620
+ * Returns {@link Failure | Failure<T>} with the supplied error message.
621
+ * @param message - Error message to be returned.
622
+ * @remarks
623
+ * A `fails` alias was added in release 5.0 due to
624
+ * issues with the name `fail` being used test frameworks and libraries.
625
+ * @public
626
+ */
627
+ export function fail<T>(message: string): Failure<T> {
628
+ return new Failure<T>(message);
629
+ }
630
+
631
+ /**
632
+ * {@inheritdoc fail}
633
+ * @public
634
+ */
635
+ export function fails<T>(message: string): Failure<T> {
636
+ return new Failure<T>(message);
637
+ }
638
+
639
+ /**
640
+ * Uses a value or calls a supplied initializer if the supplied value is undefined.
641
+ * @param value - the value
642
+ * @param initializer - a function that initializes the value if it is undefined
643
+ * @returns `Success` with the value if it is defined, or the result of calling the initializer function.
644
+ * @public
645
+ */
646
+ export function useOrInitialize<T>(value: T | undefined, initializer: () => Result<T>): Result<T> {
647
+ return value !== undefined ? succeed(value) : initializer();
648
+ }
649
+
650
+ /**
651
+ * Callback to be called when a {@link DetailedResult | DetailedResult} encounters success.
652
+ * @remarks
653
+ * A success callback can return a different result type than it receives, allowing
654
+ * success results to chain through intermediate result types.
655
+ * @public
656
+ */
657
+ export type DetailedSuccessContinuation<T, TD, TN> = (value: T, detail?: TD) => DetailedResult<TN, TD>;
658
+
659
+ /**
660
+ * Callback to be called when a {@link DetailedResult | DetailedResult} encounters a failure.
661
+ * @remarks
662
+ * A failure callback can change {@link DetailedFailure | DetailedFailure<T, TD>} to
663
+ * {@link DetailedSuccess | DetailedSuccess<T, TD>} (e.g. by returning a default value)
664
+ * or it can change or embellish the error message, but it cannot change the success return type.
665
+ * @public
666
+ */
667
+ export type DetailedFailureContinuation<T, TD> = (message: string, detail?: TD) => DetailedResult<T, TD>;
668
+
669
+ /**
670
+ * A {@link DetailedSuccess | DetailedSuccess} extends {@link Success | Success} to report optional success
671
+ * details in addition to the error message.
672
+ * @public
673
+ */
674
+ export class DetailedSuccess<T, TD> extends Success<T> {
675
+ /**
676
+ * @internal
677
+ */
678
+ protected _detail?: TD;
679
+
680
+ /**
681
+ * Constructs a new {@link DetailedSuccess | DetailedSuccess<T, TD>} with the supplied
682
+ * value and detail.
683
+ * @param value - The value to be returned.
684
+ * @param detail - An optional successful detail to be returned. If omitted, detail
685
+ * will be `undefined`.
686
+ */
687
+ public constructor(value: T, detail?: TD) {
688
+ super(value);
689
+ this._detail = detail;
690
+ }
691
+
692
+ /**
693
+ * The success detail associated with this {@link DetailedSuccess}, or `undefined` if
694
+ * no detail was supplied.
695
+ */
696
+ public get detail(): TD | undefined {
697
+ return this._detail;
698
+ }
699
+
700
+ /**
701
+ * Reports that this {@link DetailedSuccess} is a success.
702
+ * @remarks
703
+ * Always true for {@link DetailedSuccess} but can be used as type guard
704
+ * to discriminate {@link DetailedSuccess} from {@link DetailedFailure} in
705
+ * a {@link DetailedResult}.
706
+ * @returns `true`
707
+ */
708
+ public isSuccess(): this is DetailedSuccess<T, TD> {
709
+ return true;
710
+ }
711
+
712
+ /**
713
+ * Invokes the supplied {@link DetailedSuccessContinuation | success callback} and propagates
714
+ * its returned {@link DetailedResult | DetailedResult<TN, TD>}.
715
+ * @remarks
716
+ * The success callback mutates the return type from `<T>` to `<TN>`.
717
+ * @param cb - The {@link DetailedSuccessContinuation | success callback} to be invoked.
718
+ * @returns The {@link DetailedResult | DetailedResult<T, TD>} returned by the success callback.
719
+ */
720
+ public onSuccess<TN>(cb: DetailedSuccessContinuation<T, TD, TN>): DetailedResult<TN, TD> {
721
+ return cb(this._value, this._detail);
722
+ }
723
+
724
+ /**
725
+ * Propagates this {@link DetailedSuccess}.
726
+ * @remarks
727
+ * Failure does not mutate return type so we can return this event directly.
728
+ * @param _cb - {@link DetailedFailureContinuation | Failure callback} to be called
729
+ * on a {@link DetailedResult} in case of failure (ignored).
730
+ * @returns `this`
731
+ */
732
+ public onFailure(__cb: DetailedFailureContinuation<T, TD>): DetailedResult<T, TD> {
733
+ return this;
734
+ }
735
+
736
+ /**
737
+ * {@inheritdoc Success.withErrorFormat}
738
+ */
739
+ public withErrorFormat(cb: ErrorFormatter): DetailedResult<T, TD> {
740
+ return this;
741
+ }
742
+
743
+ /**
744
+ * Creates a {@link DetailedSuccess | DetailedSuccess<T, TD>} with the supplied value and
745
+ * optional detail.
746
+ */
747
+ public static with<T, TD>(value: T, detail?: TD): DetailedSuccess<T, TD> {
748
+ return new DetailedSuccess<T, TD>(value, detail);
749
+ }
750
+
751
+ /**
752
+ * Returns this {@link DetailedSuccess} as a {@link Result}.
753
+ */
754
+ public get asResult(): Result<T> {
755
+ return this;
756
+ }
757
+ }
758
+
759
+ /**
760
+ * A {@link DetailedFailure | DetailedFailure<T, TD>} extends {@link Failure | Failure<T>} to report optional
761
+ * failure details in addition to the error message.
762
+ * @public
763
+ */
764
+ export class DetailedFailure<T, TD> extends Failure<T> {
765
+ /**
766
+ * @internal
767
+ */
768
+ protected _detail?: TD;
769
+
770
+ /**
771
+ * Constructs a new {@link DetailedFailure | DetailedFailure<T, TD>} with the supplied
772
+ * message and detail.
773
+ * @param message - The message to be returned.
774
+ * @param detail - The error detail to be returned.
775
+ */
776
+ public constructor(message: string, detail?: TD) {
777
+ super(message);
778
+ this._detail = detail;
779
+ }
780
+
781
+ /**
782
+ * The error detail associated with this {@link DetailedFailure}.
783
+ */
784
+ public get detail(): TD | undefined {
785
+ return this._detail;
786
+ }
787
+
788
+ /**
789
+ * Reports that this {@link DetailedFailure} is a failure.
790
+ * @remarks
791
+ * Always true for {@link DetailedFailure} but can be used as type guard
792
+ * to discriminate {@link DetailedSuccess} from {@link DetailedFailure} in
793
+ * a {@link DetailedResult}.
794
+ * @returns `true`
795
+ */
796
+ public isFailure(): this is DetailedFailure<T, TD> {
797
+ return true;
798
+ }
799
+
800
+ /**
801
+ * Propagates the error message and detail from this result.
802
+ * @remarks
803
+ * Mutates the success type as the success callback would have, but does not
804
+ * call the success callback.
805
+ * @param _cb - {@link DetailedSuccessContinuation | Success callback} to be called
806
+ * on a {@link DetailedResult} in case of success (ignored).
807
+ * @returns A new {@link DetailedFailure | DetailedFailure<TN, TD>} which contains
808
+ * the error message and detail from this one.
809
+ */
810
+ public onSuccess<TN>(__cb: DetailedSuccessContinuation<T, TD, TN>): DetailedResult<TN, TD> {
811
+ return new DetailedFailure<TN, TD>(this._message, this._detail);
812
+ }
813
+
814
+ /**
815
+ * Invokes the supplied {@link DetailedFailureContinuation | failure callback} and propagates
816
+ * its returned {@link DetailedResult | DetailedResult<T, TD>}.
817
+ * @param cb - The {@link DetailedFailureContinuation | failure callback} to be invoked.
818
+ * @returns The {@link DetailedResult | DetailedResult<T, TD>} returned by the failure callback.
819
+ */
820
+ public onFailure(cb: DetailedFailureContinuation<T, TD>): DetailedResult<T, TD> {
821
+ return cb(this._message, this._detail);
822
+ }
823
+
824
+ /**
825
+ * {@inheritdoc IResult.withErrorFormat}
826
+ */
827
+ public withErrorFormat(cb: ErrorFormatter<TD>): DetailedResult<T, TD> {
828
+ return failWithDetail(cb(this._message, this._detail), this._detail);
829
+ }
830
+
831
+ public orThrow(logOrFormat?: IResultLogger<TD> | ErrorFormatter<TD>): never;
832
+ public orThrow(cb: ErrorFormatter): never;
833
+ public orThrow(logOrFormat?: IResultLogger<TD> | ErrorFormatter<TD>): never {
834
+ if (logOrFormat !== undefined) {
835
+ if (typeof logOrFormat === 'function') {
836
+ throw new Error(logOrFormat(this._message, this._detail));
837
+ } else {
838
+ logOrFormat.error(this._message, this._detail);
839
+ }
840
+ }
841
+ throw new Error(this._message);
842
+ }
843
+
844
+ /**
845
+ * Returns this {@link DetailedFailure} as a {@link Result}.
846
+ */
847
+ public get asResult(): Result<T> {
848
+ return this;
849
+ }
850
+
851
+ /**
852
+ * Creates a {@link DetailedFailure | DetailedFailure<T, TD>} with the supplied error message
853
+ * and optional detail.
854
+ * @param message - The error message to be returned.
855
+ * @param detail - The error detail to be returned.
856
+ * @returns The resulting {@link DetailedFailure | DetailedFailure<T, TD>} with the supplied
857
+ * error message and detail.
858
+ * @public
859
+ */
860
+ public static with<T, TD>(message: string, detail?: TD): DetailedFailure<T, TD> {
861
+ return new DetailedFailure<T, TD>(message, detail);
862
+ }
863
+ }
864
+
865
+ /**
866
+ * Type inference to determine the result type `T` of a {@link DetailedResult | DetailedResult<T, TD>}.
867
+ * @beta
868
+ */
869
+ export type DetailedResult<T, TD> = DetailedSuccess<T, TD> | DetailedFailure<T, TD>;
870
+
871
+ /**
872
+ * Type inference to determine the detail type `TD` of a {@link DetailedResult | DetailedResult<T, TD>}.
873
+ * @beta
874
+ */
875
+ export type ResultDetailType<T> = T extends DetailedResult<unknown, infer TD> ? TD : never;
876
+
877
+ /**
878
+ * Returns {@link DetailedSuccess | DetailedSuccess<T, TD>} with a supplied value and optional
879
+ * detail.
880
+ * @param value - The value of type `<T>` to be returned.
881
+ * @param detail - An optional detail of type `<TD>` to be returned.
882
+ * @returns A {@link DetailedSuccess | DetailedSuccess<T, TD>} with the supplied value
883
+ * and detail, if supplied.
884
+ * @remarks
885
+ * The `succeedsWithDetail` alias was added in release 5.0 for
886
+ * naming consistency with {@link fails | fails}, which was added to avoid conflicts
887
+ * with test frameworks and libraries.
888
+ * @public
889
+ */
890
+ export function succeedWithDetail<T, TD>(value: T, detail?: TD): DetailedSuccess<T, TD> {
891
+ return new DetailedSuccess<T, TD>(value, detail);
892
+ }
893
+
894
+ /**
895
+ * {@inheritdoc succeedWithDetail}
896
+ * @public
897
+ */
898
+ export function succeedsWithDetail<T, TD>(value: T, detail?: TD): DetailedSuccess<T, TD> {
899
+ return new DetailedSuccess<T, TD>(value, detail);
900
+ }
901
+
902
+ /**
903
+ * Returns {@link DetailedFailure | DetailedFailure<T, TD>} with a supplied error message and detail.
904
+ * @param message - The error message to be returned.
905
+ * @param detail - The event detail to be returned.
906
+ * @returns An {@link DetailedFailure | DetailedFailure<T, TD>} with the supplied error
907
+ * message and detail.
908
+ * @remarks
909
+ * The `failsWithDetail` alias was added in release 5.0 for naming consistency
910
+ * with {@link fails | fails}, which was added to avoid conflicts with test frameworks and libraries.
911
+ * @public
912
+ */
913
+ export function failWithDetail<T, TD>(message: string, detail?: TD): DetailedFailure<T, TD> {
914
+ return new DetailedFailure<T, TD>(message, detail);
915
+ }
916
+
917
+ /**
918
+ * {@inheritdoc failWithDetail}
919
+ * @public
920
+ */
921
+ export function failsWithDetail<T, TD>(message: string, detail?: TD): DetailedFailure<T, TD> {
922
+ return new DetailedFailure<T, TD>(message, detail);
923
+ }
924
+
925
+ /**
926
+ * Propagates a {@link Success} or {@link Failure} {@link Result}, adding supplied
927
+ * event details as appropriate.
928
+ * @param result - The {@link Result} to be propagated.
929
+ * @param detail - The event detail (type `<TD>`) to be added to the {@link Result | result}.
930
+ * @param successDetail - An optional distinct event detail to be added to {@link Success} results. If `successDetail`
931
+ * is omitted or `undefined`, then `detail` will be applied to {@link Success} results.
932
+ * @returns A new {@link DetailedResult | DetailedResult<T, TD>} with the success value or error
933
+ * message from the original `result` but with the specified detail added.
934
+ * @public
935
+ */
936
+ export function propagateWithDetail<T, TD>(
937
+ result: Result<T>,
938
+ detail: TD,
939
+ successDetail?: TD
940
+ ): DetailedResult<T, TD> {
941
+ return result.isSuccess()
942
+ ? succeedWithDetail(result.value, successDetail ?? detail)
943
+ : failWithDetail(result.message, detail);
944
+ }
945
+
946
+ /**
947
+ * Wraps a function which might throw to convert exception results
948
+ * to {@link Failure}.
949
+ * @param func - The function to be captured.
950
+ * @returns Returns {@link Success} with a value of type `<T>` on
951
+ * success , or {@link Failure} with the thrown error message if
952
+ * `func` throws an `Error`.
953
+ * @public
954
+ */
955
+ export function captureResult<T>(func: () => T): Result<T> {
956
+ try {
957
+ return succeed(func());
958
+ } catch (err) {
959
+ return fail((err as Error).message);
960
+ }
961
+ }