ts-data-forge 3.2.0 → 3.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (426) hide show
  1. package/README.md +45 -21
  2. package/dist/array/impl/array-utils-creation.d.mts +116 -0
  3. package/dist/array/impl/array-utils-creation.d.mts.map +1 -0
  4. package/dist/array/impl/array-utils-creation.mjs +110 -0
  5. package/dist/array/impl/array-utils-creation.mjs.map +1 -0
  6. package/dist/array/impl/array-utils-element-access.d.mts +61 -0
  7. package/dist/array/impl/array-utils-element-access.d.mts.map +1 -0
  8. package/dist/array/impl/array-utils-element-access.mjs +66 -0
  9. package/dist/array/impl/array-utils-element-access.mjs.map +1 -0
  10. package/dist/array/impl/array-utils-iterators.d.mts +59 -0
  11. package/dist/array/impl/array-utils-iterators.d.mts.map +1 -0
  12. package/dist/array/impl/array-utils-iterators.mjs +104 -0
  13. package/dist/array/impl/array-utils-iterators.mjs.map +1 -0
  14. package/dist/array/impl/array-utils-modification.d.mts +154 -0
  15. package/dist/array/impl/array-utils-modification.d.mts.map +1 -0
  16. package/dist/array/impl/array-utils-modification.mjs +139 -0
  17. package/dist/array/impl/array-utils-modification.mjs.map +1 -0
  18. package/dist/array/impl/array-utils-reducing-value.d.mts +214 -0
  19. package/dist/array/impl/array-utils-reducing-value.d.mts.map +1 -0
  20. package/dist/array/impl/array-utils-reducing-value.mjs +160 -0
  21. package/dist/array/impl/array-utils-reducing-value.mjs.map +1 -0
  22. package/dist/array/impl/array-utils-search.d.mts +179 -0
  23. package/dist/array/impl/array-utils-search.d.mts.map +1 -0
  24. package/dist/array/impl/array-utils-search.mjs +153 -0
  25. package/dist/array/impl/array-utils-search.mjs.map +1 -0
  26. package/dist/array/impl/array-utils-set-op.d.mts +100 -0
  27. package/dist/array/impl/array-utils-set-op.d.mts.map +1 -0
  28. package/dist/array/impl/array-utils-set-op.mjs +137 -0
  29. package/dist/array/impl/array-utils-set-op.mjs.map +1 -0
  30. package/dist/array/impl/array-utils-size.d.mts +24 -0
  31. package/dist/array/impl/array-utils-size.d.mts.map +1 -0
  32. package/dist/array/impl/array-utils-size.mjs +28 -0
  33. package/dist/array/impl/array-utils-size.mjs.map +1 -0
  34. package/dist/array/impl/array-utils-slice-clamped.d.mts +18 -0
  35. package/dist/array/impl/array-utils-slice-clamped.d.mts.map +1 -0
  36. package/dist/array/impl/array-utils-slice-clamped.mjs +49 -0
  37. package/dist/array/impl/array-utils-slice-clamped.mjs.map +1 -0
  38. package/dist/array/impl/array-utils-slicing.d.mts +120 -0
  39. package/dist/array/impl/array-utils-slicing.d.mts.map +1 -0
  40. package/dist/array/impl/array-utils-slicing.mjs +140 -0
  41. package/dist/array/impl/array-utils-slicing.mjs.map +1 -0
  42. package/dist/array/impl/array-utils-transformation.d.mts +348 -0
  43. package/dist/array/impl/array-utils-transformation.d.mts.map +1 -0
  44. package/dist/array/impl/array-utils-transformation.mjs +331 -0
  45. package/dist/array/impl/array-utils-transformation.mjs.map +1 -0
  46. package/dist/array/impl/array-utils-validation.d.mts +149 -0
  47. package/dist/array/impl/array-utils-validation.d.mts.map +1 -0
  48. package/dist/array/impl/array-utils-validation.mjs +166 -0
  49. package/dist/array/impl/array-utils-validation.mjs.map +1 -0
  50. package/dist/array/impl/index.d.mts +13 -0
  51. package/dist/array/impl/index.d.mts.map +1 -0
  52. package/dist/array/impl/index.mjs +13 -0
  53. package/dist/array/impl/index.mjs.map +1 -0
  54. package/dist/array/index.d.mts +1 -1
  55. package/dist/array/index.d.mts.map +1 -1
  56. package/dist/array/index.mjs +2 -1
  57. package/dist/array/index.mjs.map +1 -1
  58. package/dist/collections/imap-mapped.d.mts +83 -253
  59. package/dist/collections/imap-mapped.d.mts.map +1 -1
  60. package/dist/collections/imap-mapped.mjs +33 -164
  61. package/dist/collections/imap-mapped.mjs.map +1 -1
  62. package/dist/collections/imap.d.mts +436 -163
  63. package/dist/collections/imap.d.mts.map +1 -1
  64. package/dist/collections/imap.mjs +74 -94
  65. package/dist/collections/imap.mjs.map +1 -1
  66. package/dist/collections/iset-mapped.d.mts +828 -345
  67. package/dist/collections/iset-mapped.d.mts.map +1 -1
  68. package/dist/collections/iset-mapped.mjs +200 -242
  69. package/dist/collections/iset-mapped.mjs.map +1 -1
  70. package/dist/collections/iset.d.mts +397 -205
  71. package/dist/collections/iset.d.mts.map +1 -1
  72. package/dist/collections/iset.mjs +102 -184
  73. package/dist/collections/iset.mjs.map +1 -1
  74. package/dist/collections/queue.d.mts +155 -135
  75. package/dist/collections/queue.d.mts.map +1 -1
  76. package/dist/collections/queue.mjs +55 -156
  77. package/dist/collections/queue.mjs.map +1 -1
  78. package/dist/collections/stack.d.mts +154 -154
  79. package/dist/collections/stack.d.mts.map +1 -1
  80. package/dist/collections/stack.mjs +54 -203
  81. package/dist/collections/stack.mjs.map +1 -1
  82. package/dist/entry-point.d.mts +3 -0
  83. package/dist/entry-point.d.mts.map +1 -0
  84. package/dist/entry-point.mjs +62 -0
  85. package/dist/entry-point.mjs.map +1 -0
  86. package/dist/expect-type.d.mts +43 -172
  87. package/dist/expect-type.d.mts.map +1 -1
  88. package/dist/expect-type.mjs +43 -172
  89. package/dist/expect-type.mjs.map +1 -1
  90. package/dist/functional/match.d.mts +35 -140
  91. package/dist/functional/match.d.mts.map +1 -1
  92. package/dist/functional/match.mjs.map +1 -1
  93. package/dist/functional/optional.d.mts +282 -160
  94. package/dist/functional/optional.d.mts.map +1 -1
  95. package/dist/functional/optional.mjs +131 -71
  96. package/dist/functional/optional.mjs.map +1 -1
  97. package/dist/functional/pipe.d.mts +59 -113
  98. package/dist/functional/pipe.d.mts.map +1 -1
  99. package/dist/functional/pipe.mjs.map +1 -1
  100. package/dist/functional/result.d.mts +433 -332
  101. package/dist/functional/result.d.mts.map +1 -1
  102. package/dist/functional/result.mjs +233 -239
  103. package/dist/functional/result.mjs.map +1 -1
  104. package/dist/globals.d.mts +12 -5
  105. package/dist/guard/has-key.d.mts +23 -74
  106. package/dist/guard/has-key.d.mts.map +1 -1
  107. package/dist/guard/has-key.mjs +23 -74
  108. package/dist/guard/has-key.mjs.map +1 -1
  109. package/dist/guard/is-non-empty-string.d.mts +20 -87
  110. package/dist/guard/is-non-empty-string.d.mts.map +1 -1
  111. package/dist/guard/is-non-empty-string.mjs +20 -87
  112. package/dist/guard/is-non-empty-string.mjs.map +1 -1
  113. package/dist/guard/is-non-null-object.d.mts +14 -84
  114. package/dist/guard/is-non-null-object.d.mts.map +1 -1
  115. package/dist/guard/is-non-null-object.mjs +14 -84
  116. package/dist/guard/is-non-null-object.mjs.map +1 -1
  117. package/dist/guard/is-primitive.d.mts +13 -126
  118. package/dist/guard/is-primitive.d.mts.map +1 -1
  119. package/dist/guard/is-primitive.mjs +13 -126
  120. package/dist/guard/is-primitive.mjs.map +1 -1
  121. package/dist/guard/is-record.d.mts +21 -132
  122. package/dist/guard/is-record.d.mts.map +1 -1
  123. package/dist/guard/is-record.mjs +21 -132
  124. package/dist/guard/is-record.mjs.map +1 -1
  125. package/dist/guard/is-type.d.mts +201 -238
  126. package/dist/guard/is-type.d.mts.map +1 -1
  127. package/dist/guard/is-type.mjs +201 -238
  128. package/dist/guard/is-type.mjs.map +1 -1
  129. package/dist/guard/key-is-in.d.mts +22 -139
  130. package/dist/guard/key-is-in.d.mts.map +1 -1
  131. package/dist/guard/key-is-in.mjs +22 -139
  132. package/dist/guard/key-is-in.mjs.map +1 -1
  133. package/dist/index.d.mts +0 -1
  134. package/dist/index.d.mts.map +1 -1
  135. package/dist/index.mjs +0 -1
  136. package/dist/index.mjs.map +1 -1
  137. package/dist/iterator/range.d.mts +29 -62
  138. package/dist/iterator/range.d.mts.map +1 -1
  139. package/dist/iterator/range.mjs.map +1 -1
  140. package/dist/json/json.d.mts +191 -121
  141. package/dist/json/json.d.mts.map +1 -1
  142. package/dist/json/json.mjs +238 -136
  143. package/dist/json/json.mjs.map +1 -1
  144. package/dist/number/branded-types/finite-number.d.mts +24 -156
  145. package/dist/number/branded-types/finite-number.d.mts.map +1 -1
  146. package/dist/number/branded-types/finite-number.mjs +27 -159
  147. package/dist/number/branded-types/finite-number.mjs.map +1 -1
  148. package/dist/number/branded-types/int.d.mts +122 -120
  149. package/dist/number/branded-types/int.d.mts.map +1 -1
  150. package/dist/number/branded-types/int.mjs +122 -120
  151. package/dist/number/branded-types/int.mjs.map +1 -1
  152. package/dist/number/branded-types/int16.d.mts +22 -30
  153. package/dist/number/branded-types/int16.d.mts.map +1 -1
  154. package/dist/number/branded-types/int16.mjs +22 -30
  155. package/dist/number/branded-types/int16.mjs.map +1 -1
  156. package/dist/number/branded-types/int32.d.mts +22 -31
  157. package/dist/number/branded-types/int32.d.mts.map +1 -1
  158. package/dist/number/branded-types/int32.mjs +22 -31
  159. package/dist/number/branded-types/int32.mjs.map +1 -1
  160. package/dist/number/branded-types/non-negative-finite-number.d.mts +28 -36
  161. package/dist/number/branded-types/non-negative-finite-number.d.mts.map +1 -1
  162. package/dist/number/branded-types/non-negative-finite-number.mjs +31 -39
  163. package/dist/number/branded-types/non-negative-finite-number.mjs.map +1 -1
  164. package/dist/number/branded-types/non-negative-int16.d.mts +24 -34
  165. package/dist/number/branded-types/non-negative-int16.d.mts.map +1 -1
  166. package/dist/number/branded-types/non-negative-int16.mjs +24 -34
  167. package/dist/number/branded-types/non-negative-int16.mjs.map +1 -1
  168. package/dist/number/branded-types/non-negative-int32.d.mts +26 -35
  169. package/dist/number/branded-types/non-negative-int32.d.mts.map +1 -1
  170. package/dist/number/branded-types/non-negative-int32.mjs +26 -35
  171. package/dist/number/branded-types/non-negative-int32.mjs.map +1 -1
  172. package/dist/number/branded-types/non-zero-finite-number.d.mts +22 -37
  173. package/dist/number/branded-types/non-zero-finite-number.d.mts.map +1 -1
  174. package/dist/number/branded-types/non-zero-finite-number.mjs +25 -40
  175. package/dist/number/branded-types/non-zero-finite-number.mjs.map +1 -1
  176. package/dist/number/branded-types/non-zero-int.d.mts +15 -30
  177. package/dist/number/branded-types/non-zero-int.d.mts.map +1 -1
  178. package/dist/number/branded-types/non-zero-int.mjs +15 -30
  179. package/dist/number/branded-types/non-zero-int.mjs.map +1 -1
  180. package/dist/number/branded-types/non-zero-int16.d.mts +27 -35
  181. package/dist/number/branded-types/non-zero-int16.d.mts.map +1 -1
  182. package/dist/number/branded-types/non-zero-int16.mjs +27 -35
  183. package/dist/number/branded-types/non-zero-int16.mjs.map +1 -1
  184. package/dist/number/branded-types/non-zero-int32.d.mts +29 -36
  185. package/dist/number/branded-types/non-zero-int32.d.mts.map +1 -1
  186. package/dist/number/branded-types/non-zero-int32.mjs +29 -36
  187. package/dist/number/branded-types/non-zero-int32.mjs.map +1 -1
  188. package/dist/number/branded-types/non-zero-safe-int.d.mts +37 -38
  189. package/dist/number/branded-types/non-zero-safe-int.d.mts.map +1 -1
  190. package/dist/number/branded-types/non-zero-safe-int.mjs +39 -40
  191. package/dist/number/branded-types/non-zero-safe-int.mjs.map +1 -1
  192. package/dist/number/branded-types/non-zero-uint16.d.mts +24 -35
  193. package/dist/number/branded-types/non-zero-uint16.d.mts.map +1 -1
  194. package/dist/number/branded-types/non-zero-uint16.mjs +24 -35
  195. package/dist/number/branded-types/non-zero-uint16.mjs.map +1 -1
  196. package/dist/number/branded-types/non-zero-uint32.d.mts +24 -35
  197. package/dist/number/branded-types/non-zero-uint32.d.mts.map +1 -1
  198. package/dist/number/branded-types/non-zero-uint32.mjs +24 -35
  199. package/dist/number/branded-types/non-zero-uint32.mjs.map +1 -1
  200. package/dist/number/branded-types/positive-finite-number.d.mts +26 -40
  201. package/dist/number/branded-types/positive-finite-number.d.mts.map +1 -1
  202. package/dist/number/branded-types/positive-finite-number.mjs +29 -43
  203. package/dist/number/branded-types/positive-finite-number.mjs.map +1 -1
  204. package/dist/number/branded-types/positive-int.d.mts +133 -123
  205. package/dist/number/branded-types/positive-int.d.mts.map +1 -1
  206. package/dist/number/branded-types/positive-int.mjs +133 -123
  207. package/dist/number/branded-types/positive-int.mjs.map +1 -1
  208. package/dist/number/branded-types/positive-int16.d.mts +24 -35
  209. package/dist/number/branded-types/positive-int16.d.mts.map +1 -1
  210. package/dist/number/branded-types/positive-int16.mjs +24 -35
  211. package/dist/number/branded-types/positive-int16.mjs.map +1 -1
  212. package/dist/number/branded-types/positive-int32.d.mts +24 -35
  213. package/dist/number/branded-types/positive-int32.d.mts.map +1 -1
  214. package/dist/number/branded-types/positive-int32.mjs +24 -35
  215. package/dist/number/branded-types/positive-int32.mjs.map +1 -1
  216. package/dist/number/branded-types/positive-safe-int.d.mts +159 -33
  217. package/dist/number/branded-types/positive-safe-int.d.mts.map +1 -1
  218. package/dist/number/branded-types/positive-safe-int.mjs +160 -34
  219. package/dist/number/branded-types/positive-safe-int.mjs.map +1 -1
  220. package/dist/number/branded-types/positive-uint16.d.mts +24 -35
  221. package/dist/number/branded-types/positive-uint16.d.mts.map +1 -1
  222. package/dist/number/branded-types/positive-uint16.mjs +24 -35
  223. package/dist/number/branded-types/positive-uint16.mjs.map +1 -1
  224. package/dist/number/branded-types/positive-uint32.d.mts +26 -36
  225. package/dist/number/branded-types/positive-uint32.d.mts.map +1 -1
  226. package/dist/number/branded-types/positive-uint32.mjs +26 -36
  227. package/dist/number/branded-types/positive-uint32.mjs.map +1 -1
  228. package/dist/number/branded-types/safe-int.d.mts +140 -99
  229. package/dist/number/branded-types/safe-int.d.mts.map +1 -1
  230. package/dist/number/branded-types/safe-int.mjs +142 -101
  231. package/dist/number/branded-types/safe-int.mjs.map +1 -1
  232. package/dist/number/branded-types/safe-uint.d.mts +24 -33
  233. package/dist/number/branded-types/safe-uint.d.mts.map +1 -1
  234. package/dist/number/branded-types/safe-uint.mjs +25 -34
  235. package/dist/number/branded-types/safe-uint.mjs.map +1 -1
  236. package/dist/number/branded-types/uint.d.mts +121 -30
  237. package/dist/number/branded-types/uint.d.mts.map +1 -1
  238. package/dist/number/branded-types/uint.mjs +121 -30
  239. package/dist/number/branded-types/uint.mjs.map +1 -1
  240. package/dist/number/branded-types/uint16.d.mts +26 -34
  241. package/dist/number/branded-types/uint16.d.mts.map +1 -1
  242. package/dist/number/branded-types/uint16.mjs +26 -34
  243. package/dist/number/branded-types/uint16.mjs.map +1 -1
  244. package/dist/number/branded-types/uint32.d.mts +26 -68
  245. package/dist/number/branded-types/uint32.d.mts.map +1 -1
  246. package/dist/number/branded-types/uint32.mjs +26 -68
  247. package/dist/number/branded-types/uint32.mjs.map +1 -1
  248. package/dist/number/enum/int8.d.mts +37 -101
  249. package/dist/number/enum/int8.d.mts.map +1 -1
  250. package/dist/number/enum/int8.mjs +39 -170
  251. package/dist/number/enum/int8.mjs.map +1 -1
  252. package/dist/number/enum/uint8.d.mts +45 -55
  253. package/dist/number/enum/uint8.d.mts.map +1 -1
  254. package/dist/number/enum/uint8.mjs +46 -155
  255. package/dist/number/enum/uint8.mjs.map +1 -1
  256. package/dist/number/num.d.mts +145 -206
  257. package/dist/number/num.d.mts.map +1 -1
  258. package/dist/number/num.mjs +143 -199
  259. package/dist/number/num.mjs.map +1 -1
  260. package/dist/number/refined-number-utils.d.mts +97 -21
  261. package/dist/number/refined-number-utils.d.mts.map +1 -1
  262. package/dist/number/refined-number-utils.mjs +91 -20
  263. package/dist/number/refined-number-utils.mjs.map +1 -1
  264. package/dist/object/object.d.mts +126 -208
  265. package/dist/object/object.d.mts.map +1 -1
  266. package/dist/object/object.mjs +68 -102
  267. package/dist/object/object.mjs.map +1 -1
  268. package/dist/others/cast-mutable.d.mts +12 -88
  269. package/dist/others/cast-mutable.d.mts.map +1 -1
  270. package/dist/others/cast-mutable.mjs +13 -89
  271. package/dist/others/cast-mutable.mjs.map +1 -1
  272. package/dist/others/cast-readonly.d.mts +12 -168
  273. package/dist/others/cast-readonly.d.mts.map +1 -1
  274. package/dist/others/cast-readonly.mjs +13 -169
  275. package/dist/others/cast-readonly.mjs.map +1 -1
  276. package/dist/others/if-then.d.mts +6 -83
  277. package/dist/others/if-then.d.mts.map +1 -1
  278. package/dist/others/if-then.mjs +6 -83
  279. package/dist/others/if-then.mjs.map +1 -1
  280. package/dist/others/map-nullable.d.mts +12 -136
  281. package/dist/others/map-nullable.d.mts.map +1 -1
  282. package/dist/others/map-nullable.mjs.map +1 -1
  283. package/dist/others/memoize-function.d.mts +14 -157
  284. package/dist/others/memoize-function.d.mts.map +1 -1
  285. package/dist/others/memoize-function.mjs +14 -157
  286. package/dist/others/memoize-function.mjs.map +1 -1
  287. package/dist/others/tuple.d.mts +33 -151
  288. package/dist/others/tuple.d.mts.map +1 -1
  289. package/dist/others/tuple.mjs +33 -151
  290. package/dist/others/tuple.mjs.map +1 -1
  291. package/dist/others/unknown-to-string.d.mts +11 -125
  292. package/dist/others/unknown-to-string.d.mts.map +1 -1
  293. package/dist/others/unknown-to-string.mjs +14 -127
  294. package/dist/others/unknown-to-string.mjs.map +1 -1
  295. package/dist/promise/promise.d.mts +33 -20
  296. package/dist/promise/promise.d.mts.map +1 -1
  297. package/dist/promise/promise.mjs +34 -21
  298. package/dist/promise/promise.mjs.map +1 -1
  299. package/dist/types.d.mts +1 -1
  300. package/package.json +54 -50
  301. package/src/array/impl/array-utils-creation.mts +192 -0
  302. package/src/array/{array-utils-creation.test.mts → impl/array-utils-creation.test.mts} +121 -72
  303. package/src/array/impl/array-utils-element-access.mts +115 -0
  304. package/src/array/impl/array-utils-element-access.test.mts +151 -0
  305. package/src/array/impl/array-utils-iterators.mts +79 -0
  306. package/src/array/impl/array-utils-iterators.test.mts +98 -0
  307. package/src/array/impl/array-utils-modification.mts +434 -0
  308. package/src/array/{array-utils-modification.test.mts → impl/array-utils-modification.test.mts} +41 -28
  309. package/src/array/{array-utils-overload-type-error.test.mts → impl/array-utils-overload-type-error.test.mts} +33 -33
  310. package/src/array/impl/array-utils-reducing-value.mts +551 -0
  311. package/src/array/{array-utils-reducing-value.test.mts → impl/array-utils-reducing-value.test.mts} +45 -50
  312. package/src/array/impl/array-utils-search.mts +509 -0
  313. package/src/array/impl/array-utils-search.test.mts +346 -0
  314. package/src/array/impl/array-utils-set-op.mts +166 -0
  315. package/src/array/{array-utils-set-op.test.mts → impl/array-utils-set-op.test.mts} +42 -35
  316. package/src/array/impl/array-utils-size.mts +30 -0
  317. package/src/array/impl/array-utils-size.test.mts +9 -0
  318. package/src/array/impl/array-utils-slice-clamped.mts +51 -0
  319. package/src/array/{array-utils-slice-clamped.test.mts → impl/array-utils-slice-clamped.test.mts} +12 -12
  320. package/src/array/impl/array-utils-slicing.mts +275 -0
  321. package/src/array/impl/array-utils-slicing.test.mts +158 -0
  322. package/src/array/impl/array-utils-transformation.mts +746 -0
  323. package/src/array/{array-utils-transformation.test.mts → impl/array-utils-transformation.test.mts} +662 -889
  324. package/src/array/impl/array-utils-validation.mts +241 -0
  325. package/src/array/{array-utils-validation.test.mts → impl/array-utils-validation.test.mts} +194 -107
  326. package/src/array/{array.test.mts → impl/array.test.mts} +2 -2
  327. package/src/array/impl/index.mts +12 -0
  328. package/src/array/index.mts +1 -1
  329. package/src/collections/imap-mapped.mts +99 -265
  330. package/src/collections/imap.mts +477 -174
  331. package/src/collections/imap.test.mts +12 -19
  332. package/src/collections/iset-mapped.mts +892 -358
  333. package/src/collections/iset.mts +429 -213
  334. package/src/collections/queue.mts +174 -200
  335. package/src/collections/stack.mts +172 -245
  336. package/src/collections/stack.test.mts +9 -1
  337. package/src/entry-point.mts +2 -0
  338. package/src/expect-type.mts +43 -172
  339. package/src/functional/match.mts +35 -145
  340. package/src/functional/optional.mts +285 -163
  341. package/src/functional/optional.test.mts +4 -1
  342. package/src/functional/pipe.mts +60 -113
  343. package/src/functional/result.mts +452 -351
  344. package/src/functional/result.test.mts +9 -2
  345. package/src/globals.d.mts +12 -5
  346. package/src/guard/has-key.mts +23 -74
  347. package/src/guard/is-non-empty-string.mts +20 -87
  348. package/src/guard/is-non-null-object.mts +14 -84
  349. package/src/guard/is-non-null-object.test.mts +1 -1
  350. package/src/guard/is-primitive.mts +13 -126
  351. package/src/guard/is-primitive.test.mts +1 -1
  352. package/src/guard/is-record.mts +21 -132
  353. package/src/guard/is-record.test.mts +0 -1
  354. package/src/guard/is-type.mts +201 -238
  355. package/src/guard/is-type.test.mts +7 -7
  356. package/src/guard/key-is-in.mts +22 -139
  357. package/src/index.mts +0 -1
  358. package/src/iterator/range.mts +29 -62
  359. package/src/json/json.mts +202 -134
  360. package/src/json/json.test.mts +1 -3
  361. package/src/number/branded-types/finite-number.mts +27 -159
  362. package/src/number/branded-types/int.mts +122 -120
  363. package/src/number/branded-types/int16.mts +22 -30
  364. package/src/number/branded-types/int16.test.mts +24 -24
  365. package/src/number/branded-types/int32.mts +22 -31
  366. package/src/number/branded-types/int32.test.mts +39 -39
  367. package/src/number/branded-types/non-negative-finite-number.mts +31 -39
  368. package/src/number/branded-types/non-negative-int16.mts +24 -34
  369. package/src/number/branded-types/non-negative-int16.test.mts +16 -16
  370. package/src/number/branded-types/non-negative-int32.mts +26 -35
  371. package/src/number/branded-types/non-negative-int32.test.mts +30 -30
  372. package/src/number/branded-types/non-zero-finite-number.mts +25 -40
  373. package/src/number/branded-types/non-zero-int.mts +15 -30
  374. package/src/number/branded-types/non-zero-int16.mts +27 -35
  375. package/src/number/branded-types/non-zero-int16.test.mts +26 -26
  376. package/src/number/branded-types/non-zero-int32.mts +29 -36
  377. package/src/number/branded-types/non-zero-int32.test.mts +45 -42
  378. package/src/number/branded-types/non-zero-safe-int.mts +39 -40
  379. package/src/number/branded-types/non-zero-uint16.mts +24 -35
  380. package/src/number/branded-types/non-zero-uint16.test.mts +16 -16
  381. package/src/number/branded-types/non-zero-uint32.mts +24 -35
  382. package/src/number/branded-types/non-zero-uint32.test.mts +28 -28
  383. package/src/number/branded-types/positive-finite-number.mts +29 -43
  384. package/src/number/branded-types/positive-int.mts +134 -124
  385. package/src/number/branded-types/positive-int16.mts +24 -35
  386. package/src/number/branded-types/positive-int16.test.mts +14 -14
  387. package/src/number/branded-types/positive-int32.mts +24 -35
  388. package/src/number/branded-types/positive-int32.test.mts +26 -26
  389. package/src/number/branded-types/positive-safe-int.mts +160 -34
  390. package/src/number/branded-types/positive-uint16.mts +24 -35
  391. package/src/number/branded-types/positive-uint16.test.mts +16 -16
  392. package/src/number/branded-types/positive-uint32.mts +26 -36
  393. package/src/number/branded-types/positive-uint32.test.mts +31 -28
  394. package/src/number/branded-types/safe-int.mts +142 -101
  395. package/src/number/branded-types/safe-uint.mts +25 -34
  396. package/src/number/branded-types/uint.mts +121 -30
  397. package/src/number/branded-types/uint16.mts +26 -34
  398. package/src/number/branded-types/uint16.test.mts +16 -16
  399. package/src/number/branded-types/uint32.mts +26 -68
  400. package/src/number/branded-types/uint32.test.mts +28 -28
  401. package/src/number/enum/int8.mts +39 -170
  402. package/src/number/enum/uint8.mts +46 -155
  403. package/src/number/num.mts +157 -212
  404. package/src/number/num.test.mts +4 -4
  405. package/src/number/refined-number-utils.mts +109 -26
  406. package/src/object/object.mts +130 -212
  407. package/src/object/object.test.mts +29 -0
  408. package/src/others/cast-mutable.mts +13 -89
  409. package/src/others/cast-mutable.test.mts +80 -0
  410. package/src/others/cast-readonly.mts +13 -169
  411. package/src/others/if-then.mts +6 -83
  412. package/src/others/map-nullable.mts +12 -136
  413. package/src/others/map-nullable.test.mts +6 -6
  414. package/src/others/memoize-function.mts +14 -157
  415. package/src/others/tuple.mts +33 -151
  416. package/src/others/unknown-to-string.mts +15 -127
  417. package/src/others/unknown-to-string.test.mts +14 -2
  418. package/src/promise/promise.mts +34 -21
  419. package/src/promise/promise.test.mts +43 -0
  420. package/dist/array/array-utils.d.mts +0 -2956
  421. package/dist/array/array-utils.d.mts.map +0 -1
  422. package/dist/array/array-utils.mjs +0 -1838
  423. package/dist/array/array-utils.mjs.map +0 -1
  424. package/src/array/array-utils-search.test.mts +0 -169
  425. package/src/array/array-utils-slicing.test.mts +0 -274
  426. package/src/array/array-utils.mts +0 -4834
@@ -9,9 +9,9 @@ const OkTypeTagName = 'ts-data-forge::Result.ok';
9
9
  const ErrTypeTagName = 'ts-data-forge::Result.err';
10
10
 
11
11
  /**
12
+ * @template S The type of the success value.
12
13
  * @internal
13
14
  * Represents the 'Ok' variant of a Result, containing a success value.
14
- * @template S The type of the success value.
15
15
  */
16
16
  type Ok_<S> = Readonly<{
17
17
  /**
@@ -25,9 +25,9 @@ type Ok_<S> = Readonly<{
25
25
  }>;
26
26
 
27
27
  /**
28
+ * @template E The type of the error value.
28
29
  * @internal
29
30
  * Represents the 'Err' variant of a Result, containing an error value.
30
- * @template E The type of the error value.
31
31
  */
32
32
  type Err_<E> = Readonly<{
33
33
  /**
@@ -42,18 +42,32 @@ type Err_<E> = Readonly<{
42
42
 
43
43
  /**
44
44
  * Represents a value that can either be a success (`Ok`) or an error (`Err`).
45
+ *
45
46
  * @template S The type of the success value.
46
47
  * @template E The type of the error value.
47
48
  */
48
49
  export type Result<S, E> = Ok_<S> | Err_<E>;
49
50
 
50
51
  /**
51
- * Namespace for the `Result` type and related functions.
52
- * Provides utilities to handle operations that can succeed or fail.
52
+ * Namespace for the `Result` type and related functions. Provides utilities to
53
+ * handle operations that can succeed or fail.
53
54
  */
54
55
  export namespace Result {
55
56
  /**
56
57
  * Checks if the given value is a `Result`.
58
+ *
59
+ * @example
60
+ *
61
+ * ```ts
62
+ * const okValue = Result.ok('success');
63
+ * const errValue = Result.err(new Error('failure'));
64
+ * const notResult = { $$tag: 'ts-data-forge::Result.ok' };
65
+ *
66
+ * assert.ok(Result.isResult(okValue));
67
+ * assert.ok(Result.isResult(errValue));
68
+ * assert.notOk(Result.isResult(notResult));
69
+ * ```
70
+ *
57
71
  * @param maybeOptional The value to check.
58
72
  * @returns `true` if the value is a `Result`, otherwise `false`.
59
73
  */
@@ -68,46 +82,52 @@ export namespace Result {
68
82
 
69
83
  /**
70
84
  * Represents a `Result` that is a success, containing a value.
85
+ *
71
86
  * @template S The type of the success value.
72
87
  */
73
88
  export type Ok<S> = Ok_<S>;
74
89
 
75
90
  /**
76
91
  * Represents a `Result` that is an error, containing an error value.
92
+ *
77
93
  * @template E The type of the error value.
78
94
  */
79
95
  export type Err<E> = Err_<E>;
80
96
 
81
97
  /**
82
- * Base type for any `Result`, used for generic constraints.
83
- * Represents a `Result` with unknown success and error types.
98
+ * Base type for any `Result`, used for generic constraints. Represents a
99
+ * `Result` with unknown success and error types.
84
100
  */
85
101
  export type Base = Result<unknown, unknown>;
86
102
 
87
103
  /**
88
- * Extracts the success value type `S` from a `Result.Ok<S>`.
89
- * If the `Result` is `Result.Err<E>`, resolves to `never`.
104
+ * Extracts the success value type `S` from a `Result.Ok<S>`. If the `Result`
105
+ * is `Result.Err<E>`, resolves to `never`.
106
+ *
90
107
  * @template R The `Result.Base` type to unwrap.
91
108
  */
92
109
  export type UnwrapOk<R extends Base> = R extends Ok<infer S> ? S : never;
93
110
 
94
111
  /**
95
- * Extracts the error value type `E` from a `Result.Err<E>`.
96
- * If the `Result` is `Result.Ok<S>`, resolves to `never`.
112
+ * Extracts the error value type `E` from a `Result.Err<E>`. If the `Result`
113
+ * is `Result.Ok<S>`, resolves to `never`.
114
+ *
97
115
  * @template R The `Result.Base` type to unwrap.
98
116
  */
99
117
  export type UnwrapErr<R extends Base> = R extends Err<infer E> ? E : never;
100
118
 
101
119
  /**
102
- * Narrows a `Result.Base` type to `Result.Ok<S>` if it is an `Ok`.
103
- * If the `Result` is `Result.Err<E>`, resolves to `never`.
120
+ * Narrows a `Result.Base` type to `Result.Ok<S>` if it is an `Ok`. If the
121
+ * `Result` is `Result.Err<E>`, resolves to `never`.
122
+ *
104
123
  * @template R The `Result.Base` type to narrow.
105
124
  */
106
125
  export type NarrowToOk<R extends Base> = R extends Err<unknown> ? never : R;
107
126
 
108
127
  /**
109
- * Narrows a `Result.Base` type to `Result.Err<E>` if it is an `Err`.
110
- * If the `Result` is `Result.Ok<S>`, resolves to `never`.
128
+ * Narrows a `Result.Base` type to `Result.Err<E>` if it is an `Err`. If the
129
+ * `Result` is `Result.Ok<S>`, resolves to `never`.
130
+ *
111
131
  * @template R The `Result.Base` type to narrow.
112
132
  */
113
133
  export type NarrowToErr<R extends Base> = R extends Ok<unknown> ? never : R;
@@ -115,30 +135,25 @@ export namespace Result {
115
135
  /**
116
136
  * Creates a `Result.Ok` containing the given success value.
117
137
  *
118
- * Use this constructor when an operation succeeds and you want to wrap
119
- * the successful result in a Result type for consistent error handling.
138
+ * Use this constructor when an operation succeeds and you want to wrap the
139
+ * successful result in a Result type for consistent error handling.
120
140
  *
121
- * @template S The type of the success value.
122
- * @param value The success value.
123
- * @returns A `Result.Ok<S>` containing the value.
124
141
  * @example
125
- * ```typescript
126
- * // Basic success case
127
- * const success = Result.ok(42);
128
- * console.log(Result.isOk(success)); // true
129
- * console.log(Result.unwrapOk(success)); // 42
130
- *
131
- * // Function that returns a Result
132
- * function divide(a: number, b: number): Result<number, string> {
133
- * if (b === 0) {
134
- * return Result.err("Division by zero");
135
- * }
136
- * return Result.ok(a / b);
137
- * }
138
142
  *
139
- * const result = divide(10, 2);
140
- * console.log(Result.unwrapOk(result)); // 5
143
+ * ```ts
144
+ * const success = Result.ok({ id: 1 });
145
+ * const failure = Result.err(new Error('missing data'));
146
+ *
147
+ * assert.deepStrictEqual(success, {
148
+ * $$tag: 'ts-data-forge::Result.ok',
149
+ * value: { id: 1 },
150
+ * });
151
+ * assert.ok(Result.isErr(failure));
141
152
  * ```
153
+ *
154
+ * @template S The type of the success value.
155
+ * @param value The success value.
156
+ * @returns A `Result.Ok<S>` containing the value.
142
157
  */
143
158
  export const ok = <S,>(value: S): Ok<S> => ({
144
159
  $$tag: OkTypeTagName,
@@ -148,42 +163,25 @@ export namespace Result {
148
163
  /**
149
164
  * Creates a `Result.Err` containing the given error value.
150
165
  *
151
- * Use this constructor when an operation fails and you want to wrap
152
- * the error information in a Result type for consistent error handling.
166
+ * Use this constructor when an operation fails and you want to wrap the error
167
+ * information in a Result type for consistent error handling.
153
168
  *
154
- * @template E The type of the error value.
155
- * @param value The error value.
156
- * @returns A `Result.Err<E>` containing the value.
157
169
  * @example
158
- * ```typescript
159
- * // Basic error case
160
- * const failure = Result.err("Something went wrong");
161
- * console.log(Result.isErr(failure)); // true
162
- * console.log(Result.unwrapErr(failure)); // "Something went wrong"
163
- *
164
- * // Function that can fail
165
- * function parseInteger(input: string): Result<number, string> {
166
- * const num = parseInt(input, 10);
167
- * if (isNaN(num)) {
168
- * return Result.err(`Invalid number format: ${input}`);
169
- * }
170
- * return Result.ok(num);
171
- * }
172
- *
173
- * const result = parseInteger("abc");
174
- * console.log(Result.unwrapErr(result)); // "Invalid number format: abc"
175
170
  *
176
- * // Using custom error types
177
- * interface ValidationError {
178
- * field: string;
179
- * message: string;
180
- * }
171
+ * ```ts
172
+ * const success = Result.ok({ id: 1 });
173
+ * const failure = Result.err(new Error('missing data'));
181
174
  *
182
- * const validationError = Result.err<ValidationError>({
183
- * field: "email",
184
- * message: "Invalid email format"
175
+ * assert.deepStrictEqual(success, {
176
+ * $$tag: 'ts-data-forge::Result.ok',
177
+ * value: { id: 1 },
185
178
  * });
179
+ * assert.ok(Result.isErr(failure));
186
180
  * ```
181
+ *
182
+ * @template E The type of the error value.
183
+ * @param value The error value.
184
+ * @returns A `Result.Err<E>` containing the value.
187
185
  */
188
186
  export const err = <E,>(value: E): Err<E> => ({
189
187
  $$tag: ErrTypeTagName,
@@ -197,167 +195,131 @@ export namespace Result {
197
195
  const toStr_ = String;
198
196
 
199
197
  /**
200
- * Checks if a `Result` is `Result.Ok`.
201
- * Acts as a type guard, narrowing the type to the success variant.
198
+ * Checks if a `Result` is `Result.Ok`. Acts as a type guard, narrowing the
199
+ * type to the success variant.
202
200
  *
203
201
  * This function is essential for type-safe Result handling, allowing
204
- * TypeScript to understand that subsequent operations will work with
205
- * the success value rather than the error value.
202
+ * TypeScript to understand that subsequent operations will work with the
203
+ * success value rather than the error value.
206
204
  *
207
- * @template R The `Result.Base` type to check.
208
- * @param result The `Result` to check.
209
- * @returns `true` if the `Result` is `Result.Ok`, otherwise `false`.
210
205
  * @example
211
- * ```typescript
212
- * // Basic type guard usage
213
- * const result: Result<number, string> = divide(10, 2);
214
- *
215
- * if (Result.isOk(result)) {
216
- * // TypeScript knows result is Result.Ok<number>
217
- * console.log(result.value); // Safe to access .value
218
- * console.log(Result.unwrapOk(result)); // 5
219
- * } else {
220
- * // TypeScript knows result is Result.Err<string>
221
- * console.log(result.value); // Error message
222
- * }
223
206
  *
224
- * // Using in conditional logic
225
- * const processResult = (r: Result<string, Error>) => {
226
- * return Result.isOk(r)
227
- * ? r.value.toUpperCase() // Safe string operations
228
- * : "Error occurred";
229
- * };
207
+ * ```ts
208
+ * const operation = Result.ok(3);
209
+ * const failure = Result.err('error');
210
+ *
211
+ * if (Result.isOk(operation)) {
212
+ * const value: number = operation.value;
213
+ * assert(value === 3);
214
+ * }
230
215
  *
231
- * // Filtering arrays of Results
232
- * const results: Result<number, string>[] = [
233
- * Result.ok(1),
234
- * Result.err("error"),
235
- * Result.ok(2)
236
- * ];
237
- * const successes = results.filter(Result.isOk);
238
- * // successes is Result.Ok<number>[]
216
+ * assert.ok(Result.isErr(failure));
239
217
  * ```
218
+ *
219
+ * @template R The `Result.Base` type to check.
220
+ * @param result The `Result` to check.
221
+ * @returns `true` if the `Result` is `Result.Ok`, otherwise `false`.
240
222
  */
241
223
  export const isOk = <R extends Base>(result: R): result is NarrowToOk<R> =>
242
224
  result.$$tag === OkTypeTagName;
243
225
 
244
226
  /**
245
- * Checks if a `Result` is `Result.Err`.
246
- * Acts as a type guard, narrowing the type to the error variant.
227
+ * Checks if a `Result` is `Result.Err`. Acts as a type guard, narrowing the
228
+ * type to the error variant.
247
229
  *
248
230
  * This function is essential for type-safe Result handling, allowing
249
- * TypeScript to understand that subsequent operations will work with
250
- * the error value rather than the success value.
231
+ * TypeScript to understand that subsequent operations will work with the
232
+ * error value rather than the success value.
251
233
  *
252
- * @template R The `Result.Base` type to check.
253
- * @param result The `Result` to check.
254
- * @returns `true` if the `Result` is `Result.Err`, otherwise `false`.
255
234
  * @example
256
- * ```typescript
257
- * // Basic type guard usage
258
- * const result: Result<number, string> = divide(10, 0);
259
- *
260
- * if (Result.isErr(result)) {
261
- * // TypeScript knows result is Result.Err<string>
262
- * console.log(result.value); // Safe to access error .value
263
- * console.log(Result.unwrapErr(result)); // "Division by zero"
264
- * } else {
265
- * // TypeScript knows result is Result.Ok<number>
266
- * console.log(result.value); // Success value
267
- * }
268
235
  *
269
- * // Error handling patterns
270
- * const handleResult = (r: Result<Data, ApiError>) => {
271
- * if (Result.isErr(r)) {
272
- * logError(r.value); // Safe error operations
273
- * return null;
274
- * }
275
- * return processData(r.value);
276
- * };
236
+ * ```ts
237
+ * const operation = Result.ok(3);
238
+ * const failure = Result.err('error');
239
+ *
240
+ * if (Result.isOk(operation)) {
241
+ * const value: number = operation.value;
242
+ * assert(value === 3);
243
+ * }
277
244
  *
278
- * // Collecting errors from multiple Results
279
- * const results: Result<string, ValidationError>[] = validateForm();
280
- * const errors = results
281
- * .filter(Result.isErr)
282
- * .map(err => err.value); // ValidationError[]
245
+ * assert.ok(Result.isErr(failure));
283
246
  * ```
247
+ *
248
+ * @template R The `Result.Base` type to check.
249
+ * @param result The `Result` to check.
250
+ * @returns `true` if the `Result` is `Result.Err`, otherwise `false`.
284
251
  */
285
252
  export const isErr = <R extends Base>(result: R): result is NarrowToErr<R> =>
286
253
  result.$$tag === ErrTypeTagName;
287
254
 
288
255
  /**
289
- * Unwraps a `Result`, returning the success value.
290
- * Throws an error if the `Result` is `Result.Err`.
256
+ * Unwraps a `Result`, returning the success value. Throws an error if the
257
+ * `Result` is `Result.Err`.
258
+ *
259
+ * This is useful when you're confident that a Result should contain a success
260
+ * value and want to treat errors as exceptional conditions. The error message
261
+ * will be constructed from the error value using the provided string
262
+ * conversion function.
263
+ *
264
+ * @example
291
265
  *
292
- * This is useful when you're confident that a Result should contain a success value
293
- * and want to treat errors as exceptional conditions. The error message will be
294
- * constructed from the error value using the provided string conversion function.
266
+ * ```ts
267
+ * const okResult = Result.ok('data');
268
+ * const errResult = Result.err(new Error('fail'));
269
+ *
270
+ * assert(Result.unwrapThrow(okResult) === 'data');
271
+ * assert.throws(() => Result.unwrapThrow(errResult), /fail/u);
272
+ * ```
295
273
  *
296
274
  * @template R The `Result.Base` type to unwrap.
297
275
  * @param result The `Result` to unwrap.
298
- * @param toStr An optional function to convert the error value to a string for the error message. Defaults to `String`.
276
+ * @param toStr An optional function to convert the error value to a string
277
+ * for the error message. Defaults to `String`.
299
278
  * @returns The success value if `Result.Ok`.
300
- * @throws {Error} Error with the stringified error value if the `Result` is `Result.Err`.
301
- * @example
302
- * ```typescript
303
- * const success = Result.ok(42);
304
- * console.log(Result.unwrapThrow(success)); // 42
305
- *
306
- * const failure = Result.err("Network error");
307
- * try {
308
- * Result.unwrapThrow(failure); // throws Error: "Network error"
309
- * } catch (error) {
310
- * console.log(error.message); // "Network error"
311
- * }
312
- * ```
279
+ * @throws {Error} Error with the stringified error value if the `Result` is
280
+ * `Result.Err`.
313
281
  */
314
282
  export const unwrapThrow = <R extends Base>(
315
283
  result: R,
316
284
  toStr: (e: UnwrapErr<R>) => string = toStr_,
317
285
  ): UnwrapOk<R> => {
318
286
  if (isErr(result)) {
319
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
287
+ // eslint-disable-next-line total-functions/no-unsafe-type-assertion
320
288
  throw new Error(toStr(result.value as UnwrapErr<R>));
321
289
  }
322
290
 
323
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
291
+ // eslint-disable-next-line total-functions/no-unsafe-type-assertion
324
292
  return result.value as UnwrapOk<R>;
325
293
  };
326
294
 
327
295
  /**
328
- * Unwraps a `Result`, returning the success value or `undefined` if it's an error.
296
+ * Unwraps a `Result`, returning the success value or `undefined` if it's an
297
+ * error.
298
+ *
299
+ * This function provides a safe way to extract success values from Results
300
+ * without throwing exceptions. It has overloaded behavior based on the type:
329
301
  *
330
- * This function provides a safe way to extract success values from Results without
331
- * throwing exceptions. It has overloaded behavior based on the type:
332
302
  * - For `Result.Ok<T>`: Always returns `T` (guaranteed by type system)
333
303
  * - For general `Result<T, E>`: Returns `T | undefined`
334
304
  *
335
- * @template R The `Result.Base` type to unwrap.
336
- * @param result The `Result` to unwrap.
337
- * @returns The success value if `Result.Ok`, otherwise `undefined`.
338
305
  * @example
339
- * ```typescript
340
- * // With guaranteed Ok - returns the value
341
- * const success = Result.ok(42);
342
- * const value = Result.unwrapOk(success); // Type: number, Value: 42
343
- *
344
- * // With general Result - may return undefined
345
- * const maybeResult: Result<string, Error> = fetchData();
346
- * const data = Result.unwrapOk(maybeResult); // Type: string | undefined
347
- *
348
- * // Safe pattern for handling both cases
349
- * const result = Result.ok("hello");
350
- * const unwrapped = Result.unwrapOk(result);
351
- * if (unwrapped !== undefined) {
352
- * console.log(unwrapped.toUpperCase()); // "HELLO"
353
- * }
354
306
  *
355
- * // Useful in conditional chains
356
- * const processResult = (r: Result<number, string>) => {
357
- * const value = Result.unwrapOk(r);
358
- * return value !== undefined ? value * 2 : 0;
359
- * };
307
+ * ```ts
308
+ * const okResult = Result.ok(42);
309
+ * const errResult = Result.err('oops');
310
+ *
311
+ * // Result.unwrapOk returns the value for Ok results
312
+ *
313
+ * assert(Result.unwrapOk(okResult) === 42);
314
+ *
315
+ * // Result.unwrapOk returns undefined for Err results
316
+ * // eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
317
+ * assert(Result.unwrapOk(errResult) === undefined);
360
318
  * ```
319
+ *
320
+ * @template R The `Result.Base` type to unwrap.
321
+ * @param result The `Result` to unwrap.
322
+ * @returns The success value if `Result.Ok`, otherwise `undefined`.
361
323
  */
362
324
  export function unwrapOk<R extends Ok<unknown>>(result: R): UnwrapOk<R>;
363
325
 
@@ -366,23 +328,34 @@ export namespace Result {
366
328
  export function unwrapOk<R extends Base>(result: R): UnwrapOk<R> | undefined {
367
329
  return isErr(result)
368
330
  ? undefined
369
- : // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
331
+ : // eslint-disable-next-line total-functions/no-unsafe-type-assertion
370
332
  (result.value as UnwrapOk<R>);
371
333
  }
372
334
 
373
335
  /**
374
- * Unwraps a `Result`, returning the success value or a default value if it is `Result.Err`.
336
+ * Unwraps a `Result`, returning the success value or a default value if it is
337
+ * `Result.Err`.
338
+ *
339
+ * @example
340
+ *
341
+ * ```ts
342
+ * const okValue = Result.ok(10);
343
+ * const errValue = Result.err('fail');
344
+ *
345
+ * assert(Result.unwrapOkOr(okValue, 0) === 10);
346
+ * assert(Result.unwrapOkOr(errValue, 0) === 0);
347
+ *
348
+ * const unwrapWithDefault = Result.unwrapOkOr(5);
349
+ *
350
+ * assert(unwrapWithDefault(Result.ok(3)) === 3);
351
+ * assert(unwrapWithDefault(Result.err('no data')) === 5);
352
+ * ```
353
+ *
375
354
  * @template R The `Result.Base` type to unwrap.
376
355
  * @template D The type of the default value.
377
356
  * @param result The `Result` to unwrap.
378
357
  * @param defaultValue The value to return if `result` is `Result.Err`.
379
358
  * @returns The success value if `Result.Ok`, otherwise `defaultValue`.
380
- * @example
381
- * ```typescript
382
- * const result = Result.ok(42);
383
- * const value = Result.unwrapOkOr(result, 0);
384
- * console.log(value); // 42
385
- * ```
386
359
  */
387
360
  export function unwrapOkOr<R extends Base, D>(
388
361
  result: R,
@@ -404,7 +377,7 @@ export namespace Result {
404
377
  case 2: {
405
378
  // Direct version: first argument is result
406
379
  const [result, defaultValue] = args;
407
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
380
+ // eslint-disable-next-line total-functions/no-unsafe-type-assertion
408
381
  return isErr(result) ? defaultValue : (result.value as UnwrapOk<R>);
409
382
  }
410
383
 
@@ -418,30 +391,31 @@ export namespace Result {
418
391
  }
419
392
 
420
393
  /**
421
- * Unwraps a `Result`, returning the error value.
422
- * Throws an error if the `Result` is `Result.Ok`.
394
+ * Unwraps a `Result`, returning the error value. Throws an error if the
395
+ * `Result` is `Result.Ok`.
423
396
  *
424
397
  * This function is used when you expect a Result to be an error and want to
425
- * extract the error value. If the Result is unexpectedly Ok, it will throw
426
- * an error with information about the unexpected success value.
398
+ * extract the error value. If the Result is unexpectedly Ok, it will throw an
399
+ * error with information about the unexpected success value.
400
+ *
401
+ * @example
402
+ *
403
+ * ```ts
404
+ * const errResult = Result.err(new Error('broken'));
405
+ * const okResult = Result.ok('value');
406
+ *
407
+ * assert(Result.unwrapErrThrow(errResult).message === 'broken');
408
+ * assert.throws(() => Result.unwrapErrThrow(okResult), /Expected Err/u);
409
+ * ```
427
410
  *
428
411
  * @template R The `Result.Base` type to unwrap.
429
412
  * @param result The `Result` to unwrap.
430
- * @param toStr An optional function to convert the success value to a string for the error message when the Result is unexpectedly Ok. Defaults to `String`.
413
+ * @param toStr An optional function to convert the success value to a string
414
+ * for the error message when the Result is unexpectedly Ok. Defaults to
415
+ * `String`.
431
416
  * @returns The error value if `Result.Err`.
432
- * @throws {Error} Error with message "Expected Err but got Ok: {value}" if the `Result` is `Result.Ok`.
433
- * @example
434
- * ```typescript
435
- * const failure = Result.err("Network timeout");
436
- * console.log(Result.unwrapErrThrow(failure)); // "Network timeout"
437
- *
438
- * const success = Result.ok(42);
439
- * try {
440
- * Result.unwrapErrThrow(success); // throws Error: "Expected Err but got Ok: 42"
441
- * } catch (error) {
442
- * console.log(error.message); // "Expected Err but got Ok: 42"
443
- * }
444
- * ```
417
+ * @throws {Error} Error with message "Expected Err but got Ok: {value}" if
418
+ * the `Result` is `Result.Ok`.
445
419
  */
446
420
  export const unwrapErrThrow = <R extends Base>(
447
421
  result: R,
@@ -449,53 +423,72 @@ export namespace Result {
449
423
  ): UnwrapErr<R> => {
450
424
  if (isOk(result)) {
451
425
  throw new Error(
452
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
426
+ // eslint-disable-next-line total-functions/no-unsafe-type-assertion
453
427
  `Expected Err but got Ok: ${toStr(result.value as UnwrapOk<R>)}`,
454
428
  );
455
429
  }
456
430
 
457
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
431
+ // eslint-disable-next-line total-functions/no-unsafe-type-assertion
458
432
  return result.value as UnwrapErr<R>;
459
433
  };
460
434
 
461
435
  /**
462
- * Unwraps a `Result`, returning the error value or `undefined` if it is `Result.Ok`.
436
+ * Unwraps a `Result`, returning the error value or `undefined` if it is
437
+ * `Result.Ok`.
463
438
  *
464
- * This provides a safe way to extract error values from Results without throwing
465
- * exceptions. Useful for error handling patterns where you want to check for
466
- * specific error conditions.
439
+ * This provides a safe way to extract error values from Results without
440
+ * throwing exceptions. Useful for error handling patterns where you want to
441
+ * check for specific error conditions.
467
442
  *
468
- * @template R The `Result.Base` type to unwrap.
469
- * @param result The `Result` to unwrap.
470
- * @returns The error value if `Result.Err`, otherwise `undefined`.
471
443
  * @example
472
- * ```typescript
473
- * const failure = Result.err("Connection failed");
474
- * console.log(Result.unwrapErr(failure)); // "Connection failed"
475
444
  *
476
- * const success = Result.ok(42);
477
- * console.log(Result.unwrapErr(success)); // undefined
445
+ * ```ts
446
+ * const okResult = Result.ok('data');
447
+ * const errResult = Result.err('problem');
448
+ *
449
+ * // Result.unwrapErr returns undefined for Ok results
450
+ * // eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
451
+ * assert(Result.unwrapErr(okResult) === undefined);
452
+ *
453
+ * // Result.unwrapErr returns the error value for Err results
454
+ *
455
+ * assert(Result.unwrapErr(errResult) === 'problem');
478
456
  * ```
457
+ *
458
+ * @template R The `Result.Base` type to unwrap.
459
+ * @param result The `Result` to unwrap.
460
+ * @returns The error value if `Result.Err`, otherwise `undefined`.
479
461
  */
480
462
  export const unwrapErr = <R extends Base>(
481
463
  result: R,
482
464
  ): UnwrapErr<R> | undefined =>
483
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
465
+ // eslint-disable-next-line total-functions/no-unsafe-type-assertion
484
466
  isErr(result) ? (result.value as UnwrapErr<R>) : undefined;
485
467
 
486
468
  /**
487
- * Unwraps a `Result`, returning the error value or a default value if it is `Result.Ok`.
469
+ * Unwraps a `Result`, returning the error value or a default value if it is
470
+ * `Result.Ok`.
471
+ *
472
+ * @example
473
+ *
474
+ * ```ts
475
+ * const okResult = Result.ok('success');
476
+ * const errResult = Result.err('failure');
477
+ *
478
+ * assert(Result.unwrapErrOr(okResult, 'default') === 'default');
479
+ * assert(Result.unwrapErrOr(errResult, 'default') === 'failure');
480
+ *
481
+ * const unwrapError = Result.unwrapErrOr('fallback error');
482
+ *
483
+ * assert(unwrapError(Result.err('boom')) === 'boom');
484
+ * assert(unwrapError(Result.ok('no error')) === 'fallback error');
485
+ * ```
486
+ *
488
487
  * @template R The `Result.Base` type to unwrap.
489
488
  * @template D The type of the default value.
490
489
  * @param result The `Result` to unwrap.
491
490
  * @param defaultValue The value to return if `result` is `Result.Ok`.
492
491
  * @returns The error value if `Result.Err`, otherwise `defaultValue`.
493
- * @example
494
- * ```typescript
495
- * const result = Result.err("failed");
496
- * const error = Result.unwrapErrOr(result, "default");
497
- * console.log(error); // "failed"
498
- * ```
499
492
  */
500
493
  export function unwrapErrOr<R extends Base, D>(
501
494
  result: R,
@@ -517,7 +510,7 @@ export namespace Result {
517
510
  case 2: {
518
511
  // Direct version: first argument is result
519
512
  const [result, defaultValue] = args;
520
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
513
+ // eslint-disable-next-line total-functions/no-unsafe-type-assertion
521
514
  return isErr(result) ? (result.value as UnwrapErr<R>) : defaultValue;
522
515
  }
523
516
 
@@ -531,25 +524,34 @@ export namespace Result {
531
524
  }
532
525
 
533
526
  /**
534
- * Maps a `Result<S, E>` to `Result<S2, E>` by applying a function to the success value.
535
- * If the `Result` is `Result.Err`, returns the original `Err`.
527
+ * Maps a `Result<S, E>` to `Result<S2, E>` by applying a function to the
528
+ * success value. If the `Result` is `Result.Err`, returns the original
529
+ * `Err`.
530
+ *
531
+ * @example
532
+ *
533
+ * ```ts
534
+ * const okNumber = Result.ok(5);
535
+ * const errMessage = Result.err('error');
536
+ *
537
+ * const doubled = Result.map(okNumber, (value) => value * 2);
538
+ * const untouchedError = Result.map(errMessage, (value: number) => value * 2);
539
+ *
540
+ * assert.deepStrictEqual(doubled, Result.ok(10));
541
+ * assert.deepStrictEqual(untouchedError, errMessage);
542
+ *
543
+ * const mapToLength = Result.map((text: string) => text.length);
544
+ *
545
+ * assert.deepStrictEqual(mapToLength(Result.ok('abc')), Result.ok(3));
546
+ * assert.deepStrictEqual(mapToLength(Result.err('bad')), Result.err('bad'));
547
+ * ```
548
+ *
536
549
  * @template R The input `Result.Base` type.
537
- * @template S2 The type of the success value returned by the mapping function.
550
+ * @template S2 The type of the success value returned by the mapping
551
+ * function.
538
552
  * @param result The `Result` to map.
539
553
  * @param mapFn The function to apply to the success value if present.
540
554
  * @returns A new `Result<S2, UnwrapErr<R>>`.
541
- * @example
542
- * ```typescript
543
- * // Regular usage
544
- * const result = Result.ok(5);
545
- * const mapped = Result.map(result, x => x * 2);
546
- * console.log(Result.unwrap(mapped)); // 10
547
- *
548
- * // Curried version for use with pipe
549
- * const doubler = Result.map((x: number) => x * 2);
550
- * const result2 = pipe(Result.ok(5)).map(doubler).value;
551
- * console.log(Result.unwrap(result2)); // 10
552
- * ```
553
555
  */
554
556
  export function map<R extends Base, S2>(
555
557
  result: R,
@@ -570,9 +572,9 @@ export namespace Result {
570
572
  case 2: {
571
573
  const [result, mapFn] = args;
572
574
  return isErr(result)
573
- ? // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
575
+ ? // eslint-disable-next-line total-functions/no-unsafe-type-assertion
574
576
  (result as Err<UnwrapErr<R>>)
575
- : // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
577
+ : // eslint-disable-next-line total-functions/no-unsafe-type-assertion
576
578
  ok(mapFn(result.value as UnwrapOk<R>));
577
579
  }
578
580
 
@@ -585,19 +587,33 @@ export namespace Result {
585
587
  }
586
588
 
587
589
  /**
588
- * Maps a `Result<S, E>` to `Result<S, E2>` by applying a function to the error value.
589
- * If the `Result` is `Result.Ok`, returns the original `Ok`.
590
+ * Maps a `Result<S, E>` to `Result<S, E2>` by applying a function to the
591
+ * error value. If the `Result` is `Result.Ok`, returns the original `Ok`.
592
+ *
593
+ * @example
594
+ *
595
+ * ```ts
596
+ * const okValue = Result.ok(3) as Result<number, string>;
597
+ * const errValue = Result.err('missing');
598
+ *
599
+ * const untouchedOk = Result.mapErr(okValue, (error) => error.toUpperCase());
600
+ * const uppercasedErr = Result.mapErr(errValue, (error) => error.toUpperCase());
601
+ *
602
+ * assert.deepStrictEqual(untouchedOk, Result.ok(3));
603
+ * assert.deepStrictEqual(uppercasedErr, Result.err('MISSING'));
604
+ *
605
+ * const mapError = Result.mapErr((error: Readonly<Error>) => error.message);
606
+ *
607
+ * const wrapped = mapError(Result.err(new Error('boom')));
608
+ *
609
+ * assert.deepStrictEqual(wrapped, Result.err('boom'));
610
+ * ```
611
+ *
590
612
  * @template R The input `Result.Base` type.
591
613
  * @template E2 The type of the error value returned by the mapping function.
592
614
  * @param result The `Result` to map.
593
615
  * @param mapFn The function to apply to the error value if present.
594
616
  * @returns A new `Result<UnwrapOk<R>, E2>`.
595
- * @example
596
- * ```typescript
597
- * const result = Result.err("error");
598
- * const mapped = Result.mapErr(result, e => e.toUpperCase());
599
- * console.log(Result.unwrapErr(mapped)); // "ERROR"
600
- * ```
601
617
  */
602
618
  export function mapErr<R extends Base, E2>(
603
619
  result: R,
@@ -618,9 +634,9 @@ export namespace Result {
618
634
  case 2: {
619
635
  const [result, mapFn] = args;
620
636
  return isOk(result)
621
- ? // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
637
+ ? // eslint-disable-next-line total-functions/no-unsafe-type-assertion
622
638
  (result as Ok<UnwrapOk<R>>)
623
- : // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
639
+ : // eslint-disable-next-line total-functions/no-unsafe-type-assertion
624
640
  err(mapFn(result.value as UnwrapErr<R>));
625
641
  }
626
642
 
@@ -633,7 +649,38 @@ export namespace Result {
633
649
  }
634
650
 
635
651
  /**
636
- * Applies one of two functions depending on whether the `Result` is `Ok` or `Err`.
652
+ * Applies one of two functions depending on whether the `Result` is `Ok` or
653
+ * `Err`.
654
+ *
655
+ * @example
656
+ *
657
+ * ```ts
658
+ * const okValue = Result.ok(2);
659
+ * const errValue = Result.err('bad');
660
+ *
661
+ * const foldedOk = Result.fold(
662
+ * okValue,
663
+ * (value) => value * 2,
664
+ * (error) => error,
665
+ * );
666
+ * const foldedErr = Result.fold(
667
+ * errValue,
668
+ * (value: number) => value * 2,
669
+ * (error) => error.toUpperCase(),
670
+ * );
671
+ *
672
+ * assert.deepStrictEqual(foldedOk, Result.ok(4));
673
+ * assert.deepStrictEqual(foldedErr, Result.err('BAD'));
674
+ *
675
+ * const foldNumbers = Result.fold(
676
+ * (value: number) => value * 3,
677
+ * (error: string) => error.length,
678
+ * );
679
+ *
680
+ * assert.deepStrictEqual(foldNumbers(Result.ok(3)), Result.ok(9));
681
+ * assert.deepStrictEqual(foldNumbers(Result.err('oops')), Result.err(4));
682
+ * ```
683
+ *
637
684
  * @template R The input `Result.Base` type.
638
685
  * @template S2 The type of the success value returned by `mapFn`.
639
686
  * @template E2 The type of the error value returned by `mapErrFn`.
@@ -641,12 +688,6 @@ export namespace Result {
641
688
  * @param mapFn The function to apply if `result` is `Ok`.
642
689
  * @param mapErrFn The function to apply if `result` is `Err`.
643
690
  * @returns A new `Result<S2, E2>` based on the applied function.
644
- * @example
645
- * ```typescript
646
- * const result = Result.ok(42);
647
- * const folded = Result.fold(result, x => x * 2, () => 0);
648
- * console.log(Result.unwrapOk(folded)); // 84
649
- * ```
650
691
  */
651
692
  export function fold<R extends Base, S2, E2>(
652
693
  result: R,
@@ -678,9 +719,9 @@ export namespace Result {
678
719
  case 3: {
679
720
  const [result, mapFn, mapErrFn] = args;
680
721
  return isOk(result)
681
- ? // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
722
+ ? // eslint-disable-next-line total-functions/no-unsafe-type-assertion
682
723
  ok(mapFn(result.value as UnwrapOk<R>))
683
- : // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
724
+ : // eslint-disable-next-line total-functions/no-unsafe-type-assertion
684
725
  err(mapErrFn(result.value as UnwrapErr<R>));
685
726
  }
686
727
 
@@ -693,23 +734,39 @@ export namespace Result {
693
734
  }
694
735
 
695
736
  /**
696
- * Applies a function that returns a `Result` to the success value of a `Result`.
697
- * If the input is `Err`, returns the original `Err`.
698
- * This is the monadic bind operation for `Result`.
737
+ * Applies a function that returns a `Result` to the success value of a
738
+ * `Result`. If the input is `Err`, returns the original `Err`. This is the
739
+ * monadic bind operation for `Result`.
740
+ *
741
+ * @example
742
+ *
743
+ * ```ts
744
+ * const parseNumber = (input: string): Result<number, string> => {
745
+ * const num = Number.parseInt(input, 10);
746
+ * return Number.isNaN(num) ? Result.err('not a number') : Result.ok(num);
747
+ * };
748
+ *
749
+ * const parsed = Result.flatMap(Result.ok('42'), parseNumber);
750
+ * const failure = Result.flatMap(Result.ok('abc'), parseNumber);
751
+ * const passthrough = Result.flatMap(Result.err('fail'), parseNumber);
752
+ *
753
+ * assert.deepStrictEqual(parsed, Result.ok(42));
754
+ * assert.deepStrictEqual(failure, Result.err('not a number'));
755
+ * assert.deepStrictEqual(passthrough, Result.err('fail'));
756
+ *
757
+ * const parseThenDouble = Result.flatMap((input: string) =>
758
+ * Result.map(parseNumber(input), (value) => value * 2),
759
+ * );
760
+ *
761
+ * assert.deepStrictEqual(parseThenDouble(Result.ok('10')), Result.ok(20));
762
+ * ```
763
+ *
699
764
  * @template R The input `Result.Base` type.
700
765
  * @template S2 The success type of the `Result` returned by the function.
701
766
  * @template E2 The error type of the `Result` returned by the function.
702
767
  * @param result The `Result` to flat map.
703
768
  * @param flatMapFn The function to apply that returns a `Result`.
704
769
  * @returns The result of applying the function, or the original `Err`.
705
- * @example
706
- * ```typescript
707
- * const divide = (a: number, b: number): Result<number, string> =>
708
- * b === 0 ? Result.err("Division by zero") : Result.ok(a / b);
709
- *
710
- * const result = Result.flatMap(Result.ok(10), x => divide(x, 2));
711
- * console.log(Result.unwrapOk(result)); // 5
712
- * ```
713
770
  */
714
771
  export function flatMap<R extends Base, S2, E2>(
715
772
  result: R,
@@ -732,9 +789,9 @@ export namespace Result {
732
789
  case 2: {
733
790
  const [result, flatMapFn] = args;
734
791
  return isErr(result)
735
- ? // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
792
+ ? // eslint-disable-next-line total-functions/no-unsafe-type-assertion
736
793
  (result as Err<UnwrapErr<R>>)
737
- : // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
794
+ : // eslint-disable-next-line total-functions/no-unsafe-type-assertion
738
795
  flatMapFn(result.value as UnwrapOk<R>);
739
796
  }
740
797
 
@@ -747,18 +804,27 @@ export namespace Result {
747
804
  }
748
805
 
749
806
  /**
750
- * Unwraps a `Result`, returning the success value or throwing an error with the provided message.
807
+ * Unwraps a `Result`, returning the success value or throwing an error with
808
+ * the provided message.
809
+ *
810
+ * @example
811
+ *
812
+ * ```ts
813
+ * const okValue = Result.ok('data');
814
+ *
815
+ * assert(Result.expectToBe(okValue, 'should have value') === 'data');
816
+ *
817
+ * const expectResult = Result.expectToBe<string>('missing result');
818
+ *
819
+ * assert.throws(() => expectResult(Result.err('boom')), /missing result/u);
820
+ * assert(expectResult(Result.ok('value')) === 'value');
821
+ * ```
822
+ *
751
823
  * @template R The `Result.Base` type to unwrap.
752
824
  * @param result The `Result` to unwrap.
753
825
  * @param message The error message to throw if the `Result` is `Result.Err`.
754
826
  * @returns The success value if `Result.Ok`.
755
827
  * @throws Error with the provided message if the `Result` is `Result.Err`.
756
- * @example
757
- * ```typescript
758
- * const result = Result.ok(42);
759
- * const value = Result.expectToBe(result, "Operation must succeed");
760
- * console.log(value); // 42
761
- * ```
762
828
  */
763
829
  export function expectToBe<R extends Base>(
764
830
  result: R,
@@ -794,20 +860,32 @@ export namespace Result {
794
860
  }
795
861
 
796
862
  /**
863
+ * @template P The Promise type.
797
864
  * @internal
798
865
  * Utility type to extract the resolved value type from a Promise.
799
- * @template P The Promise type.
800
- * @example
801
- * UnwrapPromise<Promise<string>> // string
802
- * UnwrapPromise<Promise<number>> // number
803
866
  */
804
867
  type UnwrapPromise<P extends Promise<unknown>> =
805
868
  P extends Promise<infer V> ? V : never;
806
869
 
807
870
  /**
808
- * Converts a Promise into a Promise that resolves to a `Result`.
809
- * If the input Promise resolves, the `Result` will be `Ok` with the resolved value.
810
- * If the input Promise rejects, the `Result` will be `Err` with the rejection reason.
871
+ * Converts a Promise into a Promise that resolves to a `Result`. If the input
872
+ * Promise resolves, the `Result` will be `Ok` with the resolved value. If the
873
+ * input Promise rejects, the `Result` will be `Err` with the rejection
874
+ * reason.
875
+ *
876
+ * @example
877
+ *
878
+ * ```ts
879
+ * const successPromise = Result.fromPromise(Promise.resolve('ok'));
880
+ * const failurePromise = Result.fromPromise(Promise.reject(new Error('fail')));
881
+ *
882
+ * const resolved = await successPromise;
883
+ * const rejected = await failurePromise;
884
+ *
885
+ * assert.deepStrictEqual(resolved, Result.ok('ok'));
886
+ * assert.ok(Result.isErr(rejected));
887
+ * ```
888
+ *
811
889
  * @template P The type of the input Promise.
812
890
  * @param promise The Promise to convert.
813
891
  * @returns A Promise that resolves to `Result<UnwrapPromise<P>, unknown>`.
@@ -815,40 +893,41 @@ export namespace Result {
815
893
  export const fromPromise = <P extends Promise<unknown>>(
816
894
  promise: P,
817
895
  ): Promise<Result<UnwrapPromise<P>, unknown>> =>
818
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
896
+ // eslint-disable-next-line total-functions/no-unsafe-type-assertion
819
897
  promise.then((v) => ok(v) as Ok<UnwrapPromise<P>>).catch(err);
820
898
 
821
899
  /**
822
900
  * Wraps a function that may throw an exception in a `Result`.
823
901
  *
824
- * This is a fundamental utility for converting traditional exception-based error
825
- * handling into Result-based error handling. Any thrown value is converted to an
826
- * Error object for consistent error handling.
902
+ * This is a fundamental utility for converting traditional exception-based
903
+ * error handling into Result-based error handling. Any thrown value is
904
+ * converted to an Error object for consistent error handling.
827
905
  *
828
906
  * If the function executes successfully, returns `Result.Ok` with the result.
829
907
  * If the function throws, returns `Result.Err` with the caught error.
830
908
  *
831
- * @template T The return type of the function.
832
- * @param fn The function to execute that may throw.
833
- * @returns A `Result<T, Error>` containing either the successful result or the caught error.
834
909
  * @example
835
- * ```typescript
836
- * // Wrapping JSON.parse which can throw
837
- * const parseJson = <T>(text: string): Result<T, Error> =>
838
- * Result.fromThrowable(() => JSON.parse(text) as T);
839
- *
840
- * const validJson = parseJson<{valid: boolean}>('{"valid": true}');
841
- * if (Result.isOk(validJson)) {
842
- * console.log(validJson.value.valid); // true
843
- * }
844
910
  *
911
+ * ```ts
912
+ * const success = Result.fromThrowable(() => 1 + 1);
913
+ * const failure = Result.fromThrowable(() => {
914
+ * throw new Error('boom');
915
+ * });
916
+ *
917
+ * assert.deepStrictEqual(success, Result.ok(2));
918
+ * assert.ok(Result.isErr(failure));
845
919
  * ```
920
+ *
921
+ * @template T The return type of the function.
922
+ * @param fn The function to execute that may throw.
923
+ * @returns A `Result<T, Error>` containing either the successful result or
924
+ * the caught error.
846
925
  */
847
926
  export const fromThrowable = <T,>(fn: () => T): Result<T, Error> => {
848
927
  try {
849
928
  return ok(fn());
850
929
  } catch (error) {
851
- if (error instanceof Error) {
930
+ if (Error.isError(error)) {
852
931
  return err(error);
853
932
  }
854
933
  const msg = unknownToString(error);
@@ -858,49 +937,51 @@ export namespace Result {
858
937
 
859
938
  /**
860
939
  * Swaps the success and error values of a `Result`.
940
+ *
941
+ * @example
942
+ *
943
+ * ```ts
944
+ * const okValue = Result.ok('value');
945
+ * const errValue = Result.err('error');
946
+ *
947
+ * assert.deepStrictEqual(Result.swap(okValue), Result.err('value'));
948
+ * assert.deepStrictEqual(Result.swap(errValue), Result.ok('error'));
949
+ * ```
950
+ *
861
951
  * @template R The input `Result.Base` type.
862
952
  * @param result The `Result` to swap.
863
953
  * @returns A new `Result` with success and error swapped.
864
- * @example
865
- * ```typescript
866
- * const okResult = Result.ok(42);
867
- * const swapped = Result.swap(okResult);
868
- * console.log(Result.isErr(swapped)); // true
869
- * console.log(Result.unwrapErr(swapped)); // 42
870
- * ```
871
954
  */
872
955
  export const swap = <R extends Base>(
873
956
  result: R,
874
957
  ): Result<UnwrapErr<R>, UnwrapOk<R>> =>
875
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
958
+ // eslint-disable-next-line total-functions/no-unsafe-type-assertion
876
959
  isOk(result) ? err(unwrapOk(result)) : ok(result.value as UnwrapErr<R>);
877
960
 
878
961
  /**
879
962
  * Converts a `Result` to an `Optional`.
880
963
  *
881
- * This conversion is useful when you want to discard error information and only
882
- * care about whether an operation succeeded. The error information is lost in
883
- * this conversion, so use it when error details are not needed.
964
+ * This conversion is useful when you want to discard error information and
965
+ * only care about whether an operation succeeded. The error information is
966
+ * lost in this conversion, so use it when error details are not needed.
884
967
  *
885
- * If the `Result` is `Ok`, returns `Some` with the value.
886
- * If the `Result` is `Err`, returns `None`.
968
+ * If the `Result` is `Ok`, returns `Some` with the value. If the `Result` is
969
+ * `Err`, returns `None`.
887
970
  *
888
- * @template R The input `Result.Base` type.
889
- * @param result The `Result` to convert.
890
- * @returns An `Optional<UnwrapOk<R>>` containing the success value or representing `None`.
891
971
  * @example
892
- * ```typescript
893
- * // Basic conversion
894
- * const okResult = Result.ok(42);
895
- * const optional = Result.toOptional(okResult);
896
- * console.log(Optional.isSome(optional)); // true
897
- * console.log(Optional.unwrap(optional)); // 42
898
972
  *
899
- * const errResult = Result.err("Network error");
900
- * const none = Result.toOptional(errResult);
901
- * console.log(Optional.isNone(none)); // true
973
+ * ```ts
974
+ * const okValue = Result.ok(7);
975
+ * const errValue = Result.err('fail');
902
976
  *
977
+ * assert.deepStrictEqual(Result.toOptional(okValue), Optional.some(7));
978
+ * assert.deepStrictEqual(Result.toOptional(errValue), Optional.none);
903
979
  * ```
980
+ *
981
+ * @template R The input `Result.Base` type.
982
+ * @param result The `Result` to convert.
983
+ * @returns An `Optional<UnwrapOk<R>>` containing the success value or
984
+ * representing `None`.
904
985
  */
905
986
  export const toOptional = <R extends Base>(
906
987
  result: R,
@@ -909,17 +990,31 @@ export namespace Result {
909
990
 
910
991
  /**
911
992
  * Returns the `Result` if it is `Ok`, otherwise returns the alternative.
993
+ *
994
+ * @example
995
+ *
996
+ * ```ts
997
+ * const primary = Result.ok('primary');
998
+ * const fallback = Result.ok('fallback');
999
+ * const failure = Result.err('failure');
1000
+ *
1001
+ * assert.deepStrictEqual(Result.orElse(primary, fallback), primary);
1002
+ * assert.deepStrictEqual(Result.orElse(failure, fallback), fallback);
1003
+ *
1004
+ * const orElseFallback = Result.orElse(Result.ok('default'));
1005
+ *
1006
+ * assert.deepStrictEqual(
1007
+ * orElseFallback(Result.err('missing')),
1008
+ * Result.ok('default'),
1009
+ * );
1010
+ * assert.deepStrictEqual(orElseFallback(Result.ok('value')), Result.ok('value'));
1011
+ * ```
1012
+ *
912
1013
  * @template R The input `Result.Base` type.
913
1014
  * @param result The `Result` to check.
914
- * @param alternative The alternative `Result` to return if the first is `Err`.
1015
+ * @param alternative The alternative `Result` to return if the first is
1016
+ * `Err`.
915
1017
  * @returns The first `Result` if `Ok`, otherwise the alternative.
916
- * @example
917
- * ```typescript
918
- * const primary = Result.err("error");
919
- * const fallback = Result.ok("default");
920
- * const result = Result.orElse(primary, fallback);
921
- * console.log(Result.unwrapOk(result)); // "default"
922
- * ```
923
1018
  */
924
1019
  export function orElse<R extends Base, R2 extends Base>(
925
1020
  result: R,
@@ -954,8 +1049,24 @@ export namespace Result {
954
1049
  }
955
1050
 
956
1051
  /**
957
- * Combines two `Result` values into a single `Result` containing a tuple.
958
- * If either `Result` is `Err`, returns the first `Err` encountered.
1052
+ * Combines two `Result` values into a single `Result` containing a tuple. If
1053
+ * either `Result` is `Err`, returns the first `Err` encountered.
1054
+ *
1055
+ * @example
1056
+ *
1057
+ * ```ts
1058
+ * const first = Result.ok('left');
1059
+ * const second = Result.ok(1);
1060
+ *
1061
+ * const expected: readonly [string, number] = ['left', 1];
1062
+ *
1063
+ * assert.deepStrictEqual(Result.zip(first, second), Result.ok(expected));
1064
+ * assert.deepStrictEqual(
1065
+ * Result.zip(first, Result.err('error')),
1066
+ * Result.err('error'),
1067
+ * );
1068
+ * ```
1069
+ *
959
1070
  * @template S1 The success type of the first `Result`.
960
1071
  * @template E1 The error type of the first `Result`.
961
1072
  * @template S2 The success type of the second `Result`.
@@ -963,16 +1074,6 @@ export namespace Result {
963
1074
  * @param resultA The first `Result`.
964
1075
  * @param resultB The second `Result`.
965
1076
  * @returns A `Result` containing a tuple of both values, or the first `Err`.
966
- * @example
967
- * ```typescript
968
- * const a = Result.ok(1);
969
- * const b = Result.ok("hello");
970
- * const zipped = Result.zip(a, b);
971
- * console.log(Result.unwrapOk(zipped)); // [1, "hello"]
972
- *
973
- * const withErr = Result.zip(a, Result.err("error"));
974
- * console.log(Result.unwrapErr(withErr)); // "error"
975
- * ```
976
1077
  */
977
1078
  export const zip = <S1, E1, S2, E2>(
978
1079
  resultA: Result<S1, E1>,