ts-data-forge 5.0.1 → 5.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +101 -50
- package/dist/array/impl/array-utils-creation.d.mts +14 -0
- package/dist/array/impl/array-utils-creation.d.mts.map +1 -1
- package/dist/array/impl/array-utils-creation.mjs +12 -0
- package/dist/array/impl/array-utils-creation.mjs.map +1 -1
- package/dist/array/impl/array-utils-element-access.d.mts +10 -0
- package/dist/array/impl/array-utils-element-access.d.mts.map +1 -1
- package/dist/array/impl/array-utils-element-access.mjs +6 -0
- package/dist/array/impl/array-utils-element-access.mjs.map +1 -1
- package/dist/array/impl/array-utils-modification.d.mts +14 -0
- package/dist/array/impl/array-utils-modification.d.mts.map +1 -1
- package/dist/array/impl/array-utils-modification.mjs.map +1 -1
- package/dist/array/impl/array-utils-reducing-value.d.mts +34 -10
- package/dist/array/impl/array-utils-reducing-value.d.mts.map +1 -1
- package/dist/array/impl/array-utils-reducing-value.mjs.map +1 -1
- package/dist/array/impl/array-utils-search.d.mts +34 -4
- package/dist/array/impl/array-utils-search.d.mts.map +1 -1
- package/dist/array/impl/array-utils-search.mjs.map +1 -1
- package/dist/array/impl/array-utils-set-op.d.mts +20 -7
- package/dist/array/impl/array-utils-set-op.d.mts.map +1 -1
- package/dist/array/impl/array-utils-set-op.mjs +20 -7
- package/dist/array/impl/array-utils-set-op.mjs.map +1 -1
- package/dist/array/impl/array-utils-size.d.mts +5 -2
- package/dist/array/impl/array-utils-size.d.mts.map +1 -1
- package/dist/array/impl/array-utils-size.mjs +5 -2
- package/dist/array/impl/array-utils-size.mjs.map +1 -1
- package/dist/array/impl/array-utils-slice-clamped.d.mts +2 -0
- package/dist/array/impl/array-utils-slice-clamped.d.mts.map +1 -1
- package/dist/array/impl/array-utils-slice-clamped.mjs.map +1 -1
- package/dist/array/impl/array-utils-slicing.d.mts +17 -2
- package/dist/array/impl/array-utils-slicing.d.mts.map +1 -1
- package/dist/array/impl/array-utils-slicing.mjs +9 -2
- package/dist/array/impl/array-utils-slicing.mjs.map +1 -1
- package/dist/array/impl/array-utils-transformation.d.mts +27 -0
- package/dist/array/impl/array-utils-transformation.d.mts.map +1 -1
- package/dist/array/impl/array-utils-transformation.mjs +6 -0
- package/dist/array/impl/array-utils-transformation.mjs.map +1 -1
- package/dist/array/impl/array-utils-validation.d.mts +35 -18
- package/dist/array/impl/array-utils-validation.d.mts.map +1 -1
- package/dist/array/impl/array-utils-validation.mjs +25 -14
- package/dist/array/impl/array-utils-validation.mjs.map +1 -1
- package/dist/collections/imap-mapped.mjs.map +1 -1
- package/dist/collections/imap.d.mts +35 -15
- package/dist/collections/imap.d.mts.map +1 -1
- package/dist/collections/imap.mjs +5 -3
- package/dist/collections/imap.mjs.map +1 -1
- package/dist/collections/iset-mapped.d.mts +46 -19
- package/dist/collections/iset-mapped.d.mts.map +1 -1
- package/dist/collections/iset-mapped.mjs +9 -2
- package/dist/collections/iset-mapped.mjs.map +1 -1
- package/dist/collections/iset.d.mts +48 -19
- package/dist/collections/iset.d.mts.map +1 -1
- package/dist/collections/iset.mjs +9 -2
- package/dist/collections/iset.mjs.map +1 -1
- package/dist/collections/queue.d.mts +65 -25
- package/dist/collections/queue.d.mts.map +1 -1
- package/dist/collections/queue.mjs +13 -5
- package/dist/collections/queue.mjs.map +1 -1
- package/dist/collections/stack.d.mts +60 -25
- package/dist/collections/stack.d.mts.map +1 -1
- package/dist/collections/stack.mjs +12 -5
- package/dist/collections/stack.mjs.map +1 -1
- package/dist/entry-point.mjs +1 -1
- package/dist/functional/match.d.mts +1 -1
- package/dist/functional/match.d.mts.map +1 -1
- package/dist/functional/match.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-expect-to-be.d.mts +3 -2
- package/dist/functional/optional/impl/optional-expect-to-be.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-expect-to-be.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-filter.d.mts +3 -0
- package/dist/functional/optional/impl/optional-filter.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-filter.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-flat-map.d.mts +2 -0
- package/dist/functional/optional/impl/optional-flat-map.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-flat-map.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-from-nullable.d.mts +2 -0
- package/dist/functional/optional/impl/optional-from-nullable.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-from-nullable.mjs +2 -0
- package/dist/functional/optional/impl/optional-from-nullable.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-is-none.d.mts +1 -1
- package/dist/functional/optional/impl/optional-is-none.mjs +1 -1
- package/dist/functional/optional/impl/optional-is-optional.d.mts +4 -2
- package/dist/functional/optional/impl/optional-is-optional.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-is-optional.mjs +4 -2
- package/dist/functional/optional/impl/optional-is-optional.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-is-some.d.mts +2 -1
- package/dist/functional/optional/impl/optional-is-some.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-is-some.mjs +2 -1
- package/dist/functional/optional/impl/optional-is-some.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-map.d.mts +2 -0
- package/dist/functional/optional/impl/optional-map.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-map.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-none.d.mts +4 -2
- package/dist/functional/optional/impl/optional-none.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-none.mjs +4 -2
- package/dist/functional/optional/impl/optional-none.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-or-else.d.mts +4 -0
- package/dist/functional/optional/impl/optional-or-else.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-or-else.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-some.d.mts +5 -3
- package/dist/functional/optional/impl/optional-some.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-some.mjs +4 -2
- package/dist/functional/optional/impl/optional-some.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-to-nullable.d.mts +4 -2
- package/dist/functional/optional/impl/optional-to-nullable.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-to-nullable.mjs +4 -2
- package/dist/functional/optional/impl/optional-to-nullable.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-unwrap-or.d.mts +7 -4
- package/dist/functional/optional/impl/optional-unwrap-or.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-unwrap-or.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-unwrap-throw.d.mts +2 -1
- package/dist/functional/optional/impl/optional-unwrap-throw.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-unwrap-throw.mjs +2 -1
- package/dist/functional/optional/impl/optional-unwrap-throw.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-unwrap.d.mts +4 -2
- package/dist/functional/optional/impl/optional-unwrap.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-unwrap.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-zip.d.mts +3 -1
- package/dist/functional/optional/impl/optional-zip.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-zip.mjs +3 -1
- package/dist/functional/optional/impl/optional-zip.mjs.map +1 -1
- package/dist/functional/pipe.d.mts +2 -2
- package/dist/functional/result/impl/result-err.d.mts +4 -2
- package/dist/functional/result/impl/result-err.d.mts.map +1 -1
- package/dist/functional/result/impl/result-err.mjs +3 -1
- package/dist/functional/result/impl/result-err.mjs.map +1 -1
- package/dist/functional/result/impl/result-expect-to-be.d.mts +3 -2
- package/dist/functional/result/impl/result-expect-to-be.d.mts.map +1 -1
- package/dist/functional/result/impl/result-expect-to-be.mjs.map +1 -1
- package/dist/functional/result/impl/result-flat-map.d.mts +5 -0
- package/dist/functional/result/impl/result-flat-map.d.mts.map +1 -1
- package/dist/functional/result/impl/result-flat-map.mjs.map +1 -1
- package/dist/functional/result/impl/result-fold.d.mts +4 -0
- package/dist/functional/result/impl/result-fold.d.mts.map +1 -1
- package/dist/functional/result/impl/result-fold.mjs.map +1 -1
- package/dist/functional/result/impl/result-from-promise.d.mts +4 -1
- package/dist/functional/result/impl/result-from-promise.d.mts.map +1 -1
- package/dist/functional/result/impl/result-from-promise.mjs +4 -1
- package/dist/functional/result/impl/result-from-promise.mjs.map +1 -1
- package/dist/functional/result/impl/result-from-throwable.d.mts +3 -1
- package/dist/functional/result/impl/result-from-throwable.d.mts.map +1 -1
- package/dist/functional/result/impl/result-from-throwable.mjs +3 -1
- package/dist/functional/result/impl/result-from-throwable.mjs.map +1 -1
- package/dist/functional/result/impl/result-is-err.d.mts +4 -2
- package/dist/functional/result/impl/result-is-err.d.mts.map +1 -1
- package/dist/functional/result/impl/result-is-err.mjs +4 -2
- package/dist/functional/result/impl/result-is-err.mjs.map +1 -1
- package/dist/functional/result/impl/result-is-ok.d.mts +4 -2
- package/dist/functional/result/impl/result-is-ok.d.mts.map +1 -1
- package/dist/functional/result/impl/result-is-ok.mjs +4 -2
- package/dist/functional/result/impl/result-is-ok.mjs.map +1 -1
- package/dist/functional/result/impl/result-is-result.d.mts +7 -3
- package/dist/functional/result/impl/result-is-result.d.mts.map +1 -1
- package/dist/functional/result/impl/result-is-result.mjs +7 -3
- package/dist/functional/result/impl/result-is-result.mjs.map +1 -1
- package/dist/functional/result/impl/result-map-err.d.mts +3 -0
- package/dist/functional/result/impl/result-map-err.d.mts.map +1 -1
- package/dist/functional/result/impl/result-map-err.mjs.map +1 -1
- package/dist/functional/result/impl/result-map.d.mts +4 -0
- package/dist/functional/result/impl/result-map.d.mts.map +1 -1
- package/dist/functional/result/impl/result-map.mjs.map +1 -1
- package/dist/functional/result/impl/result-ok.d.mts +4 -2
- package/dist/functional/result/impl/result-ok.d.mts.map +1 -1
- package/dist/functional/result/impl/result-ok.mjs +3 -1
- package/dist/functional/result/impl/result-ok.mjs.map +1 -1
- package/dist/functional/result/impl/result-or-else.d.mts +4 -0
- package/dist/functional/result/impl/result-or-else.d.mts.map +1 -1
- package/dist/functional/result/impl/result-or-else.mjs.map +1 -1
- package/dist/functional/result/impl/result-swap.d.mts +2 -0
- package/dist/functional/result/impl/result-swap.d.mts.map +1 -1
- package/dist/functional/result/impl/result-swap.mjs +2 -0
- package/dist/functional/result/impl/result-swap.mjs.map +1 -1
- package/dist/functional/result/impl/result-to-optional.d.mts +2 -0
- package/dist/functional/result/impl/result-to-optional.d.mts.map +1 -1
- package/dist/functional/result/impl/result-to-optional.mjs +2 -0
- package/dist/functional/result/impl/result-to-optional.mjs.map +1 -1
- package/dist/functional/result/impl/result-unwrap-err-or.d.mts +7 -4
- package/dist/functional/result/impl/result-unwrap-err-or.d.mts.map +1 -1
- package/dist/functional/result/impl/result-unwrap-err-or.mjs.map +1 -1
- package/dist/functional/result/impl/result-unwrap-err-throw.d.mts +3 -1
- package/dist/functional/result/impl/result-unwrap-err-throw.d.mts.map +1 -1
- package/dist/functional/result/impl/result-unwrap-err-throw.mjs +3 -1
- package/dist/functional/result/impl/result-unwrap-err-throw.mjs.map +1 -1
- package/dist/functional/result/impl/result-unwrap-err.d.mts +3 -2
- package/dist/functional/result/impl/result-unwrap-err.d.mts.map +1 -1
- package/dist/functional/result/impl/result-unwrap-err.mjs +3 -2
- package/dist/functional/result/impl/result-unwrap-err.mjs.map +1 -1
- package/dist/functional/result/impl/result-unwrap-ok-or.d.mts +7 -4
- package/dist/functional/result/impl/result-unwrap-ok-or.d.mts.map +1 -1
- package/dist/functional/result/impl/result-unwrap-ok-or.mjs.map +1 -1
- package/dist/functional/result/impl/result-unwrap-ok.d.mts +3 -2
- package/dist/functional/result/impl/result-unwrap-ok.d.mts.map +1 -1
- package/dist/functional/result/impl/result-unwrap-ok.mjs.map +1 -1
- package/dist/functional/result/impl/result-unwrap-throw.d.mts +3 -1
- package/dist/functional/result/impl/result-unwrap-throw.d.mts.map +1 -1
- package/dist/functional/result/impl/result-unwrap-throw.mjs +3 -1
- package/dist/functional/result/impl/result-unwrap-throw.mjs.map +1 -1
- package/dist/functional/result/impl/result-zip.d.mts +3 -1
- package/dist/functional/result/impl/result-zip.d.mts.map +1 -1
- package/dist/functional/result/impl/result-zip.mjs +3 -1
- package/dist/functional/result/impl/result-zip.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-err.d.mts +3 -2
- package/dist/functional/ternary-result/impl/ternary-result-err.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-err.mjs +2 -1
- package/dist/functional/ternary-result/impl/ternary-result-err.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-expect-to-be.d.mts +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-expect-to-be.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-expect-to-be.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-flat-map.d.mts +4 -0
- package/dist/functional/ternary-result/impl/ternary-result-flat-map.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-flat-map.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-fold.d.mts +2 -0
- package/dist/functional/ternary-result/impl/ternary-result-fold.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-fold.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-promise.d.mts +3 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-promise.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-promise.mjs +3 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-promise.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-throwable.d.mts +3 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-throwable.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-throwable.mjs +3 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-throwable.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-ok.d.mts +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-is-ok.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-ok.mjs +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-is-ok.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.d.mts +7 -3
- package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.mjs +7 -3
- package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-warn.d.mts +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-is-warn.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-warn.mjs +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-is-warn.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-map-err.d.mts +3 -0
- package/dist/functional/ternary-result/impl/ternary-result-map-err.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-map-err.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-map-warn.d.mts +3 -0
- package/dist/functional/ternary-result/impl/ternary-result-map-warn.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-map-warn.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-map.d.mts +6 -0
- package/dist/functional/ternary-result/impl/ternary-result-map.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-map.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-ok.d.mts +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-ok.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-ok.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-or-else.d.mts +2 -0
- package/dist/functional/ternary-result/impl/ternary-result-or-else.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-or-else.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-to-optional.d.mts +2 -0
- package/dist/functional/ternary-result/impl/ternary-result-to-optional.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-to-optional.mjs +2 -0
- package/dist/functional/ternary-result/impl/ternary-result-to-optional.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-or.d.mts +2 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-or.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-or.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-throw.d.mts +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-throw.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-throw.mjs +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-throw.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err.d.mts +2 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err.mjs +2 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok-or.d.mts +2 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok-or.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok-or.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok.d.mts +2 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.d.mts +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.mjs +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-or.d.mts +2 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-or.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-or.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.d.mts +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.mjs +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn.d.mts +2 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn.mjs +2 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-warn.d.mts +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-warn.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-warn.mjs.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-zip.d.mts +4 -0
- package/dist/functional/ternary-result/impl/ternary-result-zip.d.mts.map +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-zip.mjs +4 -0
- package/dist/functional/ternary-result/impl/ternary-result-zip.mjs.map +1 -1
- package/dist/globals.d.mts +12 -1
- package/dist/guard/has-key.d.mts +6 -3
- package/dist/guard/has-key.d.mts.map +1 -1
- package/dist/guard/has-key.mjs +2 -2
- package/dist/guard/index.mjs +1 -1
- package/dist/guard/is-record.d.mts +31 -0
- package/dist/guard/is-record.d.mts.map +1 -1
- package/dist/guard/is-record.mjs +32 -1
- package/dist/guard/is-record.mjs.map +1 -1
- package/dist/guard/is-type.d.mts +3 -0
- package/dist/guard/is-type.d.mts.map +1 -1
- package/dist/guard/is-type.mjs +3 -0
- package/dist/guard/is-type.mjs.map +1 -1
- package/dist/guard/key-is-in.d.mts +3 -2
- package/dist/guard/key-is-in.d.mts.map +1 -1
- package/dist/guard/key-is-in.mjs +3 -2
- package/dist/guard/key-is-in.mjs.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/iterator/range.d.mts +4 -0
- package/dist/iterator/range.d.mts.map +1 -1
- package/dist/iterator/range.mjs +1 -0
- package/dist/iterator/range.mjs.map +1 -1
- package/dist/json/json.d.mts +42 -21
- package/dist/json/json.d.mts.map +1 -1
- package/dist/json/json.mjs +42 -21
- package/dist/json/json.mjs.map +1 -1
- package/dist/number/branded-types/int.d.mts +33 -19
- package/dist/number/branded-types/int.d.mts.map +1 -1
- package/dist/number/branded-types/int.mjs +33 -19
- package/dist/number/branded-types/int.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-safe-int.mjs +2 -2
- package/dist/number/branded-types/positive-int.d.mts +34 -20
- package/dist/number/branded-types/positive-int.d.mts.map +1 -1
- package/dist/number/branded-types/positive-int.mjs +34 -20
- package/dist/number/branded-types/positive-int.mjs.map +1 -1
- package/dist/number/branded-types/positive-safe-int.d.mts +47 -26
- package/dist/number/branded-types/positive-safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/positive-safe-int.mjs +48 -27
- package/dist/number/branded-types/positive-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/safe-int.d.mts +48 -27
- package/dist/number/branded-types/safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/safe-int.mjs +50 -29
- package/dist/number/branded-types/safe-int.mjs.map +1 -1
- package/dist/number/branded-types/safe-uint.mjs +1 -1
- package/dist/number/branded-types/uint.d.mts +31 -19
- package/dist/number/branded-types/uint.d.mts.map +1 -1
- package/dist/number/branded-types/uint.mjs +31 -19
- package/dist/number/branded-types/uint.mjs.map +1 -1
- package/dist/number/num.d.mts +29 -20
- package/dist/number/num.d.mts.map +1 -1
- package/dist/number/num.mjs +24 -17
- package/dist/number/num.mjs.map +1 -1
- package/dist/number/refined-number-utils.d.mts +33 -11
- package/dist/number/refined-number-utils.d.mts.map +1 -1
- package/dist/number/refined-number-utils.mjs +33 -11
- package/dist/number/refined-number-utils.mjs.map +1 -1
- package/dist/object/object.d.mts +12 -3
- package/dist/object/object.d.mts.map +1 -1
- package/dist/object/object.mjs +10 -3
- package/dist/object/object.mjs.map +1 -1
- package/dist/others/map-nullable.mjs.map +1 -1
- package/dist/others/tuple.d.mts +14 -3
- package/dist/others/tuple.d.mts.map +1 -1
- package/dist/others/tuple.mjs +14 -3
- package/dist/others/tuple.mjs.map +1 -1
- package/dist/others/unknown-to-string.d.mts.map +1 -1
- package/dist/others/unknown-to-string.mjs.map +1 -1
- package/dist/promise/promise.d.mts +8 -4
- package/dist/promise/promise.d.mts.map +1 -1
- package/dist/promise/promise.mjs +8 -4
- package/dist/promise/promise.mjs.map +1 -1
- package/package.json +26 -28
- package/src/array/impl/array-utils-creation.mts +20 -0
- package/src/array/impl/array-utils-creation.test.mts +72 -1
- package/src/array/impl/array-utils-element-access.mts +13 -0
- package/src/array/impl/array-utils-element-access.test.mts +36 -15
- package/src/array/impl/array-utils-iterators.test.mts +17 -0
- package/src/array/impl/array-utils-modification.mts +78 -19
- package/src/array/impl/array-utils-modification.test.mts +75 -0
- package/src/array/impl/array-utils-overload-type-error.test.mts +19 -0
- package/src/array/impl/array-utils-reducing-value.mts +61 -16
- package/src/array/impl/array-utils-reducing-value.test.mts +107 -25
- package/src/array/impl/array-utils-search.mts +57 -4
- package/src/array/impl/array-utils-search.test.mts +61 -10
- package/src/array/impl/array-utils-set-op.mts +26 -7
- package/src/array/impl/array-utils-set-op.test.mts +129 -13
- package/src/array/impl/array-utils-size.mts +5 -2
- package/src/array/impl/array-utils-size.test.mts +1 -0
- package/src/array/impl/array-utils-slice-clamped.mts +7 -0
- package/src/array/impl/array-utils-slice-clamped.test.mts +20 -0
- package/src/array/impl/array-utils-slicing.mts +29 -2
- package/src/array/impl/array-utils-slicing.test.mts +57 -0
- package/src/array/impl/array-utils-transformation.mts +58 -0
- package/src/array/impl/array-utils-transformation.test.mts +115 -3
- package/src/array/impl/array-utils-validation.mts +41 -18
- package/src/array/impl/array-utils-validation.test.mts +176 -78
- package/src/array/impl/array.test.mts +18 -6
- package/src/collections/imap-mapped.mts +11 -0
- package/src/collections/imap-mapped.test.mts +86 -18
- package/src/collections/imap.mts +47 -15
- package/src/collections/imap.test.mts +137 -31
- package/src/collections/iset-mapped.mts +54 -19
- package/src/collections/iset-mapped.test.mts +163 -52
- package/src/collections/iset.mts +53 -19
- package/src/collections/iset.test.mts +323 -130
- package/src/collections/queue.mts +78 -25
- package/src/collections/queue.test.mts +65 -35
- package/src/collections/stack.mts +67 -25
- package/src/collections/stack.test.mts +59 -15
- package/src/functional/match.mts +17 -1
- package/src/functional/match.test.mts +2 -0
- package/src/functional/optional/impl/optional-expect-to-be.mts +5 -2
- package/src/functional/optional/impl/optional-filter.mts +6 -0
- package/src/functional/optional/impl/optional-flat-map.mts +4 -0
- package/src/functional/optional/impl/optional-from-nullable.mts +2 -0
- package/src/functional/optional/impl/optional-is-none.mts +1 -1
- package/src/functional/optional/impl/optional-is-optional.mts +4 -2
- package/src/functional/optional/impl/optional-is-some.mts +2 -1
- package/src/functional/optional/impl/optional-map.mts +5 -0
- package/src/functional/optional/impl/optional-none.mts +4 -2
- package/src/functional/optional/impl/optional-or-else.mts +6 -0
- package/src/functional/optional/impl/optional-some.mts +5 -3
- package/src/functional/optional/impl/optional-to-nullable.mts +4 -2
- package/src/functional/optional/impl/optional-unwrap-or.mts +9 -4
- package/src/functional/optional/impl/optional-unwrap-throw.mts +2 -1
- package/src/functional/optional/impl/optional-unwrap.mts +4 -2
- package/src/functional/optional/impl/optional-zip.mts +3 -1
- package/src/functional/optional.test.mts +126 -54
- package/src/functional/pipe.mts +2 -2
- package/src/functional/pipe.test.mts +7 -4
- package/src/functional/result/impl/result-err.mts +4 -2
- package/src/functional/result/impl/result-expect-to-be.mts +5 -2
- package/src/functional/result/impl/result-flat-map.mts +7 -0
- package/src/functional/result/impl/result-fold.mts +6 -0
- package/src/functional/result/impl/result-from-promise.mts +4 -1
- package/src/functional/result/impl/result-from-throwable.mts +5 -1
- package/src/functional/result/impl/result-is-err.mts +4 -2
- package/src/functional/result/impl/result-is-ok.mts +4 -2
- package/src/functional/result/impl/result-is-result.mts +7 -3
- package/src/functional/result/impl/result-map-err.mts +5 -0
- package/src/functional/result/impl/result-map.mts +6 -0
- package/src/functional/result/impl/result-ok.mts +4 -2
- package/src/functional/result/impl/result-or-else.mts +6 -0
- package/src/functional/result/impl/result-swap.mts +2 -0
- package/src/functional/result/impl/result-to-optional.mts +2 -0
- package/src/functional/result/impl/result-unwrap-err-or.mts +9 -4
- package/src/functional/result/impl/result-unwrap-err-throw.mts +3 -1
- package/src/functional/result/impl/result-unwrap-err.mts +3 -2
- package/src/functional/result/impl/result-unwrap-ok-or.mts +9 -4
- package/src/functional/result/impl/result-unwrap-ok.mts +4 -2
- package/src/functional/result/impl/result-unwrap-throw.mts +3 -1
- package/src/functional/result/impl/result-zip.mts +3 -1
- package/src/functional/result.test.mts +182 -152
- package/src/functional/ternary-result/impl/ternary-result-err.mts +3 -2
- package/src/functional/ternary-result/impl/ternary-result-expect-to-be.mts +4 -0
- package/src/functional/ternary-result/impl/ternary-result-flat-map.mts +7 -0
- package/src/functional/ternary-result/impl/ternary-result-fold.mts +5 -0
- package/src/functional/ternary-result/impl/ternary-result-from-promise.mts +3 -1
- package/src/functional/ternary-result/impl/ternary-result-from-throwable.mts +4 -1
- package/src/functional/ternary-result/impl/ternary-result-is-ok.mts +1 -0
- package/src/functional/ternary-result/impl/ternary-result-is-ternary-result.mts +7 -3
- package/src/functional/ternary-result/impl/ternary-result-is-warn.mts +1 -0
- package/src/functional/ternary-result/impl/ternary-result-map-err.mts +6 -0
- package/src/functional/ternary-result/impl/ternary-result-map-warn.mts +6 -0
- package/src/functional/ternary-result/impl/ternary-result-map.mts +9 -0
- package/src/functional/ternary-result/impl/ternary-result-ok.mts +1 -1
- package/src/functional/ternary-result/impl/ternary-result-or-else.mts +5 -0
- package/src/functional/ternary-result/impl/ternary-result-to-optional.mts +2 -0
- package/src/functional/ternary-result/impl/ternary-result-unwrap-err-or.mts +5 -0
- package/src/functional/ternary-result/impl/ternary-result-unwrap-err-throw.mts +1 -0
- package/src/functional/ternary-result/impl/ternary-result-unwrap-err.mts +2 -0
- package/src/functional/ternary-result/impl/ternary-result-unwrap-ok-or.mts +5 -0
- package/src/functional/ternary-result/impl/ternary-result-unwrap-ok.mts +3 -0
- package/src/functional/ternary-result/impl/ternary-result-unwrap-throw.mts +1 -0
- package/src/functional/ternary-result/impl/ternary-result-unwrap-warn-or.mts +5 -0
- package/src/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.mts +1 -0
- package/src/functional/ternary-result/impl/ternary-result-unwrap-warn.mts +2 -0
- package/src/functional/ternary-result/impl/ternary-result-warn.mts +4 -1
- package/src/functional/ternary-result/impl/ternary-result-zip.mts +4 -0
- package/src/functional/ternary-result.test.mts +99 -8
- package/src/globals.d.mts +12 -1
- package/src/guard/has-key.mts +8 -5
- package/src/guard/has-key.test.mts +9 -5
- package/src/guard/is-error.test.mts +143 -0
- package/src/guard/is-non-empty-string.test.mts +44 -24
- package/src/guard/is-non-null-object.test.mts +45 -26
- package/src/guard/is-primitive.test.mts +51 -28
- package/src/guard/is-record.mts +34 -0
- package/src/guard/is-record.test.mts +28 -7
- package/src/guard/is-type.mts +3 -0
- package/src/guard/is-type.test.mts +285 -155
- package/src/guard/key-is-in.mts +3 -2
- package/src/guard/key-is-in.test.mts +2 -2
- package/src/iterator/range.mts +9 -0
- package/src/json/json.mts +50 -21
- package/src/json/json.test.mts +224 -176
- package/src/number/branded-types/finite-number.test.mts +51 -11
- package/src/number/branded-types/int.mts +33 -19
- package/src/number/branded-types/int.test.mts +56 -14
- package/src/number/branded-types/int16.test.mts +63 -16
- package/src/number/branded-types/int32.test.mts +63 -16
- package/src/number/branded-types/non-negative-finite-number.test.mts +52 -12
- package/src/number/branded-types/non-negative-int16.test.mts +59 -16
- package/src/number/branded-types/non-negative-int32.test.mts +59 -16
- package/src/number/branded-types/non-zero-finite-number.test.mts +65 -14
- package/src/number/branded-types/non-zero-int.test.mts +72 -19
- package/src/number/branded-types/non-zero-int16.test.mts +60 -16
- package/src/number/branded-types/non-zero-int32.test.mts +60 -16
- package/src/number/branded-types/non-zero-safe-int.mts +2 -2
- package/src/number/branded-types/non-zero-safe-int.test.mts +85 -23
- package/src/number/branded-types/non-zero-uint16.test.mts +53 -16
- package/src/number/branded-types/non-zero-uint32.test.mts +53 -16
- package/src/number/branded-types/positive-finite-number.test.mts +59 -14
- package/src/number/branded-types/positive-int.mts +34 -20
- package/src/number/branded-types/positive-int.test.mts +64 -17
- package/src/number/branded-types/positive-int16.test.mts +50 -15
- package/src/number/branded-types/positive-int32.test.mts +50 -15
- package/src/number/branded-types/positive-safe-int.mts +48 -27
- package/src/number/branded-types/positive-safe-int.test.mts +68 -19
- package/src/number/branded-types/positive-uint16.test.mts +53 -16
- package/src/number/branded-types/positive-uint32.test.mts +53 -16
- package/src/number/branded-types/safe-int.mts +50 -29
- package/src/number/branded-types/safe-int.test.mts +70 -18
- package/src/number/branded-types/safe-uint.mts +1 -1
- package/src/number/branded-types/safe-uint.test.mts +65 -17
- package/src/number/branded-types/uint.mts +31 -19
- package/src/number/branded-types/uint.test.mts +58 -15
- package/src/number/branded-types/uint16.test.mts +56 -15
- package/src/number/branded-types/uint32.test.mts +56 -15
- package/src/number/enum/int8.test.mts +7 -1
- package/src/number/enum/uint8.test.mts +7 -1
- package/src/number/num.mts +32 -21
- package/src/number/num.test.mts +88 -26
- package/src/number/refined-number-utils.mts +38 -13
- package/src/object/object.mts +19 -3
- package/src/object/object.test.mts +16 -6
- package/src/others/cast-mutable.test.mts +8 -1
- package/src/others/cast-readonly.test.mts +21 -4
- package/src/others/if-then.test.mts +40 -24
- package/src/others/map-nullable.mts +3 -0
- package/src/others/map-nullable.test.mts +27 -0
- package/src/others/memoize-function.test.mts +41 -0
- package/src/others/tuple.mts +14 -3
- package/src/others/unknown-to-string.mts +2 -0
- package/src/others/unknown-to-string.test.mts +10 -0
- package/src/promise/promise.mts +8 -4
- package/src/promise/promise.test.mts +8 -13
|
@@ -8,8 +8,10 @@ describe('Result test', () => {
|
|
|
8
8
|
test('creates Ok result', () => {
|
|
9
9
|
const result = Result.ok(42);
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
assert.isTrue(Result.isOk(result));
|
|
12
|
+
|
|
13
|
+
assert.isFalse(Result.isErr(result));
|
|
14
|
+
|
|
13
15
|
expect(result.value).toBe(42);
|
|
14
16
|
|
|
15
17
|
expectType<typeof result, Result<number, never>>('<=');
|
|
@@ -18,7 +20,8 @@ describe('Result test', () => {
|
|
|
18
20
|
test('creates Ok result with string', () => {
|
|
19
21
|
const result = Result.ok('success');
|
|
20
22
|
|
|
21
|
-
|
|
23
|
+
assert.isTrue(Result.isOk(result));
|
|
24
|
+
|
|
22
25
|
expect(result.value).toBe('success');
|
|
23
26
|
|
|
24
27
|
expectType<typeof result, Result<string, never>>('<=');
|
|
@@ -29,8 +32,10 @@ describe('Result test', () => {
|
|
|
29
32
|
test('creates Err result', () => {
|
|
30
33
|
const result = Result.err('error message');
|
|
31
34
|
|
|
32
|
-
|
|
33
|
-
|
|
35
|
+
assert.isTrue(Result.isErr(result));
|
|
36
|
+
|
|
37
|
+
assert.isFalse(Result.isOk(result));
|
|
38
|
+
|
|
34
39
|
expect(result.value).toBe('error message');
|
|
35
40
|
|
|
36
41
|
expectType<typeof result, Result<never, string>>('<=');
|
|
@@ -39,7 +44,8 @@ describe('Result test', () => {
|
|
|
39
44
|
test('creates Err result with number', () => {
|
|
40
45
|
const result = Result.err(404);
|
|
41
46
|
|
|
42
|
-
|
|
47
|
+
assert.isTrue(Result.isErr(result));
|
|
48
|
+
|
|
43
49
|
expect(result.value).toBe(404);
|
|
44
50
|
|
|
45
51
|
expectType<typeof result, Result<never, number>>('<=');
|
|
@@ -49,6 +55,7 @@ describe('Result test', () => {
|
|
|
49
55
|
describe('isOk', () => {
|
|
50
56
|
test('type guard for Ok results', () => {
|
|
51
57
|
const result: Result<number, string> = Result.ok(42);
|
|
58
|
+
|
|
52
59
|
if (Result.isOk(result)) {
|
|
53
60
|
expectType<typeof result, Ok<number>>('<=');
|
|
54
61
|
|
|
@@ -59,13 +66,14 @@ describe('Result test', () => {
|
|
|
59
66
|
test('returns false for Err results', () => {
|
|
60
67
|
const result: Result<number, string> = Result.err('error');
|
|
61
68
|
|
|
62
|
-
|
|
69
|
+
assert.isFalse(Result.isOk(result));
|
|
63
70
|
});
|
|
64
71
|
});
|
|
65
72
|
|
|
66
73
|
describe('isErr', () => {
|
|
67
74
|
test('type guard for Err results', () => {
|
|
68
75
|
const result: Result<number, string> = Result.err('error');
|
|
76
|
+
|
|
69
77
|
if (Result.isErr(result)) {
|
|
70
78
|
expectType<typeof result, Err<string>>('<=');
|
|
71
79
|
|
|
@@ -76,7 +84,7 @@ describe('Result test', () => {
|
|
|
76
84
|
test('returns false for Ok results', () => {
|
|
77
85
|
const result: Result<number, string> = Result.ok(42);
|
|
78
86
|
|
|
79
|
-
|
|
87
|
+
assert.isFalse(Result.isErr(result));
|
|
80
88
|
});
|
|
81
89
|
});
|
|
82
90
|
|
|
@@ -84,47 +92,52 @@ describe('Result test', () => {
|
|
|
84
92
|
test('recognizes Ok results', () => {
|
|
85
93
|
const result = Result.ok(42);
|
|
86
94
|
|
|
87
|
-
|
|
95
|
+
assert.isTrue(Result.isResult(result));
|
|
88
96
|
});
|
|
89
97
|
|
|
90
98
|
test('recognizes Err results', () => {
|
|
91
99
|
const result = Result.err('error');
|
|
92
100
|
|
|
93
|
-
|
|
101
|
+
assert.isTrue(Result.isResult(result));
|
|
94
102
|
});
|
|
95
103
|
|
|
96
104
|
test('rejects non-Result values', () => {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
105
|
+
assert.isFalse(Result.isResult(42));
|
|
106
|
+
|
|
107
|
+
assert.isFalse(Result.isResult('string'));
|
|
108
|
+
|
|
109
|
+
assert.isFalse(Result.isResult(null));
|
|
110
|
+
|
|
111
|
+
assert.isFalse(Result.isResult(undefined));
|
|
112
|
+
|
|
113
|
+
assert.isFalse(Result.isResult({}));
|
|
114
|
+
|
|
115
|
+
assert.isFalse(Result.isResult({ type: 'unknown', value: 42 }));
|
|
103
116
|
});
|
|
104
117
|
});
|
|
105
118
|
|
|
106
119
|
describe('map', () => {
|
|
107
120
|
test('maps Ok result', () => {
|
|
108
121
|
const result = Result.ok(5);
|
|
122
|
+
|
|
109
123
|
const mapped = Result.map(result, (x) => x * 2);
|
|
110
124
|
|
|
111
|
-
|
|
125
|
+
assert.isTrue(Result.isOk(mapped));
|
|
126
|
+
|
|
127
|
+
expect(mapped.value).toBe(10);
|
|
112
128
|
|
|
113
|
-
if (Result.isOk(mapped)) {
|
|
114
|
-
expect(mapped.value).toBe(10);
|
|
115
|
-
}
|
|
116
129
|
expectType<typeof mapped, Result<number, never>>('<=');
|
|
117
130
|
});
|
|
118
131
|
|
|
119
132
|
test('preserves Err result', () => {
|
|
120
133
|
const result: Result<number, string> = Result.err('error');
|
|
134
|
+
|
|
121
135
|
const mapped = Result.map(result, (x) => x * 2);
|
|
122
136
|
|
|
123
|
-
|
|
137
|
+
assert.isTrue(Result.isErr(mapped));
|
|
138
|
+
|
|
139
|
+
expect(mapped.value).toBe('error');
|
|
124
140
|
|
|
125
|
-
if (Result.isErr(mapped)) {
|
|
126
|
-
expect(mapped.value).toBe('error');
|
|
127
|
-
}
|
|
128
141
|
expectType<typeof mapped, Result<number, string>>('<=');
|
|
129
142
|
});
|
|
130
143
|
|
|
@@ -132,104 +145,99 @@ describe('Result test', () => {
|
|
|
132
145
|
const doubler = Result.map((x: number) => x * 2);
|
|
133
146
|
|
|
134
147
|
const okResult = Result.ok(5);
|
|
148
|
+
|
|
135
149
|
const mapped = doubler(okResult);
|
|
136
150
|
|
|
137
|
-
|
|
151
|
+
assert.isTrue(Result.isOk(mapped));
|
|
138
152
|
|
|
139
|
-
|
|
140
|
-
expect(mapped.value).toBe(10);
|
|
141
|
-
}
|
|
153
|
+
expect(mapped.value).toBe(10);
|
|
142
154
|
|
|
143
155
|
const errResult: Result<number, string> = Result.err('error');
|
|
156
|
+
|
|
144
157
|
const mappedErr = doubler(errResult);
|
|
145
158
|
|
|
146
|
-
|
|
159
|
+
assert.isTrue(Result.isErr(mappedErr));
|
|
147
160
|
|
|
148
|
-
|
|
149
|
-
expect(mappedErr.value).toBe('error');
|
|
150
|
-
}
|
|
161
|
+
expect(mappedErr.value).toBe('error');
|
|
151
162
|
});
|
|
152
163
|
|
|
153
164
|
test('should work with pipe when curried', () => {
|
|
154
165
|
const doubler = Result.map((x: number) => x * 2);
|
|
166
|
+
|
|
155
167
|
const toStringFn = Result.map((x: number) => x.toString());
|
|
156
168
|
|
|
157
169
|
const result = pipe(Result.ok(5)).map(doubler).map(toStringFn).value;
|
|
158
170
|
|
|
159
|
-
|
|
171
|
+
assert.isTrue(Result.isOk(result));
|
|
160
172
|
|
|
161
|
-
|
|
162
|
-
expect(result.value).toBe('10');
|
|
163
|
-
}
|
|
173
|
+
expect(result.value).toBe('10');
|
|
164
174
|
});
|
|
165
175
|
});
|
|
166
176
|
|
|
167
177
|
describe('mapErr', () => {
|
|
168
178
|
test('maps Err result', () => {
|
|
169
179
|
const result: Result<number, string> = Result.err('error');
|
|
180
|
+
|
|
170
181
|
const mapped = Result.mapErr(result, (e) => e.toUpperCase());
|
|
171
182
|
|
|
172
|
-
|
|
183
|
+
assert.isTrue(Result.isErr(mapped));
|
|
184
|
+
|
|
185
|
+
expect(mapped.value).toBe('ERROR');
|
|
173
186
|
|
|
174
|
-
if (Result.isErr(mapped)) {
|
|
175
|
-
expect(mapped.value).toBe('ERROR');
|
|
176
|
-
}
|
|
177
187
|
expectType<typeof mapped, Result<number, string>>('<=');
|
|
178
188
|
});
|
|
179
189
|
|
|
180
190
|
test('preserves Ok result', () => {
|
|
181
191
|
const result: Result<number, string> = Result.ok(42);
|
|
192
|
+
|
|
182
193
|
const mapped = Result.mapErr(result, (e: string) => e.toUpperCase());
|
|
183
194
|
|
|
184
|
-
|
|
195
|
+
assert.isTrue(Result.isOk(mapped));
|
|
185
196
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
expectType<typeof mapped, Result<number, string>>('~=');
|
|
197
|
+
expect(mapped.value).toBe(42);
|
|
198
|
+
|
|
199
|
+
expectType<typeof mapped, Ok<number>>('=');
|
|
190
200
|
});
|
|
191
201
|
|
|
192
202
|
test('should support curried form', () => {
|
|
193
203
|
const errorUppercase = Result.mapErr((e: string) => e.toUpperCase());
|
|
194
204
|
|
|
195
205
|
const errResult: Result<number, string> = Result.err('error');
|
|
206
|
+
|
|
196
207
|
const mapped = errorUppercase(errResult);
|
|
197
208
|
|
|
198
|
-
|
|
209
|
+
assert.isTrue(Result.isErr(mapped));
|
|
199
210
|
|
|
200
|
-
|
|
201
|
-
expect(mapped.value).toBe('ERROR');
|
|
202
|
-
}
|
|
211
|
+
expect(mapped.value).toBe('ERROR');
|
|
203
212
|
|
|
204
213
|
const okResult: Result<number, string> = Result.ok(42);
|
|
214
|
+
|
|
205
215
|
const mappedOk = errorUppercase(okResult);
|
|
206
216
|
|
|
207
|
-
|
|
217
|
+
assert.isTrue(Result.isOk(mappedOk));
|
|
208
218
|
|
|
209
|
-
|
|
210
|
-
expect(mappedOk.value).toBe(42);
|
|
211
|
-
}
|
|
219
|
+
expect(mappedOk.value).toBe(42);
|
|
212
220
|
});
|
|
213
221
|
|
|
214
222
|
test('should work with pipe when curried', () => {
|
|
215
223
|
const errorUppercase = Result.mapErr((e: string) => e.toUpperCase());
|
|
224
|
+
|
|
216
225
|
const errorPrefix = Result.mapErr((e: string) => `ERROR: ${e}`);
|
|
217
226
|
|
|
218
227
|
const result = pipe(Result.err('failed'))
|
|
219
228
|
.map(errorUppercase)
|
|
220
229
|
.map(errorPrefix).value;
|
|
221
230
|
|
|
222
|
-
|
|
231
|
+
assert.isTrue(Result.isErr(result));
|
|
223
232
|
|
|
224
|
-
|
|
225
|
-
expect(result.value).toBe('ERROR: FAILED');
|
|
226
|
-
}
|
|
233
|
+
expect(result.value).toBe('ERROR: FAILED');
|
|
227
234
|
});
|
|
228
235
|
});
|
|
229
236
|
|
|
230
237
|
describe('unwrapThrow', () => {
|
|
231
238
|
test('unwraps Ok result', () => {
|
|
232
239
|
const result = Result.ok(42);
|
|
240
|
+
|
|
233
241
|
const value = Result.unwrapThrow(result);
|
|
234
242
|
|
|
235
243
|
expect(value).toBe(42);
|
|
@@ -247,6 +255,7 @@ describe('Result test', () => {
|
|
|
247
255
|
describe('unwrapOkOr', () => {
|
|
248
256
|
test('unwraps Ok result', () => {
|
|
249
257
|
const result = Result.ok(42);
|
|
258
|
+
|
|
250
259
|
const value = Result.unwrapOkOr(result, 0);
|
|
251
260
|
|
|
252
261
|
expect(value).toBe(42);
|
|
@@ -256,6 +265,7 @@ describe('Result test', () => {
|
|
|
256
265
|
|
|
257
266
|
test('returns default for Err result', () => {
|
|
258
267
|
const result: Result<number, string> = Result.err('error');
|
|
268
|
+
|
|
259
269
|
const value = Result.unwrapOkOr(result, 0);
|
|
260
270
|
|
|
261
271
|
expect(value).toBe(0);
|
|
@@ -267,11 +277,13 @@ describe('Result test', () => {
|
|
|
267
277
|
const unwrapWithDefault = Result.unwrapOkOr(42);
|
|
268
278
|
|
|
269
279
|
const okResult = Result.ok(100);
|
|
280
|
+
|
|
270
281
|
const successValue = unwrapWithDefault(okResult);
|
|
271
282
|
|
|
272
283
|
expect(successValue).toBe(100);
|
|
273
284
|
|
|
274
285
|
const errResult: Result<number, string> = Result.err('failed');
|
|
286
|
+
|
|
275
287
|
const defaultValue = unwrapWithDefault(errResult);
|
|
276
288
|
|
|
277
289
|
expect(defaultValue).toBe(42);
|
|
@@ -295,6 +307,7 @@ describe('Result test', () => {
|
|
|
295
307
|
describe('unwrapErr', () => {
|
|
296
308
|
test('unwraps Err result', () => {
|
|
297
309
|
const result: Result<number, string> = Result.err('error');
|
|
310
|
+
|
|
298
311
|
const value = Result.unwrapErr(result);
|
|
299
312
|
|
|
300
313
|
expect(value).toBe('error');
|
|
@@ -317,34 +330,34 @@ describe('Result test', () => {
|
|
|
317
330
|
describe('fold', () => {
|
|
318
331
|
test('folds Ok result', () => {
|
|
319
332
|
const result = Result.ok(42);
|
|
333
|
+
|
|
320
334
|
const folded = Result.fold(
|
|
321
335
|
result,
|
|
322
336
|
(x) => x * 2,
|
|
323
337
|
() => 0,
|
|
324
338
|
);
|
|
325
339
|
|
|
326
|
-
|
|
340
|
+
assert.isTrue(Result.isOk(folded));
|
|
327
341
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
expectType<typeof folded, Result<number, number>>('=');
|
|
342
|
+
expect(folded.value).toBe(84);
|
|
343
|
+
|
|
344
|
+
expectType<typeof folded, Ok<number>>('=');
|
|
332
345
|
});
|
|
333
346
|
|
|
334
347
|
test('folds Err result', () => {
|
|
335
348
|
const result: Result<number, string> = Result.err('error');
|
|
349
|
+
|
|
336
350
|
const folded = Result.fold(
|
|
337
351
|
result,
|
|
338
352
|
(x) => x * 2,
|
|
339
353
|
(e) => e.length,
|
|
340
354
|
);
|
|
341
355
|
|
|
342
|
-
|
|
356
|
+
assert.isTrue(Result.isErr(folded));
|
|
343
357
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
expectType<typeof folded, Result<number, number>>('=');
|
|
358
|
+
expect(folded.value).toBe(5); // length of 'error'
|
|
359
|
+
|
|
360
|
+
expectType<typeof folded, Err<number>>('=');
|
|
348
361
|
});
|
|
349
362
|
|
|
350
363
|
test('should support curried form', () => {
|
|
@@ -354,22 +367,20 @@ describe('Result test', () => {
|
|
|
354
367
|
);
|
|
355
368
|
|
|
356
369
|
const okResult = Result.ok(42);
|
|
370
|
+
|
|
357
371
|
const foldedOk = folder(okResult);
|
|
358
372
|
|
|
359
|
-
|
|
373
|
+
assert.isTrue(Result.isOk(foldedOk));
|
|
360
374
|
|
|
361
|
-
|
|
362
|
-
expect(foldedOk.value).toBe(84);
|
|
363
|
-
}
|
|
375
|
+
expect(foldedOk.value).toBe(84);
|
|
364
376
|
|
|
365
377
|
const errResult: Result<number, string> = Result.err('error');
|
|
378
|
+
|
|
366
379
|
const foldedErr = folder(errResult);
|
|
367
380
|
|
|
368
|
-
|
|
381
|
+
assert.isTrue(Result.isErr(foldedErr));
|
|
369
382
|
|
|
370
|
-
|
|
371
|
-
expect(foldedErr.value).toBe(5);
|
|
372
|
-
}
|
|
383
|
+
expect(foldedErr.value).toBe(5);
|
|
373
384
|
});
|
|
374
385
|
|
|
375
386
|
test('should work with pipe when curried', () => {
|
|
@@ -380,19 +391,15 @@ describe('Result test', () => {
|
|
|
380
391
|
|
|
381
392
|
const result = pipe(Result.ok(21)).map(folder).value;
|
|
382
393
|
|
|
383
|
-
|
|
394
|
+
assert.isTrue(Result.isOk(result));
|
|
384
395
|
|
|
385
|
-
|
|
386
|
-
expect(result.value).toBe(42);
|
|
387
|
-
}
|
|
396
|
+
expect(result.value).toBe(42);
|
|
388
397
|
|
|
389
398
|
const errorResult = pipe(Result.err('error')).map(folder).value;
|
|
390
399
|
|
|
391
|
-
|
|
400
|
+
assert.isTrue(Result.isErr(errorResult));
|
|
392
401
|
|
|
393
|
-
|
|
394
|
-
expect(errorResult.value).toBe(0);
|
|
395
|
-
}
|
|
402
|
+
expect(errorResult.value).toBe(0);
|
|
396
403
|
});
|
|
397
404
|
});
|
|
398
405
|
|
|
@@ -403,23 +410,27 @@ describe('Result test', () => {
|
|
|
403
410
|
|
|
404
411
|
const result = await Result.fromPromise(asyncFn());
|
|
405
412
|
|
|
406
|
-
|
|
413
|
+
assert.isTrue(Result.isOk(result));
|
|
414
|
+
|
|
407
415
|
expect(Result.unwrapOk(result)).toBe(42);
|
|
408
416
|
});
|
|
409
417
|
|
|
410
418
|
test('handles async functions that reject', async () => {
|
|
411
419
|
const error = new Error('Async error');
|
|
420
|
+
|
|
412
421
|
const asyncFn = async (): Promise<number> =>
|
|
413
422
|
Promise.reject(error).then(() => 42);
|
|
414
423
|
|
|
415
424
|
const result = await Result.fromPromise(asyncFn());
|
|
416
425
|
|
|
417
|
-
|
|
426
|
+
assert.isTrue(Result.isErr(result));
|
|
427
|
+
|
|
418
428
|
expect(Result.unwrapErr(result)).toBe(error);
|
|
419
429
|
});
|
|
420
430
|
|
|
421
431
|
test('works with different promise types', async () => {
|
|
422
432
|
const stringPromise = Promise.resolve('hello');
|
|
433
|
+
|
|
423
434
|
const result = await Result.fromPromise(stringPromise);
|
|
424
435
|
|
|
425
436
|
expect(Result.unwrapOk(result)).toBe('hello');
|
|
@@ -456,6 +467,7 @@ describe('Result test', () => {
|
|
|
456
467
|
test('should support chaining multiple flatMaps', () => {
|
|
457
468
|
const parseNumber = (s: string): Result<number, string> => {
|
|
458
469
|
const n = Number(s);
|
|
470
|
+
|
|
459
471
|
return Number.isNaN(n) ? Result.err('Not a number') : Result.ok(n);
|
|
460
472
|
};
|
|
461
473
|
|
|
@@ -481,35 +493,32 @@ describe('Result test', () => {
|
|
|
481
493
|
const divideBy2 = Result.flatMap((x: number) => divide(x, 2));
|
|
482
494
|
|
|
483
495
|
const okResult = Result.ok(10);
|
|
496
|
+
|
|
484
497
|
const result = divideBy2(okResult);
|
|
485
498
|
|
|
486
|
-
|
|
499
|
+
assert.isTrue(Result.isOk(result));
|
|
487
500
|
|
|
488
|
-
|
|
489
|
-
expect(result.value).toBe(5);
|
|
490
|
-
}
|
|
501
|
+
expect(result.value).toBe(5);
|
|
491
502
|
|
|
492
503
|
const divideByZero = Result.flatMap((x: number) => divide(x, 0));
|
|
504
|
+
|
|
493
505
|
const errorResult = divideByZero(Result.ok(10));
|
|
494
506
|
|
|
495
|
-
|
|
507
|
+
assert.isTrue(Result.isErr(errorResult));
|
|
496
508
|
|
|
497
|
-
|
|
498
|
-
expect(errorResult.value).toBe('Division by zero');
|
|
499
|
-
}
|
|
509
|
+
expect(errorResult.value).toBe('Division by zero');
|
|
500
510
|
|
|
501
511
|
const initialError = divideBy2(Result.err('initial error'));
|
|
502
512
|
|
|
503
|
-
|
|
513
|
+
assert.isTrue(Result.isErr(initialError));
|
|
504
514
|
|
|
505
|
-
|
|
506
|
-
expect(initialError.value).toBe('initial error');
|
|
507
|
-
}
|
|
515
|
+
expect(initialError.value).toBe('initial error');
|
|
508
516
|
});
|
|
509
517
|
|
|
510
518
|
test('should work with pipe when curried', () => {
|
|
511
519
|
const parseNumber = (s: string): Result<number, string> => {
|
|
512
520
|
const n = Number(s);
|
|
521
|
+
|
|
513
522
|
return Number.isNaN(n) ? Result.err('Not a number') : Result.ok(n);
|
|
514
523
|
};
|
|
515
524
|
|
|
@@ -517,32 +526,35 @@ describe('Result test', () => {
|
|
|
517
526
|
n > 0 ? Result.ok(n * 2) : Result.err('Not positive');
|
|
518
527
|
|
|
519
528
|
const parser = Result.flatMap(parseNumber);
|
|
529
|
+
|
|
520
530
|
const doubler = Result.flatMap(doubleIfPositive);
|
|
521
531
|
|
|
522
532
|
const result = pipe(Result.ok('42')).map(parser).map(doubler).value;
|
|
523
533
|
|
|
524
|
-
|
|
534
|
+
assert.isTrue(Result.isOk(result));
|
|
525
535
|
|
|
526
|
-
|
|
527
|
-
expect(result.value).toBe(84);
|
|
528
|
-
}
|
|
536
|
+
expect(result.value).toBe(84);
|
|
529
537
|
});
|
|
530
538
|
});
|
|
531
539
|
|
|
532
540
|
describe('swap', () => {
|
|
533
541
|
test('should swap Ok to Err', () => {
|
|
534
542
|
const okResult = Result.ok(42);
|
|
543
|
+
|
|
535
544
|
const swapped = Result.swap(okResult);
|
|
536
545
|
|
|
537
|
-
|
|
546
|
+
assert.isTrue(Result.isErr(swapped));
|
|
547
|
+
|
|
538
548
|
expect(Result.unwrapErr(swapped)).toBe(42);
|
|
539
549
|
});
|
|
540
550
|
|
|
541
551
|
test('should swap Err to Ok', () => {
|
|
542
552
|
const errResult = Result.err('error');
|
|
553
|
+
|
|
543
554
|
const swapped = Result.swap(errResult);
|
|
544
555
|
|
|
545
|
-
|
|
556
|
+
assert.isTrue(Result.isOk(swapped));
|
|
557
|
+
|
|
546
558
|
expect(Result.unwrapOk(swapped)).toBe('error');
|
|
547
559
|
});
|
|
548
560
|
});
|
|
@@ -550,17 +562,20 @@ describe('Result test', () => {
|
|
|
550
562
|
describe('toOptional', () => {
|
|
551
563
|
test('should convert Ok to Some-like', () => {
|
|
552
564
|
const okResult = Result.ok(42);
|
|
565
|
+
|
|
553
566
|
const optional = Result.toOptional(okResult);
|
|
554
567
|
|
|
555
|
-
|
|
568
|
+
assert.isTrue(Optional.isSome(optional));
|
|
569
|
+
|
|
556
570
|
expect(Optional.unwrapThrow(optional)).toBe(42);
|
|
557
571
|
});
|
|
558
572
|
|
|
559
573
|
test('should convert Err to None-like', () => {
|
|
560
574
|
const errResult = Result.err('error');
|
|
575
|
+
|
|
561
576
|
const optional = Result.toOptional(errResult);
|
|
562
577
|
|
|
563
|
-
|
|
578
|
+
assert.isTrue(Optional.isNone(optional));
|
|
564
579
|
});
|
|
565
580
|
});
|
|
566
581
|
|
|
@@ -591,6 +606,7 @@ describe('Result test', () => {
|
|
|
591
606
|
describe('unwrapErrOr', () => {
|
|
592
607
|
test('should return error value for Err result', () => {
|
|
593
608
|
const result = Result.err('error message');
|
|
609
|
+
|
|
594
610
|
const value = Result.unwrapErrOr(result, 'default');
|
|
595
611
|
|
|
596
612
|
expect(value).toBe('error message');
|
|
@@ -598,6 +614,7 @@ describe('Result test', () => {
|
|
|
598
614
|
|
|
599
615
|
test('should return default value for Ok result', () => {
|
|
600
616
|
const result = Result.ok(42);
|
|
617
|
+
|
|
601
618
|
const value = Result.unwrapErrOr(result, 'default');
|
|
602
619
|
|
|
603
620
|
expect(value).toBe('default');
|
|
@@ -607,11 +624,13 @@ describe('Result test', () => {
|
|
|
607
624
|
const unwrapErrorWithDefault = Result.unwrapErrOr('unknown error');
|
|
608
625
|
|
|
609
626
|
const errResult: Result<number, string> = Result.err('failed');
|
|
627
|
+
|
|
610
628
|
const errorValue = unwrapErrorWithDefault(errResult);
|
|
611
629
|
|
|
612
630
|
expect(errorValue).toBe('failed');
|
|
613
631
|
|
|
614
632
|
const okResult: Result<number, string> = Result.ok(42);
|
|
633
|
+
|
|
615
634
|
const defaultValue = unwrapErrorWithDefault(okResult);
|
|
616
635
|
|
|
617
636
|
expect(defaultValue).toBe('unknown error');
|
|
@@ -637,6 +656,7 @@ describe('Result test', () => {
|
|
|
637
656
|
describe('expectToBe', () => {
|
|
638
657
|
test('should return value for Ok result', () => {
|
|
639
658
|
const result = Result.ok(42);
|
|
659
|
+
|
|
640
660
|
const value = Result.expectToBe(result, 'Expected valid number');
|
|
641
661
|
|
|
642
662
|
expect(value).toBe(42);
|
|
@@ -654,6 +674,7 @@ describe('Result test', () => {
|
|
|
654
674
|
const mustBeOk = Result.expectToBe('Expected successful result');
|
|
655
675
|
|
|
656
676
|
const okResult = Result.ok('success');
|
|
677
|
+
|
|
657
678
|
const value = mustBeOk(okResult);
|
|
658
679
|
|
|
659
680
|
expect(value).toBe('success');
|
|
@@ -679,7 +700,9 @@ describe('Result test', () => {
|
|
|
679
700
|
describe('orElse', () => {
|
|
680
701
|
test('should return the first Result if it is Ok', () => {
|
|
681
702
|
const primary = Result.ok(42);
|
|
703
|
+
|
|
682
704
|
const fallback = Result.ok(100);
|
|
705
|
+
|
|
683
706
|
const result = Result.orElse(primary, fallback);
|
|
684
707
|
|
|
685
708
|
expect(Result.unwrapOk(result)).toBe(42);
|
|
@@ -687,7 +710,9 @@ describe('Result test', () => {
|
|
|
687
710
|
|
|
688
711
|
test('should return the alternative if the first is Err', () => {
|
|
689
712
|
const primary = Result.err('error');
|
|
713
|
+
|
|
690
714
|
const fallback = Result.ok('default');
|
|
715
|
+
|
|
691
716
|
const result = Result.orElse(primary, fallback);
|
|
692
717
|
|
|
693
718
|
expect(Result.unwrapOk(result)).toBe('default');
|
|
@@ -695,7 +720,9 @@ describe('Result test', () => {
|
|
|
695
720
|
|
|
696
721
|
test('should return Err if both are Err', () => {
|
|
697
722
|
const primary = Result.err('error1');
|
|
723
|
+
|
|
698
724
|
const fallback = Result.err('error2');
|
|
725
|
+
|
|
699
726
|
const result = Result.orElse(primary, fallback);
|
|
700
727
|
|
|
701
728
|
expect(Result.unwrapErr(result)).toBe('error2');
|
|
@@ -705,22 +732,20 @@ describe('Result test', () => {
|
|
|
705
732
|
const fallbackTo = Result.orElse(Result.ok('fallback'));
|
|
706
733
|
|
|
707
734
|
const okResult = Result.ok('primary');
|
|
735
|
+
|
|
708
736
|
const result = fallbackTo(okResult);
|
|
709
737
|
|
|
710
|
-
|
|
738
|
+
assert.isTrue(Result.isOk(result));
|
|
711
739
|
|
|
712
|
-
|
|
713
|
-
expect(result.value).toBe('primary');
|
|
714
|
-
}
|
|
740
|
+
expect(result.value).toBe('primary');
|
|
715
741
|
|
|
716
742
|
const errResult: Result<string, string> = Result.err('failed');
|
|
743
|
+
|
|
717
744
|
const fallbackResult = fallbackTo(errResult);
|
|
718
745
|
|
|
719
|
-
|
|
746
|
+
assert.isTrue(Result.isOk(fallbackResult));
|
|
720
747
|
|
|
721
|
-
|
|
722
|
-
expect(fallbackResult.value).toBe('fallback');
|
|
723
|
-
}
|
|
748
|
+
expect(fallbackResult.value).toBe('fallback');
|
|
724
749
|
});
|
|
725
750
|
|
|
726
751
|
test('should work with pipe when curried', () => {
|
|
@@ -728,26 +753,24 @@ describe('Result test', () => {
|
|
|
728
753
|
|
|
729
754
|
const okResult = pipe(Result.ok('original')).map(fallbackTo).value;
|
|
730
755
|
|
|
731
|
-
|
|
756
|
+
assert.isTrue(Result.isOk(okResult));
|
|
732
757
|
|
|
733
|
-
|
|
734
|
-
expect(okResult.value).toBe('original');
|
|
735
|
-
}
|
|
758
|
+
expect(okResult.value).toBe('original');
|
|
736
759
|
|
|
737
760
|
const errResult = pipe(Result.err('network error')).map(fallbackTo).value;
|
|
738
761
|
|
|
739
|
-
|
|
762
|
+
assert.isTrue(Result.isOk(errResult));
|
|
740
763
|
|
|
741
|
-
|
|
742
|
-
expect(errResult.value).toBe('backup');
|
|
743
|
-
}
|
|
764
|
+
expect(errResult.value).toBe('backup');
|
|
744
765
|
});
|
|
745
766
|
});
|
|
746
767
|
|
|
747
768
|
describe('zip', () => {
|
|
748
769
|
test('should combine two Ok values into a tuple', () => {
|
|
749
770
|
const a = Result.ok(1);
|
|
771
|
+
|
|
750
772
|
const b = Result.ok('hello');
|
|
773
|
+
|
|
751
774
|
const zipped = Result.zip(a, b);
|
|
752
775
|
|
|
753
776
|
assert.deepStrictEqual(Result.unwrapOk(zipped), [1, 'hello']);
|
|
@@ -755,7 +778,9 @@ describe('Result test', () => {
|
|
|
755
778
|
|
|
756
779
|
test('should return first Err if first is Err', () => {
|
|
757
780
|
const a = Result.err('error1');
|
|
781
|
+
|
|
758
782
|
const b = Result.ok('hello');
|
|
783
|
+
|
|
759
784
|
const zipped = Result.zip(a, b);
|
|
760
785
|
|
|
761
786
|
expect(Result.unwrapErr(zipped)).toBe('error1');
|
|
@@ -763,7 +788,9 @@ describe('Result test', () => {
|
|
|
763
788
|
|
|
764
789
|
test('should return second Err if second is Err', () => {
|
|
765
790
|
const a = Result.ok(1);
|
|
791
|
+
|
|
766
792
|
const b = Result.err('error2');
|
|
793
|
+
|
|
767
794
|
const zipped = Result.zip(a, b);
|
|
768
795
|
|
|
769
796
|
expect(Result.unwrapErr(zipped)).toBe('error2');
|
|
@@ -771,7 +798,9 @@ describe('Result test', () => {
|
|
|
771
798
|
|
|
772
799
|
test('should return first Err if both are Err', () => {
|
|
773
800
|
const a = Result.err('error1');
|
|
801
|
+
|
|
774
802
|
const b = Result.err('error2');
|
|
803
|
+
|
|
775
804
|
const zipped = Result.zip(a, b);
|
|
776
805
|
|
|
777
806
|
expect(Result.unwrapErr(zipped)).toBe('error1');
|
|
@@ -782,7 +811,8 @@ describe('Result test', () => {
|
|
|
782
811
|
test('should return Ok when function succeeds', () => {
|
|
783
812
|
const result = Result.fromThrowable(() => 42);
|
|
784
813
|
|
|
785
|
-
|
|
814
|
+
assert.isTrue(Result.isOk(result));
|
|
815
|
+
|
|
786
816
|
expect(Result.unwrapOk(result)).toBe(42);
|
|
787
817
|
|
|
788
818
|
expectType<typeof result, Result<number, Error>>('<=');
|
|
@@ -790,44 +820,45 @@ describe('Result test', () => {
|
|
|
790
820
|
|
|
791
821
|
test('should return Ok with object when function succeeds', () => {
|
|
792
822
|
const obj = { name: 'test', value: 123 };
|
|
823
|
+
|
|
793
824
|
const result = Result.fromThrowable(() => obj);
|
|
794
825
|
|
|
795
|
-
|
|
826
|
+
assert.isTrue(Result.isOk(result));
|
|
796
827
|
|
|
797
828
|
assert.deepStrictEqual(Result.unwrapOk(result), obj);
|
|
798
829
|
});
|
|
799
830
|
|
|
800
831
|
test('should return Err when function throws Error', () => {
|
|
801
832
|
const errorMessage = 'Something went wrong';
|
|
833
|
+
|
|
802
834
|
const result = Result.fromThrowable(() => {
|
|
803
835
|
throw new Error(errorMessage);
|
|
804
836
|
});
|
|
805
837
|
|
|
806
|
-
|
|
838
|
+
assert.isTrue(Result.isErr(result));
|
|
807
839
|
|
|
808
|
-
|
|
809
|
-
const error = result.value;
|
|
840
|
+
const error = result.value;
|
|
810
841
|
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
842
|
+
expect(error).toBeInstanceOf(Error);
|
|
843
|
+
|
|
844
|
+
expect(error.message).toBe(errorMessage);
|
|
814
845
|
});
|
|
815
846
|
|
|
816
847
|
test('should return Err when function throws string', () => {
|
|
817
848
|
const errorMessage = 'String error';
|
|
849
|
+
|
|
818
850
|
const result = Result.fromThrowable(() => {
|
|
819
851
|
// eslint-disable-next-line @typescript-eslint/only-throw-error
|
|
820
852
|
throw errorMessage;
|
|
821
853
|
});
|
|
822
854
|
|
|
823
|
-
|
|
855
|
+
assert.isTrue(Result.isErr(result));
|
|
824
856
|
|
|
825
|
-
|
|
826
|
-
const error = result.value;
|
|
857
|
+
const error = result.value;
|
|
827
858
|
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
859
|
+
expect(error).toBeInstanceOf(Error);
|
|
860
|
+
|
|
861
|
+
expect(error.message).toBe(errorMessage);
|
|
831
862
|
});
|
|
832
863
|
|
|
833
864
|
test('should return Err when function throws non-string primitive', () => {
|
|
@@ -836,37 +867,35 @@ describe('Result test', () => {
|
|
|
836
867
|
throw 404;
|
|
837
868
|
});
|
|
838
869
|
|
|
839
|
-
|
|
870
|
+
assert.isTrue(Result.isErr(result));
|
|
840
871
|
|
|
841
|
-
|
|
842
|
-
const error = result.value;
|
|
872
|
+
const error = result.value;
|
|
843
873
|
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
874
|
+
expect(error).toBeInstanceOf(Error);
|
|
875
|
+
|
|
876
|
+
expect(error.message).toBe('404');
|
|
847
877
|
});
|
|
848
878
|
|
|
849
879
|
test('should work with JSON.parse', () => {
|
|
850
880
|
const validJson = '{"key": "value"}';
|
|
881
|
+
|
|
851
882
|
const invalidJson = '{invalid json}';
|
|
852
883
|
|
|
853
884
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
854
885
|
const validResult = Result.fromThrowable(() => JSON.parse(validJson));
|
|
855
886
|
|
|
856
|
-
|
|
887
|
+
assert.isTrue(Result.isOk(validResult));
|
|
857
888
|
|
|
858
889
|
assert.deepStrictEqual(Result.unwrapOk(validResult), { key: 'value' });
|
|
859
890
|
|
|
860
891
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
861
892
|
const invalidResult = Result.fromThrowable(() => JSON.parse(invalidJson));
|
|
862
893
|
|
|
863
|
-
|
|
894
|
+
assert.isTrue(Result.isErr(invalidResult));
|
|
864
895
|
|
|
865
|
-
|
|
866
|
-
const error = invalidResult.value;
|
|
896
|
+
const error = invalidResult.value;
|
|
867
897
|
|
|
868
|
-
|
|
869
|
-
}
|
|
898
|
+
expect(error).toBeInstanceOf(Error);
|
|
870
899
|
});
|
|
871
900
|
|
|
872
901
|
test('should work with array access', () => {
|
|
@@ -875,20 +904,21 @@ describe('Result test', () => {
|
|
|
875
904
|
// This won't throw, but demonstrates the pattern
|
|
876
905
|
const result = Result.fromThrowable(() => {
|
|
877
906
|
const index = 5;
|
|
907
|
+
|
|
878
908
|
const value = arr[index];
|
|
909
|
+
|
|
879
910
|
if (value === undefined) {
|
|
880
911
|
throw new Error('Index out of bounds');
|
|
881
912
|
}
|
|
913
|
+
|
|
882
914
|
return value;
|
|
883
915
|
});
|
|
884
916
|
|
|
885
|
-
|
|
917
|
+
assert.isTrue(Result.isErr(result));
|
|
886
918
|
|
|
887
|
-
|
|
888
|
-
const error = result.value;
|
|
919
|
+
const error = result.value;
|
|
889
920
|
|
|
890
|
-
|
|
891
|
-
}
|
|
921
|
+
expect(error.message).toBe('Index out of bounds');
|
|
892
922
|
});
|
|
893
923
|
|
|
894
924
|
test('should preserve function return type', () => {
|