ts-data-forge 3.3.1 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/array/impl/array-utils-element-access.d.mts +3 -4
- package/dist/array/impl/array-utils-element-access.d.mts.map +1 -1
- package/dist/array/impl/array-utils-element-access.mjs +8 -8
- package/dist/array/impl/array-utils-element-access.mjs.map +1 -1
- package/dist/array/impl/array-utils-reducing-value.d.mts +8 -9
- package/dist/array/impl/array-utils-reducing-value.d.mts.map +1 -1
- package/dist/array/impl/array-utils-reducing-value.mjs +9 -7
- package/dist/array/impl/array-utils-reducing-value.mjs.map +1 -1
- package/dist/array/impl/array-utils-search.d.mts +0 -1
- package/dist/array/impl/array-utils-search.d.mts.map +1 -1
- package/dist/array/impl/array-utils-search.mjs +6 -6
- package/dist/array/impl/array-utils-search.mjs.map +1 -1
- package/dist/array/impl/array-utils-transformation.mjs +2 -4
- package/dist/array/impl/array-utils-transformation.mjs.map +1 -1
- package/dist/collections/imap-mapped.d.mts +0 -1
- package/dist/collections/imap-mapped.d.mts.map +1 -1
- package/dist/collections/imap-mapped.mjs +7 -6
- package/dist/collections/imap-mapped.mjs.map +1 -1
- package/dist/collections/imap.d.mts +0 -1
- package/dist/collections/imap.d.mts.map +1 -1
- package/dist/collections/imap.mjs +11 -9
- package/dist/collections/imap.mjs.map +1 -1
- package/dist/collections/queue.d.mts +0 -1
- package/dist/collections/queue.d.mts.map +1 -1
- package/dist/collections/queue.mjs +4 -4
- package/dist/collections/queue.mjs.map +1 -1
- package/dist/collections/stack.d.mts +0 -1
- package/dist/collections/stack.d.mts.map +1 -1
- package/dist/collections/stack.mjs +4 -4
- package/dist/collections/stack.mjs.map +1 -1
- package/dist/entry-point.d.mts +1 -0
- package/dist/entry-point.d.mts.map +1 -1
- package/dist/entry-point.mjs +8 -4
- package/dist/entry-point.mjs.map +1 -1
- package/dist/functional/index.d.mts +3 -2
- package/dist/functional/index.d.mts.map +1 -1
- package/dist/functional/index.mjs +6 -2
- package/dist/functional/index.mjs.map +1 -1
- package/dist/functional/optional/impl/index.d.mts +18 -0
- package/dist/functional/optional/impl/index.d.mts.map +1 -0
- package/dist/functional/optional/impl/index.mjs +17 -0
- package/dist/functional/optional/impl/index.mjs.map +1 -0
- package/dist/functional/optional/impl/optional-expect-to-be.d.mts +29 -0
- package/dist/functional/optional/impl/optional-expect-to-be.d.mts.map +1 -0
- package/dist/functional/optional/impl/optional-expect-to-be.mjs +25 -0
- package/dist/functional/optional/impl/optional-expect-to-be.mjs.map +1 -0
- package/dist/functional/optional/impl/optional-filter.d.mts +29 -0
- package/dist/functional/optional/impl/optional-filter.d.mts.map +1 -0
- package/dist/functional/optional/impl/optional-filter.mjs +28 -0
- package/dist/functional/optional/impl/optional-filter.mjs.map +1 -0
- package/dist/functional/optional/impl/optional-flat-map.d.mts +33 -0
- package/dist/functional/optional/impl/optional-flat-map.d.mts.map +1 -0
- package/dist/functional/optional/impl/optional-flat-map.mjs +21 -0
- package/dist/functional/optional/impl/optional-flat-map.mjs.map +1 -0
- package/dist/functional/optional/impl/optional-from-nullable.d.mts +24 -0
- package/dist/functional/optional/impl/optional-from-nullable.d.mts.map +1 -0
- package/dist/functional/optional/impl/optional-from-nullable.mjs +29 -0
- package/dist/functional/optional/impl/optional-from-nullable.mjs.map +1 -0
- package/dist/functional/optional/impl/optional-is-none.d.mts +22 -0
- package/dist/functional/optional/impl/optional-is-none.d.mts.map +1 -0
- package/dist/functional/optional/impl/optional-is-none.mjs +25 -0
- package/dist/functional/optional/impl/optional-is-none.mjs.map +1 -0
- package/dist/functional/optional/impl/optional-is-optional.d.mts +18 -0
- package/dist/functional/optional/impl/optional-is-optional.d.mts.map +1 -0
- package/dist/functional/optional/impl/optional-is-optional.mjs +27 -0
- package/dist/functional/optional/impl/optional-is-optional.mjs.map +1 -0
- package/dist/functional/optional/impl/optional-is-some.d.mts +22 -0
- package/dist/functional/optional/impl/optional-is-some.d.mts.map +1 -0
- package/dist/functional/optional/impl/optional-is-some.mjs +25 -0
- package/dist/functional/optional/impl/optional-is-some.mjs.map +1 -0
- package/dist/functional/optional/impl/optional-map.d.mts +31 -0
- package/dist/functional/optional/impl/optional-map.d.mts.map +1 -0
- package/dist/functional/optional/impl/optional-map.mjs +25 -0
- package/dist/functional/optional/impl/optional-map.mjs.map +1 -0
- package/dist/functional/optional/impl/optional-none.d.mts +16 -0
- package/dist/functional/optional/impl/optional-none.d.mts.map +1 -0
- package/dist/functional/optional/impl/optional-none.mjs +20 -0
- package/dist/functional/optional/impl/optional-none.mjs.map +1 -0
- package/dist/functional/optional/impl/optional-or-else.d.mts +35 -0
- package/dist/functional/optional/impl/optional-or-else.d.mts.map +1 -0
- package/dist/functional/optional/impl/optional-or-else.mjs +19 -0
- package/dist/functional/optional/impl/optional-or-else.mjs.map +1 -0
- package/dist/functional/optional/impl/optional-some.d.mts +19 -0
- package/dist/functional/optional/impl/optional-some.d.mts.map +1 -0
- package/dist/functional/optional/impl/optional-some.mjs +26 -0
- package/dist/functional/optional/impl/optional-some.mjs.map +1 -0
- package/dist/functional/optional/impl/optional-to-nullable.d.mts +27 -0
- package/dist/functional/optional/impl/optional-to-nullable.d.mts.map +1 -0
- package/dist/functional/optional/impl/optional-to-nullable.mjs +31 -0
- package/dist/functional/optional/impl/optional-to-nullable.mjs.map +1 -0
- package/dist/functional/optional/impl/optional-unwrap-or.d.mts +33 -0
- package/dist/functional/optional/impl/optional-unwrap-or.d.mts.map +1 -0
- package/dist/functional/optional/impl/optional-unwrap-or.mjs +19 -0
- package/dist/functional/optional/impl/optional-unwrap-or.mjs.map +1 -0
- package/dist/functional/optional/impl/optional-unwrap-throw.d.mts +29 -0
- package/dist/functional/optional/impl/optional-unwrap-throw.d.mts.map +1 -0
- package/dist/functional/optional/impl/optional-unwrap-throw.mjs +38 -0
- package/dist/functional/optional/impl/optional-unwrap-throw.mjs.map +1 -0
- package/dist/functional/optional/impl/optional-unwrap.d.mts +28 -0
- package/dist/functional/optional/impl/optional-unwrap.d.mts.map +1 -0
- package/dist/functional/optional/impl/optional-unwrap.mjs +11 -0
- package/dist/functional/optional/impl/optional-unwrap.mjs.map +1 -0
- package/dist/functional/optional/impl/optional-zip.d.mts +31 -0
- package/dist/functional/optional/impl/optional-zip.d.mts.map +1 -0
- package/dist/functional/optional/impl/optional-zip.mjs +39 -0
- package/dist/functional/optional/impl/optional-zip.mjs.map +1 -0
- package/dist/functional/optional/impl/tag.d.mts +5 -0
- package/dist/functional/optional/impl/tag.d.mts.map +1 -0
- package/dist/functional/optional/impl/tag.mjs +7 -0
- package/dist/functional/optional/impl/tag.mjs.map +1 -0
- package/dist/functional/optional/impl/types.d.mts +22 -0
- package/dist/functional/optional/impl/types.d.mts.map +1 -0
- package/dist/functional/optional/impl/types.mjs +2 -0
- package/dist/functional/optional/impl/types.mjs.map +1 -0
- package/dist/functional/optional/index.d.mts +2 -0
- package/dist/functional/optional/index.d.mts.map +1 -0
- package/dist/functional/optional/index.mjs +3 -0
- package/dist/functional/optional/index.mjs.map +1 -0
- package/dist/functional/pipe.d.mts +4 -4
- package/dist/functional/pipe.d.mts.map +1 -1
- package/dist/functional/pipe.mjs +4 -3
- package/dist/functional/pipe.mjs.map +1 -1
- package/dist/functional/result/impl/index.d.mts +24 -0
- package/dist/functional/result/impl/index.d.mts.map +1 -0
- package/dist/functional/result/impl/index.mjs +23 -0
- package/dist/functional/result/impl/index.mjs.map +1 -0
- package/dist/functional/result/impl/result-err.d.mts +25 -0
- package/dist/functional/result/impl/result-err.d.mts.map +1 -0
- package/dist/functional/result/impl/result-err.mjs +32 -0
- package/dist/functional/result/impl/result-err.mjs.map +1 -0
- package/dist/functional/result/impl/result-expect-to-be.d.mts +27 -0
- package/dist/functional/result/impl/result-expect-to-be.d.mts.map +1 -0
- package/dist/functional/result/impl/result-expect-to-be.mjs +26 -0
- package/dist/functional/result/impl/result-expect-to-be.mjs.map +1 -0
- package/dist/functional/result/impl/result-flat-map.d.mts +39 -0
- package/dist/functional/result/impl/result-flat-map.d.mts.map +1 -0
- package/dist/functional/result/impl/result-flat-map.mjs +24 -0
- package/dist/functional/result/impl/result-flat-map.mjs.map +1 -0
- package/dist/functional/result/impl/result-fold.d.mts +45 -0
- package/dist/functional/result/impl/result-fold.d.mts.map +1 -0
- package/dist/functional/result/impl/result-fold.mjs +26 -0
- package/dist/functional/result/impl/result-fold.mjs.map +1 -0
- package/dist/functional/result/impl/result-from-promise.d.mts +32 -0
- package/dist/functional/result/impl/result-from-promise.d.mts.map +1 -0
- package/dist/functional/result/impl/result-from-promise.mjs +32 -0
- package/dist/functional/result/impl/result-from-promise.mjs.map +1 -0
- package/dist/functional/result/impl/result-from-throwable.d.mts +29 -0
- package/dist/functional/result/impl/result-from-throwable.d.mts.map +1 -0
- package/dist/functional/result/impl/result-from-throwable.mjs +46 -0
- package/dist/functional/result/impl/result-from-throwable.mjs.map +1 -0
- package/dist/functional/result/impl/result-is-err.d.mts +20 -0
- package/dist/functional/result/impl/result-is-err.d.mts.map +1 -0
- package/dist/functional/result/impl/result-is-err.mjs +23 -0
- package/dist/functional/result/impl/result-is-err.mjs.map +1 -0
- package/dist/functional/result/impl/result-is-ok.d.mts +20 -0
- package/dist/functional/result/impl/result-is-ok.d.mts.map +1 -0
- package/dist/functional/result/impl/result-is-ok.mjs +23 -0
- package/dist/functional/result/impl/result-is-ok.mjs.map +1 -0
- package/dist/functional/result/impl/result-is-result.d.mts +17 -0
- package/dist/functional/result/impl/result-is-result.d.mts.map +1 -0
- package/dist/functional/result/impl/result-is-result.mjs +26 -0
- package/dist/functional/result/impl/result-is-result.mjs.map +1 -0
- package/dist/functional/result/impl/result-map-err.d.mts +33 -0
- package/dist/functional/result/impl/result-map-err.d.mts.map +1 -0
- package/dist/functional/result/impl/result-map-err.mjs +25 -0
- package/dist/functional/result/impl/result-map-err.mjs.map +1 -0
- package/dist/functional/result/impl/result-map.d.mts +34 -0
- package/dist/functional/result/impl/result-map.d.mts.map +1 -0
- package/dist/functional/result/impl/result-map.mjs +25 -0
- package/dist/functional/result/impl/result-map.mjs.map +1 -0
- package/dist/functional/result/impl/result-ok.d.mts +25 -0
- package/dist/functional/result/impl/result-ok.d.mts.map +1 -0
- package/dist/functional/result/impl/result-ok.mjs +32 -0
- package/dist/functional/result/impl/result-ok.mjs.map +1 -0
- package/dist/functional/result/impl/result-or-else.d.mts +32 -0
- package/dist/functional/result/impl/result-or-else.d.mts.map +1 -0
- package/dist/functional/result/impl/result-or-else.mjs +20 -0
- package/dist/functional/result/impl/result-or-else.mjs.map +1 -0
- package/dist/functional/result/impl/result-swap.d.mts +20 -0
- package/dist/functional/result/impl/result-swap.d.mts.map +1 -0
- package/dist/functional/result/impl/result-swap.mjs +28 -0
- package/dist/functional/result/impl/result-swap.mjs.map +1 -0
- package/dist/functional/result/impl/result-to-optional.d.mts +28 -0
- package/dist/functional/result/impl/result-to-optional.d.mts.map +1 -0
- package/dist/functional/result/impl/result-to-optional.mjs +34 -0
- package/dist/functional/result/impl/result-to-optional.mjs.map +1 -0
- package/dist/functional/result/impl/result-unwrap-err-or.d.mts +29 -0
- package/dist/functional/result/impl/result-unwrap-err-or.d.mts.map +1 -0
- package/dist/functional/result/impl/result-unwrap-err-or.mjs +20 -0
- package/dist/functional/result/impl/result-unwrap-err-or.mjs.map +1 -0
- package/dist/functional/result/impl/result-unwrap-err-throw.d.mts +30 -0
- package/dist/functional/result/impl/result-unwrap-err-throw.d.mts.map +1 -0
- package/dist/functional/result/impl/result-unwrap-err-throw.mjs +47 -0
- package/dist/functional/result/impl/result-unwrap-err-throw.mjs.map +1 -0
- package/dist/functional/result/impl/result-unwrap-err.d.mts +31 -0
- package/dist/functional/result/impl/result-unwrap-err.d.mts.map +1 -0
- package/dist/functional/result/impl/result-unwrap-err.mjs +36 -0
- package/dist/functional/result/impl/result-unwrap-err.mjs.map +1 -0
- package/dist/functional/result/impl/result-unwrap-ok-or.d.mts +29 -0
- package/dist/functional/result/impl/result-unwrap-ok-or.d.mts.map +1 -0
- package/dist/functional/result/impl/result-unwrap-ok-or.mjs +20 -0
- package/dist/functional/result/impl/result-unwrap-ok-or.mjs.map +1 -0
- package/dist/functional/result/impl/result-unwrap-ok.d.mts +23 -0
- package/dist/functional/result/impl/result-unwrap-ok.d.mts.map +1 -0
- package/dist/functional/result/impl/result-unwrap-ok.mjs +11 -0
- package/dist/functional/result/impl/result-unwrap-ok.mjs.map +1 -0
- package/dist/functional/result/impl/result-unwrap-throw.d.mts +30 -0
- package/dist/functional/result/impl/result-unwrap-throw.d.mts.map +1 -0
- package/dist/functional/result/impl/result-unwrap-throw.mjs +41 -0
- package/dist/functional/result/impl/result-unwrap-throw.mjs.map +1 -0
- package/dist/functional/result/impl/result-zip.d.mts +29 -0
- package/dist/functional/result/impl/result-zip.d.mts.map +1 -0
- package/dist/functional/result/impl/result-zip.mjs +38 -0
- package/dist/functional/result/impl/result-zip.mjs.map +1 -0
- package/dist/functional/result/impl/tag.d.mts +5 -0
- package/dist/functional/result/impl/tag.d.mts.map +1 -0
- package/dist/functional/result/impl/tag.mjs +7 -0
- package/dist/functional/result/impl/tag.mjs.map +1 -0
- package/dist/functional/result/impl/types.d.mts +29 -0
- package/dist/functional/result/impl/types.d.mts.map +1 -0
- package/dist/functional/result/impl/types.mjs +2 -0
- package/dist/functional/result/impl/types.mjs.map +1 -0
- package/dist/functional/result/index.d.mts +2 -0
- package/dist/functional/result/index.d.mts.map +1 -0
- package/dist/functional/result/index.mjs +3 -0
- package/dist/functional/result/index.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/index.d.mts +29 -0
- package/dist/functional/ternary-result/impl/index.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/index.mjs +28 -0
- package/dist/functional/ternary-result/impl/index.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/tag.d.mts +7 -0
- package/dist/functional/ternary-result/impl/tag.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/tag.mjs +9 -0
- package/dist/functional/ternary-result/impl/tag.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-err.d.mts +14 -0
- package/dist/functional/ternary-result/impl/ternary-result-err.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-err.mjs +21 -0
- package/dist/functional/ternary-result/impl/ternary-result-err.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-expect-to-be.d.mts +18 -0
- package/dist/functional/ternary-result/impl/ternary-result-expect-to-be.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-expect-to-be.mjs +24 -0
- package/dist/functional/ternary-result/impl/ternary-result-expect-to-be.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-flat-map.d.mts +29 -0
- package/dist/functional/ternary-result/impl/ternary-result-flat-map.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-flat-map.mjs +41 -0
- package/dist/functional/ternary-result/impl/ternary-result-flat-map.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-fold.d.mts +27 -0
- package/dist/functional/ternary-result/impl/ternary-result-fold.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-fold.mjs +36 -0
- package/dist/functional/ternary-result/impl/ternary-result-fold.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-from-promise.d.mts +20 -0
- package/dist/functional/ternary-result/impl/ternary-result-from-promise.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-from-promise.mjs +24 -0
- package/dist/functional/ternary-result/impl/ternary-result-from-promise.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-from-throwable.d.mts +17 -0
- package/dist/functional/ternary-result/impl/ternary-result-from-throwable.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-from-throwable.mjs +33 -0
- package/dist/functional/ternary-result/impl/ternary-result-from-throwable.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-is-err.d.mts +20 -0
- package/dist/functional/ternary-result/impl/ternary-result-is-err.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-is-err.mjs +23 -0
- package/dist/functional/ternary-result/impl/ternary-result-is-err.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-is-ok.d.mts +21 -0
- package/dist/functional/ternary-result/impl/ternary-result-is-ok.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-is-ok.mjs +24 -0
- package/dist/functional/ternary-result/impl/ternary-result-is-ok.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.d.mts +17 -0
- package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.mjs +28 -0
- package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-is-warn.d.mts +21 -0
- package/dist/functional/ternary-result/impl/ternary-result-is-warn.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-is-warn.mjs +24 -0
- package/dist/functional/ternary-result/impl/ternary-result-is-warn.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-map-err.d.mts +23 -0
- package/dist/functional/ternary-result/impl/ternary-result-map-err.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-map-err.mjs +26 -0
- package/dist/functional/ternary-result/impl/ternary-result-map-err.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-map-warn.d.mts +21 -0
- package/dist/functional/ternary-result/impl/ternary-result-map-warn.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-map-warn.mjs +29 -0
- package/dist/functional/ternary-result/impl/ternary-result-map-warn.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-map.d.mts +29 -0
- package/dist/functional/ternary-result/impl/ternary-result-map.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-map.mjs +35 -0
- package/dist/functional/ternary-result/impl/ternary-result-map.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-ok.d.mts +16 -0
- package/dist/functional/ternary-result/impl/ternary-result-ok.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-ok.mjs +23 -0
- package/dist/functional/ternary-result/impl/ternary-result-ok.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-or-else.d.mts +26 -0
- package/dist/functional/ternary-result/impl/ternary-result-or-else.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-or-else.mjs +19 -0
- package/dist/functional/ternary-result/impl/ternary-result-or-else.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-to-optional.d.mts +16 -0
- package/dist/functional/ternary-result/impl/ternary-result-to-optional.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-to-optional.mjs +25 -0
- package/dist/functional/ternary-result/impl/ternary-result-to-optional.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-or.d.mts +22 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-or.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-or.mjs +17 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-or.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-throw.d.mts +18 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-throw.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-throw.mjs +36 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-throw.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err.d.mts +17 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err.mjs +22 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok-or.d.mts +22 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok-or.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok-or.mjs +17 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok-or.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok.d.mts +17 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok.mjs +13 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.d.mts +18 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.mjs +36 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-or.d.mts +19 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-or.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-or.mjs +17 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-or.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.d.mts +18 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.mjs +32 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn.d.mts +17 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn.mjs +22 -0
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-warn.d.mts +18 -0
- package/dist/functional/ternary-result/impl/ternary-result-warn.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-warn.mjs +26 -0
- package/dist/functional/ternary-result/impl/ternary-result-warn.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-zip.d.mts +26 -0
- package/dist/functional/ternary-result/impl/ternary-result-zip.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/ternary-result-zip.mjs +48 -0
- package/dist/functional/ternary-result/impl/ternary-result-zip.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/types.d.mts +25 -0
- package/dist/functional/ternary-result/impl/types.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/types.mjs +2 -0
- package/dist/functional/ternary-result/impl/types.mjs.map +1 -0
- package/dist/functional/ternary-result/impl/variant-name.d.mts +3 -0
- package/dist/functional/ternary-result/impl/variant-name.d.mts.map +1 -0
- package/dist/functional/ternary-result/impl/variant-name.mjs +16 -0
- package/dist/functional/ternary-result/impl/variant-name.mjs.map +1 -0
- package/dist/functional/ternary-result/index.d.mts +2 -0
- package/dist/functional/ternary-result/index.d.mts.map +1 -0
- package/dist/functional/ternary-result/index.mjs +3 -0
- package/dist/functional/ternary-result/index.mjs.map +1 -0
- package/dist/globals.d.mts +136 -0
- package/dist/index.d.mts +0 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +0 -4
- package/dist/index.mjs.map +1 -1
- package/dist/json/json.d.mts +0 -1
- package/dist/json/json.d.mts.map +1 -1
- package/dist/json/json.mjs +8 -8
- package/dist/json/json.mjs.map +1 -1
- package/dist/number/branded-types/finite-number.mjs +1 -1
- package/dist/number/branded-types/finite-number.mjs.map +1 -1
- package/dist/number/branded-types/int.mjs +1 -1
- package/dist/number/branded-types/int.mjs.map +1 -1
- package/dist/number/branded-types/int16.mjs +1 -1
- package/dist/number/branded-types/int16.mjs.map +1 -1
- package/dist/number/branded-types/int32.mjs +1 -1
- package/dist/number/branded-types/int32.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-finite-number.mjs +1 -1
- package/dist/number/branded-types/non-negative-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-int16.mjs +1 -1
- package/dist/number/branded-types/non-negative-int16.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-int32.mjs +1 -1
- package/dist/number/branded-types/non-negative-int32.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-finite-number.mjs +1 -1
- package/dist/number/branded-types/non-zero-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int.mjs +1 -1
- package/dist/number/branded-types/non-zero-int.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int16.mjs +1 -1
- package/dist/number/branded-types/non-zero-int16.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int32.mjs +1 -1
- package/dist/number/branded-types/non-zero-int32.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-safe-int.mjs +1 -1
- package/dist/number/branded-types/non-zero-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-uint16.mjs +1 -1
- package/dist/number/branded-types/non-zero-uint16.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-uint32.mjs +1 -1
- package/dist/number/branded-types/non-zero-uint32.mjs.map +1 -1
- package/dist/number/branded-types/positive-finite-number.mjs +1 -1
- package/dist/number/branded-types/positive-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/positive-int.mjs +1 -1
- package/dist/number/branded-types/positive-int.mjs.map +1 -1
- package/dist/number/branded-types/positive-int16.mjs +1 -1
- package/dist/number/branded-types/positive-int16.mjs.map +1 -1
- package/dist/number/branded-types/positive-int32.mjs +1 -1
- package/dist/number/branded-types/positive-int32.mjs.map +1 -1
- package/dist/number/branded-types/positive-safe-int.mjs +1 -1
- package/dist/number/branded-types/positive-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/positive-uint16.mjs +1 -1
- package/dist/number/branded-types/positive-uint16.mjs.map +1 -1
- package/dist/number/branded-types/positive-uint32.mjs +1 -1
- package/dist/number/branded-types/positive-uint32.mjs.map +1 -1
- package/dist/number/branded-types/safe-int.mjs +1 -1
- 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/safe-uint.mjs.map +1 -1
- package/dist/number/branded-types/uint.mjs +1 -1
- package/dist/number/branded-types/uint.mjs.map +1 -1
- package/dist/number/branded-types/uint16.mjs +1 -1
- package/dist/number/branded-types/uint16.mjs.map +1 -1
- package/dist/number/branded-types/uint32.mjs +1 -1
- package/dist/number/branded-types/uint32.mjs.map +1 -1
- package/dist/number/enum/int8.mjs +1 -1
- package/dist/number/enum/int8.mjs.map +1 -1
- package/dist/number/enum/uint8.mjs +1 -1
- package/dist/number/enum/uint8.mjs.map +1 -1
- package/dist/number/refined-number-utils.mjs +0 -2
- package/dist/number/refined-number-utils.mjs.map +1 -1
- package/dist/promise/promise.d.mts +0 -1
- package/dist/promise/promise.d.mts.map +1 -1
- package/dist/promise/promise.mjs +2 -3
- package/dist/promise/promise.mjs.map +1 -1
- package/package.json +27 -21
- package/src/array/impl/array-utils-element-access.mts +6 -6
- package/src/array/impl/array-utils-element-access.test.mts +6 -6
- package/src/array/impl/array-utils-reducing-value.mts +8 -24
- package/src/array/impl/array-utils-reducing-value.test.mts +8 -8
- package/src/array/impl/array-utils-slice-clamped.test.mts +1 -1
- package/src/entry-point.mts +1 -0
- package/src/functional/index.mts +3 -2
- package/src/functional/optional/impl/index.mts +17 -0
- package/src/functional/optional/impl/optional-expect-to-be.mts +65 -0
- package/src/functional/optional/impl/optional-filter.mts +71 -0
- package/src/functional/optional/impl/optional-flat-map.mts +67 -0
- package/src/functional/optional/impl/optional-from-nullable.mts +28 -0
- package/src/functional/optional/impl/optional-is-none.mts +25 -0
- package/src/functional/optional/impl/optional-is-optional.mts +27 -0
- package/src/functional/optional/impl/optional-is-some.mts +25 -0
- package/src/functional/optional/impl/optional-map.mts +69 -0
- package/src/functional/optional/impl/optional-none.mts +17 -0
- package/src/functional/optional/impl/optional-or-else.mts +73 -0
- package/src/functional/optional/impl/optional-some.mts +23 -0
- package/src/functional/optional/impl/optional-to-nullable.mts +31 -0
- package/src/functional/optional/impl/optional-unwrap-or.mts +64 -0
- package/src/functional/optional/impl/optional-unwrap-throw.mts +39 -0
- package/src/functional/optional/impl/optional-unwrap.mts +41 -0
- package/src/functional/optional/impl/optional-zip.mts +40 -0
- package/src/functional/optional/impl/tag.mts +6 -0
- package/src/functional/optional/impl/types.mts +28 -0
- package/src/functional/optional/index.mts +1 -0
- package/src/functional/optional.test.mts +7 -8
- package/src/functional/pipe.mts +5 -5
- package/src/functional/pipe.test.mts +1 -1
- package/src/functional/result/impl/index.mts +23 -0
- package/src/functional/result/impl/result-err.mts +29 -0
- package/src/functional/result/impl/result-expect-to-be.mts +63 -0
- package/src/functional/result/impl/result-flat-map.mts +79 -0
- package/src/functional/result/impl/result-fold.mts +95 -0
- package/src/functional/result/impl/result-from-promise.mts +39 -0
- package/src/functional/result/impl/result-from-throwable.mts +42 -0
- package/src/functional/result/impl/result-is-err.mts +23 -0
- package/src/functional/result/impl/result-is-ok.mts +23 -0
- package/src/functional/result/impl/result-is-result.mts +23 -0
- package/src/functional/result/impl/result-map-err.mts +72 -0
- package/src/functional/result/impl/result-map.mts +73 -0
- package/src/functional/result/impl/result-ok.mts +29 -0
- package/src/functional/result/impl/result-or-else.mts +63 -0
- package/src/functional/result/impl/result-swap.mts +28 -0
- package/src/functional/result/impl/result-to-optional.mts +34 -0
- package/src/functional/result/impl/result-unwrap-err-or.mts +60 -0
- package/src/functional/result/impl/result-unwrap-err-throw.mts +53 -0
- package/src/functional/result/impl/result-unwrap-err.mts +36 -0
- package/src/functional/result/impl/result-unwrap-ok-or.mts +60 -0
- package/src/functional/result/impl/result-unwrap-ok.mts +35 -0
- package/src/functional/result/impl/result-unwrap-throw.mts +43 -0
- package/src/functional/result/impl/result-zip.mts +39 -0
- package/src/functional/result/impl/tag.mts +6 -0
- package/src/functional/result/impl/types.mts +35 -0
- package/src/functional/result/index.mts +1 -0
- package/src/functional/result.test.mts +5 -4
- package/src/functional/ternary-result/impl/index.mts +28 -0
- package/src/functional/ternary-result/impl/tag.mts +11 -0
- package/src/functional/ternary-result/impl/ternary-result-err.mts +18 -0
- package/src/functional/ternary-result/impl/ternary-result-expect-to-be.mts +53 -0
- package/src/functional/ternary-result/impl/ternary-result-flat-map.mts +95 -0
- package/src/functional/ternary-result/impl/ternary-result-fold.mts +93 -0
- package/src/functional/ternary-result/impl/ternary-result-from-promise.mts +27 -0
- package/src/functional/ternary-result/impl/ternary-result-from-throwable.mts +31 -0
- package/src/functional/ternary-result/impl/ternary-result-is-err.mts +23 -0
- package/src/functional/ternary-result/impl/ternary-result-is-ok.mts +24 -0
- package/src/functional/ternary-result/impl/ternary-result-is-ternary-result.mts +27 -0
- package/src/functional/ternary-result/impl/ternary-result-is-warn.mts +24 -0
- package/src/functional/ternary-result/impl/ternary-result-map-err.mts +64 -0
- package/src/functional/ternary-result/impl/ternary-result-map-warn.mts +66 -0
- package/src/functional/ternary-result/impl/ternary-result-map.mts +81 -0
- package/src/functional/ternary-result/impl/ternary-result-ok.mts +20 -0
- package/src/functional/ternary-result/impl/ternary-result-or-else.mts +66 -0
- package/src/functional/ternary-result/impl/ternary-result-to-optional.mts +25 -0
- package/src/functional/ternary-result/impl/ternary-result-unwrap-err-or.mts +45 -0
- package/src/functional/ternary-result/impl/ternary-result-unwrap-err-throw.mts +40 -0
- package/src/functional/ternary-result/impl/ternary-result-unwrap-err.mts +22 -0
- package/src/functional/ternary-result/impl/ternary-result-unwrap-ok-or.mts +45 -0
- package/src/functional/ternary-result/impl/ternary-result-unwrap-ok.mts +32 -0
- package/src/functional/ternary-result/impl/ternary-result-unwrap-throw.mts +45 -0
- package/src/functional/ternary-result/impl/ternary-result-unwrap-warn-or.mts +42 -0
- package/src/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.mts +38 -0
- package/src/functional/ternary-result/impl/ternary-result-unwrap-warn.mts +22 -0
- package/src/functional/ternary-result/impl/ternary-result-warn.mts +23 -0
- package/src/functional/ternary-result/impl/ternary-result-zip.mts +53 -0
- package/src/functional/ternary-result/impl/types.mts +39 -0
- package/src/functional/ternary-result/impl/variant-name.mts +17 -0
- package/src/functional/ternary-result/index.mts +1 -0
- package/src/functional/ternary-result.test.mts +198 -0
- package/src/globals.d.mts +136 -0
- package/src/guard/is-non-empty-string.test.mts +1 -1
- package/src/guard/is-non-null-object.test.mts +3 -3
- package/src/guard/is-primitive.test.mts +3 -3
- package/src/guard/is-type.test.mts +3 -3
- package/src/index.mts +0 -1
- package/src/number/branded-types/finite-number.mts +7 -7
- package/src/number/branded-types/int.mts +7 -7
- package/src/number/branded-types/int16.mts +9 -9
- package/src/number/branded-types/int32.mts +9 -9
- package/src/number/branded-types/non-negative-finite-number.mts +8 -8
- package/src/number/branded-types/non-negative-int16.mts +9 -9
- package/src/number/branded-types/non-negative-int32.mts +9 -9
- package/src/number/branded-types/non-zero-finite-number.mts +7 -7
- package/src/number/branded-types/non-zero-int.mts +7 -7
- package/src/number/branded-types/non-zero-int16.mts +9 -9
- package/src/number/branded-types/non-zero-int32.mts +9 -9
- package/src/number/branded-types/non-zero-safe-int.mts +9 -9
- package/src/number/branded-types/non-zero-uint16.mts +9 -9
- package/src/number/branded-types/non-zero-uint32.mts +9 -9
- package/src/number/branded-types/positive-finite-number.mts +8 -8
- package/src/number/branded-types/positive-int.mts +8 -8
- package/src/number/branded-types/positive-int16.mts +9 -9
- package/src/number/branded-types/positive-int32.mts +9 -9
- package/src/number/branded-types/positive-safe-int.mts +9 -9
- package/src/number/branded-types/positive-uint16.mts +9 -9
- package/src/number/branded-types/positive-uint32.mts +9 -9
- package/src/number/branded-types/safe-int.mts +9 -9
- package/src/number/branded-types/safe-uint.mts +9 -9
- package/src/number/branded-types/uint.mts +8 -8
- package/src/number/branded-types/uint16.mts +9 -9
- package/src/number/branded-types/uint32.mts +9 -9
- package/src/number/enum/int8.mts +3 -3
- package/src/number/enum/int8.test.mts +9 -9
- package/src/number/enum/uint8.mts +3 -3
- package/src/number/enum/uint8.test.mts +8 -8
- package/dist/functional/optional.d.mts +0 -482
- package/dist/functional/optional.d.mts.map +0 -1
- package/dist/functional/optional.mjs +0 -328
- package/dist/functional/optional.mjs.map +0 -1
- package/dist/functional/result.d.mts +0 -712
- package/dist/functional/result.d.mts.map +0 -1
- package/dist/functional/result.mjs +0 -539
- package/dist/functional/result.mjs.map +0 -1
- package/src/functional/optional.mts +0 -713
- package/src/functional/result.mts +0 -1087
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { err } from './ternary-result-err.mjs';
|
|
2
|
+
import { isOk } from './ternary-result-is-ok.mjs';
|
|
3
|
+
import { isWarn } from './ternary-result-is-warn.mjs';
|
|
4
|
+
import { ok } from './ternary-result-ok.mjs';
|
|
5
|
+
import { warn } from './ternary-result-warn.mjs';
|
|
6
|
+
import { type UnwrapErr, type UnwrapOk, type UnwrapWarn } from './types.mjs';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Applies the provided mapper based on the current variant.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* const okFold = TernaryResult.fold(
|
|
15
|
+
* TernaryResult.ok(2),
|
|
16
|
+
* (value) => value * 2,
|
|
17
|
+
* (warn: string) => warn.length,
|
|
18
|
+
* (error: string) => error.toUpperCase(),
|
|
19
|
+
* );
|
|
20
|
+
* const warnFold = TernaryResult.fold(
|
|
21
|
+
* TernaryResult.warn(2, 'spike'),
|
|
22
|
+
* (value: number) => value,
|
|
23
|
+
* (warn: string) => warn.toUpperCase(),
|
|
24
|
+
* (error: string) => error,
|
|
25
|
+
* );
|
|
26
|
+
*
|
|
27
|
+
* assert.deepStrictEqual(okFold, TernaryResult.ok(4));
|
|
28
|
+
* assert.deepStrictEqual(warnFold, TernaryResult.warn(2, 'SPIKE'));
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export function fold<R extends UnknownTernaryResult, S2, W2, E2>(
|
|
32
|
+
result: R,
|
|
33
|
+
mapOk: (value: UnwrapOk<R>) => S2,
|
|
34
|
+
mapWarn: (value: UnwrapWarn<R>) => W2,
|
|
35
|
+
mapErr: (error: UnwrapErr<R>) => E2,
|
|
36
|
+
): TernaryResult<S2, E2, W2>;
|
|
37
|
+
|
|
38
|
+
// Curried version
|
|
39
|
+
export function fold<S, W, E, S2, W2, E2>(
|
|
40
|
+
mapOk: (value: S) => S2,
|
|
41
|
+
mapWarn: (value: W) => W2,
|
|
42
|
+
mapErr: (error: E) => E2,
|
|
43
|
+
): (result: TernaryResult<S, E, W>) => TernaryResult<S2, E2, W2>;
|
|
44
|
+
|
|
45
|
+
export function fold<R extends UnknownTernaryResult, S2, W2, E2>(
|
|
46
|
+
...args:
|
|
47
|
+
| readonly [
|
|
48
|
+
result: R,
|
|
49
|
+
mapOk: (value: UnwrapOk<R>) => S2,
|
|
50
|
+
mapWarn: (value: UnwrapWarn<R>) => W2,
|
|
51
|
+
mapErr: (error: UnwrapErr<R>) => E2,
|
|
52
|
+
]
|
|
53
|
+
| readonly [
|
|
54
|
+
mapOk: (value: UnwrapOk<R>) => S2,
|
|
55
|
+
mapWarn: (value: UnwrapWarn<R>) => W2,
|
|
56
|
+
mapErr: (error: UnwrapErr<R>) => E2,
|
|
57
|
+
]
|
|
58
|
+
): TernaryResult<S2, E2, W2> | ((result: R) => TernaryResult<S2, E2, W2>) {
|
|
59
|
+
switch (args.length) {
|
|
60
|
+
case 4: {
|
|
61
|
+
const [result, mapOk, mapWarn, mapErr] = args;
|
|
62
|
+
return foldImpl(result, mapOk, mapWarn, mapErr);
|
|
63
|
+
}
|
|
64
|
+
case 3: {
|
|
65
|
+
const [mapOk, mapWarn, mapErr] = args;
|
|
66
|
+
return (result: R) => foldImpl(result, mapOk, mapWarn, mapErr);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const foldImpl = <R extends UnknownTernaryResult, S2, W2, E2>(
|
|
72
|
+
result: R,
|
|
73
|
+
mapOk: (value: UnwrapOk<R>) => S2,
|
|
74
|
+
mapWarnFn: (warning: UnwrapWarn<R>) => W2,
|
|
75
|
+
mapErrFn: (error: UnwrapErr<R>) => E2,
|
|
76
|
+
): TernaryResult<S2, E2, W2> => {
|
|
77
|
+
if (isOk(result)) {
|
|
78
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
79
|
+
return ok(mapOk(result.value as UnwrapOk<R>));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (isWarn(result)) {
|
|
83
|
+
return warn(
|
|
84
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
85
|
+
mapOk(result.value as UnwrapOk<R>),
|
|
86
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
87
|
+
mapWarnFn(result.warning as UnwrapWarn<R>),
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
92
|
+
return err(mapErrFn(result.value as UnwrapErr<R>));
|
|
93
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { err } from './ternary-result-err.mjs';
|
|
2
|
+
import { ok } from './ternary-result-ok.mjs';
|
|
3
|
+
|
|
4
|
+
/** @internal Utility type extracting the resolved value from a Promise. */
|
|
5
|
+
type UnwrapPromise<P extends Promise<unknown>> =
|
|
6
|
+
P extends Promise<infer V> ? V : never;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Converts a Promise into a Promise that resolves to a `TernaryResult`.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* const resolved = await TernaryResult.fromPromise(Promise.resolve('ok'));
|
|
15
|
+
* const rejected = await TernaryResult.fromPromise(
|
|
16
|
+
* Promise.reject(new Error('fail')),
|
|
17
|
+
* );
|
|
18
|
+
*
|
|
19
|
+
* assert.deepStrictEqual(resolved, TernaryResult.ok('ok'));
|
|
20
|
+
* assert.ok(TernaryResult.isErr(rejected));
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export const fromPromise = <P extends Promise<unknown>>(
|
|
24
|
+
promise: P,
|
|
25
|
+
): Promise<TernaryResult<UnwrapPromise<P>, unknown, never>> =>
|
|
26
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
27
|
+
promise.then((value) => ok(value) as TernaryOk<UnwrapPromise<P>>).catch(err);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { unknownToString } from '../../../others/index.mjs';
|
|
2
|
+
import { err } from './ternary-result-err.mjs';
|
|
3
|
+
import { ok } from './ternary-result-ok.mjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Wraps a potentially-throwing function in a `TernaryResult`.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* const success = TernaryResult.fromThrowable(() => 1 + 1);
|
|
12
|
+
* const failure = TernaryResult.fromThrowable(() => {
|
|
13
|
+
* throw new Error('boom');
|
|
14
|
+
* });
|
|
15
|
+
*
|
|
16
|
+
* assert.deepStrictEqual(success, TernaryResult.ok(2));
|
|
17
|
+
* assert.ok(TernaryResult.isErr(failure));
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export const fromThrowable = <T,>(
|
|
21
|
+
fn: () => T,
|
|
22
|
+
): TernaryResult<T, Error, never> => {
|
|
23
|
+
try {
|
|
24
|
+
return ok(fn());
|
|
25
|
+
} catch (error) {
|
|
26
|
+
if (Error.isError(error)) {
|
|
27
|
+
return err(error);
|
|
28
|
+
}
|
|
29
|
+
return err(new Error(unknownToString(error)));
|
|
30
|
+
}
|
|
31
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ErrTypeTagName } from './tag.mjs';
|
|
2
|
+
import { type NarrowToErr } from './types.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Type guard for the Err variant.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* const maybeErr = TernaryResult.err('boom') as TernaryResult<
|
|
11
|
+
* number,
|
|
12
|
+
* string,
|
|
13
|
+
* string
|
|
14
|
+
* >;
|
|
15
|
+
*
|
|
16
|
+
* if (TernaryResult.isErr(maybeErr)) {
|
|
17
|
+
* assert.strictEqual(maybeErr.value, 'boom');
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export const isErr = <R extends UnknownTernaryResult>(
|
|
22
|
+
result: R,
|
|
23
|
+
): result is NarrowToErr<R> => result.$$tag === ErrTypeTagName;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { OkTypeTagName } from './tag.mjs';
|
|
2
|
+
import { type NarrowToOk } from './types.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Type guard for the Ok variant.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* const maybeNumber = TernaryResult.ok(42) as TernaryResult<
|
|
11
|
+
* number,
|
|
12
|
+
* string,
|
|
13
|
+
* string
|
|
14
|
+
* >;
|
|
15
|
+
*
|
|
16
|
+
* if (TernaryResult.isOk(maybeNumber)) {
|
|
17
|
+
* const value: number = maybeNumber.value;
|
|
18
|
+
* assert.strictEqual(value, 42);
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export const isOk = <R extends UnknownTernaryResult>(
|
|
23
|
+
result: R,
|
|
24
|
+
): result is NarrowToOk<R> => result.$$tag === OkTypeTagName;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { hasKey, isRecord } from '../../../guard/index.mjs';
|
|
2
|
+
import { ErrTypeTagName, OkTypeTagName, WarnTypeTagName } from './tag.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Checks whether the provided value is a {@link TernaryResult}.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* const okValue = TernaryResult.ok('done');
|
|
11
|
+
* const warnValue = TernaryResult.warn('done', 'retry later');
|
|
12
|
+
* const notResult = { $$tag: 'ts-data-forge::Result.ok' };
|
|
13
|
+
*
|
|
14
|
+
* assert.ok(TernaryResult.isTernaryResult(okValue));
|
|
15
|
+
* assert.ok(TernaryResult.isTernaryResult(warnValue));
|
|
16
|
+
* assert.ok(!TernaryResult.isTernaryResult(notResult));
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export const isTernaryResult = (
|
|
20
|
+
maybeResult: unknown,
|
|
21
|
+
): maybeResult is UnknownTernaryResult =>
|
|
22
|
+
isRecord(maybeResult) &&
|
|
23
|
+
hasKey(maybeResult, '$$tag') &&
|
|
24
|
+
hasKey(maybeResult, 'value') &&
|
|
25
|
+
((maybeResult.$$tag === WarnTypeTagName && hasKey(maybeResult, 'warning')) ||
|
|
26
|
+
maybeResult.$$tag === OkTypeTagName ||
|
|
27
|
+
maybeResult.$$tag === ErrTypeTagName);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { WarnTypeTagName } from './tag.mjs';
|
|
2
|
+
import { type NarrowToWarn } from './types.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Type guard for the Warn variant.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* const maybeWarn = TernaryResult.warn('value', 'check logs') as TernaryResult<
|
|
11
|
+
* string,
|
|
12
|
+
* string,
|
|
13
|
+
* string
|
|
14
|
+
* >;
|
|
15
|
+
*
|
|
16
|
+
* if (TernaryResult.isWarn(maybeWarn)) {
|
|
17
|
+
* assert.strictEqual(maybeWarn.value, 'value');
|
|
18
|
+
* assert.strictEqual(maybeWarn.warning, 'check logs');
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export const isWarn = <R extends UnknownTernaryResult>(
|
|
23
|
+
result: R,
|
|
24
|
+
): result is NarrowToWarn<R> => result.$$tag === WarnTypeTagName;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { err } from './ternary-result-err.mjs';
|
|
2
|
+
import { isErr } from './ternary-result-is-err.mjs';
|
|
3
|
+
import { type UnwrapErr, type UnwrapOk, type UnwrapWarn } from './types.mjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Maps the Err variant while leaving Ok/Warn untouched.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* const errValue = TernaryResult.err('boom');
|
|
12
|
+
* const mappedErr = TernaryResult.mapErr(errValue, (error) =>
|
|
13
|
+
* error.toUpperCase(),
|
|
14
|
+
* );
|
|
15
|
+
* const warnPassthrough = TernaryResult.mapErr(
|
|
16
|
+
* TernaryResult.warn(2, 'slow'),
|
|
17
|
+
* (error: string) => `${error}!`,
|
|
18
|
+
* );
|
|
19
|
+
*
|
|
20
|
+
* assert.deepStrictEqual(mappedErr, TernaryResult.err('BOOM'));
|
|
21
|
+
* assert.deepStrictEqual(warnPassthrough, TernaryResult.warn(2, 'slow'));
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export function mapErr<R extends UnknownTernaryResult, E2>(
|
|
25
|
+
result: R,
|
|
26
|
+
mapFn: (error: UnwrapErr<R>) => E2,
|
|
27
|
+
): TernaryResult<UnwrapOk<R>, E2, UnwrapWarn<R>>;
|
|
28
|
+
|
|
29
|
+
// Curried version
|
|
30
|
+
export function mapErr<E, E2>(
|
|
31
|
+
mapFn: (error: E) => E2,
|
|
32
|
+
): <S, W>(result: TernaryResult<S, E, W>) => TernaryResult<S, E2, W>;
|
|
33
|
+
|
|
34
|
+
export function mapErr<R extends UnknownTernaryResult, E2>(
|
|
35
|
+
...args:
|
|
36
|
+
| readonly [result: R, mapFn: (error: UnwrapErr<R>) => E2]
|
|
37
|
+
| readonly [mapFn: (error: UnwrapErr<R>) => E2]
|
|
38
|
+
):
|
|
39
|
+
| TernaryResult<UnwrapOk<R>, E2, UnwrapWarn<R>>
|
|
40
|
+
| ((result: R) => TernaryResult<UnwrapOk<R>, E2, UnwrapWarn<R>>) {
|
|
41
|
+
switch (args.length) {
|
|
42
|
+
case 2: {
|
|
43
|
+
const [result, mapFn] = args;
|
|
44
|
+
return mapErrImpl(result, mapFn);
|
|
45
|
+
}
|
|
46
|
+
case 1: {
|
|
47
|
+
const [mapFn] = args;
|
|
48
|
+
return (result: R) => mapErrImpl(result, mapFn);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const mapErrImpl = <R extends UnknownTernaryResult, E2>(
|
|
54
|
+
result: R,
|
|
55
|
+
mapFn: (error: UnwrapErr<R>) => E2,
|
|
56
|
+
): TernaryResult<UnwrapOk<R>, E2, UnwrapWarn<R>> => {
|
|
57
|
+
if (isErr(result)) {
|
|
58
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
59
|
+
return err(mapFn(result.value as UnwrapErr<R>));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
63
|
+
return result as TernaryResult<UnwrapOk<R>, E2, UnwrapWarn<R>>;
|
|
64
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { isWarn } from './ternary-result-is-warn.mjs';
|
|
2
|
+
import { warn } from './ternary-result-warn.mjs';
|
|
3
|
+
import { type UnwrapErr, type UnwrapOk, type UnwrapWarn } from './types.mjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Maps the Warn variant while leaving Ok/Err untouched.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* const warnValue = TernaryResult.warn(2, 'slow');
|
|
12
|
+
* const mappedWarn = TernaryResult.mapWarn(warnValue, (warning) => `${warning}!`);
|
|
13
|
+
* const okPassthrough = TernaryResult.mapWarn(
|
|
14
|
+
* TernaryResult.ok(3),
|
|
15
|
+
* (value: number) => value * 2,
|
|
16
|
+
* );
|
|
17
|
+
*
|
|
18
|
+
* assert.deepStrictEqual(mappedWarn, TernaryResult.warn(2, 'slow!'));
|
|
19
|
+
* assert.deepStrictEqual(okPassthrough, TernaryResult.ok(3));
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export function mapWarn<R extends UnknownTernaryResult, W2>(
|
|
23
|
+
result: R,
|
|
24
|
+
mapFn: (warning: UnwrapWarn<R>) => W2,
|
|
25
|
+
): TernaryResult<UnwrapOk<R>, UnwrapErr<R>, W2>;
|
|
26
|
+
|
|
27
|
+
// Curried version
|
|
28
|
+
export function mapWarn<W, W2>(
|
|
29
|
+
mapFn: (warning: W) => W2,
|
|
30
|
+
): <S, E>(result: TernaryResult<S, E, W>) => TernaryResult<S, E, W2>;
|
|
31
|
+
|
|
32
|
+
export function mapWarn<R extends UnknownTernaryResult, W2>(
|
|
33
|
+
...args:
|
|
34
|
+
| readonly [result: R, mapFn: (warning: UnwrapWarn<R>) => W2]
|
|
35
|
+
| readonly [mapFn: (warning: UnwrapWarn<R>) => W2]
|
|
36
|
+
):
|
|
37
|
+
| TernaryResult<UnwrapOk<R>, UnwrapErr<R>, W2>
|
|
38
|
+
| ((result: R) => TernaryResult<UnwrapOk<R>, UnwrapErr<R>, W2>) {
|
|
39
|
+
switch (args.length) {
|
|
40
|
+
case 2: {
|
|
41
|
+
const [result, mapFn] = args;
|
|
42
|
+
return mapWarnImpl(result, mapFn);
|
|
43
|
+
}
|
|
44
|
+
case 1: {
|
|
45
|
+
const [mapFn] = args;
|
|
46
|
+
return (result: R) => mapWarnImpl(result, mapFn);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const mapWarnImpl = <R extends UnknownTernaryResult, W2>(
|
|
52
|
+
result: R,
|
|
53
|
+
mapFn: (warning: UnwrapWarn<R>) => W2,
|
|
54
|
+
): TernaryResult<UnwrapOk<R>, UnwrapErr<R>, W2> => {
|
|
55
|
+
if (isWarn(result)) {
|
|
56
|
+
return warn(
|
|
57
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
58
|
+
result.value as UnwrapOk<R>,
|
|
59
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
60
|
+
mapFn(result.warning as UnwrapWarn<R>),
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
65
|
+
return result as TernaryResult<UnwrapOk<R>, UnwrapErr<R>, W2>;
|
|
66
|
+
};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { isOk } from './ternary-result-is-ok.mjs';
|
|
2
|
+
import { isWarn } from './ternary-result-is-warn.mjs';
|
|
3
|
+
import { ok } from './ternary-result-ok.mjs';
|
|
4
|
+
import { warn } from './ternary-result-warn.mjs';
|
|
5
|
+
import { type UnwrapErr, type UnwrapOk, type UnwrapWarn } from './types.mjs';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Maps the Ok variant while leaving Warn/Err untouched.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* const okNumber = TernaryResult.ok(5);
|
|
14
|
+
* const warnValue = TernaryResult.warn(5, 'slow');
|
|
15
|
+
* const errValue = TernaryResult.err('bad');
|
|
16
|
+
*
|
|
17
|
+
* const doubled = TernaryResult.map(okNumber, (value) => value * 2);
|
|
18
|
+
* const warnPassthrough = TernaryResult.map(
|
|
19
|
+
* warnValue,
|
|
20
|
+
* (value: number) => value * 2,
|
|
21
|
+
* );
|
|
22
|
+
* const errPassthrough = TernaryResult.map(
|
|
23
|
+
* errValue,
|
|
24
|
+
* (value: number) => value * 2,
|
|
25
|
+
* );
|
|
26
|
+
*
|
|
27
|
+
* assert.deepStrictEqual(doubled, TernaryResult.ok(10));
|
|
28
|
+
* assert.deepStrictEqual(warnPassthrough, TernaryResult.warn(10, 'slow'));
|
|
29
|
+
* assert.deepStrictEqual(errPassthrough, errValue);
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export function map<R extends UnknownTernaryResult, S2>(
|
|
33
|
+
result: R,
|
|
34
|
+
mapFn: (value: UnwrapOk<R>) => S2,
|
|
35
|
+
): TernaryResult<S2, UnwrapErr<R>, UnwrapWarn<R>>;
|
|
36
|
+
|
|
37
|
+
// Curried version
|
|
38
|
+
export function map<S, S2>(
|
|
39
|
+
mapFn: (value: S) => S2,
|
|
40
|
+
): <W, E>(result: TernaryResult<S, E, W>) => TernaryResult<S2, E, W>;
|
|
41
|
+
|
|
42
|
+
export function map<R extends UnknownTernaryResult, S2>(
|
|
43
|
+
...args:
|
|
44
|
+
| readonly [result: R, mapFn: (value: UnwrapOk<R>) => S2]
|
|
45
|
+
| readonly [mapFn: (value: UnwrapOk<R>) => S2]
|
|
46
|
+
):
|
|
47
|
+
| TernaryResult<S2, UnwrapErr<R>, UnwrapWarn<R>>
|
|
48
|
+
| ((result: R) => TernaryResult<S2, UnwrapErr<R>, UnwrapWarn<R>>) {
|
|
49
|
+
switch (args.length) {
|
|
50
|
+
case 2: {
|
|
51
|
+
const [result, mapFn] = args;
|
|
52
|
+
return mapImpl(result, mapFn);
|
|
53
|
+
}
|
|
54
|
+
case 1: {
|
|
55
|
+
const [mapFn] = args;
|
|
56
|
+
return (result: R) => mapImpl(result, mapFn);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const mapImpl = <R extends UnknownTernaryResult, S2>(
|
|
62
|
+
result: R,
|
|
63
|
+
mapFn: (value: UnwrapOk<R>) => S2,
|
|
64
|
+
): TernaryResult<S2, UnwrapErr<R>, UnwrapWarn<R>> => {
|
|
65
|
+
if (isOk(result)) {
|
|
66
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
67
|
+
return ok(mapFn(result.value as UnwrapOk<R>));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (isWarn(result)) {
|
|
71
|
+
return warn(
|
|
72
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
73
|
+
mapFn(result.value as UnwrapOk<R>),
|
|
74
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
75
|
+
result.warning as UnwrapWarn<R>,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
80
|
+
return result as TernaryErr<UnwrapErr<R>>;
|
|
81
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { OkTypeTagName } from './tag.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a `TernaryResult.Ok` containing the provided value.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* const success = TernaryResult.ok({ id: 1 });
|
|
10
|
+
*
|
|
11
|
+
* assert.deepStrictEqual(success, {
|
|
12
|
+
* $$tag: 'ts-data-forge::Result.ok',
|
|
13
|
+
* value: { id: 1 },
|
|
14
|
+
* });
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export const ok = <S,>(value: S): TernaryOk<S> => ({
|
|
18
|
+
$$tag: OkTypeTagName,
|
|
19
|
+
value,
|
|
20
|
+
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { isOk } from './ternary-result-is-ok.mjs';
|
|
2
|
+
import { isWarn } from './ternary-result-is-warn.mjs';
|
|
3
|
+
import { type NarrowToOk, type NarrowToWarn } from './types.mjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns the original result if it is Ok, otherwise the provided fallback.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* const fallback = TernaryResult.ok('fallback');
|
|
12
|
+
*
|
|
13
|
+
* assert.deepStrictEqual(
|
|
14
|
+
* TernaryResult.orElse(TernaryResult.ok('value'), fallback),
|
|
15
|
+
* TernaryResult.ok('value'),
|
|
16
|
+
* );
|
|
17
|
+
* assert.deepStrictEqual(
|
|
18
|
+
* TernaryResult.orElse(TernaryResult.warn('value', 'warn'), fallback),
|
|
19
|
+
* TernaryResult.warn('value', 'warn'),
|
|
20
|
+
* );
|
|
21
|
+
* assert.deepStrictEqual(
|
|
22
|
+
* TernaryResult.orElse(TernaryResult.err('err'), fallback),
|
|
23
|
+
* fallback,
|
|
24
|
+
* );
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export function orElse<
|
|
28
|
+
R extends UnknownTernaryResult,
|
|
29
|
+
R2 extends UnknownTernaryResult,
|
|
30
|
+
>(result: R, alternative: R2): NarrowToOk<R> | NarrowToWarn<R> | R2;
|
|
31
|
+
|
|
32
|
+
// Curried version
|
|
33
|
+
export function orElse<S, W, E, S2, W2, E2>(
|
|
34
|
+
alternative: TernaryResult<S2, E2, W2>,
|
|
35
|
+
): (
|
|
36
|
+
result: TernaryResult<S, E, W>,
|
|
37
|
+
) => TernaryResult<S, E, W> | TernaryResult<S2, E2, W2>;
|
|
38
|
+
|
|
39
|
+
export function orElse<
|
|
40
|
+
R extends UnknownTernaryResult,
|
|
41
|
+
R2 extends UnknownTernaryResult,
|
|
42
|
+
>(
|
|
43
|
+
...args: readonly [result: R, alternative: R2] | readonly [alternative: R2]
|
|
44
|
+
):
|
|
45
|
+
| (NarrowToOk<R> | NarrowToWarn<R> | R2)
|
|
46
|
+
| ((result: R) => NarrowToOk<R> | NarrowToWarn<R> | R2) {
|
|
47
|
+
switch (args.length) {
|
|
48
|
+
case 2: {
|
|
49
|
+
const [result, alternative] = args;
|
|
50
|
+
return orElseImpl(result, alternative);
|
|
51
|
+
}
|
|
52
|
+
case 1: {
|
|
53
|
+
const [alternative] = args;
|
|
54
|
+
return (result: R) => orElseImpl(result, alternative);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const orElseImpl = <
|
|
60
|
+
R extends UnknownTernaryResult,
|
|
61
|
+
R2 extends UnknownTernaryResult,
|
|
62
|
+
>(
|
|
63
|
+
result: R,
|
|
64
|
+
alternative: R2,
|
|
65
|
+
): NarrowToOk<R> | NarrowToWarn<R> | R2 =>
|
|
66
|
+
isOk(result) || isWarn(result) ? result : alternative;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Optional } from '../../optional/index.mjs';
|
|
2
|
+
import { isOk } from './ternary-result-is-ok.mjs';
|
|
3
|
+
import { isWarn } from './ternary-result-is-warn.mjs';
|
|
4
|
+
import { type UnwrapOk } from './types.mjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Converts a `TernaryResult` into an `Optional` by keeping only Ok values.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* const okValue = TernaryResult.ok(7);
|
|
13
|
+
* const warnValue = TernaryResult.warn(7, 'warn');
|
|
14
|
+
*
|
|
15
|
+
* assert.deepStrictEqual(TernaryResult.toOptional(okValue), Optional.some(7));
|
|
16
|
+
* assert.deepStrictEqual(TernaryResult.toOptional(warnValue), Optional.some(7));
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export const toOptional = <R extends UnknownTernaryResult>(
|
|
20
|
+
result: R,
|
|
21
|
+
): Optional<UnwrapOk<R>> =>
|
|
22
|
+
isOk(result) || isWarn(result)
|
|
23
|
+
? // eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
24
|
+
Optional.some(result.value as UnwrapOk<R>)
|
|
25
|
+
: Optional.none;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { unwrapErr } from './ternary-result-unwrap-err.mjs';
|
|
2
|
+
import { type UnwrapErr } from './types.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Returns the Err value or the provided default.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* const okValue = TernaryResult.ok('value');
|
|
11
|
+
*
|
|
12
|
+
* assert.strictEqual(TernaryResult.unwrapErrOr(okValue, 'default'), 'default');
|
|
13
|
+
* const unwrapErr = TernaryResult.unwrapErrOr('fallback error');
|
|
14
|
+
*
|
|
15
|
+
* assert.strictEqual(unwrapErr(TernaryResult.err('boom')), 'boom');
|
|
16
|
+
* assert.strictEqual(
|
|
17
|
+
* unwrapErr(TernaryResult.warn('value', 'warn')),
|
|
18
|
+
* 'fallback error',
|
|
19
|
+
* );
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export function unwrapErrOr<R extends UnknownTernaryResult, D>(
|
|
23
|
+
result: R,
|
|
24
|
+
defaultValue: D,
|
|
25
|
+
): D | UnwrapErr<R>;
|
|
26
|
+
|
|
27
|
+
// Curried version
|
|
28
|
+
export function unwrapErrOr<E, D>(
|
|
29
|
+
defaultValue: D,
|
|
30
|
+
): <S, W>(result: TernaryResult<S, E, W>) => D | E;
|
|
31
|
+
|
|
32
|
+
export function unwrapErrOr<R extends UnknownTernaryResult, D>(
|
|
33
|
+
...args: readonly [result: R, defaultValue: D] | readonly [defaultValue: D]
|
|
34
|
+
): D | UnwrapErr<R> | ((result: R) => D | UnwrapErr<R>) {
|
|
35
|
+
switch (args.length) {
|
|
36
|
+
case 2: {
|
|
37
|
+
const [result, defaultValue] = args;
|
|
38
|
+
return unwrapErr(result) ?? defaultValue;
|
|
39
|
+
}
|
|
40
|
+
case 1: {
|
|
41
|
+
const [defaultValue] = args;
|
|
42
|
+
return (result: R) => unwrapErr(result) ?? defaultValue;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { unknownToString } from '../../../others/index.mjs';
|
|
2
|
+
import { isErr } from './ternary-result-is-err.mjs';
|
|
3
|
+
import { isWarn } from './ternary-result-is-warn.mjs';
|
|
4
|
+
import { type UnwrapErr, type UnwrapOk, type UnwrapWarn } from './types.mjs';
|
|
5
|
+
import { variantName } from './variant-name.mjs';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Returns the Err value or throws if the result is not Err.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* const errValue = TernaryResult.err('boom');
|
|
14
|
+
*
|
|
15
|
+
* assert.strictEqual(TernaryResult.unwrapErrThrow(errValue), 'boom');
|
|
16
|
+
* assert.throws(
|
|
17
|
+
* () => TernaryResult.unwrapErrThrow(TernaryResult.ok('value')),
|
|
18
|
+
* /Expected Err/u,
|
|
19
|
+
* );
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export const unwrapErrThrow = <R extends UnknownTernaryResult>(
|
|
23
|
+
result: R,
|
|
24
|
+
toStr: (value: UnwrapOk<R> | UnwrapWarn<R>) => string = unknownToString,
|
|
25
|
+
): UnwrapErr<R> => {
|
|
26
|
+
if (isErr(result)) {
|
|
27
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
28
|
+
return result.value as UnwrapErr<R>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const variant = variantName(result.$$tag);
|
|
32
|
+
|
|
33
|
+
const payload = isWarn(result)
|
|
34
|
+
? // eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
35
|
+
(result.warning as UnwrapWarn<R> as UnwrapOk<R> | UnwrapWarn<R>)
|
|
36
|
+
: // eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
37
|
+
(result.value as UnwrapOk<R> as UnwrapOk<R> | UnwrapWarn<R>);
|
|
38
|
+
|
|
39
|
+
throw new Error(`Expected Err but got ${variant}: ${toStr(payload)}`);
|
|
40
|
+
};
|