ts-data-forge 14.6.4 → 14.7.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/README.md +2 -1
- package/dist/expect-type.d.mts +2 -70
- package/dist/expect-type.d.mts.map +1 -1
- package/dist/expect-type.mjs +1 -68
- package/dist/expect-type.mjs.map +1 -1
- package/dist/functional/index.d.mts +11 -6
- package/dist/functional/index.d.mts.map +1 -1
- package/dist/functional/index.mjs +10 -6
- package/dist/functional/index.mjs.map +1 -1
- package/dist/guard/has-key.d.mts +2 -51
- package/dist/guard/has-key.d.mts.map +1 -1
- package/dist/guard/has-key.mjs +1 -41
- package/dist/guard/has-key.mjs.map +1 -1
- package/dist/guard/is-non-null-object.d.mts +2 -34
- package/dist/guard/is-non-null-object.d.mts.map +1 -1
- package/dist/guard/is-non-null-object.mjs +1 -34
- package/dist/guard/is-non-null-object.mjs.map +1 -1
- package/dist/guard/is-record.d.mts +2 -89
- package/dist/guard/is-record.d.mts.map +1 -1
- package/dist/guard/is-record.mjs +1 -88
- package/dist/guard/is-record.mjs.map +1 -1
- package/dist/guard/key-is-in.d.mts +2 -42
- package/dist/guard/key-is-in.d.mts.map +1 -1
- package/dist/guard/key-is-in.mjs +1 -40
- package/dist/guard/key-is-in.mjs.map +1 -1
- package/dist/number/num.d.mts +9 -1
- package/dist/number/num.d.mts.map +1 -1
- package/dist/number/num.mjs +24 -16
- package/dist/number/num.mjs.map +1 -1
- package/dist/others/index.d.mts +1 -0
- package/dist/others/index.d.mts.map +1 -1
- package/dist/others/index.mjs +1 -0
- package/dist/others/index.mjs.map +1 -1
- package/dist/others/panic-error.d.mts +9 -0
- package/dist/others/panic-error.d.mts.map +1 -0
- package/dist/others/panic-error.mjs +9 -0
- package/dist/others/panic-error.mjs.map +1 -0
- package/dist/others/unknown-to-string.d.mts +2 -37
- package/dist/others/unknown-to-string.d.mts.map +1 -1
- package/dist/others/unknown-to-string.mjs +1 -92
- package/dist/others/unknown-to-string.mjs.map +1 -1
- package/dist/types.d.mts +1 -41
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs +0 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +5 -5
- package/src/expect-type.mts +2 -78
- package/src/functional/index.mts +31 -6
- package/src/guard/has-key.mts +2 -72
- package/src/guard/is-non-null-object.mts +2 -36
- package/src/guard/is-record.mts +2 -95
- package/src/guard/key-is-in.mts +2 -49
- package/src/number/num.mts +36 -28
- package/src/others/index.mts +1 -0
- package/src/others/panic-error.mts +8 -0
- package/src/others/unknown-to-string.mts +2 -111
- package/src/types.mts +4 -54
- package/dist/functional/async-result/async-result.d.mts +0 -16
- package/dist/functional/async-result/async-result.d.mts.map +0 -1
- package/dist/functional/async-result/async-result.mjs +0 -2
- package/dist/functional/async-result/async-result.mjs.map +0 -1
- package/dist/functional/async-result/impl/async-result-flat-map.d.mts +0 -39
- package/dist/functional/async-result/impl/async-result-flat-map.d.mts.map +0 -1
- package/dist/functional/async-result/impl/async-result-flat-map.mjs +0 -23
- package/dist/functional/async-result/impl/async-result-flat-map.mjs.map +0 -1
- package/dist/functional/async-result/impl/async-result-from-promise.d.mts +0 -47
- package/dist/functional/async-result/impl/async-result-from-promise.d.mts.map +0 -1
- package/dist/functional/async-result/impl/async-result-from-promise.mjs +0 -10
- package/dist/functional/async-result/impl/async-result-from-promise.mjs.map +0 -1
- package/dist/functional/async-result/impl/async-result-from-throwable.d.mts +0 -53
- package/dist/functional/async-result/impl/async-result-from-throwable.d.mts.map +0 -1
- package/dist/functional/async-result/impl/async-result-from-throwable.mjs +0 -15
- package/dist/functional/async-result/impl/async-result-from-throwable.mjs.map +0 -1
- package/dist/functional/async-result/impl/async-result-map-err.d.mts +0 -35
- package/dist/functional/async-result/impl/async-result-map-err.d.mts.map +0 -1
- package/dist/functional/async-result/impl/async-result-map-err.mjs +0 -20
- package/dist/functional/async-result/impl/async-result-map-err.mjs.map +0 -1
- package/dist/functional/async-result/impl/async-result-map.d.mts +0 -45
- package/dist/functional/async-result/impl/async-result-map.d.mts.map +0 -1
- package/dist/functional/async-result/impl/async-result-map.mjs +0 -20
- package/dist/functional/async-result/impl/async-result-map.mjs.map +0 -1
- package/dist/functional/async-result/impl/async-result-unwrap-or.d.mts +0 -34
- package/dist/functional/async-result/impl/async-result-unwrap-or.d.mts.map +0 -1
- package/dist/functional/async-result/impl/async-result-unwrap-or.mjs +0 -37
- package/dist/functional/async-result/impl/async-result-unwrap-or.mjs.map +0 -1
- package/dist/functional/async-result/impl/index.d.mts +0 -8
- package/dist/functional/async-result/impl/index.d.mts.map +0 -1
- package/dist/functional/async-result/impl/index.mjs +0 -8
- package/dist/functional/async-result/impl/index.mjs.map +0 -1
- package/dist/functional/async-result/impl/types.d.mts +0 -45
- package/dist/functional/async-result/impl/types.d.mts.map +0 -1
- package/dist/functional/async-result/impl/types.mjs +0 -2
- package/dist/functional/async-result/impl/types.mjs.map +0 -1
- package/dist/functional/async-result/index.d.mts +0 -2
- package/dist/functional/async-result/index.d.mts.map +0 -1
- package/dist/functional/async-result/index.mjs +0 -2
- package/dist/functional/async-result/index.mjs.map +0 -1
- package/dist/functional/match.d.mts +0 -80
- package/dist/functional/match.d.mts.map +0 -1
- package/dist/functional/match.mjs +0 -31
- package/dist/functional/match.mjs.map +0 -1
- package/dist/functional/optional/impl/index.d.mts +0 -19
- package/dist/functional/optional/impl/index.d.mts.map +0 -1
- package/dist/functional/optional/impl/index.mjs +0 -19
- package/dist/functional/optional/impl/index.mjs.map +0 -1
- package/dist/functional/optional/impl/optional-expect-to-be.d.mts +0 -37
- package/dist/functional/optional/impl/optional-expect-to-be.d.mts.map +0 -1
- package/dist/functional/optional/impl/optional-expect-to-be.mjs +0 -22
- package/dist/functional/optional/impl/optional-expect-to-be.mjs.map +0 -1
- package/dist/functional/optional/impl/optional-filter.d.mts +0 -33
- package/dist/functional/optional/impl/optional-filter.d.mts.map +0 -1
- package/dist/functional/optional/impl/optional-filter.mjs +0 -25
- package/dist/functional/optional/impl/optional-filter.mjs.map +0 -1
- package/dist/functional/optional/impl/optional-flat-map.d.mts +0 -39
- package/dist/functional/optional/impl/optional-flat-map.d.mts.map +0 -1
- package/dist/functional/optional/impl/optional-flat-map.mjs +0 -18
- package/dist/functional/optional/impl/optional-flat-map.mjs.map +0 -1
- package/dist/functional/optional/impl/optional-from-nullable.d.mts +0 -27
- package/dist/functional/optional/impl/optional-from-nullable.d.mts.map +0 -1
- package/dist/functional/optional/impl/optional-from-nullable.mjs +0 -28
- package/dist/functional/optional/impl/optional-from-nullable.mjs.map +0 -1
- package/dist/functional/optional/impl/optional-is-none.d.mts +0 -23
- package/dist/functional/optional/impl/optional-is-none.d.mts.map +0 -1
- package/dist/functional/optional/impl/optional-is-none.mjs +0 -22
- package/dist/functional/optional/impl/optional-is-none.mjs.map +0 -1
- package/dist/functional/optional/impl/optional-is-optional.d.mts +0 -21
- package/dist/functional/optional/impl/optional-is-optional.d.mts.map +0 -1
- package/dist/functional/optional/impl/optional-is-optional.mjs +0 -26
- package/dist/functional/optional/impl/optional-is-optional.mjs.map +0 -1
- package/dist/functional/optional/impl/optional-is-some.d.mts +0 -24
- package/dist/functional/optional/impl/optional-is-some.d.mts.map +0 -1
- package/dist/functional/optional/impl/optional-is-some.mjs +0 -23
- package/dist/functional/optional/impl/optional-is-some.mjs.map +0 -1
- package/dist/functional/optional/impl/optional-map.d.mts +0 -34
- package/dist/functional/optional/impl/optional-map.d.mts.map +0 -1
- package/dist/functional/optional/impl/optional-map.mjs +0 -22
- package/dist/functional/optional/impl/optional-map.mjs.map +0 -1
- package/dist/functional/optional/impl/optional-match.d.mts +0 -48
- package/dist/functional/optional/impl/optional-match.d.mts.map +0 -1
- package/dist/functional/optional/impl/optional-match.mjs +0 -20
- package/dist/functional/optional/impl/optional-match.mjs.map +0 -1
- package/dist/functional/optional/impl/optional-none.d.mts +0 -19
- package/dist/functional/optional/impl/optional-none.d.mts.map +0 -1
- package/dist/functional/optional/impl/optional-none.mjs +0 -19
- package/dist/functional/optional/impl/optional-none.mjs.map +0 -1
- package/dist/functional/optional/impl/optional-or-else.d.mts +0 -43
- package/dist/functional/optional/impl/optional-or-else.d.mts.map +0 -1
- package/dist/functional/optional/impl/optional-or-else.mjs +0 -16
- package/dist/functional/optional/impl/optional-or-else.mjs.map +0 -1
- package/dist/functional/optional/impl/optional-some.d.mts +0 -22
- package/dist/functional/optional/impl/optional-some.d.mts.map +0 -1
- package/dist/functional/optional/impl/optional-some.mjs +0 -25
- package/dist/functional/optional/impl/optional-some.mjs.map +0 -1
- package/dist/functional/optional/impl/optional-to-nullable.d.mts +0 -30
- package/dist/functional/optional/impl/optional-to-nullable.d.mts.map +0 -1
- package/dist/functional/optional/impl/optional-to-nullable.mjs +0 -30
- package/dist/functional/optional/impl/optional-to-nullable.mjs.map +0 -1
- package/dist/functional/optional/impl/optional-unwrap-or.d.mts +0 -37
- package/dist/functional/optional/impl/optional-unwrap-or.d.mts.map +0 -1
- package/dist/functional/optional/impl/optional-unwrap-or.mjs +0 -16
- package/dist/functional/optional/impl/optional-unwrap-or.mjs.map +0 -1
- package/dist/functional/optional/impl/optional-unwrap-throw.d.mts +0 -31
- package/dist/functional/optional/impl/optional-unwrap-throw.d.mts.map +0 -1
- package/dist/functional/optional/impl/optional-unwrap-throw.mjs +0 -36
- package/dist/functional/optional/impl/optional-unwrap-throw.mjs.map +0 -1
- package/dist/functional/optional/impl/optional-unwrap.d.mts +0 -32
- package/dist/functional/optional/impl/optional-unwrap.d.mts.map +0 -1
- package/dist/functional/optional/impl/optional-unwrap.mjs +0 -8
- package/dist/functional/optional/impl/optional-unwrap.mjs.map +0 -1
- package/dist/functional/optional/impl/optional-zip.d.mts +0 -35
- package/dist/functional/optional/impl/optional-zip.d.mts.map +0 -1
- package/dist/functional/optional/impl/optional-zip.mjs +0 -39
- package/dist/functional/optional/impl/optional-zip.mjs.map +0 -1
- package/dist/functional/optional/impl/tag.d.mts +0 -6
- package/dist/functional/optional/impl/tag.d.mts.map +0 -1
- package/dist/functional/optional/impl/tag.mjs +0 -4
- package/dist/functional/optional/impl/tag.mjs.map +0 -1
- package/dist/functional/optional/impl/types.d.mts +0 -24
- package/dist/functional/optional/impl/types.d.mts.map +0 -1
- package/dist/functional/optional/impl/types.mjs +0 -2
- package/dist/functional/optional/impl/types.mjs.map +0 -1
- package/dist/functional/optional/index.d.mts +0 -2
- package/dist/functional/optional/index.d.mts.map +0 -1
- package/dist/functional/optional/index.mjs +0 -2
- package/dist/functional/optional/index.mjs.map +0 -1
- package/dist/functional/optional/optional.d.mts +0 -16
- package/dist/functional/optional/optional.d.mts.map +0 -1
- package/dist/functional/optional/optional.mjs +0 -2
- package/dist/functional/optional/optional.mjs.map +0 -1
- package/dist/functional/pipe.d.mts +0 -102
- package/dist/functional/pipe.d.mts.map +0 -1
- package/dist/functional/pipe.mjs +0 -45
- package/dist/functional/pipe.mjs.map +0 -1
- package/dist/functional/result/impl/index.d.mts +0 -28
- package/dist/functional/result/impl/index.d.mts.map +0 -1
- package/dist/functional/result/impl/index.mjs +0 -28
- package/dist/functional/result/impl/index.mjs.map +0 -1
- package/dist/functional/result/impl/result-err.d.mts +0 -28
- package/dist/functional/result/impl/result-err.d.mts.map +0 -1
- package/dist/functional/result/impl/result-err.mjs +0 -31
- package/dist/functional/result/impl/result-err.mjs.map +0 -1
- package/dist/functional/result/impl/result-expect-to-be.d.mts +0 -33
- package/dist/functional/result/impl/result-expect-to-be.d.mts.map +0 -1
- package/dist/functional/result/impl/result-expect-to-be.mjs +0 -23
- package/dist/functional/result/impl/result-expect-to-be.mjs.map +0 -1
- package/dist/functional/result/impl/result-flat-map.d.mts +0 -45
- package/dist/functional/result/impl/result-flat-map.d.mts.map +0 -1
- package/dist/functional/result/impl/result-flat-map.mjs +0 -21
- package/dist/functional/result/impl/result-flat-map.mjs.map +0 -1
- package/dist/functional/result/impl/result-fold.d.mts +0 -50
- package/dist/functional/result/impl/result-fold.d.mts.map +0 -1
- package/dist/functional/result/impl/result-fold.mjs +0 -23
- package/dist/functional/result/impl/result-fold.mjs.map +0 -1
- package/dist/functional/result/impl/result-from-optional.d.mts +0 -41
- package/dist/functional/result/impl/result-from-optional.d.mts.map +0 -1
- package/dist/functional/result/impl/result-from-optional.mjs +0 -21
- package/dist/functional/result/impl/result-from-optional.mjs.map +0 -1
- package/dist/functional/result/impl/result-from-promise.d.mts +0 -43
- package/dist/functional/result/impl/result-from-promise.d.mts.map +0 -1
- package/dist/functional/result/impl/result-from-promise.mjs +0 -39
- package/dist/functional/result/impl/result-from-promise.mjs.map +0 -1
- package/dist/functional/result/impl/result-from-throwable.d.mts +0 -32
- package/dist/functional/result/impl/result-from-throwable.d.mts.map +0 -1
- package/dist/functional/result/impl/result-from-throwable.mjs +0 -46
- package/dist/functional/result/impl/result-from-throwable.mjs.map +0 -1
- package/dist/functional/result/impl/result-is-err.d.mts +0 -23
- package/dist/functional/result/impl/result-is-err.d.mts.map +0 -1
- package/dist/functional/result/impl/result-is-err.mjs +0 -22
- package/dist/functional/result/impl/result-is-err.mjs.map +0 -1
- package/dist/functional/result/impl/result-is-ok.d.mts +0 -23
- package/dist/functional/result/impl/result-is-ok.d.mts.map +0 -1
- package/dist/functional/result/impl/result-is-ok.mjs +0 -22
- package/dist/functional/result/impl/result-is-ok.mjs.map +0 -1
- package/dist/functional/result/impl/result-is-result.d.mts +0 -22
- package/dist/functional/result/impl/result-is-result.d.mts.map +0 -1
- package/dist/functional/result/impl/result-is-result.mjs +0 -26
- package/dist/functional/result/impl/result-is-result.mjs.map +0 -1
- package/dist/functional/result/impl/result-map-err.d.mts +0 -39
- package/dist/functional/result/impl/result-map-err.d.mts.map +0 -1
- package/dist/functional/result/impl/result-map-err.mjs +0 -22
- package/dist/functional/result/impl/result-map-err.mjs.map +0 -1
- package/dist/functional/result/impl/result-map.d.mts +0 -39
- package/dist/functional/result/impl/result-map.d.mts.map +0 -1
- package/dist/functional/result/impl/result-map.mjs +0 -22
- package/dist/functional/result/impl/result-map.mjs.map +0 -1
- package/dist/functional/result/impl/result-match.d.mts +0 -47
- package/dist/functional/result/impl/result-match.d.mts.map +0 -1
- package/dist/functional/result/impl/result-match.mjs +0 -20
- package/dist/functional/result/impl/result-match.mjs.map +0 -1
- package/dist/functional/result/impl/result-ok.d.mts +0 -28
- package/dist/functional/result/impl/result-ok.d.mts.map +0 -1
- package/dist/functional/result/impl/result-ok.mjs +0 -31
- package/dist/functional/result/impl/result-ok.mjs.map +0 -1
- package/dist/functional/result/impl/result-or-else.d.mts +0 -40
- package/dist/functional/result/impl/result-or-else.d.mts.map +0 -1
- package/dist/functional/result/impl/result-or-else.mjs +0 -17
- package/dist/functional/result/impl/result-or-else.mjs.map +0 -1
- package/dist/functional/result/impl/result-safe-try.d.mts +0 -51
- package/dist/functional/result/impl/result-safe-try.d.mts.map +0 -1
- package/dist/functional/result/impl/result-safe-try.mjs +0 -35
- package/dist/functional/result/impl/result-safe-try.mjs.map +0 -1
- package/dist/functional/result/impl/result-safe-unwrap.d.mts +0 -36
- package/dist/functional/result/impl/result-safe-unwrap.d.mts.map +0 -1
- package/dist/functional/result/impl/result-safe-unwrap.mjs +0 -42
- package/dist/functional/result/impl/result-safe-unwrap.mjs.map +0 -1
- package/dist/functional/result/impl/result-swap.d.mts +0 -23
- package/dist/functional/result/impl/result-swap.d.mts.map +0 -1
- package/dist/functional/result/impl/result-swap.mjs +0 -27
- package/dist/functional/result/impl/result-swap.mjs.map +0 -1
- package/dist/functional/result/impl/result-to-optional.d.mts +0 -32
- package/dist/functional/result/impl/result-to-optional.d.mts.map +0 -1
- package/dist/functional/result/impl/result-to-optional.mjs +0 -32
- package/dist/functional/result/impl/result-to-optional.mjs.map +0 -1
- package/dist/functional/result/impl/result-unwrap-err-or.d.mts +0 -33
- package/dist/functional/result/impl/result-unwrap-err-or.d.mts.map +0 -1
- package/dist/functional/result/impl/result-unwrap-err-or.mjs +0 -17
- package/dist/functional/result/impl/result-unwrap-err-or.mjs.map +0 -1
- package/dist/functional/result/impl/result-unwrap-err-throw.d.mts +0 -33
- package/dist/functional/result/impl/result-unwrap-err-throw.d.mts.map +0 -1
- package/dist/functional/result/impl/result-unwrap-err-throw.mjs +0 -46
- package/dist/functional/result/impl/result-unwrap-err-throw.mjs.map +0 -1
- package/dist/functional/result/impl/result-unwrap-err.d.mts +0 -33
- package/dist/functional/result/impl/result-unwrap-err.d.mts.map +0 -1
- package/dist/functional/result/impl/result-unwrap-err.mjs +0 -34
- package/dist/functional/result/impl/result-unwrap-err.mjs.map +0 -1
- package/dist/functional/result/impl/result-unwrap-ok-or.d.mts +0 -33
- package/dist/functional/result/impl/result-unwrap-ok-or.d.mts.map +0 -1
- package/dist/functional/result/impl/result-unwrap-ok-or.mjs +0 -17
- package/dist/functional/result/impl/result-unwrap-ok-or.mjs.map +0 -1
- package/dist/functional/result/impl/result-unwrap-ok.d.mts +0 -26
- package/dist/functional/result/impl/result-unwrap-ok.d.mts.map +0 -1
- package/dist/functional/result/impl/result-unwrap-ok.mjs +0 -8
- package/dist/functional/result/impl/result-unwrap-ok.mjs.map +0 -1
- package/dist/functional/result/impl/result-unwrap-throw.d.mts +0 -33
- package/dist/functional/result/impl/result-unwrap-throw.d.mts.map +0 -1
- package/dist/functional/result/impl/result-unwrap-throw.mjs +0 -40
- package/dist/functional/result/impl/result-unwrap-throw.mjs.map +0 -1
- package/dist/functional/result/impl/result-zip.d.mts +0 -32
- package/dist/functional/result/impl/result-zip.d.mts.map +0 -1
- package/dist/functional/result/impl/result-zip.mjs +0 -37
- package/dist/functional/result/impl/result-zip.mjs.map +0 -1
- package/dist/functional/result/impl/tag.d.mts +0 -6
- package/dist/functional/result/impl/tag.d.mts.map +0 -1
- package/dist/functional/result/impl/tag.mjs +0 -4
- package/dist/functional/result/impl/tag.mjs.map +0 -1
- package/dist/functional/result/impl/types.d.mts +0 -31
- package/dist/functional/result/impl/types.d.mts.map +0 -1
- package/dist/functional/result/impl/types.mjs +0 -2
- package/dist/functional/result/impl/types.mjs.map +0 -1
- package/dist/functional/result/index.d.mts +0 -2
- package/dist/functional/result/index.d.mts.map +0 -1
- package/dist/functional/result/index.mjs +0 -2
- package/dist/functional/result/index.mjs.map +0 -1
- package/dist/functional/result/result.d.mts +0 -16
- package/dist/functional/result/result.d.mts.map +0 -1
- package/dist/functional/result/result.mjs +0 -2
- package/dist/functional/result/result.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/index.d.mts +0 -29
- package/dist/functional/ternary-result/impl/index.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/index.mjs +0 -29
- package/dist/functional/ternary-result/impl/index.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/tag.d.mts +0 -8
- package/dist/functional/ternary-result/impl/tag.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/tag.mjs +0 -6
- package/dist/functional/ternary-result/impl/tag.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-err.d.mts +0 -16
- package/dist/functional/ternary-result/impl/ternary-result-err.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-err.mjs +0 -19
- package/dist/functional/ternary-result/impl/ternary-result-err.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-expect-to-be.d.mts +0 -24
- package/dist/functional/ternary-result/impl/ternary-result-expect-to-be.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-expect-to-be.mjs +0 -21
- package/dist/functional/ternary-result/impl/ternary-result-expect-to-be.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-flat-map.d.mts +0 -39
- package/dist/functional/ternary-result/impl/ternary-result-flat-map.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-flat-map.mjs +0 -38
- package/dist/functional/ternary-result/impl/ternary-result-flat-map.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-fold.d.mts +0 -30
- package/dist/functional/ternary-result/impl/ternary-result-fold.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-fold.mjs +0 -33
- package/dist/functional/ternary-result/impl/ternary-result-fold.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-promise.d.mts +0 -23
- package/dist/functional/ternary-result/impl/ternary-result-from-promise.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-promise.mjs +0 -23
- package/dist/functional/ternary-result/impl/ternary-result-from-promise.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-throwable.d.mts +0 -20
- package/dist/functional/ternary-result/impl/ternary-result-from-throwable.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-throwable.mjs +0 -33
- package/dist/functional/ternary-result/impl/ternary-result-from-throwable.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-err.d.mts +0 -21
- package/dist/functional/ternary-result/impl/ternary-result-is-err.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-err.mjs +0 -20
- package/dist/functional/ternary-result/impl/ternary-result-is-err.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-ok.d.mts +0 -23
- package/dist/functional/ternary-result/impl/ternary-result-is-ok.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-ok.mjs +0 -22
- package/dist/functional/ternary-result/impl/ternary-result-is-ok.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.d.mts +0 -22
- package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.mjs +0 -28
- package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-warn.d.mts +0 -22
- package/dist/functional/ternary-result/impl/ternary-result-is-warn.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-warn.mjs +0 -21
- package/dist/functional/ternary-result/impl/ternary-result-is-warn.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-map-err.d.mts +0 -27
- package/dist/functional/ternary-result/impl/ternary-result-map-err.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-map-err.mjs +0 -23
- package/dist/functional/ternary-result/impl/ternary-result-map-err.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-map-warn.d.mts +0 -28
- package/dist/functional/ternary-result/impl/ternary-result-map-warn.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-map-warn.mjs +0 -26
- package/dist/functional/ternary-result/impl/ternary-result-map-warn.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-map.d.mts +0 -36
- package/dist/functional/ternary-result/impl/ternary-result-map.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-map.mjs +0 -32
- package/dist/functional/ternary-result/impl/ternary-result-map.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-ok.d.mts +0 -17
- package/dist/functional/ternary-result/impl/ternary-result-ok.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-ok.mjs +0 -20
- package/dist/functional/ternary-result/impl/ternary-result-ok.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-or-else.d.mts +0 -29
- package/dist/functional/ternary-result/impl/ternary-result-or-else.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-or-else.mjs +0 -16
- package/dist/functional/ternary-result/impl/ternary-result-or-else.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-to-optional.d.mts +0 -23
- package/dist/functional/ternary-result/impl/ternary-result-to-optional.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-to-optional.mjs +0 -26
- package/dist/functional/ternary-result/impl/ternary-result-to-optional.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-or.d.mts +0 -28
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-or.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-or.mjs +0 -14
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-or.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-throw.d.mts +0 -20
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-throw.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-throw.mjs +0 -34
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-throw.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err.d.mts +0 -20
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err.mjs +0 -21
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-err.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok-or.d.mts +0 -25
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok-or.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok-or.mjs +0 -14
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok-or.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok.d.mts +0 -20
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok.mjs +0 -10
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.d.mts +0 -21
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.mjs +0 -35
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-or.d.mts +0 -22
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-or.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-or.mjs +0 -14
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-or.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.d.mts +0 -20
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.mjs +0 -30
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn.d.mts +0 -20
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn.mjs +0 -21
- package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-warn.d.mts +0 -19
- package/dist/functional/ternary-result/impl/ternary-result-warn.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-warn.mjs +0 -23
- package/dist/functional/ternary-result/impl/ternary-result-warn.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-zip.d.mts +0 -34
- package/dist/functional/ternary-result/impl/ternary-result-zip.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/ternary-result-zip.mjs +0 -52
- package/dist/functional/ternary-result/impl/ternary-result-zip.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/types.d.mts +0 -26
- package/dist/functional/ternary-result/impl/types.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/types.mjs +0 -2
- package/dist/functional/ternary-result/impl/types.mjs.map +0 -1
- package/dist/functional/ternary-result/impl/variant-name.d.mts +0 -4
- package/dist/functional/ternary-result/impl/variant-name.d.mts.map +0 -1
- package/dist/functional/ternary-result/impl/variant-name.mjs +0 -13
- package/dist/functional/ternary-result/impl/variant-name.mjs.map +0 -1
- package/dist/functional/ternary-result/index.d.mts +0 -2
- package/dist/functional/ternary-result/index.d.mts.map +0 -1
- package/dist/functional/ternary-result/index.mjs +0 -2
- package/dist/functional/ternary-result/index.mjs.map +0 -1
- package/dist/functional/ternary-result/ternary-result.d.mts +0 -40
- package/dist/functional/ternary-result/ternary-result.d.mts.map +0 -1
- package/dist/functional/ternary-result/ternary-result.mjs +0 -2
- package/dist/functional/ternary-result/ternary-result.mjs.map +0 -1
- package/src/functional/async-result/async-result.mts +0 -17
- package/src/functional/async-result/impl/async-result-flat-map.mts +0 -99
- package/src/functional/async-result/impl/async-result-from-promise.mts +0 -69
- package/src/functional/async-result/impl/async-result-from-throwable.mts +0 -78
- package/src/functional/async-result/impl/async-result-map-err.mts +0 -84
- package/src/functional/async-result/impl/async-result-map.mts +0 -94
- package/src/functional/async-result/impl/async-result-unwrap-or.mts +0 -42
- package/src/functional/async-result/impl/index.mts +0 -7
- package/src/functional/async-result/impl/types.mts +0 -51
- package/src/functional/async-result/index.mts +0 -1
- package/src/functional/async-result.test.mts +0 -291
- package/src/functional/match.mts +0 -193
- package/src/functional/match.test.mts +0 -180
- package/src/functional/optional/impl/index.mts +0 -18
- package/src/functional/optional/impl/optional-expect-to-be.mts +0 -75
- package/src/functional/optional/impl/optional-filter.mts +0 -78
- package/src/functional/optional/impl/optional-flat-map.mts +0 -75
- package/src/functional/optional/impl/optional-from-nullable.mts +0 -31
- package/src/functional/optional/impl/optional-is-none.mts +0 -26
- package/src/functional/optional/impl/optional-is-optional.mts +0 -30
- package/src/functional/optional/impl/optional-is-some.mts +0 -27
- package/src/functional/optional/impl/optional-map.mts +0 -90
- package/src/functional/optional/impl/optional-match.mts +0 -108
- package/src/functional/optional/impl/optional-none.mts +0 -20
- package/src/functional/optional/impl/optional-or-else.mts +0 -83
- package/src/functional/optional/impl/optional-some.mts +0 -27
- package/src/functional/optional/impl/optional-to-nullable.mts +0 -34
- package/src/functional/optional/impl/optional-unwrap-or.mts +0 -70
- package/src/functional/optional/impl/optional-unwrap-throw.mts +0 -41
- package/src/functional/optional/impl/optional-unwrap.mts +0 -45
- package/src/functional/optional/impl/optional-zip.mts +0 -44
- package/src/functional/optional/impl/tag.mts +0 -8
- package/src/functional/optional/impl/types.mts +0 -31
- package/src/functional/optional/index.mts +0 -1
- package/src/functional/optional/optional.mts +0 -19
- package/src/functional/optional.test.mts +0 -883
- package/src/functional/pipe.mts +0 -180
- package/src/functional/pipe.test.mts +0 -155
- package/src/functional/result/impl/index.mts +0 -27
- package/src/functional/result/impl/result-err.mts +0 -33
- package/src/functional/result/impl/result-expect-to-be.mts +0 -73
- package/src/functional/result/impl/result-flat-map.mts +0 -88
- package/src/functional/result/impl/result-fold.mts +0 -102
- package/src/functional/result/impl/result-from-optional.mts +0 -80
- package/src/functional/result/impl/result-from-promise.mts +0 -51
- package/src/functional/result/impl/result-from-throwable.mts +0 -48
- package/src/functional/result/impl/result-is-err.mts +0 -26
- package/src/functional/result/impl/result-is-ok.mts +0 -26
- package/src/functional/result/impl/result-is-result.mts +0 -28
- package/src/functional/result/impl/result-map-err.mts +0 -96
- package/src/functional/result/impl/result-map.mts +0 -96
- package/src/functional/result/impl/result-match.mts +0 -108
- package/src/functional/result/impl/result-ok.mts +0 -33
- package/src/functional/result/impl/result-or-else.mts +0 -73
- package/src/functional/result/impl/result-safe-try.mts +0 -110
- package/src/functional/result/impl/result-safe-unwrap.mts +0 -50
- package/src/functional/result/impl/result-swap.mts +0 -31
- package/src/functional/result/impl/result-to-optional.mts +0 -37
- package/src/functional/result/impl/result-unwrap-err-or.mts +0 -66
- package/src/functional/result/impl/result-unwrap-err-throw.mts +0 -56
- package/src/functional/result/impl/result-unwrap-err.mts +0 -38
- package/src/functional/result/impl/result-unwrap-ok-or.mts +0 -66
- package/src/functional/result/impl/result-unwrap-ok.mts +0 -39
- package/src/functional/result/impl/result-unwrap-throw.mts +0 -46
- package/src/functional/result/impl/result-zip.mts +0 -42
- package/src/functional/result/impl/tag.mts +0 -8
- package/src/functional/result/impl/types.mts +0 -38
- package/src/functional/result/index.mts +0 -1
- package/src/functional/result/result.mts +0 -19
- package/src/functional/result.test.mts +0 -1349
- package/src/functional/ternary-result/impl/index.mts +0 -28
- package/src/functional/ternary-result/impl/tag.mts +0 -17
- package/src/functional/ternary-result/impl/ternary-result-err.mts +0 -21
- package/src/functional/ternary-result/impl/ternary-result-expect-to-be.mts +0 -65
- package/src/functional/ternary-result/impl/ternary-result-flat-map.mts +0 -112
- package/src/functional/ternary-result/impl/ternary-result-fold.mts +0 -102
- package/src/functional/ternary-result/impl/ternary-result-from-promise.mts +0 -30
- package/src/functional/ternary-result/impl/ternary-result-from-throwable.mts +0 -36
- package/src/functional/ternary-result/impl/ternary-result-is-err.mts +0 -24
- package/src/functional/ternary-result/impl/ternary-result-is-ok.mts +0 -26
- package/src/functional/ternary-result/impl/ternary-result-is-ternary-result.mts +0 -32
- package/src/functional/ternary-result/impl/ternary-result-is-warn.mts +0 -25
- package/src/functional/ternary-result/impl/ternary-result-map-err.mts +0 -74
- package/src/functional/ternary-result/impl/ternary-result-map-warn.mts +0 -79
- package/src/functional/ternary-result/impl/ternary-result-map.mts +0 -95
- package/src/functional/ternary-result/impl/ternary-result-ok.mts +0 -22
- package/src/functional/ternary-result/impl/ternary-result-or-else.mts +0 -72
- package/src/functional/ternary-result/impl/ternary-result-to-optional.mts +0 -31
- package/src/functional/ternary-result/impl/ternary-result-unwrap-err-or.mts +0 -54
- package/src/functional/ternary-result/impl/ternary-result-unwrap-err-throw.mts +0 -42
- package/src/functional/ternary-result/impl/ternary-result-unwrap-err.mts +0 -25
- package/src/functional/ternary-result/impl/ternary-result-unwrap-ok-or.mts +0 -51
- package/src/functional/ternary-result/impl/ternary-result-unwrap-ok.mts +0 -39
- package/src/functional/ternary-result/impl/ternary-result-unwrap-throw.mts +0 -48
- package/src/functional/ternary-result/impl/ternary-result-unwrap-warn-or.mts +0 -48
- package/src/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.mts +0 -40
- package/src/functional/ternary-result/impl/ternary-result-unwrap-warn.mts +0 -25
- package/src/functional/ternary-result/impl/ternary-result-warn.mts +0 -28
- package/src/functional/ternary-result/impl/ternary-result-zip.mts +0 -61
- package/src/functional/ternary-result/impl/types.mts +0 -46
- package/src/functional/ternary-result/impl/variant-name.mts +0 -18
- package/src/functional/ternary-result/index.mts +0 -1
- package/src/functional/ternary-result/ternary-result.mts +0 -50
- package/src/functional/ternary-result.test.mts +0 -310
- package/src/guard/has-key.test.mts +0 -111
- package/src/guard/is-non-null-object.test.mts +0 -109
- package/src/guard/is-record.test.mts +0 -195
- package/src/guard/key-is-in.test.mts +0 -21
- package/src/others/unknown-to-string.test.mts +0 -295
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { Optional, type UnknownOptional } from '../../optional/index.mjs';
|
|
2
|
-
import { type Result } from '../result.mjs';
|
|
3
|
-
import { err } from './result-err.mjs';
|
|
4
|
-
import { ok } from './result-ok.mjs';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Converts an {@link Optional} to a `Result` (the inverse of
|
|
8
|
-
* {@link Result.toOptional}, and the equivalent of Rust's `Option::ok_or`).
|
|
9
|
-
*
|
|
10
|
-
* If the {@link Optional} is `Some`, returns `Ok` with the contained value.
|
|
11
|
-
* If it is `None`, returns `Err` with the provided error value.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
*
|
|
15
|
-
* ```ts
|
|
16
|
-
* assert.deepStrictEqual(
|
|
17
|
-
* Result.fromOptional(Optional.some(42), 'missing'),
|
|
18
|
-
* Result.ok(42),
|
|
19
|
-
* );
|
|
20
|
-
*
|
|
21
|
-
* assert.deepStrictEqual(
|
|
22
|
-
* Result.fromOptional(Optional.none, 'missing'),
|
|
23
|
-
* Result.err('missing'),
|
|
24
|
-
* );
|
|
25
|
-
*
|
|
26
|
-
* const withMissingError = Result.fromOptional('missing');
|
|
27
|
-
*
|
|
28
|
-
* assert.deepStrictEqual(withMissingError(Optional.some(1)), Result.ok(1));
|
|
29
|
-
*
|
|
30
|
-
* assert.deepStrictEqual(
|
|
31
|
-
* withMissingError(Optional.none),
|
|
32
|
-
* Result.err('missing'),
|
|
33
|
-
* );
|
|
34
|
-
* ```
|
|
35
|
-
*
|
|
36
|
-
* @template O The input `UnknownOptional` type.
|
|
37
|
-
* @template E The type of the error value used for the `None` case.
|
|
38
|
-
* @param optional The `Optional` to convert.
|
|
39
|
-
* @param error The error value to use if the `Optional` is `None`.
|
|
40
|
-
* @returns `Ok` with the contained value, or `Err` with `error`.
|
|
41
|
-
*/
|
|
42
|
-
export function fromOptional<O extends UnknownOptional, const E>(
|
|
43
|
-
optional: O,
|
|
44
|
-
error: E,
|
|
45
|
-
): Result<Optional.Unwrap<O>, E>;
|
|
46
|
-
|
|
47
|
-
// Curried version
|
|
48
|
-
export function fromOptional<const E>(
|
|
49
|
-
error: E,
|
|
50
|
-
): <O extends UnknownOptional>(optional: O) => Result<Optional.Unwrap<O>, E>;
|
|
51
|
-
|
|
52
|
-
export function fromOptional<O extends UnknownOptional, const E>(
|
|
53
|
-
...args: readonly [optional: O, error: E] | readonly [error: E]
|
|
54
|
-
):
|
|
55
|
-
| Result<Optional.Unwrap<O>, E>
|
|
56
|
-
| ((optional: O) => Result<Optional.Unwrap<O>, E>) {
|
|
57
|
-
switch (args.length) {
|
|
58
|
-
case 2: {
|
|
59
|
-
const [optional, error] = args;
|
|
60
|
-
|
|
61
|
-
return fromOptionalImpl(optional, error);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
case 1: {
|
|
65
|
-
// Curried version: the only argument is the error value
|
|
66
|
-
const [error] = args;
|
|
67
|
-
|
|
68
|
-
return (optional: O) => fromOptionalImpl(optional, error);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const fromOptionalImpl = <O extends UnknownOptional, E>(
|
|
74
|
-
optional: O,
|
|
75
|
-
error: E,
|
|
76
|
-
): Result<Optional.Unwrap<O>, E> =>
|
|
77
|
-
Optional.isSome(optional)
|
|
78
|
-
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
79
|
-
ok(Optional.unwrap(optional)!)
|
|
80
|
-
: err(error);
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { type Ok } from '../../../types.mjs';
|
|
2
|
-
import { type Result } from '../result.mjs';
|
|
3
|
-
import { err } from './result-err.mjs';
|
|
4
|
-
import { ok } from './result-ok.mjs';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @template P The Promise type.
|
|
8
|
-
* @internal
|
|
9
|
-
* Utility type to extract the resolved value type from a Promise.
|
|
10
|
-
*/
|
|
11
|
-
type UnwrapPromise<P extends Promise<unknown>> =
|
|
12
|
-
P extends Promise<infer V> ? V : never;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Converts a Promise into a Promise that resolves to a `Result`. If the input
|
|
16
|
-
* Promise resolves, the `Result` will be `Ok` with the resolved value. If the
|
|
17
|
-
* input Promise rejects, the `Result` will be `Err` with the rejection
|
|
18
|
-
* reason.
|
|
19
|
-
*
|
|
20
|
-
* The error channel is `unknown`, since the rejection reason is carried
|
|
21
|
-
* through untouched. For a concrete error type, use `AsyncResult.fromPromise`
|
|
22
|
-
* with a `mapError` function; called without one it behaves exactly as this
|
|
23
|
-
* function does.
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
*
|
|
27
|
-
* ```ts
|
|
28
|
-
* const successPromise = Result.fromPromise(Promise.resolve('ok'));
|
|
29
|
-
*
|
|
30
|
-
* const failurePromise = Result.fromPromise(
|
|
31
|
-
* Promise.reject(new Error('fail')),
|
|
32
|
-
* );
|
|
33
|
-
*
|
|
34
|
-
* const resolved = await successPromise;
|
|
35
|
-
*
|
|
36
|
-
* const rejected = await failurePromise;
|
|
37
|
-
*
|
|
38
|
-
* assert.deepStrictEqual(resolved, Result.ok('ok'));
|
|
39
|
-
*
|
|
40
|
-
* assert.isTrue(Result.isErr(rejected));
|
|
41
|
-
* ```
|
|
42
|
-
*
|
|
43
|
-
* @template P The type of the input Promise.
|
|
44
|
-
* @param promise The Promise to convert.
|
|
45
|
-
* @returns A Promise that resolves to `Result<UnwrapPromise<P>, unknown>`.
|
|
46
|
-
*/
|
|
47
|
-
export const fromPromise = <P extends Promise<unknown>>(
|
|
48
|
-
promise: P,
|
|
49
|
-
): Promise<Result<UnwrapPromise<P>, unknown>> =>
|
|
50
|
-
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
51
|
-
promise.then((v) => ok(v) as Ok<UnwrapPromise<P>>).catch(err);
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { isError } from '@sindresorhus/is';
|
|
2
|
-
import { unknownToString } from '../../../others/index.mjs';
|
|
3
|
-
import { type Result } from '../result.mjs';
|
|
4
|
-
import { err } from './result-err.mjs';
|
|
5
|
-
import { ok } from './result-ok.mjs';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Wraps a function that may throw an exception in a `Result`.
|
|
9
|
-
*
|
|
10
|
-
* This is a fundamental utility for converting traditional exception-based
|
|
11
|
-
* error handling into Result-based error handling. Any thrown value is
|
|
12
|
-
* converted to an Error object for consistent error handling.
|
|
13
|
-
*
|
|
14
|
-
* If the function executes successfully, returns `Result.Ok` with the result.
|
|
15
|
-
* If the function throws, returns `Result.Err` with the caught error.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
*
|
|
19
|
-
* ```ts
|
|
20
|
-
* const success = Result.fromThrowable(() => 1 + 1);
|
|
21
|
-
*
|
|
22
|
-
* const failure = Result.fromThrowable(() => {
|
|
23
|
-
* throw new Error('boom');
|
|
24
|
-
* });
|
|
25
|
-
*
|
|
26
|
-
* assert.deepStrictEqual(success, Result.ok(2));
|
|
27
|
-
*
|
|
28
|
-
* assert.isTrue(Result.isErr(failure));
|
|
29
|
-
* ```
|
|
30
|
-
*
|
|
31
|
-
* @template T The return type of the function.
|
|
32
|
-
* @param fn The function to execute that may throw.
|
|
33
|
-
* @returns A `Result<T, Error>` containing either the successful result or
|
|
34
|
-
* the caught error.
|
|
35
|
-
*/
|
|
36
|
-
export const fromThrowable = <T,>(fn: () => T): Result<T, Error> => {
|
|
37
|
-
try {
|
|
38
|
-
return ok(fn());
|
|
39
|
-
} catch (error) {
|
|
40
|
-
if (isError(error)) {
|
|
41
|
-
return err(error);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const msg = unknownToString(error);
|
|
45
|
-
|
|
46
|
-
return err(new Error(msg));
|
|
47
|
-
}
|
|
48
|
-
};
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { type UnknownResult } from '../result.mjs';
|
|
2
|
-
import { ErrTypeTagName } from './tag.mjs';
|
|
3
|
-
import { type NarrowToErr } from './types.mjs';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Type guard for the error variant.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
*
|
|
10
|
-
* ```ts
|
|
11
|
-
* const operation = Result.ok(3);
|
|
12
|
-
*
|
|
13
|
-
* const failure = Result.err('error');
|
|
14
|
-
*
|
|
15
|
-
* if (Result.isOk(operation)) {
|
|
16
|
-
* const value: number = operation.value;
|
|
17
|
-
*
|
|
18
|
-
* assert.isTrue(value === 3);
|
|
19
|
-
* }
|
|
20
|
-
*
|
|
21
|
-
* assert.isTrue(Result.isErr(failure));
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
export const isErr = <R extends UnknownResult>(
|
|
25
|
-
result: R,
|
|
26
|
-
): result is NarrowToErr<R> => result.$$tag === ErrTypeTagName;
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { type UnknownResult } from '../result.mjs';
|
|
2
|
-
import { OkTypeTagName } from './tag.mjs';
|
|
3
|
-
import { type NarrowToOk } from './types.mjs';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Type guard for the ok variant.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
*
|
|
10
|
-
* ```ts
|
|
11
|
-
* const operation = Result.ok(3);
|
|
12
|
-
*
|
|
13
|
-
* const failure = Result.err('error');
|
|
14
|
-
*
|
|
15
|
-
* if (Result.isOk(operation)) {
|
|
16
|
-
* const value: number = operation.value;
|
|
17
|
-
*
|
|
18
|
-
* assert.isTrue(value === 3);
|
|
19
|
-
* }
|
|
20
|
-
*
|
|
21
|
-
* assert.isTrue(Result.isErr(failure));
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
export const isOk = <R extends UnknownResult>(
|
|
25
|
-
result: R,
|
|
26
|
-
): result is NarrowToOk<R> => result.$$tag === OkTypeTagName;
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { hasKey, isRecord } from '../../../guard/index.mjs';
|
|
2
|
-
import { type UnknownResult } from '../result.mjs';
|
|
3
|
-
import { ErrTypeTagName, OkTypeTagName } from './tag.mjs';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Checks whether the provided value is a {@link Result<S, E>}.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
*
|
|
10
|
-
* ```ts
|
|
11
|
-
* const okValue = Result.ok('success');
|
|
12
|
-
*
|
|
13
|
-
* const errValue = Result.err(new Error('failure'));
|
|
14
|
-
*
|
|
15
|
-
* const notResult = { $$tag: 'ts-data-forge::Result.ok' } as const;
|
|
16
|
-
*
|
|
17
|
-
* assert.isTrue(Result.isResult(okValue));
|
|
18
|
-
*
|
|
19
|
-
* assert.isTrue(Result.isResult(errValue));
|
|
20
|
-
*
|
|
21
|
-
* assert.isFalse(Result.isResult(notResult));
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
export const isResult = (maybeResult: unknown): maybeResult is UnknownResult =>
|
|
25
|
-
isRecord(maybeResult) &&
|
|
26
|
-
hasKey(maybeResult, '$$tag') &&
|
|
27
|
-
hasKey(maybeResult, 'value') &&
|
|
28
|
-
(maybeResult.$$tag === ErrTypeTagName || maybeResult.$$tag === OkTypeTagName);
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { type Ok } from '../../../types.mjs';
|
|
2
|
-
import { type Result, type UnknownResult } from '../result.mjs';
|
|
3
|
-
import { err } from './result-err.mjs';
|
|
4
|
-
import { isOk } from './result-is-ok.mjs';
|
|
5
|
-
import { type UnwrapErr, type UnwrapOk } from './types.mjs';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Maps a `Result<S, E>` to `Result<S, E2>` by applying a function to the
|
|
9
|
-
* error value. If the `Result` is `Result.Ok`, returns the original `Ok`.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
*
|
|
13
|
-
* ```ts
|
|
14
|
-
* const okValue = Result.ok(3) as Result<number, string>;
|
|
15
|
-
*
|
|
16
|
-
* const errValue = Result.err('missing');
|
|
17
|
-
*
|
|
18
|
-
* const untouchedOk = Result.mapErr(okValue, (error) => error.toUpperCase());
|
|
19
|
-
*
|
|
20
|
-
* const uppercasedErr = Result.mapErr(errValue, (error) =>
|
|
21
|
-
* error.toUpperCase(),
|
|
22
|
-
* );
|
|
23
|
-
*
|
|
24
|
-
* assert.deepStrictEqual(untouchedOk, Result.ok(3));
|
|
25
|
-
*
|
|
26
|
-
* assert.deepStrictEqual(uppercasedErr, Result.err('MISSING'));
|
|
27
|
-
*
|
|
28
|
-
* const mapError = Result.mapErr((error: Readonly<Error>) => error.message);
|
|
29
|
-
*
|
|
30
|
-
* const wrapped = mapError(Result.err(new Error('boom')));
|
|
31
|
-
*
|
|
32
|
-
* assert.deepStrictEqual(wrapped, Result.err('boom'));
|
|
33
|
-
* ```
|
|
34
|
-
*
|
|
35
|
-
* @template R The input `UnknownResult` type.
|
|
36
|
-
* @template E2 The type of the error value returned by the mapping function.
|
|
37
|
-
* @param result The `Result` to map.
|
|
38
|
-
* @param mapFn The function to apply to the error value if present.
|
|
39
|
-
* @returns A new `Result<UnwrapOk<R>, E2>`.
|
|
40
|
-
*/
|
|
41
|
-
export function mapErr<R extends UnknownResult, E2>(
|
|
42
|
-
result: R,
|
|
43
|
-
mapFn: (error: UnwrapErr<R>) => E2,
|
|
44
|
-
): Result<UnwrapOk<R>, E2>;
|
|
45
|
-
|
|
46
|
-
// Curried version
|
|
47
|
-
//
|
|
48
|
-
// The returned function is generic over the *whole* result, mirroring the
|
|
49
|
-
// direct overload above, rather than fixing its parameter to `Result<S, E>`.
|
|
50
|
-
// Fixing it makes the curried form unusable from a caller that is itself
|
|
51
|
-
// generic over the result: with `E` bound to `UnwrapErr<R>` at currying time the
|
|
52
|
-
// parameter becomes `Result<...>` in terms of `R`, and TypeScript cannot show
|
|
53
|
-
// a bare `R extends UnknownResult` is assignable to that — although every
|
|
54
|
-
// concrete instantiation of `R` is.
|
|
55
|
-
//
|
|
56
|
-
// The conditional is what keeps the check: `R` is inferred from the argument
|
|
57
|
-
// and its error type still has to satisfy `E`, so the mismatched case stays an
|
|
58
|
-
// error. The cost is the message it produces — "not assignable to `never`"
|
|
59
|
-
// rather than naming the two results.
|
|
60
|
-
export function mapErr<E, E2>(
|
|
61
|
-
mapFn: (error: E) => E2,
|
|
62
|
-
): <R extends UnknownResult>(
|
|
63
|
-
result: UnwrapErr<R> extends E ? R : never,
|
|
64
|
-
) => Result<UnwrapOk<R>, E2>;
|
|
65
|
-
|
|
66
|
-
export function mapErr<R extends UnknownResult, E2>(
|
|
67
|
-
...args:
|
|
68
|
-
| readonly [result: R, mapFn: (error: UnwrapErr<R>) => E2]
|
|
69
|
-
| readonly [mapFn: (error: UnwrapErr<R>) => E2]
|
|
70
|
-
): Result<UnwrapOk<R>, E2> | ((result: R) => Result<UnwrapOk<R>, E2>) {
|
|
71
|
-
switch (args.length) {
|
|
72
|
-
case 2: {
|
|
73
|
-
// Direct version: first argument is result
|
|
74
|
-
const [result, mapFn] = args;
|
|
75
|
-
|
|
76
|
-
return mapErrImpl(result, mapFn);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
case 1: {
|
|
80
|
-
// Curried version: first argument is mapping function
|
|
81
|
-
const [mapFn] = args;
|
|
82
|
-
|
|
83
|
-
return (result: R) => mapErrImpl(result, mapFn);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const mapErrImpl = <R extends UnknownResult, E2>(
|
|
89
|
-
result: R,
|
|
90
|
-
mapFn: (error: UnwrapErr<R>) => E2,
|
|
91
|
-
): Result<UnwrapOk<R>, E2> =>
|
|
92
|
-
isOk(result)
|
|
93
|
-
? // eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
94
|
-
(result as Ok<UnwrapOk<R>>)
|
|
95
|
-
: // eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
96
|
-
err(mapFn(result.value as UnwrapErr<R>));
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { type Err } from '../../../types.mjs';
|
|
2
|
-
import { type Result, type UnknownResult } from '../result.mjs';
|
|
3
|
-
import { isErr } from './result-is-err.mjs';
|
|
4
|
-
import { ok } from './result-ok.mjs';
|
|
5
|
-
import { type UnwrapErr, type UnwrapOk } from './types.mjs';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Maps a `Result<S, E>` to `Result<S2, E>` by applying a function to the
|
|
9
|
-
* success value. If the `Result` is `Result.Err`, returns the original
|
|
10
|
-
* `Err`.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
*
|
|
14
|
-
* ```ts
|
|
15
|
-
* const okNumber = Result.ok(5);
|
|
16
|
-
*
|
|
17
|
-
* const errMessage = Result.err('error');
|
|
18
|
-
*
|
|
19
|
-
* const doubled = Result.map(okNumber, (value) => value * 2);
|
|
20
|
-
*
|
|
21
|
-
* const untouchedError = Result.map(errMessage, (value: number) => value * 2);
|
|
22
|
-
*
|
|
23
|
-
* assert.deepStrictEqual(doubled, Result.ok(10));
|
|
24
|
-
*
|
|
25
|
-
* assert.deepStrictEqual(untouchedError, errMessage);
|
|
26
|
-
*
|
|
27
|
-
* const mapToLength = Result.map((text: string) => text.length);
|
|
28
|
-
*
|
|
29
|
-
* assert.deepStrictEqual(mapToLength(Result.ok('abc')), Result.ok(3));
|
|
30
|
-
*
|
|
31
|
-
* assert.deepStrictEqual(mapToLength(Result.err('bad')), Result.err('bad'));
|
|
32
|
-
* ```
|
|
33
|
-
*
|
|
34
|
-
* @template R The input `UnknownResult` type.
|
|
35
|
-
* @template S2 The type of the success value returned by the mapping
|
|
36
|
-
* function.
|
|
37
|
-
* @param result The `Result` to map.
|
|
38
|
-
* @param mapFn The function to apply to the success value if present.
|
|
39
|
-
* @returns A new `Result<S2, UnwrapErr<R>>`.
|
|
40
|
-
*/
|
|
41
|
-
export function map<R extends UnknownResult, S2>(
|
|
42
|
-
result: R,
|
|
43
|
-
mapFn: (value: UnwrapOk<R>) => S2,
|
|
44
|
-
): Result<S2, UnwrapErr<R>>;
|
|
45
|
-
|
|
46
|
-
// Curried version
|
|
47
|
-
//
|
|
48
|
-
// The returned function is generic over the *whole* result, mirroring the
|
|
49
|
-
// direct overload above, rather than fixing its parameter to `Result<S, E>`.
|
|
50
|
-
// Fixing it makes the curried form unusable from a caller that is itself
|
|
51
|
-
// generic over the result: with `S` bound to `UnwrapOk<R>` at currying time the
|
|
52
|
-
// parameter becomes `Result<...>` in terms of `R`, and TypeScript cannot show
|
|
53
|
-
// a bare `R extends UnknownResult` is assignable to that — although every
|
|
54
|
-
// concrete instantiation of `R` is.
|
|
55
|
-
//
|
|
56
|
-
// The conditional is what keeps the check: `R` is inferred from the argument
|
|
57
|
-
// and its success type still has to satisfy `S`, so the mismatched case stays an
|
|
58
|
-
// error. The cost is the message it produces — "not assignable to `never`"
|
|
59
|
-
// rather than naming the two results.
|
|
60
|
-
export function map<S, S2>(
|
|
61
|
-
mapFn: (value: S) => S2,
|
|
62
|
-
): <R extends UnknownResult>(
|
|
63
|
-
result: UnwrapOk<R> extends S ? R : never,
|
|
64
|
-
) => Result<S2, UnwrapErr<R>>;
|
|
65
|
-
|
|
66
|
-
export function map<R extends UnknownResult, S2>(
|
|
67
|
-
...args:
|
|
68
|
-
| readonly [result: R, mapFn: (value: UnwrapOk<R>) => S2]
|
|
69
|
-
| readonly [mapFn: (value: UnwrapOk<R>) => S2]
|
|
70
|
-
): Result<S2, UnwrapErr<R>> | ((result: R) => Result<S2, UnwrapErr<R>>) {
|
|
71
|
-
switch (args.length) {
|
|
72
|
-
case 2: {
|
|
73
|
-
// Direct version: first argument is result
|
|
74
|
-
const [result, mapFn] = args;
|
|
75
|
-
|
|
76
|
-
return mapImpl(result, mapFn);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
case 1: {
|
|
80
|
-
// Curried version
|
|
81
|
-
const [mapFn] = args;
|
|
82
|
-
|
|
83
|
-
return (result: R) => mapImpl(result, mapFn);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const mapImpl = <R extends UnknownResult, S2>(
|
|
89
|
-
result: R,
|
|
90
|
-
mapFn: (value: UnwrapOk<R>) => S2,
|
|
91
|
-
): Result<S2, UnwrapErr<R>> =>
|
|
92
|
-
isErr(result)
|
|
93
|
-
? // eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
94
|
-
(result as Err<UnwrapErr<R>>)
|
|
95
|
-
: // eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
96
|
-
ok(mapFn(result.value as UnwrapOk<R>));
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import { type UnknownResult } from '../result.mjs';
|
|
2
|
-
import { isOk } from './result-is-ok.mjs';
|
|
3
|
-
import { type UnwrapErr, type UnwrapOk } from './types.mjs';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Folds a `Result` into a plain value by applying one of two case handlers:
|
|
7
|
-
* `ok` if the `Result` is `Ok`, `err` otherwise. This is the function form of
|
|
8
|
-
* pattern matching on a `Result`.
|
|
9
|
-
*
|
|
10
|
-
* Unlike {@link Result.fold}, which maps both sides and returns another
|
|
11
|
-
* `Result`, `match` unwraps: both branches produce the final value directly.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
*
|
|
15
|
-
* ```ts
|
|
16
|
-
* const doubledOrZero = Result.match(Result.ok(21), {
|
|
17
|
-
* ok: (value) => value * 2,
|
|
18
|
-
* err: () => 0,
|
|
19
|
-
* });
|
|
20
|
-
*
|
|
21
|
-
* assert.deepStrictEqual(doubledOrZero, 42);
|
|
22
|
-
*
|
|
23
|
-
* const matcher = Result.match({
|
|
24
|
-
* ok: (value: number) => value + 1,
|
|
25
|
-
* err: (error: string) => error.length,
|
|
26
|
-
* });
|
|
27
|
-
*
|
|
28
|
-
* assert.deepStrictEqual(matcher(Result.ok(1)), 2);
|
|
29
|
-
*
|
|
30
|
-
* assert.deepStrictEqual(matcher(Result.err('oops')), 4);
|
|
31
|
-
* ```
|
|
32
|
-
*
|
|
33
|
-
* @template R The input `UnknownResult` type.
|
|
34
|
-
* @template S2 The type returned by the `ok` case.
|
|
35
|
-
* @template E2 The type returned by the `err` case.
|
|
36
|
-
* @param result The `Result` to match on.
|
|
37
|
-
* @param cases An object with an `ok` handler receiving the success value and
|
|
38
|
-
* an `err` handler receiving the error value.
|
|
39
|
-
* @returns The value produced by whichever case handler was applied.
|
|
40
|
-
*/
|
|
41
|
-
export function match<R extends UnknownResult, S2, E2>(
|
|
42
|
-
result: R,
|
|
43
|
-
cases: Readonly<{
|
|
44
|
-
ok: (value: UnwrapOk<R>) => S2;
|
|
45
|
-
err: (error: UnwrapErr<R>) => E2;
|
|
46
|
-
}>,
|
|
47
|
-
): E2 | S2;
|
|
48
|
-
|
|
49
|
-
// Curried version
|
|
50
|
-
//
|
|
51
|
-
// The returned function is generic over the *whole* result, mirroring the
|
|
52
|
-
// direct overload above; see the comment on the curried overload of
|
|
53
|
-
// `Result.map` for why the parameter is a conditional type instead of a plain
|
|
54
|
-
// `Result<S, E>`. Both sides have to be checked here, hence the nested
|
|
55
|
-
// conditional.
|
|
56
|
-
export function match<S, E, S2, E2>(
|
|
57
|
-
cases: Readonly<{
|
|
58
|
-
ok: (value: S) => S2;
|
|
59
|
-
err: (error: E) => E2;
|
|
60
|
-
}>,
|
|
61
|
-
): <R extends UnknownResult>(
|
|
62
|
-
result: UnwrapOk<R> extends S ? (UnwrapErr<R> extends E ? R : never) : never,
|
|
63
|
-
) => E2 | S2;
|
|
64
|
-
|
|
65
|
-
export function match<R extends UnknownResult, S2, E2>(
|
|
66
|
-
...args:
|
|
67
|
-
| readonly [
|
|
68
|
-
result: R,
|
|
69
|
-
cases: Readonly<{
|
|
70
|
-
ok: (value: UnwrapOk<R>) => S2;
|
|
71
|
-
err: (error: UnwrapErr<R>) => E2;
|
|
72
|
-
}>,
|
|
73
|
-
]
|
|
74
|
-
| readonly [
|
|
75
|
-
cases: Readonly<{
|
|
76
|
-
ok: (value: UnwrapOk<R>) => S2;
|
|
77
|
-
err: (error: UnwrapErr<R>) => E2;
|
|
78
|
-
}>,
|
|
79
|
-
]
|
|
80
|
-
): E2 | S2 | ((result: R) => E2 | S2) {
|
|
81
|
-
switch (args.length) {
|
|
82
|
-
case 2: {
|
|
83
|
-
const [result, cases] = args;
|
|
84
|
-
|
|
85
|
-
return matchImpl(result, cases);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
case 1: {
|
|
89
|
-
// Curried version: the only argument is the case handlers
|
|
90
|
-
const [cases] = args;
|
|
91
|
-
|
|
92
|
-
return (result: R) => matchImpl(result, cases);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const matchImpl = <R extends UnknownResult, S2, E2>(
|
|
98
|
-
result: R,
|
|
99
|
-
cases: Readonly<{
|
|
100
|
-
ok: (value: UnwrapOk<R>) => S2;
|
|
101
|
-
err: (error: UnwrapErr<R>) => E2;
|
|
102
|
-
}>,
|
|
103
|
-
): E2 | S2 =>
|
|
104
|
-
isOk(result)
|
|
105
|
-
? // eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
106
|
-
cases.ok(result.value as UnwrapOk<R>)
|
|
107
|
-
: // eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
108
|
-
cases.err(result.value as UnwrapErr<R>);
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { type Ok } from '../../../types.mjs';
|
|
2
|
-
import { OkTypeTagName } from './tag.mjs';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Creates a `Result.Ok` containing the given success value.
|
|
6
|
-
*
|
|
7
|
-
* Use this constructor when an operation succeeds and you want to wrap the
|
|
8
|
-
* successful result in a Result type for consistent error handling.
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
*
|
|
12
|
-
* ```ts
|
|
13
|
-
* const success = Result.ok({ id: 1 });
|
|
14
|
-
*
|
|
15
|
-
* const failure = Result.err(new Error('missing data'));
|
|
16
|
-
*
|
|
17
|
-
* assert.deepStrictEqual(success, {
|
|
18
|
-
* $$tag: 'ts-data-forge::Result.ok',
|
|
19
|
-
* value: { id: 1 },
|
|
20
|
-
* });
|
|
21
|
-
*
|
|
22
|
-
* assert.isTrue(Result.isErr(failure));
|
|
23
|
-
* ```
|
|
24
|
-
*
|
|
25
|
-
* @template S The type of the success value.
|
|
26
|
-
* @param value The success value.
|
|
27
|
-
* @returns A `Result.Ok<S>` containing the value.
|
|
28
|
-
*/
|
|
29
|
-
export const ok = <const S,>(value: S): Ok<S> =>
|
|
30
|
-
({
|
|
31
|
-
$$tag: OkTypeTagName,
|
|
32
|
-
value,
|
|
33
|
-
}) as const;
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { type UnknownResult } from '../result.mjs';
|
|
2
|
-
import { isOk } from './result-is-ok.mjs';
|
|
3
|
-
import { type NarrowToOk } from './types.mjs';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Returns the `Result` if it is `Ok`, otherwise returns the alternative.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
*
|
|
10
|
-
* ```ts
|
|
11
|
-
* const primary = Result.ok('primary');
|
|
12
|
-
*
|
|
13
|
-
* const fallback = Result.ok('fallback');
|
|
14
|
-
*
|
|
15
|
-
* const failure = Result.err('failure');
|
|
16
|
-
*
|
|
17
|
-
* assert.deepStrictEqual(Result.orElse(primary, fallback), primary);
|
|
18
|
-
*
|
|
19
|
-
* assert.deepStrictEqual(Result.orElse(failure, fallback), fallback);
|
|
20
|
-
*
|
|
21
|
-
* const orElseFallback = Result.orElse(Result.ok('default'));
|
|
22
|
-
*
|
|
23
|
-
* assert.deepStrictEqual(
|
|
24
|
-
* orElseFallback(Result.err('missing')),
|
|
25
|
-
* Result.ok('default'),
|
|
26
|
-
* );
|
|
27
|
-
*
|
|
28
|
-
* assert.deepStrictEqual(
|
|
29
|
-
* orElseFallback(Result.ok('value')),
|
|
30
|
-
* Result.ok('value'),
|
|
31
|
-
* );
|
|
32
|
-
* ```
|
|
33
|
-
*
|
|
34
|
-
* @template R The input `UnknownResult` type.
|
|
35
|
-
* @param result The `Result` to check.
|
|
36
|
-
* @param alternative The alternative `Result` to return if the first is
|
|
37
|
-
* `Err`.
|
|
38
|
-
* @returns The first `Result` if `Ok`, otherwise the alternative.
|
|
39
|
-
*/
|
|
40
|
-
export function orElse<R extends UnknownResult, R2 extends UnknownResult>(
|
|
41
|
-
result: R,
|
|
42
|
-
alternative: R2,
|
|
43
|
-
): NarrowToOk<R> | R2;
|
|
44
|
-
|
|
45
|
-
// Curried version
|
|
46
|
-
export function orElse<R2 extends UnknownResult>(
|
|
47
|
-
alternative: R2,
|
|
48
|
-
): <R extends UnknownResult>(result: R) => NarrowToOk<R> | R2;
|
|
49
|
-
|
|
50
|
-
export function orElse<R extends UnknownResult, R2 extends UnknownResult>(
|
|
51
|
-
...args: readonly [result: R, alternative: R2] | readonly [alternative: R2]
|
|
52
|
-
): (NarrowToOk<R> | R2) | ((result: R) => R | R2) {
|
|
53
|
-
switch (args.length) {
|
|
54
|
-
case 2: {
|
|
55
|
-
// Direct version: first argument is result
|
|
56
|
-
const [result, alternative] = args;
|
|
57
|
-
|
|
58
|
-
return orElseImpl(result, alternative);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
case 1: {
|
|
62
|
-
// Curried version
|
|
63
|
-
const [alternative] = args;
|
|
64
|
-
|
|
65
|
-
return (result: R) => orElseImpl(result, alternative);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const orElseImpl = <R extends UnknownResult, R2 extends UnknownResult>(
|
|
71
|
-
result: R,
|
|
72
|
-
alternative: R2,
|
|
73
|
-
): NarrowToOk<R> | R2 => (isOk(result) ? result : alternative);
|