@fgv/ts-utils 5.0.0-2 → 5.0.0-21

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 (311) 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/ts-utils.d.ts +20 -8
  6. package/dist/tsdoc-metadata.json +1 -1
  7. package/lib/packlets/base/result.d.ts +8 -0
  8. package/lib/packlets/base/result.js +11 -0
  9. package/lib/packlets/hash/crcNormalizer.js +3 -1
  10. package/lib/packlets/validation/array.d.ts +2 -1
  11. package/lib/packlets/validation/array.js +2 -1
  12. package/lib/packlets/validation/common.d.ts +0 -8
  13. package/lib/packlets/validation/field.d.ts +1 -1
  14. package/lib/packlets/validation/field.js +1 -1
  15. package/lib/packlets/validation/genericValidator.d.ts +1 -6
  16. package/lib/packlets/validation/genericValidator.js +11 -11
  17. package/lib/packlets/validation/object.d.ts +1 -1
  18. package/lib/packlets/validation/object.js +1 -1
  19. package/lib/packlets/validation/oneOf.d.ts +2 -1
  20. package/lib/packlets/validation/oneOf.js +2 -1
  21. package/lib/packlets/validation/typeGuard.d.ts +3 -2
  22. package/lib/packlets/validation/typeGuard.js +2 -1
  23. package/lib/packlets/validation/validator.d.ts +7 -0
  24. package/lib/packlets/validation/validatorBase.d.ts +2 -1
  25. package/lib/packlets/validation/validatorBase.js +1 -1
  26. package/lib/packlets/validation/validators.d.ts +2 -2
  27. package/lib/packlets/validation/validators.js +3 -3
  28. package/lib/test/helpers/jest/helpers/fsHelpers.d.ts +52 -0
  29. package/lib/test/helpers/jest/helpers/fsHelpers.js +180 -0
  30. package/lib/test/helpers/jest/helpers/index.d.ts +2 -0
  31. package/lib/test/helpers/jest/helpers/index.js +18 -0
  32. package/lib/test/helpers/jest/index.d.ts +3 -0
  33. package/lib/test/helpers/jest/index.js +27 -0
  34. package/lib/test/helpers/jest/matchers/index.d.ts +16 -0
  35. package/lib/test/helpers/jest/matchers/index.js +19 -0
  36. package/lib/test/helpers/jest/matchers/toFail/index.d.ts +16 -0
  37. package/lib/test/helpers/jest/matchers/toFail/index.js +25 -0
  38. package/lib/test/helpers/jest/matchers/toFail/predicate.d.ts +4 -0
  39. package/lib/test/helpers/jest/matchers/toFail/predicate.js +9 -0
  40. package/lib/test/helpers/jest/matchers/toFailTest/index.d.ts +16 -0
  41. package/lib/test/helpers/jest/matchers/toFailTest/index.js +30 -0
  42. package/lib/test/helpers/jest/matchers/toFailTest/predicate.d.ts +3 -0
  43. package/lib/test/helpers/jest/matchers/toFailTest/predicate.js +12 -0
  44. package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/index.d.ts +17 -0
  45. package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/index.js +26 -0
  46. package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/predicate.d.ts +4 -0
  47. package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/predicate.js +15 -0
  48. package/lib/test/helpers/jest/matchers/toFailTestWith/index.d.ts +18 -0
  49. package/lib/test/helpers/jest/matchers/toFailTestWith/index.js +31 -0
  50. package/lib/test/helpers/jest/matchers/toFailTestWith/predicate.d.ts +4 -0
  51. package/lib/test/helpers/jest/matchers/toFailTestWith/predicate.js +30 -0
  52. package/lib/test/helpers/jest/matchers/toFailWith/index.d.ts +18 -0
  53. package/lib/test/helpers/jest/matchers/toFailWith/index.js +29 -0
  54. package/lib/test/helpers/jest/matchers/toFailWith/predicate.d.ts +4 -0
  55. package/lib/test/helpers/jest/matchers/toFailWith/predicate.js +20 -0
  56. package/lib/test/helpers/jest/matchers/toFailWithDetail/index.d.ts +19 -0
  57. package/lib/test/helpers/jest/matchers/toFailWithDetail/index.js +29 -0
  58. package/lib/test/helpers/jest/matchers/toFailWithDetail/predicate.d.ts +4 -0
  59. package/lib/test/helpers/jest/matchers/toFailWithDetail/predicate.js +26 -0
  60. package/lib/test/helpers/jest/matchers/toSucceed/index.d.ts +16 -0
  61. package/lib/test/helpers/jest/matchers/toSucceed/index.js +25 -0
  62. package/lib/test/helpers/jest/matchers/toSucceed/predicate.d.ts +4 -0
  63. package/lib/test/helpers/jest/matchers/toSucceed/predicate.js +9 -0
  64. package/lib/test/helpers/jest/matchers/toSucceedAndMatchInlineSnapshot/index.d.ts +17 -0
  65. package/lib/test/helpers/jest/matchers/toSucceedAndMatchInlineSnapshot/index.js +26 -0
  66. package/lib/test/helpers/jest/matchers/toSucceedAndMatchSnapshot/index.d.ts +17 -0
  67. package/lib/test/helpers/jest/matchers/toSucceedAndMatchSnapshot/index.js +26 -0
  68. package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/index.d.ts +19 -0
  69. package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/index.js +55 -0
  70. package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/predicate.d.ts +4 -0
  71. package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/predicate.js +21 -0
  72. package/lib/test/helpers/jest/matchers/toSucceedWith/index.d.ts +18 -0
  73. package/lib/test/helpers/jest/matchers/toSucceedWith/index.js +29 -0
  74. package/lib/test/helpers/jest/matchers/toSucceedWith/predicate.d.ts +4 -0
  75. package/lib/test/helpers/jest/matchers/toSucceedWith/predicate.js +16 -0
  76. package/lib/test/helpers/jest/matchers/toSucceedWithDetail/index.d.ts +18 -0
  77. package/lib/test/helpers/jest/matchers/toSucceedWithDetail/index.js +29 -0
  78. package/lib/test/helpers/jest/matchers/toSucceedWithDetail/predicate.d.ts +4 -0
  79. package/lib/test/helpers/jest/matchers/toSucceedWithDetail/predicate.js +21 -0
  80. package/lib/test/helpers/jest/resolvers/cli.d.ts +5 -0
  81. package/lib/test/helpers/jest/resolvers/cli.js +48 -0
  82. package/lib/test/helpers/jest/resolvers/ide.d.ts +5 -0
  83. package/lib/test/helpers/jest/resolvers/ide.js +48 -0
  84. package/lib/test/helpers/jest/ts-utils.d.ts +2 -0
  85. package/lib/test/helpers/jest/ts-utils.js +18 -0
  86. package/lib/test/helpers/jest/types/index.d.ts +79 -0
  87. package/lib/test/helpers/jest/types/index.js +4 -0
  88. package/lib/test/helpers/jest/utils/matcherHelpers.d.ts +6 -0
  89. package/lib/test/helpers/jest/utils/matcherHelpers.js +53 -0
  90. package/lib/test/helpers/jest/utils/snapshotResolver.d.ts +4 -0
  91. package/lib/test/helpers/jest/utils/snapshotResolver.js +19 -0
  92. package/lib/test/unit/collections/collectible.test.d.ts +2 -0
  93. package/lib/test/unit/collections/collector.test.d.ts +2 -0
  94. package/lib/test/unit/collections/collectors.test.d.ts +2 -0
  95. package/lib/test/unit/collections/convertingCollector.test.d.ts +2 -0
  96. package/lib/test/unit/collections/helpers.d.ts +44 -0
  97. package/lib/test/unit/collections/helpers.js +115 -0
  98. package/lib/test/unit/collections/resultMap.test.d.ts +2 -0
  99. package/lib/test/unit/collections/validatingCollector.test.d.ts +2 -0
  100. package/lib/test/unit/collections/validatingConvertingCollector.test.d.ts +2 -0
  101. package/lib/test/unit/collections/validatingResultMap.test.d.ts +2 -0
  102. package/lib/test/unit/converter.test.d.ts +2 -0
  103. package/lib/test/unit/converters.test.d.ts +2 -0
  104. package/lib/test/unit/defaultingConverter.test.d.ts +2 -0
  105. package/lib/test/unit/file-tree/fileItem.test.d.ts +2 -0
  106. package/lib/test/unit/file-tree/fileTree.test.d.ts +2 -0
  107. package/lib/test/unit/file-tree/fsTree.test.d.ts +2 -0
  108. package/lib/test/unit/file-tree/inMemoryTree.test.d.ts +2 -0
  109. package/lib/test/unit/file-tree/sample/rootFile.json +1 -0
  110. package/lib/test/unit/file-tree/sample/some/other/path/file2.json +1 -0
  111. package/lib/test/unit/file-tree/sample/some/path/below/file4.json +1 -0
  112. package/lib/test/unit/file-tree/sample/some/path/file1.json +1 -0
  113. package/lib/test/unit/file-tree/sample/some/path/file3.json +1 -0
  114. package/lib/test/unit/hash.test.d.ts +2 -0
  115. package/lib/test/unit/logger.test.d.ts +2 -0
  116. package/lib/test/unit/mapResults.test.d.ts +3 -0
  117. package/lib/test/unit/messageAggregator.test.d.ts +3 -0
  118. package/lib/test/unit/normalize.test.d.ts +2 -0
  119. package/lib/test/unit/result.test.d.ts +3 -0
  120. package/lib/test/unit/utils.test.d.ts +3 -0
  121. package/lib/test/unit/validation/array.test.d.ts +2 -0
  122. package/lib/test/unit/validation/boolean.test.d.ts +2 -0
  123. package/lib/test/unit/validation/genericValidator.test.d.ts +2 -0
  124. package/lib/test/unit/validation/number.test.d.ts +2 -0
  125. package/lib/test/unit/validation/object.test.d.ts +2 -0
  126. package/lib/test/unit/validation/oneOf.test.d.ts +2 -0
  127. package/lib/test/unit/validation/recordOf.test.d.ts +2 -0
  128. package/lib/test/unit/validation/recursiveValidation.test.d.ts +2 -0
  129. package/lib/test/unit/validation/string.test.d.ts +2 -0
  130. package/lib/test/unit/validation/traits.test.d.ts +2 -0
  131. package/lib/test/unit/validation/typeGuard.test.d.ts +2 -0
  132. package/lib/test/unit/validation/validators.test.d.ts +2 -0
  133. package/package.json +9 -9
  134. package/src/index.ts +63 -0
  135. package/src/packlets/base/brand.ts +28 -0
  136. package/src/packlets/base/index.ts +31 -0
  137. package/src/packlets/base/logger.ts +156 -0
  138. package/src/packlets/base/mapResults.ts +302 -0
  139. package/src/packlets/base/messageAggregator.ts +120 -0
  140. package/src/packlets/base/normalize.ts +144 -0
  141. package/src/packlets/base/result.ts +961 -0
  142. package/src/packlets/base/utils.ts +236 -0
  143. package/src/packlets/collections/collectible.ts +238 -0
  144. package/src/packlets/collections/collector.ts +273 -0
  145. package/src/packlets/collections/collectorValidator.ts +178 -0
  146. package/src/packlets/collections/common.ts +27 -0
  147. package/src/packlets/collections/convertingCollector.ts +223 -0
  148. package/src/packlets/collections/convertingCollectorValidator.ts +164 -0
  149. package/src/packlets/collections/index.ts +39 -0
  150. package/src/packlets/collections/keyValueConverters.ts +142 -0
  151. package/src/packlets/collections/readonlyResultMap.ts +99 -0
  152. package/src/packlets/collections/resultMap.ts +322 -0
  153. package/src/packlets/collections/resultMapValidator.ts +188 -0
  154. package/src/packlets/collections/utils.ts +33 -0
  155. package/src/packlets/collections/validatingCollector.ts +123 -0
  156. package/src/packlets/collections/validatingConvertingCollector.ts +113 -0
  157. package/src/packlets/collections/validatingResultMap.ts +94 -0
  158. package/src/packlets/conversion/baseConverter.ts +330 -0
  159. package/src/packlets/conversion/converter.ts +260 -0
  160. package/src/packlets/conversion/converters.ts +1039 -0
  161. package/src/packlets/conversion/defaultingConverter.ts +188 -0
  162. package/src/packlets/conversion/index.ts +30 -0
  163. package/src/packlets/conversion/objectConverter.ts +259 -0
  164. package/src/packlets/conversion/stringConverter.ts +165 -0
  165. package/src/packlets/file-tree/directoryItem.ts +84 -0
  166. package/src/packlets/file-tree/fileItem.ts +119 -0
  167. package/src/packlets/file-tree/fileTree.ts +152 -0
  168. package/src/packlets/file-tree/fileTreeAccessors.ts +176 -0
  169. package/src/packlets/file-tree/fsTree.ts +118 -0
  170. package/src/packlets/file-tree/in-memory/inMemoryTree.ts +171 -0
  171. package/src/packlets/file-tree/in-memory/index.ts +23 -0
  172. package/src/packlets/file-tree/in-memory/treeBuilder.ts +203 -0
  173. package/src/packlets/file-tree/index.ts +31 -0
  174. package/src/packlets/hash/crcNormalizer.ts +81 -0
  175. package/src/packlets/hash/hashingNormalizer.ts +102 -0
  176. package/src/packlets/hash/index.ts +24 -0
  177. package/src/packlets/validation/array.ts +84 -0
  178. package/src/packlets/validation/boolean.ts +63 -0
  179. package/src/packlets/validation/classes.ts +38 -0
  180. package/src/packlets/validation/common.ts +28 -0
  181. package/src/packlets/validation/field.ts +99 -0
  182. package/src/packlets/validation/genericValidator.ts +204 -0
  183. package/src/packlets/validation/index.ts +31 -0
  184. package/src/packlets/validation/number.ts +66 -0
  185. package/src/packlets/validation/object.ts +208 -0
  186. package/src/packlets/validation/oneOf.ts +78 -0
  187. package/src/packlets/validation/string.ts +66 -0
  188. package/src/packlets/validation/traits.ts +113 -0
  189. package/src/packlets/validation/typeGuard.ts +83 -0
  190. package/src/packlets/validation/validator.ts +157 -0
  191. package/src/packlets/validation/validatorBase.ts +66 -0
  192. package/src/packlets/validation/validators.ts +254 -0
  193. package/CHANGELOG.md +0 -135
  194. package/lib/index.d.ts.map +0 -1
  195. package/lib/index.js.map +0 -1
  196. package/lib/packlets/base/brand.d.ts.map +0 -1
  197. package/lib/packlets/base/brand.js.map +0 -1
  198. package/lib/packlets/base/index.d.ts.map +0 -1
  199. package/lib/packlets/base/index.js.map +0 -1
  200. package/lib/packlets/base/logger.d.ts.map +0 -1
  201. package/lib/packlets/base/logger.js.map +0 -1
  202. package/lib/packlets/base/mapResults.d.ts.map +0 -1
  203. package/lib/packlets/base/mapResults.js.map +0 -1
  204. package/lib/packlets/base/messageAggregator.d.ts.map +0 -1
  205. package/lib/packlets/base/messageAggregator.js.map +0 -1
  206. package/lib/packlets/base/normalize.d.ts.map +0 -1
  207. package/lib/packlets/base/normalize.js.map +0 -1
  208. package/lib/packlets/base/result.d.ts.map +0 -1
  209. package/lib/packlets/base/result.js.map +0 -1
  210. package/lib/packlets/base/utils.d.ts.map +0 -1
  211. package/lib/packlets/base/utils.js.map +0 -1
  212. package/lib/packlets/collections/collectible.d.ts.map +0 -1
  213. package/lib/packlets/collections/collectible.js.map +0 -1
  214. package/lib/packlets/collections/collector.d.ts.map +0 -1
  215. package/lib/packlets/collections/collector.js.map +0 -1
  216. package/lib/packlets/collections/collectorValidator.d.ts.map +0 -1
  217. package/lib/packlets/collections/collectorValidator.js.map +0 -1
  218. package/lib/packlets/collections/common.d.ts.map +0 -1
  219. package/lib/packlets/collections/common.js.map +0 -1
  220. package/lib/packlets/collections/convertingCollector.d.ts.map +0 -1
  221. package/lib/packlets/collections/convertingCollector.js.map +0 -1
  222. package/lib/packlets/collections/convertingCollectorValidator.d.ts.map +0 -1
  223. package/lib/packlets/collections/convertingCollectorValidator.js.map +0 -1
  224. package/lib/packlets/collections/index.d.ts.map +0 -1
  225. package/lib/packlets/collections/index.js.map +0 -1
  226. package/lib/packlets/collections/keyValueConverters.d.ts.map +0 -1
  227. package/lib/packlets/collections/keyValueConverters.js.map +0 -1
  228. package/lib/packlets/collections/readonlyResultMap.d.ts.map +0 -1
  229. package/lib/packlets/collections/readonlyResultMap.js.map +0 -1
  230. package/lib/packlets/collections/resultMap.d.ts.map +0 -1
  231. package/lib/packlets/collections/resultMap.js.map +0 -1
  232. package/lib/packlets/collections/resultMapValidator.d.ts.map +0 -1
  233. package/lib/packlets/collections/resultMapValidator.js.map +0 -1
  234. package/lib/packlets/collections/utils.d.ts.map +0 -1
  235. package/lib/packlets/collections/utils.js.map +0 -1
  236. package/lib/packlets/collections/validatingCollector.d.ts.map +0 -1
  237. package/lib/packlets/collections/validatingCollector.js.map +0 -1
  238. package/lib/packlets/collections/validatingConvertingCollector.d.ts.map +0 -1
  239. package/lib/packlets/collections/validatingConvertingCollector.js.map +0 -1
  240. package/lib/packlets/collections/validatingResultMap.d.ts.map +0 -1
  241. package/lib/packlets/collections/validatingResultMap.js.map +0 -1
  242. package/lib/packlets/conversion/baseConverter.d.ts.map +0 -1
  243. package/lib/packlets/conversion/baseConverter.js.map +0 -1
  244. package/lib/packlets/conversion/converter.d.ts.map +0 -1
  245. package/lib/packlets/conversion/converter.js.map +0 -1
  246. package/lib/packlets/conversion/converters.d.ts.map +0 -1
  247. package/lib/packlets/conversion/converters.js.map +0 -1
  248. package/lib/packlets/conversion/defaultingConverter.d.ts.map +0 -1
  249. package/lib/packlets/conversion/defaultingConverter.js.map +0 -1
  250. package/lib/packlets/conversion/index.d.ts.map +0 -1
  251. package/lib/packlets/conversion/index.js.map +0 -1
  252. package/lib/packlets/conversion/objectConverter.d.ts.map +0 -1
  253. package/lib/packlets/conversion/objectConverter.js.map +0 -1
  254. package/lib/packlets/conversion/stringConverter.d.ts.map +0 -1
  255. package/lib/packlets/conversion/stringConverter.js.map +0 -1
  256. package/lib/packlets/file-tree/directoryItem.d.ts.map +0 -1
  257. package/lib/packlets/file-tree/directoryItem.js.map +0 -1
  258. package/lib/packlets/file-tree/fileItem.d.ts.map +0 -1
  259. package/lib/packlets/file-tree/fileItem.js.map +0 -1
  260. package/lib/packlets/file-tree/fileTree.d.ts.map +0 -1
  261. package/lib/packlets/file-tree/fileTree.js.map +0 -1
  262. package/lib/packlets/file-tree/fileTreeAccessors.d.ts.map +0 -1
  263. package/lib/packlets/file-tree/fileTreeAccessors.js.map +0 -1
  264. package/lib/packlets/file-tree/fsTree.d.ts.map +0 -1
  265. package/lib/packlets/file-tree/fsTree.js.map +0 -1
  266. package/lib/packlets/file-tree/in-memory/inMemoryTree.d.ts.map +0 -1
  267. package/lib/packlets/file-tree/in-memory/inMemoryTree.js.map +0 -1
  268. package/lib/packlets/file-tree/in-memory/index.d.ts.map +0 -1
  269. package/lib/packlets/file-tree/in-memory/index.js.map +0 -1
  270. package/lib/packlets/file-tree/in-memory/treeBuilder.d.ts.map +0 -1
  271. package/lib/packlets/file-tree/in-memory/treeBuilder.js.map +0 -1
  272. package/lib/packlets/file-tree/index.d.ts.map +0 -1
  273. package/lib/packlets/file-tree/index.js.map +0 -1
  274. package/lib/packlets/hash/crcNormalizer.d.ts.map +0 -1
  275. package/lib/packlets/hash/crcNormalizer.js.map +0 -1
  276. package/lib/packlets/hash/hashingNormalizer.d.ts.map +0 -1
  277. package/lib/packlets/hash/hashingNormalizer.js.map +0 -1
  278. package/lib/packlets/hash/index.d.ts.map +0 -1
  279. package/lib/packlets/hash/index.js.map +0 -1
  280. package/lib/packlets/validation/array.d.ts.map +0 -1
  281. package/lib/packlets/validation/array.js.map +0 -1
  282. package/lib/packlets/validation/boolean.d.ts.map +0 -1
  283. package/lib/packlets/validation/boolean.js.map +0 -1
  284. package/lib/packlets/validation/classes.d.ts.map +0 -1
  285. package/lib/packlets/validation/classes.js.map +0 -1
  286. package/lib/packlets/validation/common.d.ts.map +0 -1
  287. package/lib/packlets/validation/common.js.map +0 -1
  288. package/lib/packlets/validation/field.d.ts.map +0 -1
  289. package/lib/packlets/validation/field.js.map +0 -1
  290. package/lib/packlets/validation/genericValidator.d.ts.map +0 -1
  291. package/lib/packlets/validation/genericValidator.js.map +0 -1
  292. package/lib/packlets/validation/index.d.ts.map +0 -1
  293. package/lib/packlets/validation/index.js.map +0 -1
  294. package/lib/packlets/validation/number.d.ts.map +0 -1
  295. package/lib/packlets/validation/number.js.map +0 -1
  296. package/lib/packlets/validation/object.d.ts.map +0 -1
  297. package/lib/packlets/validation/object.js.map +0 -1
  298. package/lib/packlets/validation/oneOf.d.ts.map +0 -1
  299. package/lib/packlets/validation/oneOf.js.map +0 -1
  300. package/lib/packlets/validation/string.d.ts.map +0 -1
  301. package/lib/packlets/validation/string.js.map +0 -1
  302. package/lib/packlets/validation/traits.d.ts.map +0 -1
  303. package/lib/packlets/validation/traits.js.map +0 -1
  304. package/lib/packlets/validation/typeGuard.d.ts.map +0 -1
  305. package/lib/packlets/validation/typeGuard.js.map +0 -1
  306. package/lib/packlets/validation/validator.d.ts.map +0 -1
  307. package/lib/packlets/validation/validator.js.map +0 -1
  308. package/lib/packlets/validation/validatorBase.d.ts.map +0 -1
  309. package/lib/packlets/validation/validatorBase.js.map +0 -1
  310. package/lib/packlets/validation/validators.d.ts.map +0 -1
  311. package/lib/packlets/validation/validators.js.map +0 -1
@@ -0,0 +1,302 @@
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
+ import { DetailedResult, IMessageAggregator, Result, fail, succeed } from './result';
24
+
25
+ /**
26
+ * Aggregates successful result values from a collection of {@link Result | Result<T>}.
27
+ * @param results - The collection of {@link Result | Result<T>} to be mapped.
28
+ * @param aggregatedErrors - Optional string array to which any error messages will be
29
+ * appended. Each error is appended as an individual string.
30
+ * @returns If all {@link Result | results} are successful, returns {@link Success} with an
31
+ * array containing all returned values. If any {@link Result | results} failed, returns
32
+ * {@link Failure} with a concatenated summary of all error messages.
33
+ * @public
34
+ */
35
+ export function mapResults<T>(
36
+ results: Iterable<Result<T>>,
37
+ aggregatedErrors?: IMessageAggregator
38
+ ): Result<T[]> {
39
+ const errors: string[] = [];
40
+ const elements: T[] = [];
41
+
42
+ for (const result of results) {
43
+ if (result.isSuccess()) {
44
+ elements.push(result.value);
45
+ } else {
46
+ errors.push(result.message);
47
+ }
48
+ }
49
+
50
+ if (errors.length > 0) {
51
+ aggregatedErrors?.addMessages(errors);
52
+ return fail(errors.join('\n'));
53
+ }
54
+ return succeed(elements);
55
+ }
56
+
57
+ /**
58
+ * Aggregates successful results from a collection of {@link DetailedResult | DetailedResult<T, TD>},
59
+ * optionally ignoring certain error details.
60
+ * @param results - The collection of {@link DetailedResult | DetailedResult<T, TD>} to be mapped.
61
+ * @param ignore - An array of error detail values (of type `<TD>`) that should be ignored.
62
+ * @param aggregatedErrors - Optional string array to which any non-ignorable error messages will be
63
+ * appended. Each error is appended as an individual string.
64
+ * @returns {@link Success} with an array containing all successful results if all results either
65
+ * succeeded or returned error details listed in `ignore`. If any results failed with details
66
+ * that cannot be ignored, returns {@link Failure} with an concatenated summary of all non-ignorable
67
+ * error messages.
68
+ * @public
69
+ */
70
+ export function mapDetailedResults<T, TD>(
71
+ results: Iterable<DetailedResult<T, TD>>,
72
+ ignore: TD[],
73
+ aggregatedErrors?: IMessageAggregator
74
+ ): Result<T[]> {
75
+ const errors: string[] = [];
76
+ const elements: T[] = [];
77
+
78
+ for (const result of results) {
79
+ if (result.isSuccess()) {
80
+ elements.push(result.value);
81
+ } else if (result.detail && !ignore.includes(result.detail)) {
82
+ errors.push(result.message);
83
+ }
84
+ }
85
+
86
+ if (errors.length > 0) {
87
+ aggregatedErrors?.addMessages(errors);
88
+ return fail(errors.join('\n'));
89
+ }
90
+ return succeed(elements);
91
+ }
92
+
93
+ /**
94
+ * Aggregates successful results from a a collection of {@link Result | Result<T>}.
95
+ * @param results - An `Iterable` of {@link Result | Result<T>} from which success
96
+ * results are to be aggregated.
97
+ * @param aggregatedErrors - Optional string array to which any returned error messages will be
98
+ * appended. Each error is appended as an individual string.
99
+ * @returns {@link Success} with an array of `<T>` if any results were successful. If
100
+ * all {@link Result | results} failed, returns {@link Failure} with a concatenated
101
+ * summary of all error messages.
102
+ * @public
103
+ */
104
+ export function mapSuccess<T>(
105
+ results: Iterable<Result<T>>,
106
+ aggregatedErrors?: IMessageAggregator
107
+ ): Result<T[]> {
108
+ const errors: string[] = [];
109
+ const elements: T[] = [];
110
+
111
+ for (const result of results) {
112
+ if (result.isSuccess()) {
113
+ elements.push(result.value);
114
+ } else {
115
+ errors.push(result.message);
116
+ }
117
+ }
118
+
119
+ if (elements.length === 0 && errors.length > 0) {
120
+ aggregatedErrors?.addMessages(errors);
121
+ return fail(errors.join('\n'));
122
+ }
123
+ return succeed(elements);
124
+ }
125
+
126
+ /**
127
+ * Aggregates error messages from a collection of {@link Result | Result<T>}.
128
+ * @param results - An iterable collection of {@link Result | Result<T>} for which
129
+ * error messages are aggregated.
130
+ * @param aggregatedErrors - Optional string array to which any returned error messages will be
131
+ * appended. Each error is appended as an individual string.
132
+ * @returns An array of strings consisting of all error messages returned by
133
+ * {@link Result | results} in the source collection. Ignores {@link Success}
134
+ * results and returns an empty array if there were no errors.
135
+ * @public
136
+ */
137
+ export function mapFailures<T>(
138
+ results: Iterable<Result<T>>,
139
+ aggregatedErrors?: IMessageAggregator
140
+ ): string[] {
141
+ const errors: string[] = [];
142
+ for (const result of results) {
143
+ if (result.isFailure()) {
144
+ errors.push(result.message);
145
+ aggregatedErrors?.addMessage(result.message);
146
+ }
147
+ }
148
+ return errors;
149
+ }
150
+
151
+ /**
152
+ * Determines if an iterable collection of {@link Result | Result<T>} were all successful.
153
+ * @param results - The collection of {@link Result | Result<T>} to be tested.
154
+ * @param successValue - The value to be returned if results are successful.
155
+ * @param aggregatedErrors - Optional string array to which any returned error messages will be
156
+ * appended. Each error is appended as an individual string.
157
+ * @returns Returns {@link Success} with `successValue` if all {@link Result | results} are successful.
158
+ * If any are unsuccessful, returns {@link Failure} with a concatenated summary of the error
159
+ * messages from all failed elements.
160
+ * @public
161
+ */
162
+ export function allSucceed<T>(
163
+ results: Iterable<Result<unknown>>,
164
+ successValue: T,
165
+ aggregatedErrors?: IMessageAggregator
166
+ ): Result<T> {
167
+ const errors: string[] = [];
168
+
169
+ if (results !== undefined) {
170
+ for (const result of results) {
171
+ if (result.isFailure()) {
172
+ errors.push(result.message);
173
+ }
174
+ }
175
+ }
176
+
177
+ if (errors.length > 0) {
178
+ aggregatedErrors?.addMessages(errors);
179
+ return fail(errors.join('\n'));
180
+ }
181
+ return succeed(successValue);
182
+ }
183
+
184
+ /**
185
+ * String-keyed record of initialization functions to be passed to {@link (populateObject:1)}
186
+ * or {@link (populateObject:2)}.
187
+ * @public
188
+ */
189
+ export type FieldInitializers<T> = { [key in keyof T]: (state: Partial<T>) => Result<T[key]> };
190
+
191
+ /**
192
+ * Options for the {@link (populateObject:1)} function.
193
+ * @public
194
+ */
195
+ // eslint-disable-next-line @typescript-eslint/naming-convention
196
+ export interface PopulateObjectOptions<T> {
197
+ /**
198
+ * If present, specifies the order in which property values should
199
+ * be evaluated. Any keys not listed are evaluated after all listed
200
+ * keys in indeterminate order. If 'order' is not present, keys
201
+ * are evaluated in indeterminate order.
202
+ */
203
+ order?: (keyof T)[];
204
+
205
+ /**
206
+ * Specify handling of `undefined` values. By default, successful
207
+ * `undefined` results are written to the result object. If this value
208
+ * is `true` then `undefined` results are suppressed for all properties.
209
+ * If this value is an array of property keys then `undefined` results
210
+ * are suppressed for those properties only.
211
+ */
212
+ suppressUndefined?: boolean | (keyof T)[];
213
+ }
214
+
215
+ /**
216
+ * Populates an an object based on a prototype full of field initializers that return {@link Result | Result<T[key]>}.
217
+ * Returns {@link Success} with the populated object if all initializers succeed, or {@link Failure} with a
218
+ * concatenated list of all error messages.
219
+ * @param initializers - An object with the shape of the target but with initializer functions for
220
+ * each property.
221
+ * @param options - An optional {@link PopulateObjectOptions | set of options} which
222
+ * modify the behavior of this call.
223
+ * @param aggregatedErrors - Optional string array to which any returned error messages will be
224
+ * appended. Each error is appended as an individual string.
225
+ * {@label WITH_OPTIONS}
226
+ * @public
227
+ */
228
+ export function populateObject<T>(
229
+ initializers: FieldInitializers<T>,
230
+ options?: PopulateObjectOptions<T>,
231
+ aggregatedErrors?: IMessageAggregator
232
+ ): Result<T>;
233
+
234
+ /**
235
+ * Populates an an object based on a prototype full of field initializers that return {@link Result | Result<T[key]>}.
236
+ * Returns {@link Success} with the populated object if all initializers succeed, or {@link Failure} with a
237
+ * concatenated list of all error messages.
238
+ * @param initializers - An object with the shape of the target but with initializer functions for
239
+ * each property.
240
+ * @param order - Optional order in which keys should be written.
241
+ * @param aggregatedErrors - Optional string array to which any returned error messages will be
242
+ * appended. Each error is appended as an individual string.
243
+ * @public
244
+ * {@label WITH_ORDER}
245
+ * @deprecated Pass {@link PopulateObjectOptions} instead.
246
+ */
247
+ export function populateObject<T>(
248
+ initializers: FieldInitializers<T>,
249
+ order: (keyof T)[] | undefined,
250
+ aggregatedErrors?: IMessageAggregator
251
+ ): Result<T>;
252
+
253
+ export function populateObject<T>(
254
+ initializers: FieldInitializers<T>,
255
+ optionsOrOrder?: PopulateObjectOptions<T> | (keyof T)[],
256
+ aggregatedErrors?: IMessageAggregator
257
+ ): Result<T> {
258
+ const options: PopulateObjectOptions<T> = optionsOrOrder
259
+ ? Array.isArray(optionsOrOrder)
260
+ ? { order: optionsOrOrder }
261
+ : optionsOrOrder
262
+ : {};
263
+ const state = {} as { [key in keyof T]: T[key] };
264
+ const errors: string[] = [];
265
+ const keys: (keyof T)[] = Array.from(options.order ?? []);
266
+ const foundKeys = new Set<keyof T>(options.order);
267
+
268
+ // start with the supplied order then append anything else we find
269
+ for (const key in initializers) {
270
+ if (!foundKeys.has(key)) {
271
+ keys.push(key);
272
+ foundKeys.add(key);
273
+ }
274
+ }
275
+
276
+ for (const key of keys) {
277
+ if (initializers[key]) {
278
+ const result = initializers[key](state);
279
+ if (result.isSuccess()) {
280
+ if (result.value === undefined) {
281
+ if (
282
+ options.suppressUndefined === true ||
283
+ (Array.isArray(options.suppressUndefined) && options.suppressUndefined.includes(key))
284
+ ) {
285
+ continue;
286
+ }
287
+ }
288
+ state[key] = result.value;
289
+ } else {
290
+ errors.push(result.message);
291
+ }
292
+ } else {
293
+ errors.push(`populateObject: Key ${String(key)} is present but has no initializer`);
294
+ }
295
+ }
296
+
297
+ if (errors.length > 0) {
298
+ aggregatedErrors?.addMessages(errors);
299
+ return fail(errors.join('\n'));
300
+ }
301
+ return succeed(state as T);
302
+ }
@@ -0,0 +1,120 @@
1
+ /*
2
+ * Copyright (c) 2024 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
+ import { IMessageAggregator, Result, fail } from './result';
24
+
25
+ /**
26
+ * A simple error aggregator to simplify collecting and reporting all errors in
27
+ * a flow.
28
+ * @public
29
+ */
30
+ export class MessageAggregator implements IMessageAggregator {
31
+ private readonly _messages: string[];
32
+
33
+ /**
34
+ * Constructs a new {@link MessageAggregator | ErrorAggregator} with an
35
+ * optionally specified initial set of error messages.
36
+ * @param errors - optional array of errors to be included
37
+ * in the aggregation.
38
+ */
39
+ public constructor(errors?: string[]) {
40
+ this._messages = errors ? Array.from(errors) : [];
41
+ }
42
+
43
+ /**
44
+ * {@inheritdoc IMessageAggregator.hasMessages}
45
+ */
46
+ public get hasMessages(): boolean {
47
+ return this._messages.length > 0;
48
+ }
49
+
50
+ /**
51
+ * {@inheritdoc IMessageAggregator.numMessages}
52
+ */
53
+ public get numMessages(): number {
54
+ return this._messages.length;
55
+ }
56
+
57
+ /**
58
+ * {@inheritdoc IMessageAggregator.messages}
59
+ */
60
+ public get messages(): string[] {
61
+ return this._messages;
62
+ }
63
+
64
+ /**
65
+ * {@inheritdoc IMessageAggregator.addMessage}
66
+ */
67
+ public addMessage(message: string | undefined): this {
68
+ if (message) {
69
+ this._messages.push(message);
70
+ }
71
+ return this;
72
+ }
73
+
74
+ /**
75
+ * {@inheritdoc IMessageAggregator.addMessages}
76
+ */
77
+ public addMessages(messages: string[] | undefined): this {
78
+ if (messages && messages.length > 0) {
79
+ this._messages.push(...messages);
80
+ }
81
+ return this;
82
+ }
83
+
84
+ /**
85
+ * {@inheritdoc IMessageAggregator.toString}
86
+ */
87
+ public toString(separator?: string): string {
88
+ return this._messages.join(separator ?? '\n');
89
+ }
90
+
91
+ /**
92
+ * If any error messages have been aggregated, returns
93
+ * {@link Failure | Failure<T>} with the aggregated
94
+ * messages concatenated using the optionally-supplied
95
+ * separator, or newline. If the supplied {@link Result | Result<T>}
96
+ * contains an error message that has not already been aggregated,
97
+ * it will be included in the aggregated messages.
98
+ *
99
+ * If no error messages have been aggregated, returns
100
+ * the supplied {@link Result | Result<T>}.
101
+ * @param result - The {@link Result | Result<T>} to be returned
102
+ * if no messages have been aggregated.
103
+ * @param separator - Optional string separator used to construct
104
+ * the error message.
105
+ * @returns {@link Failure | Failure<T>} with an aggregated message
106
+ * if any error messages were collected, the supplied
107
+ * {@link Result | Result<T>} otherwise.
108
+ */
109
+ public returnOrReport<T>(result: Result<T>, separator?: string): Result<T> {
110
+ if (!this.hasMessages) {
111
+ return result;
112
+ }
113
+ if (!result.success) {
114
+ if (!this._messages.find((s) => s === result.message)) {
115
+ return fail([...this._messages, result.message].join(separator ?? '\n'));
116
+ }
117
+ }
118
+ return fail(this.toString(separator));
119
+ }
120
+ }
@@ -0,0 +1,144 @@
1
+ /*
2
+ * Copyright (c) 2021 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
+ import { mapResults } from './mapResults';
24
+ import { Result, fail, succeed } from './result';
25
+
26
+ /**
27
+ * @internal
28
+ */
29
+ export type Entry<T> = [string | number | symbol, T];
30
+
31
+ /**
32
+ * @internal
33
+ */
34
+ export type ResultEntry<T> = [string | number | symbol, Result<T>];
35
+
36
+ /**
37
+ * Normalizes an arbitrary JSON object
38
+ * @public
39
+ */
40
+ export class Normalizer {
41
+ /**
42
+ * Normalizes the supplied value
43
+ *
44
+ * @param from - The value to be normalized
45
+ * @returns A normalized version of the value
46
+ */
47
+ public normalize<T>(from: T): Result<T> {
48
+ switch (typeof from) {
49
+ case 'string':
50
+ case 'bigint':
51
+ case 'boolean':
52
+ case 'number':
53
+ case 'symbol':
54
+ case 'undefined':
55
+ return this.normalizeLiteral(from);
56
+ case 'object':
57
+ if (from === null || from instanceof Date || from instanceof RegExp) {
58
+ return this.normalizeLiteral(from);
59
+ } else if (Array.isArray(from)) {
60
+ return this._normalizeArray(from) as unknown as Result<T>;
61
+ } else if (from instanceof Map) {
62
+ return succeed(new Map(this.normalizeEntries(from.entries())) as unknown as T);
63
+ } else if (from instanceof Set) {
64
+ return succeed(new Set(this.normalizeEntries(from.entries())) as unknown as T);
65
+ }
66
+ const obj: { [key in number | string | symbol]: unknown } = {};
67
+ for (const e of this.normalizeEntries(Object.entries(from as unknown as object))) {
68
+ obj[e[0]] = e[1];
69
+ }
70
+ return succeed(obj as T);
71
+ }
72
+ return fail(`normalize: Unexpected type - cannot normalize '${typeof from}'`);
73
+ }
74
+
75
+ /**
76
+ * Compares two property names from some object being normalized.
77
+ * @param k1 - First key to be compared.
78
+ * @param k2 - Second key to be compared.
79
+ * @returns `1` if `k1` is greater, `-1` if `k2` is greater and
80
+ * `0` if they are equal.
81
+ * @internal
82
+ */
83
+ protected _compareKeys(k1: unknown, k2: unknown): number {
84
+ const cs1 = String(k1);
85
+ const cs2 = String(k2);
86
+ if (cs1 > cs2) {
87
+ return 1;
88
+ }
89
+ if (cs2 > cs1) {
90
+ return -1;
91
+ }
92
+ /* c8 ignore next 2 */
93
+ return 0;
94
+ }
95
+
96
+ /**
97
+ * Normalizes an array of object property entries (e.g. as returned by `Object.entries()`).
98
+ * @remarks
99
+ * Converts property names (entry key) to string and then sorts as string.
100
+ * @param entries - The entries to be normalized.
101
+ * @returns A normalized sorted array of entries.
102
+ */
103
+ public normalizeEntries<T = unknown>(entries: Iterable<Entry<T>>): Entry<T>[] {
104
+ return Array.from(entries)
105
+ .sort((e1, e2) => this._compareKeys(e1[0], e2[0]))
106
+ .map((e) => [e[0], this.normalize(e[1])] as ResultEntry<T>)
107
+ .filter((e) => e[1].isSuccess())
108
+ .map((e) => [e[0], e[1].orThrow()]);
109
+ }
110
+
111
+ protected _normalizeArray(from: unknown[]): Result<unknown[]> {
112
+ return mapResults(from.map((v) => this.normalize(v)));
113
+ }
114
+
115
+ /**
116
+ * Normalizes the supplied literal value
117
+ * @param from - The literal value to be normalized.
118
+ * @returns A normalized value for the literal.
119
+ */
120
+ public normalizeLiteral<T>(from: T): Result<T> {
121
+ // TODO: Apply configurable normalization rules
122
+ switch (typeof from) {
123
+ case 'string':
124
+ return succeed(from);
125
+ case 'bigint':
126
+ case 'boolean':
127
+ case 'number':
128
+ case 'symbol':
129
+ case 'undefined':
130
+ return succeed(from);
131
+ }
132
+ if (from === null) {
133
+ return succeed(from);
134
+ }
135
+ if (from instanceof Date) {
136
+ return succeed(from);
137
+ }
138
+ if (from instanceof RegExp) {
139
+ return succeed(from);
140
+ }
141
+ /* c8 ignore next 2 */
142
+ return fail(`cannot normalize ${JSON.stringify(from)}`);
143
+ }
144
+ }