ts-data-forge 14.6.3 → 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/array/impl/array-utils-modification.mjs +1 -1
- package/dist/array/impl/array-utils-transformation.mjs +2 -2
- 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 +6 -6
- 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,1349 +0,0 @@
|
|
|
1
|
-
import { expectType } from '../expect-type.mjs';
|
|
2
|
-
import { Num } from '../number/index.mjs';
|
|
3
|
-
import { type Err, type Ok } from '../types.mjs';
|
|
4
|
-
import { Optional } from './optional/index.mjs';
|
|
5
|
-
import { pipe } from './pipe.mjs';
|
|
6
|
-
import { Result, type UnknownResult } from './result/index.mjs';
|
|
7
|
-
|
|
8
|
-
describe('Result test', () => {
|
|
9
|
-
describe('ok', () => {
|
|
10
|
-
test('creates Ok result', () => {
|
|
11
|
-
const result = Result.ok(42);
|
|
12
|
-
|
|
13
|
-
assert.isTrue(Result.isOk(result));
|
|
14
|
-
|
|
15
|
-
assert.isFalse(Result.isErr(result));
|
|
16
|
-
|
|
17
|
-
expect(result.value).toBe(42);
|
|
18
|
-
|
|
19
|
-
expectType<typeof result, Result<number, never>>('<=');
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test('creates Ok result with string', () => {
|
|
23
|
-
const result = Result.ok('success');
|
|
24
|
-
|
|
25
|
-
assert.isTrue(Result.isOk(result));
|
|
26
|
-
|
|
27
|
-
expect(result.value).toBe('success');
|
|
28
|
-
|
|
29
|
-
expectType<typeof result, Result<string, never>>('<=');
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
describe('err', () => {
|
|
34
|
-
test('creates Err result', () => {
|
|
35
|
-
const result = Result.err('error message');
|
|
36
|
-
|
|
37
|
-
assert.isTrue(Result.isErr(result));
|
|
38
|
-
|
|
39
|
-
assert.isFalse(Result.isOk(result));
|
|
40
|
-
|
|
41
|
-
expect(result.value).toBe('error message');
|
|
42
|
-
|
|
43
|
-
expectType<typeof result, Result<never, string>>('<=');
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
test('creates Err result with number', () => {
|
|
47
|
-
const result = Result.err(404);
|
|
48
|
-
|
|
49
|
-
assert.isTrue(Result.isErr(result));
|
|
50
|
-
|
|
51
|
-
expect(result.value).toBe(404);
|
|
52
|
-
|
|
53
|
-
expectType<typeof result, Result<never, number>>('<=');
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
describe('isOk', () => {
|
|
58
|
-
test('type guard for Ok results', () => {
|
|
59
|
-
const result: Result<number, string> = Result.ok(42);
|
|
60
|
-
|
|
61
|
-
if (Result.isOk(result)) {
|
|
62
|
-
expectType<typeof result, Ok<number>>('<=');
|
|
63
|
-
|
|
64
|
-
expect(result.value).toBe(42);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
test('returns false for Err results', () => {
|
|
69
|
-
const result: Result<number, string> = Result.err('error');
|
|
70
|
-
|
|
71
|
-
assert.isFalse(Result.isOk(result));
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
describe('isErr', () => {
|
|
76
|
-
test('type guard for Err results', () => {
|
|
77
|
-
const result: Result<number, string> = Result.err('error');
|
|
78
|
-
|
|
79
|
-
if (Result.isErr(result)) {
|
|
80
|
-
expectType<typeof result, Err<string>>('<=');
|
|
81
|
-
|
|
82
|
-
expect(result.value).toBe('error');
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
test('returns false for Ok results', () => {
|
|
87
|
-
const result: Result<number, string> = Result.ok(42);
|
|
88
|
-
|
|
89
|
-
assert.isFalse(Result.isErr(result));
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
describe('isResult', () => {
|
|
94
|
-
test('recognizes Ok results', () => {
|
|
95
|
-
const result = Result.ok(42);
|
|
96
|
-
|
|
97
|
-
assert.isTrue(Result.isResult(result));
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
test('recognizes Err results', () => {
|
|
101
|
-
const result = Result.err('error');
|
|
102
|
-
|
|
103
|
-
assert.isTrue(Result.isResult(result));
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
test('rejects non-Result values', () => {
|
|
107
|
-
assert.isFalse(Result.isResult(42));
|
|
108
|
-
|
|
109
|
-
assert.isFalse(Result.isResult('string'));
|
|
110
|
-
|
|
111
|
-
assert.isFalse(Result.isResult(null));
|
|
112
|
-
|
|
113
|
-
assert.isFalse(Result.isResult(undefined));
|
|
114
|
-
|
|
115
|
-
assert.isFalse(Result.isResult({}));
|
|
116
|
-
|
|
117
|
-
assert.isFalse(Result.isResult({ type: 'unknown', value: 42 }));
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
describe('map', () => {
|
|
122
|
-
test('maps Ok result', () => {
|
|
123
|
-
const result = Result.ok(5);
|
|
124
|
-
|
|
125
|
-
const mapped = Result.map(result, (x) => x * 2);
|
|
126
|
-
|
|
127
|
-
assert.isTrue(Result.isOk(mapped));
|
|
128
|
-
|
|
129
|
-
expect(mapped.value).toBe(10);
|
|
130
|
-
|
|
131
|
-
expectType<typeof mapped, Result<number, never>>('<=');
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
test('preserves Err result', () => {
|
|
135
|
-
const result: Result<number, string> = Result.err('error');
|
|
136
|
-
|
|
137
|
-
const mapped = Result.map(result, (x) => x * 2);
|
|
138
|
-
|
|
139
|
-
assert.isTrue(Result.isErr(mapped));
|
|
140
|
-
|
|
141
|
-
expect(mapped.value).toBe('error');
|
|
142
|
-
|
|
143
|
-
expectType<typeof mapped, Result<number, string>>('<=');
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
test('should support curried form', () => {
|
|
147
|
-
const doubler = Result.map((x: number) => x * 2);
|
|
148
|
-
|
|
149
|
-
const okResult = Result.ok(5);
|
|
150
|
-
|
|
151
|
-
const mapped = doubler(okResult);
|
|
152
|
-
|
|
153
|
-
assert.isTrue(Result.isOk(mapped));
|
|
154
|
-
|
|
155
|
-
expect(mapped.value).toBe(10);
|
|
156
|
-
|
|
157
|
-
const errResult: Result<number, string> = Result.err('error');
|
|
158
|
-
|
|
159
|
-
const mappedErr = doubler(errResult);
|
|
160
|
-
|
|
161
|
-
assert.isTrue(Result.isErr(mappedErr));
|
|
162
|
-
|
|
163
|
-
expect(mappedErr.value).toBe('error');
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
test('should work with pipe when curried', () => {
|
|
167
|
-
const doubler = Result.map((x: number) => x * 2);
|
|
168
|
-
|
|
169
|
-
const toStringFn = Result.map((x: number) => x.toString());
|
|
170
|
-
|
|
171
|
-
const result = pipe(Result.ok(5)).map(doubler).map(toStringFn).value;
|
|
172
|
-
|
|
173
|
-
assert.isTrue(Result.isOk(result));
|
|
174
|
-
|
|
175
|
-
expect(result.value).toBe('10');
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
test('curried form is usable from a caller generic over the result', () => {
|
|
179
|
-
// The motivating case: with the curried parameter fixed to
|
|
180
|
-
// `Result<S, E>`, `S` binds to `Result.UnwrapOk<R>` here and a bare
|
|
181
|
-
// `R extends UnknownResult` is not assignable to that, so this only
|
|
182
|
-
// compiles because the returned function is generic over the whole
|
|
183
|
-
// result.
|
|
184
|
-
const liftMap =
|
|
185
|
-
<R extends UnknownResult, S2>(mapFn: (x: Result.UnwrapOk<R>) => S2) =>
|
|
186
|
-
(result: R): Result<S2, Result.UnwrapErr<R>> =>
|
|
187
|
-
Result.map(mapFn)(result);
|
|
188
|
-
|
|
189
|
-
const lengthOf = liftMap<Result<string, 'boom'>, number>((s) => s.length);
|
|
190
|
-
|
|
191
|
-
const mapped = lengthOf(Result.ok('abc'));
|
|
192
|
-
|
|
193
|
-
expectType<typeof mapped, Result<number, 'boom'>>('=');
|
|
194
|
-
|
|
195
|
-
assert.deepStrictEqual(mapped, Result.ok(3));
|
|
196
|
-
|
|
197
|
-
assert.deepStrictEqual(lengthOf(Result.err('boom')), Result.err('boom'));
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
test('curried form keeps the error type and rejects a mismatch', () => {
|
|
201
|
-
const mapToLength = Result.map((text: string) => text.length);
|
|
202
|
-
|
|
203
|
-
const mapped = mapToLength(Result.ok('abc'));
|
|
204
|
-
|
|
205
|
-
expectType<typeof mapped, Result<number, never>>('=');
|
|
206
|
-
|
|
207
|
-
assert.deepStrictEqual(mapped, Result.ok(3));
|
|
208
|
-
|
|
209
|
-
const failed = mapToLength(Result.err('boom' as const));
|
|
210
|
-
|
|
211
|
-
expectType<typeof failed, Result<number, 'boom'>>('=');
|
|
212
|
-
|
|
213
|
-
assert.deepStrictEqual(failed, Result.err('boom'));
|
|
214
|
-
|
|
215
|
-
// @ts-expect-error a Result<number, …> is not a Result<string, …>
|
|
216
|
-
const rejected = (): unknown => mapToLength(Result.ok(42));
|
|
217
|
-
|
|
218
|
-
expect(rejected).toBeTypeOf('function');
|
|
219
|
-
});
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
describe('mapErr', () => {
|
|
223
|
-
test('maps Err result', () => {
|
|
224
|
-
const result: Result<number, string> = Result.err('error');
|
|
225
|
-
|
|
226
|
-
const mapped = Result.mapErr(result, (e) => e.toUpperCase());
|
|
227
|
-
|
|
228
|
-
assert.isTrue(Result.isErr(mapped));
|
|
229
|
-
|
|
230
|
-
expect(mapped.value).toBe('ERROR');
|
|
231
|
-
|
|
232
|
-
expectType<typeof mapped, Result<number, string>>('<=');
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
test('preserves Ok result', () => {
|
|
236
|
-
const result: Result<number, string> = Result.ok(42);
|
|
237
|
-
|
|
238
|
-
const mapped = Result.mapErr(result, (e: string) => e.toUpperCase());
|
|
239
|
-
|
|
240
|
-
assert.isTrue(Result.isOk(mapped));
|
|
241
|
-
|
|
242
|
-
expect(mapped.value).toBe(42);
|
|
243
|
-
|
|
244
|
-
expectType<typeof mapped, Ok<number>>('=');
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
test('should support curried form', () => {
|
|
248
|
-
const errorUppercase = Result.mapErr((e: string) => e.toUpperCase());
|
|
249
|
-
|
|
250
|
-
const errResult: Result<number, string> = Result.err('error');
|
|
251
|
-
|
|
252
|
-
const mapped = errorUppercase(errResult);
|
|
253
|
-
|
|
254
|
-
assert.isTrue(Result.isErr(mapped));
|
|
255
|
-
|
|
256
|
-
expect(mapped.value).toBe('ERROR');
|
|
257
|
-
|
|
258
|
-
const okResult: Result<number, string> = Result.ok(42);
|
|
259
|
-
|
|
260
|
-
const mappedOk = errorUppercase(okResult);
|
|
261
|
-
|
|
262
|
-
assert.isTrue(Result.isOk(mappedOk));
|
|
263
|
-
|
|
264
|
-
expect(mappedOk.value).toBe(42);
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
test('should work with pipe when curried', () => {
|
|
268
|
-
const errorUppercase = Result.mapErr((e: string) => e.toUpperCase());
|
|
269
|
-
|
|
270
|
-
const errorPrefix = Result.mapErr((e: string) => `ERROR: ${e}`);
|
|
271
|
-
|
|
272
|
-
const result = pipe(Result.err('failed'))
|
|
273
|
-
.map(errorUppercase)
|
|
274
|
-
.map(errorPrefix).value;
|
|
275
|
-
|
|
276
|
-
assert.isTrue(Result.isErr(result));
|
|
277
|
-
|
|
278
|
-
expect(result.value).toBe('ERROR: FAILED');
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
test('curried form is usable from a caller generic over the result', () => {
|
|
282
|
-
const liftMapErr =
|
|
283
|
-
<R extends UnknownResult, E2>(mapFn: (x: Result.UnwrapErr<R>) => E2) =>
|
|
284
|
-
(result: R): Result<Result.UnwrapOk<R>, E2> =>
|
|
285
|
-
Result.mapErr(mapFn)(result);
|
|
286
|
-
|
|
287
|
-
const shout = liftMapErr<Result<number, string>, string>((e) =>
|
|
288
|
-
e.toUpperCase(),
|
|
289
|
-
);
|
|
290
|
-
|
|
291
|
-
const mapped = shout(Result.err('failed'));
|
|
292
|
-
|
|
293
|
-
expectType<typeof mapped, Result<number, string>>('=');
|
|
294
|
-
|
|
295
|
-
assert.deepStrictEqual(mapped, Result.err('FAILED'));
|
|
296
|
-
|
|
297
|
-
assert.deepStrictEqual(shout(Result.ok(1)), Result.ok(1));
|
|
298
|
-
});
|
|
299
|
-
|
|
300
|
-
test('curried form keeps the ok type and rejects a mismatch', () => {
|
|
301
|
-
const shout = Result.mapErr((e: string) => e.toUpperCase());
|
|
302
|
-
|
|
303
|
-
const mapped = shout(Result.err('failed'));
|
|
304
|
-
|
|
305
|
-
expectType<typeof mapped, Result<never, string>>('=');
|
|
306
|
-
|
|
307
|
-
assert.deepStrictEqual(mapped, Result.err('FAILED'));
|
|
308
|
-
|
|
309
|
-
// @ts-expect-error a Result<…, number> is not a Result<…, string>
|
|
310
|
-
const rejected = (): unknown => shout(Result.err(42));
|
|
311
|
-
|
|
312
|
-
expect(rejected).toBeTypeOf('function');
|
|
313
|
-
});
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
describe('unwrapThrow', () => {
|
|
317
|
-
test('unwraps Ok result', () => {
|
|
318
|
-
const result = Result.ok(42);
|
|
319
|
-
|
|
320
|
-
const value = Result.unwrapThrow(result);
|
|
321
|
-
|
|
322
|
-
expect(value).toBe(42);
|
|
323
|
-
|
|
324
|
-
expectType<typeof value, number>('<=');
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
test('throws on Err result', () => {
|
|
328
|
-
const result = Result.err('error message');
|
|
329
|
-
|
|
330
|
-
expect(() => Result.unwrapThrow(result)).toThrow('error message');
|
|
331
|
-
});
|
|
332
|
-
});
|
|
333
|
-
|
|
334
|
-
describe('unwrapOkOr', () => {
|
|
335
|
-
test('unwraps Ok result', () => {
|
|
336
|
-
const result = Result.ok(42);
|
|
337
|
-
|
|
338
|
-
const value = Result.unwrapOkOr(result, 0);
|
|
339
|
-
|
|
340
|
-
expect(value).toBe(42);
|
|
341
|
-
|
|
342
|
-
expectType<typeof value, number>('<=');
|
|
343
|
-
});
|
|
344
|
-
|
|
345
|
-
test('returns default for Err result', () => {
|
|
346
|
-
const result: Result<number, string> = Result.err('error');
|
|
347
|
-
|
|
348
|
-
const value = Result.unwrapOkOr(result, 0);
|
|
349
|
-
|
|
350
|
-
expect(value).toBe(0);
|
|
351
|
-
|
|
352
|
-
expectType<typeof value, number>('<=');
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
test('should support curried form', () => {
|
|
356
|
-
const unwrapWithDefault = Result.unwrapOkOr(42);
|
|
357
|
-
|
|
358
|
-
const okResult = Result.ok(100);
|
|
359
|
-
|
|
360
|
-
const successValue = unwrapWithDefault(okResult);
|
|
361
|
-
|
|
362
|
-
expect(successValue).toBe(100);
|
|
363
|
-
|
|
364
|
-
const errResult: Result<number, string> = Result.err('failed');
|
|
365
|
-
|
|
366
|
-
const defaultValue = unwrapWithDefault(errResult);
|
|
367
|
-
|
|
368
|
-
expect(defaultValue).toBe(42);
|
|
369
|
-
});
|
|
370
|
-
|
|
371
|
-
test('should work with pipe when curried', () => {
|
|
372
|
-
const unwrapWithDefault = Result.unwrapOkOr(0);
|
|
373
|
-
|
|
374
|
-
const successResult = pipe(Result.ok(200)).map(unwrapWithDefault).value;
|
|
375
|
-
|
|
376
|
-
expect(successResult).toBe(200);
|
|
377
|
-
|
|
378
|
-
const errorResult = pipe(Result.err('computation failed')).map(
|
|
379
|
-
unwrapWithDefault,
|
|
380
|
-
).value;
|
|
381
|
-
|
|
382
|
-
expect(errorResult).toBe(0);
|
|
383
|
-
});
|
|
384
|
-
});
|
|
385
|
-
|
|
386
|
-
describe('unwrapErr', () => {
|
|
387
|
-
test('unwraps Err result', () => {
|
|
388
|
-
const result: Result<number, string> = Result.err('error');
|
|
389
|
-
|
|
390
|
-
const value = Result.unwrapErr(result);
|
|
391
|
-
|
|
392
|
-
expect(value).toBe('error');
|
|
393
|
-
|
|
394
|
-
expectType<typeof value, string | undefined>('<=');
|
|
395
|
-
});
|
|
396
|
-
|
|
397
|
-
test('returns undefined for Ok result', () => {
|
|
398
|
-
const result: Result<number, string> = Result.ok(42);
|
|
399
|
-
|
|
400
|
-
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
|
|
401
|
-
const value = Result.unwrapErr(result);
|
|
402
|
-
|
|
403
|
-
expect(value).toBeUndefined();
|
|
404
|
-
|
|
405
|
-
expectType<typeof value, string | undefined>('<=');
|
|
406
|
-
});
|
|
407
|
-
});
|
|
408
|
-
|
|
409
|
-
describe('fold', () => {
|
|
410
|
-
test('folds Ok result', () => {
|
|
411
|
-
const result = Result.ok(42);
|
|
412
|
-
|
|
413
|
-
const folded = Result.fold(
|
|
414
|
-
result,
|
|
415
|
-
(x) => x * 2,
|
|
416
|
-
() => 0,
|
|
417
|
-
);
|
|
418
|
-
|
|
419
|
-
assert.isTrue(Result.isOk(folded));
|
|
420
|
-
|
|
421
|
-
expect(folded.value).toBe(84);
|
|
422
|
-
|
|
423
|
-
expectType<typeof folded, Ok<number>>('=');
|
|
424
|
-
});
|
|
425
|
-
|
|
426
|
-
test('folds Err result', () => {
|
|
427
|
-
const result: Result<number, string> = Result.err('error');
|
|
428
|
-
|
|
429
|
-
const folded = Result.fold(
|
|
430
|
-
result,
|
|
431
|
-
(x) => x * 2,
|
|
432
|
-
(e) => e.length,
|
|
433
|
-
);
|
|
434
|
-
|
|
435
|
-
assert.isTrue(Result.isErr(folded));
|
|
436
|
-
|
|
437
|
-
expect(folded.value).toBe(5); // length of 'error'
|
|
438
|
-
|
|
439
|
-
expectType<typeof folded, Err<number>>('=');
|
|
440
|
-
});
|
|
441
|
-
|
|
442
|
-
test('should support curried form', () => {
|
|
443
|
-
const folder = Result.fold(
|
|
444
|
-
(x: number) => x * 2,
|
|
445
|
-
(e: string) => e.length,
|
|
446
|
-
);
|
|
447
|
-
|
|
448
|
-
const okResult = Result.ok(42);
|
|
449
|
-
|
|
450
|
-
const foldedOk = folder(okResult);
|
|
451
|
-
|
|
452
|
-
assert.isTrue(Result.isOk(foldedOk));
|
|
453
|
-
|
|
454
|
-
expect(foldedOk.value).toBe(84);
|
|
455
|
-
|
|
456
|
-
const errResult: Result<number, string> = Result.err('error');
|
|
457
|
-
|
|
458
|
-
const foldedErr = folder(errResult);
|
|
459
|
-
|
|
460
|
-
assert.isTrue(Result.isErr(foldedErr));
|
|
461
|
-
|
|
462
|
-
expect(foldedErr.value).toBe(5);
|
|
463
|
-
});
|
|
464
|
-
|
|
465
|
-
test('should work with pipe when curried', () => {
|
|
466
|
-
const folder = Result.fold(
|
|
467
|
-
(x: number) => x * 2,
|
|
468
|
-
() => 0,
|
|
469
|
-
);
|
|
470
|
-
|
|
471
|
-
const result = pipe(Result.ok(21)).map(folder).value;
|
|
472
|
-
|
|
473
|
-
assert.isTrue(Result.isOk(result));
|
|
474
|
-
|
|
475
|
-
expect(result.value).toBe(42);
|
|
476
|
-
|
|
477
|
-
const errorResult = pipe(Result.err('error')).map(folder).value;
|
|
478
|
-
|
|
479
|
-
assert.isTrue(Result.isErr(errorResult));
|
|
480
|
-
|
|
481
|
-
expect(errorResult.value).toBe(0);
|
|
482
|
-
});
|
|
483
|
-
});
|
|
484
|
-
|
|
485
|
-
describe('fromPromise', () => {
|
|
486
|
-
test('handles async functions that resolve', async () => {
|
|
487
|
-
const asyncFn = async (): Promise<number> => {
|
|
488
|
-
await Promise.resolve();
|
|
489
|
-
|
|
490
|
-
return 42;
|
|
491
|
-
};
|
|
492
|
-
|
|
493
|
-
const result = await Result.fromPromise(asyncFn());
|
|
494
|
-
|
|
495
|
-
assert.isTrue(Result.isOk(result));
|
|
496
|
-
|
|
497
|
-
expect(Result.unwrapOk(result)).toBe(42);
|
|
498
|
-
});
|
|
499
|
-
|
|
500
|
-
test('handles async functions that reject', async () => {
|
|
501
|
-
const error = new Error('Async error');
|
|
502
|
-
|
|
503
|
-
const asyncFn = async (): Promise<number> =>
|
|
504
|
-
Promise.reject(error).then(() => 42);
|
|
505
|
-
|
|
506
|
-
const result = await Result.fromPromise(asyncFn());
|
|
507
|
-
|
|
508
|
-
assert.isTrue(Result.isErr(result));
|
|
509
|
-
|
|
510
|
-
expect(Result.unwrapErr(result)).toBe(error);
|
|
511
|
-
});
|
|
512
|
-
|
|
513
|
-
test('works with different promise types', async () => {
|
|
514
|
-
const stringPromise = Promise.resolve('hello');
|
|
515
|
-
|
|
516
|
-
const result = await Result.fromPromise(stringPromise);
|
|
517
|
-
|
|
518
|
-
expect(Result.unwrapOk(result)).toBe('hello');
|
|
519
|
-
|
|
520
|
-
expectType<typeof result, Result<string, unknown>>('=');
|
|
521
|
-
});
|
|
522
|
-
});
|
|
523
|
-
|
|
524
|
-
describe('flatMap', () => {
|
|
525
|
-
test('should chain operations that return Result', () => {
|
|
526
|
-
const divide = (a: number, b: number): Result<number, string> =>
|
|
527
|
-
b === 0
|
|
528
|
-
? Result.err('Division by zero')
|
|
529
|
-
: // eslint-disable-next-line total-functions/no-partial-division
|
|
530
|
-
Result.ok(a / b);
|
|
531
|
-
|
|
532
|
-
const result = Result.flatMap(Result.ok(10), (x) => divide(x, 2));
|
|
533
|
-
|
|
534
|
-
expect(Result.unwrapOk(result)).toBe(5);
|
|
535
|
-
|
|
536
|
-
const error = Result.flatMap(Result.ok(10), (x) => divide(x, 0));
|
|
537
|
-
|
|
538
|
-
expect(Result.unwrapErr(error)).toBe('Division by zero');
|
|
539
|
-
});
|
|
540
|
-
|
|
541
|
-
test('should return Err if input is Err', () => {
|
|
542
|
-
const result = Result.flatMap(Result.err('initial error'), (_: never) =>
|
|
543
|
-
Result.ok(42),
|
|
544
|
-
);
|
|
545
|
-
|
|
546
|
-
expect(Result.unwrapErr(result)).toBe('initial error');
|
|
547
|
-
});
|
|
548
|
-
|
|
549
|
-
test('should support chaining multiple flatMaps', () => {
|
|
550
|
-
const parseNumber = (s: string): Result<number, string> => {
|
|
551
|
-
const n = Result.unwrapOkOr(Num.safeParseFloat(s), Number.NaN);
|
|
552
|
-
|
|
553
|
-
return Number.isNaN(n) ? Result.err('Not a number') : Result.ok(n);
|
|
554
|
-
};
|
|
555
|
-
|
|
556
|
-
const divide = (a: number, b: number): Result<number, string> =>
|
|
557
|
-
// eslint-disable-next-line total-functions/no-partial-division
|
|
558
|
-
b === 0 ? Result.err('Division by zero') : Result.ok(a / b);
|
|
559
|
-
|
|
560
|
-
const result = Result.flatMap(
|
|
561
|
-
Result.flatMap(parseNumber('100'), (x) => divide(x, 2)),
|
|
562
|
-
(x) => Result.ok(x + 10),
|
|
563
|
-
);
|
|
564
|
-
|
|
565
|
-
expect(Result.unwrapOk(result)).toBe(60);
|
|
566
|
-
});
|
|
567
|
-
|
|
568
|
-
test('should support curried form', () => {
|
|
569
|
-
const divide = (a: number, b: number): Result<number, string> =>
|
|
570
|
-
b === 0
|
|
571
|
-
? Result.err('Division by zero')
|
|
572
|
-
: // eslint-disable-next-line total-functions/no-partial-division
|
|
573
|
-
Result.ok(a / b);
|
|
574
|
-
|
|
575
|
-
const divideBy2 = Result.flatMap((x: number) => divide(x, 2));
|
|
576
|
-
|
|
577
|
-
const okResult = Result.ok(10);
|
|
578
|
-
|
|
579
|
-
const result = divideBy2(okResult);
|
|
580
|
-
|
|
581
|
-
assert.isTrue(Result.isOk(result));
|
|
582
|
-
|
|
583
|
-
expect(result.value).toBe(5);
|
|
584
|
-
|
|
585
|
-
const divideByZero = Result.flatMap((x: number) => divide(x, 0));
|
|
586
|
-
|
|
587
|
-
const errorResult = divideByZero(Result.ok(10));
|
|
588
|
-
|
|
589
|
-
assert.isTrue(Result.isErr(errorResult));
|
|
590
|
-
|
|
591
|
-
expect(errorResult.value).toBe('Division by zero');
|
|
592
|
-
|
|
593
|
-
const initialError = divideBy2(Result.err('initial error'));
|
|
594
|
-
|
|
595
|
-
assert.isTrue(Result.isErr(initialError));
|
|
596
|
-
|
|
597
|
-
expect(initialError.value).toBe('initial error');
|
|
598
|
-
});
|
|
599
|
-
|
|
600
|
-
test('should work with pipe when curried', () => {
|
|
601
|
-
const parseNumber = (s: string): Result<number, string> => {
|
|
602
|
-
const n = Result.unwrapOkOr(Num.safeParseFloat(s), Number.NaN);
|
|
603
|
-
|
|
604
|
-
return Number.isNaN(n) ? Result.err('Not a number') : Result.ok(n);
|
|
605
|
-
};
|
|
606
|
-
|
|
607
|
-
const doubleIfPositive = (n: number): Result<number, string> =>
|
|
608
|
-
n > 0 ? Result.ok(n * 2) : Result.err('Not positive');
|
|
609
|
-
|
|
610
|
-
const parser = Result.flatMap(parseNumber);
|
|
611
|
-
|
|
612
|
-
const doubler = Result.flatMap(doubleIfPositive);
|
|
613
|
-
|
|
614
|
-
const result = pipe(Result.ok('42')).map(parser).map(doubler).value;
|
|
615
|
-
|
|
616
|
-
assert.isTrue(Result.isOk(result));
|
|
617
|
-
|
|
618
|
-
expect(result.value).toBe(84);
|
|
619
|
-
});
|
|
620
|
-
});
|
|
621
|
-
|
|
622
|
-
describe('swap', () => {
|
|
623
|
-
test('should swap Ok to Err', () => {
|
|
624
|
-
const okResult = Result.ok(42);
|
|
625
|
-
|
|
626
|
-
const swapped = Result.swap(okResult);
|
|
627
|
-
|
|
628
|
-
assert.isTrue(Result.isErr(swapped));
|
|
629
|
-
|
|
630
|
-
expect(Result.unwrapErr(swapped)).toBe(42);
|
|
631
|
-
});
|
|
632
|
-
|
|
633
|
-
test('should swap Err to Ok', () => {
|
|
634
|
-
const errResult = Result.err('error');
|
|
635
|
-
|
|
636
|
-
const swapped = Result.swap(errResult);
|
|
637
|
-
|
|
638
|
-
assert.isTrue(Result.isOk(swapped));
|
|
639
|
-
|
|
640
|
-
expect(Result.unwrapOk(swapped)).toBe('error');
|
|
641
|
-
});
|
|
642
|
-
});
|
|
643
|
-
|
|
644
|
-
describe('toOptional', () => {
|
|
645
|
-
test('should convert Ok to Some-like', () => {
|
|
646
|
-
const okResult = Result.ok(42);
|
|
647
|
-
|
|
648
|
-
const optional = Result.toOptional(okResult);
|
|
649
|
-
|
|
650
|
-
assert.isTrue(Optional.isSome(optional));
|
|
651
|
-
|
|
652
|
-
expect(Optional.unwrapThrow(optional)).toBe(42);
|
|
653
|
-
});
|
|
654
|
-
|
|
655
|
-
test('should convert Err to None-like', () => {
|
|
656
|
-
const errResult = Result.err('error');
|
|
657
|
-
|
|
658
|
-
const optional = Result.toOptional(errResult);
|
|
659
|
-
|
|
660
|
-
assert.isTrue(Optional.isNone(optional));
|
|
661
|
-
});
|
|
662
|
-
});
|
|
663
|
-
|
|
664
|
-
describe('unwrapErrThrow', () => {
|
|
665
|
-
test('should return error value for Err', () => {
|
|
666
|
-
const result = Result.err('error message');
|
|
667
|
-
|
|
668
|
-
expect(Result.unwrapErrThrow(result)).toBe('error message');
|
|
669
|
-
});
|
|
670
|
-
|
|
671
|
-
test('should throw for Ok', () => {
|
|
672
|
-
const result = Result.ok(42);
|
|
673
|
-
|
|
674
|
-
expect(() => Result.unwrapErrThrow(result)).toThrow(
|
|
675
|
-
'Expected Err but got Ok: 42',
|
|
676
|
-
);
|
|
677
|
-
});
|
|
678
|
-
|
|
679
|
-
test('should use custom toString function', () => {
|
|
680
|
-
const result = Result.ok({ id: 1, name: 'test' });
|
|
681
|
-
|
|
682
|
-
expect(() =>
|
|
683
|
-
Result.unwrapErrThrow(result, (obj) => `Object(id=${obj.id})`),
|
|
684
|
-
).toThrow('Expected Err but got Ok: Object(id=1)');
|
|
685
|
-
});
|
|
686
|
-
});
|
|
687
|
-
|
|
688
|
-
describe('unwrapErrOr', () => {
|
|
689
|
-
test('should return error value for Err result', () => {
|
|
690
|
-
const result = Result.err('error message');
|
|
691
|
-
|
|
692
|
-
const value = Result.unwrapErrOr(result, 'default');
|
|
693
|
-
|
|
694
|
-
expect(value).toBe('error message');
|
|
695
|
-
});
|
|
696
|
-
|
|
697
|
-
test('should return default value for Ok result', () => {
|
|
698
|
-
const result = Result.ok(42);
|
|
699
|
-
|
|
700
|
-
const value = Result.unwrapErrOr(result, 'default');
|
|
701
|
-
|
|
702
|
-
expect(value).toBe('default');
|
|
703
|
-
});
|
|
704
|
-
|
|
705
|
-
test('should support curried form', () => {
|
|
706
|
-
const unwrapErrorWithDefault = Result.unwrapErrOr('unknown error');
|
|
707
|
-
|
|
708
|
-
const errResult: Result<number, string> = Result.err('failed');
|
|
709
|
-
|
|
710
|
-
const errorValue = unwrapErrorWithDefault(errResult);
|
|
711
|
-
|
|
712
|
-
expect(errorValue).toBe('failed');
|
|
713
|
-
|
|
714
|
-
const okResult: Result<number, string> = Result.ok(42);
|
|
715
|
-
|
|
716
|
-
const defaultValue = unwrapErrorWithDefault(okResult);
|
|
717
|
-
|
|
718
|
-
expect(defaultValue).toBe('unknown error');
|
|
719
|
-
});
|
|
720
|
-
|
|
721
|
-
test('should work with pipe when curried', () => {
|
|
722
|
-
const unwrapErrorWithDefault = Result.unwrapErrOr('unknown error');
|
|
723
|
-
|
|
724
|
-
const errorResult = pipe(Result.err('network failure')).map(
|
|
725
|
-
unwrapErrorWithDefault,
|
|
726
|
-
).value;
|
|
727
|
-
|
|
728
|
-
expect(errorResult).toBe('network failure');
|
|
729
|
-
|
|
730
|
-
const okResult = pipe(Result.ok('success')).map(
|
|
731
|
-
unwrapErrorWithDefault,
|
|
732
|
-
).value;
|
|
733
|
-
|
|
734
|
-
expect(okResult).toBe('unknown error');
|
|
735
|
-
});
|
|
736
|
-
});
|
|
737
|
-
|
|
738
|
-
describe('expectToBe', () => {
|
|
739
|
-
test('should return value for Ok result', () => {
|
|
740
|
-
const result = Result.ok(42);
|
|
741
|
-
|
|
742
|
-
const value = Result.expectToBe(result, 'Expected valid number');
|
|
743
|
-
|
|
744
|
-
expect(value).toBe(42);
|
|
745
|
-
});
|
|
746
|
-
|
|
747
|
-
test('should throw custom error for Err result', () => {
|
|
748
|
-
const result = Result.err('failed');
|
|
749
|
-
|
|
750
|
-
expect(() => Result.expectToBe(result, 'Operation must succeed')).toThrow(
|
|
751
|
-
'Operation must succeed',
|
|
752
|
-
);
|
|
753
|
-
});
|
|
754
|
-
|
|
755
|
-
test('should support curried form', () => {
|
|
756
|
-
const mustBeOk = Result.expectToBe('Expected successful result');
|
|
757
|
-
|
|
758
|
-
const okResult = Result.ok('success');
|
|
759
|
-
|
|
760
|
-
const value = mustBeOk(okResult);
|
|
761
|
-
|
|
762
|
-
expect(value).toBe('success');
|
|
763
|
-
|
|
764
|
-
const errResult: Result<string, string> = Result.err('failed');
|
|
765
|
-
|
|
766
|
-
expect(() => mustBeOk(errResult)).toThrow('Expected successful result');
|
|
767
|
-
});
|
|
768
|
-
|
|
769
|
-
test('should work with pipe when curried', () => {
|
|
770
|
-
const mustBeOk = Result.expectToBe('Validation failed');
|
|
771
|
-
|
|
772
|
-
const successResult = pipe(Result.ok(100)).map(mustBeOk).value;
|
|
773
|
-
|
|
774
|
-
expect(successResult).toBe(100);
|
|
775
|
-
|
|
776
|
-
expect(
|
|
777
|
-
() => pipe(Result.err('validation error')).map(mustBeOk).value,
|
|
778
|
-
).toThrow('Validation failed');
|
|
779
|
-
});
|
|
780
|
-
});
|
|
781
|
-
|
|
782
|
-
describe('orElse', () => {
|
|
783
|
-
test('should return the first Result if it is Ok', () => {
|
|
784
|
-
const primary = Result.ok(42);
|
|
785
|
-
|
|
786
|
-
const fallback = Result.ok(100);
|
|
787
|
-
|
|
788
|
-
const result = Result.orElse(primary, fallback);
|
|
789
|
-
|
|
790
|
-
expect(Result.unwrapOk(result)).toBe(42);
|
|
791
|
-
});
|
|
792
|
-
|
|
793
|
-
test('should return the alternative if the first is Err', () => {
|
|
794
|
-
const primary = Result.err('error');
|
|
795
|
-
|
|
796
|
-
const fallback = Result.ok('default');
|
|
797
|
-
|
|
798
|
-
const result = Result.orElse(primary, fallback);
|
|
799
|
-
|
|
800
|
-
expect(Result.unwrapOk(result)).toBe('default');
|
|
801
|
-
});
|
|
802
|
-
|
|
803
|
-
test('should return Err if both are Err', () => {
|
|
804
|
-
const primary = Result.err('error1');
|
|
805
|
-
|
|
806
|
-
const fallback = Result.err('error2');
|
|
807
|
-
|
|
808
|
-
const result = Result.orElse(primary, fallback);
|
|
809
|
-
|
|
810
|
-
expect(Result.unwrapErr(result)).toBe('error2');
|
|
811
|
-
});
|
|
812
|
-
|
|
813
|
-
test('should support curried form', () => {
|
|
814
|
-
const fallbackTo = Result.orElse(Result.ok('fallback'));
|
|
815
|
-
|
|
816
|
-
const okResult = Result.ok('primary');
|
|
817
|
-
|
|
818
|
-
const result = fallbackTo(okResult);
|
|
819
|
-
|
|
820
|
-
assert.isTrue(Result.isOk(result));
|
|
821
|
-
|
|
822
|
-
expect(result.value).toBe('primary');
|
|
823
|
-
|
|
824
|
-
const errResult: Result<string, string> = Result.err('failed');
|
|
825
|
-
|
|
826
|
-
const fallbackResult = fallbackTo(errResult);
|
|
827
|
-
|
|
828
|
-
assert.isTrue(Result.isOk(fallbackResult));
|
|
829
|
-
|
|
830
|
-
expect(fallbackResult.value).toBe('fallback');
|
|
831
|
-
});
|
|
832
|
-
|
|
833
|
-
test('should work with pipe when curried', () => {
|
|
834
|
-
const fallbackTo = Result.orElse(Result.ok('backup'));
|
|
835
|
-
|
|
836
|
-
const okResult = pipe(Result.ok('original')).map(fallbackTo).value;
|
|
837
|
-
|
|
838
|
-
assert.isTrue(Result.isOk(okResult));
|
|
839
|
-
|
|
840
|
-
expect(okResult.value).toBe('original');
|
|
841
|
-
|
|
842
|
-
const errResult = pipe(Result.err('network error')).map(fallbackTo).value;
|
|
843
|
-
|
|
844
|
-
assert.isTrue(Result.isOk(errResult));
|
|
845
|
-
|
|
846
|
-
expect(errResult.value).toBe('backup');
|
|
847
|
-
});
|
|
848
|
-
});
|
|
849
|
-
|
|
850
|
-
describe('zip', () => {
|
|
851
|
-
test('should combine two Ok values into a tuple', () => {
|
|
852
|
-
const a = Result.ok(1);
|
|
853
|
-
|
|
854
|
-
const b = Result.ok('hello');
|
|
855
|
-
|
|
856
|
-
const zipped = Result.zip(a, b);
|
|
857
|
-
|
|
858
|
-
assert.deepStrictEqual(Result.unwrapOk(zipped), [1, 'hello']);
|
|
859
|
-
});
|
|
860
|
-
|
|
861
|
-
test('should return first Err if first is Err', () => {
|
|
862
|
-
const a = Result.err('error1');
|
|
863
|
-
|
|
864
|
-
const b = Result.ok('hello');
|
|
865
|
-
|
|
866
|
-
const zipped = Result.zip(a, b);
|
|
867
|
-
|
|
868
|
-
expect(Result.unwrapErr(zipped)).toBe('error1');
|
|
869
|
-
});
|
|
870
|
-
|
|
871
|
-
test('should return second Err if second is Err', () => {
|
|
872
|
-
const a = Result.ok(1);
|
|
873
|
-
|
|
874
|
-
const b = Result.err('error2');
|
|
875
|
-
|
|
876
|
-
const zipped = Result.zip(a, b);
|
|
877
|
-
|
|
878
|
-
expect(Result.unwrapErr(zipped)).toBe('error2');
|
|
879
|
-
});
|
|
880
|
-
|
|
881
|
-
test('should return first Err if both are Err', () => {
|
|
882
|
-
const a = Result.err('error1');
|
|
883
|
-
|
|
884
|
-
const b = Result.err('error2');
|
|
885
|
-
|
|
886
|
-
const zipped = Result.zip(a, b);
|
|
887
|
-
|
|
888
|
-
expect(Result.unwrapErr(zipped)).toBe('error1');
|
|
889
|
-
});
|
|
890
|
-
});
|
|
891
|
-
|
|
892
|
-
describe('fromThrowable', () => {
|
|
893
|
-
test('should return Ok when function succeeds', () => {
|
|
894
|
-
const result = Result.fromThrowable(() => 42);
|
|
895
|
-
|
|
896
|
-
assert.isTrue(Result.isOk(result));
|
|
897
|
-
|
|
898
|
-
expect(Result.unwrapOk(result)).toBe(42);
|
|
899
|
-
|
|
900
|
-
expectType<typeof result, Result<number, Error>>('<=');
|
|
901
|
-
});
|
|
902
|
-
|
|
903
|
-
test('should return Ok with object when function succeeds', () => {
|
|
904
|
-
const obj = { name: 'test', value: 123 } as const;
|
|
905
|
-
|
|
906
|
-
const result = Result.fromThrowable(() => obj);
|
|
907
|
-
|
|
908
|
-
assert.isTrue(Result.isOk(result));
|
|
909
|
-
|
|
910
|
-
assert.deepStrictEqual(Result.unwrapOk(result), obj);
|
|
911
|
-
});
|
|
912
|
-
|
|
913
|
-
test('should return Err when function throws Error', () => {
|
|
914
|
-
const errorMessage = 'Something went wrong';
|
|
915
|
-
|
|
916
|
-
const result = Result.fromThrowable(() => {
|
|
917
|
-
throw new Error(errorMessage);
|
|
918
|
-
});
|
|
919
|
-
|
|
920
|
-
assert.isTrue(Result.isErr(result));
|
|
921
|
-
|
|
922
|
-
const error = result.value;
|
|
923
|
-
|
|
924
|
-
expect(error).toBeInstanceOf(Error);
|
|
925
|
-
|
|
926
|
-
expect(error.message).toBe(errorMessage);
|
|
927
|
-
});
|
|
928
|
-
|
|
929
|
-
test('should return Err when function throws string', () => {
|
|
930
|
-
const errorMessage = 'String error';
|
|
931
|
-
|
|
932
|
-
const result = Result.fromThrowable(() => {
|
|
933
|
-
// eslint-disable-next-line @typescript-eslint/only-throw-error
|
|
934
|
-
throw errorMessage;
|
|
935
|
-
});
|
|
936
|
-
|
|
937
|
-
assert.isTrue(Result.isErr(result));
|
|
938
|
-
|
|
939
|
-
const error = result.value;
|
|
940
|
-
|
|
941
|
-
expect(error).toBeInstanceOf(Error);
|
|
942
|
-
|
|
943
|
-
expect(error.message).toBe(errorMessage);
|
|
944
|
-
});
|
|
945
|
-
|
|
946
|
-
test('should return Err when function throws non-string primitive', () => {
|
|
947
|
-
const result = Result.fromThrowable(() => {
|
|
948
|
-
// eslint-disable-next-line @typescript-eslint/only-throw-error
|
|
949
|
-
throw 404;
|
|
950
|
-
});
|
|
951
|
-
|
|
952
|
-
assert.isTrue(Result.isErr(result));
|
|
953
|
-
|
|
954
|
-
const error = result.value;
|
|
955
|
-
|
|
956
|
-
expect(error).toBeInstanceOf(Error);
|
|
957
|
-
|
|
958
|
-
expect(error.message).toBe('404');
|
|
959
|
-
});
|
|
960
|
-
|
|
961
|
-
test('should work with JSON.parse', () => {
|
|
962
|
-
const validJson = '{"key": "value"}';
|
|
963
|
-
|
|
964
|
-
const invalidJson = '{invalid json}';
|
|
965
|
-
|
|
966
|
-
const validResult = Result.fromThrowable(() => JSON.parse(validJson));
|
|
967
|
-
|
|
968
|
-
assert.isTrue(Result.isOk(validResult));
|
|
969
|
-
|
|
970
|
-
assert.deepStrictEqual(Result.unwrapOk(validResult), { key: 'value' });
|
|
971
|
-
|
|
972
|
-
const invalidResult = Result.fromThrowable(() => JSON.parse(invalidJson));
|
|
973
|
-
|
|
974
|
-
assert.isTrue(Result.isErr(invalidResult));
|
|
975
|
-
|
|
976
|
-
const error = invalidResult.value;
|
|
977
|
-
|
|
978
|
-
expect(error).toBeInstanceOf(Error);
|
|
979
|
-
});
|
|
980
|
-
|
|
981
|
-
test('should work with array access', () => {
|
|
982
|
-
const arr = [1, 2, 3] as readonly number[];
|
|
983
|
-
|
|
984
|
-
// This won't throw, but demonstrates the pattern
|
|
985
|
-
const result = Result.fromThrowable(() => {
|
|
986
|
-
const index = 5;
|
|
987
|
-
|
|
988
|
-
const value = arr[index];
|
|
989
|
-
|
|
990
|
-
if (value === undefined) {
|
|
991
|
-
throw new Error('Index out of bounds');
|
|
992
|
-
}
|
|
993
|
-
|
|
994
|
-
return value;
|
|
995
|
-
});
|
|
996
|
-
|
|
997
|
-
assert.isTrue(Result.isErr(result));
|
|
998
|
-
|
|
999
|
-
const error = result.value;
|
|
1000
|
-
|
|
1001
|
-
expect(error.message).toBe('Index out of bounds');
|
|
1002
|
-
});
|
|
1003
|
-
|
|
1004
|
-
test('should preserve function return type', () => {
|
|
1005
|
-
expectTypeOf(Result.fromThrowable(() => 'hello')).toEqualTypeOf<
|
|
1006
|
-
Result<string, Error>
|
|
1007
|
-
>();
|
|
1008
|
-
|
|
1009
|
-
expectTypeOf(Result.fromThrowable(() => 42)).toEqualTypeOf<
|
|
1010
|
-
Result<number, Error>
|
|
1011
|
-
>();
|
|
1012
|
-
|
|
1013
|
-
expectTypeOf(Result.fromThrowable(() => true)).toEqualTypeOf<
|
|
1014
|
-
Result<boolean, Error>
|
|
1015
|
-
>();
|
|
1016
|
-
});
|
|
1017
|
-
});
|
|
1018
|
-
|
|
1019
|
-
describe('match', () => {
|
|
1020
|
-
test('should apply the ok case for Ok', () => {
|
|
1021
|
-
const result = Result.match(Result.ok(21), {
|
|
1022
|
-
ok: (value) => value * 2,
|
|
1023
|
-
err: () => 0,
|
|
1024
|
-
});
|
|
1025
|
-
|
|
1026
|
-
expectType<typeof result, number>('=');
|
|
1027
|
-
|
|
1028
|
-
assert.deepStrictEqual(result, 42);
|
|
1029
|
-
});
|
|
1030
|
-
|
|
1031
|
-
test('should apply the err case for Err', () => {
|
|
1032
|
-
const result = Result.match(Result.err('failure'), {
|
|
1033
|
-
ok: () => 'ok',
|
|
1034
|
-
err: (error) => `error: ${error}`,
|
|
1035
|
-
});
|
|
1036
|
-
|
|
1037
|
-
assert.deepStrictEqual(result, 'error: failure');
|
|
1038
|
-
});
|
|
1039
|
-
|
|
1040
|
-
test('should support Result union inputs', () => {
|
|
1041
|
-
// Created through a function so that the const is not flow-narrowed to
|
|
1042
|
-
// the Ok side.
|
|
1043
|
-
const makeResult = (): Result<number, string> => Result.ok(2);
|
|
1044
|
-
|
|
1045
|
-
const result = makeResult();
|
|
1046
|
-
|
|
1047
|
-
const folded = Result.match(result, {
|
|
1048
|
-
ok: (value) => value + 1,
|
|
1049
|
-
err: (error) => error.length,
|
|
1050
|
-
});
|
|
1051
|
-
|
|
1052
|
-
expectType<typeof folded, number>('=');
|
|
1053
|
-
|
|
1054
|
-
assert.deepStrictEqual(folded, 3);
|
|
1055
|
-
});
|
|
1056
|
-
|
|
1057
|
-
test('curried version should work', () => {
|
|
1058
|
-
const matcher = Result.match({
|
|
1059
|
-
ok: (value: number) => value + 1,
|
|
1060
|
-
err: (error: string) => error.length,
|
|
1061
|
-
});
|
|
1062
|
-
|
|
1063
|
-
assert.deepStrictEqual(matcher(Result.ok(1)), 2);
|
|
1064
|
-
|
|
1065
|
-
assert.deepStrictEqual(matcher(Result.err('oops')), 4);
|
|
1066
|
-
});
|
|
1067
|
-
|
|
1068
|
-
test('curried version should compose with Array.map and pipe', () => {
|
|
1069
|
-
const matcher = Result.match({
|
|
1070
|
-
ok: (value: number) => `ok:${value}`,
|
|
1071
|
-
err: (error: string) => `err:${error}`,
|
|
1072
|
-
});
|
|
1073
|
-
|
|
1074
|
-
const results: readonly Result<number, string>[] = [
|
|
1075
|
-
Result.ok(1),
|
|
1076
|
-
Result.err('bad'),
|
|
1077
|
-
] as const;
|
|
1078
|
-
|
|
1079
|
-
assert.deepStrictEqual(results.map(matcher), ['ok:1', 'err:bad']);
|
|
1080
|
-
|
|
1081
|
-
const single: Result<number, string> = Result.ok(3);
|
|
1082
|
-
|
|
1083
|
-
assert.deepStrictEqual(pipe(single).map(matcher).value, 'ok:3');
|
|
1084
|
-
});
|
|
1085
|
-
});
|
|
1086
|
-
|
|
1087
|
-
describe('fromOptional', () => {
|
|
1088
|
-
test('should convert Some to Ok', () => {
|
|
1089
|
-
const result = Result.fromOptional(Optional.some(42), 'missing');
|
|
1090
|
-
|
|
1091
|
-
expectType<typeof result, Result<42, 'missing'>>('=');
|
|
1092
|
-
|
|
1093
|
-
assert.deepStrictEqual(result, Result.ok(42));
|
|
1094
|
-
});
|
|
1095
|
-
|
|
1096
|
-
test('should convert None to Err with the provided error', () => {
|
|
1097
|
-
const result = Result.fromOptional(Optional.none, 'missing');
|
|
1098
|
-
|
|
1099
|
-
assert.isTrue(Result.isErr(result));
|
|
1100
|
-
|
|
1101
|
-
assert.deepStrictEqual(result.value, 'missing');
|
|
1102
|
-
});
|
|
1103
|
-
|
|
1104
|
-
test('should convert Some(undefined) to Ok(undefined), not Err', () => {
|
|
1105
|
-
const makeOptional = (): Optional<undefined> => Optional.some(undefined);
|
|
1106
|
-
|
|
1107
|
-
const result = Result.fromOptional(makeOptional(), 'missing');
|
|
1108
|
-
|
|
1109
|
-
assert.isTrue(Result.isOk(result));
|
|
1110
|
-
|
|
1111
|
-
assert.deepStrictEqual(result.value, undefined);
|
|
1112
|
-
});
|
|
1113
|
-
|
|
1114
|
-
test('should support Optional union inputs', () => {
|
|
1115
|
-
// Created through a function so that the const is not flow-narrowed to
|
|
1116
|
-
// the Some side.
|
|
1117
|
-
const makeOptional = (): Optional<number> => Optional.some(2);
|
|
1118
|
-
|
|
1119
|
-
const result = Result.fromOptional(makeOptional(), 'missing');
|
|
1120
|
-
|
|
1121
|
-
expectType<typeof result, Result<number, 'missing'>>('=');
|
|
1122
|
-
|
|
1123
|
-
assert.deepStrictEqual(result, Result.ok(2));
|
|
1124
|
-
});
|
|
1125
|
-
|
|
1126
|
-
test('curried version should work', () => {
|
|
1127
|
-
const withMissingError = Result.fromOptional('missing');
|
|
1128
|
-
|
|
1129
|
-
const result = withMissingError(Optional.some(1));
|
|
1130
|
-
|
|
1131
|
-
expectType<typeof result, Result<1, 'missing'>>('=');
|
|
1132
|
-
|
|
1133
|
-
assert.deepStrictEqual(result, Result.ok(1));
|
|
1134
|
-
|
|
1135
|
-
assert.deepStrictEqual(
|
|
1136
|
-
withMissingError(Optional.none),
|
|
1137
|
-
Result.err('missing'),
|
|
1138
|
-
);
|
|
1139
|
-
});
|
|
1140
|
-
});
|
|
1141
|
-
|
|
1142
|
-
describe('safeTry', () => {
|
|
1143
|
-
test('should return the body result when every yielded Result is Ok', () => {
|
|
1144
|
-
const result = Result.safeTry(function* () {
|
|
1145
|
-
const x = yield* Result.safeUnwrap(Result.ok(2));
|
|
1146
|
-
|
|
1147
|
-
const y = yield* Result.safeUnwrap(Result.ok(3));
|
|
1148
|
-
|
|
1149
|
-
return Result.ok(x + y);
|
|
1150
|
-
});
|
|
1151
|
-
|
|
1152
|
-
expectType<typeof result, Result<number, never>>('=');
|
|
1153
|
-
|
|
1154
|
-
assert.deepStrictEqual(result, Result.ok(5));
|
|
1155
|
-
});
|
|
1156
|
-
|
|
1157
|
-
test('should short-circuit on the first Err', () => {
|
|
1158
|
-
const makeFailing = (): Result<number, string> =>
|
|
1159
|
-
Result.err('first error');
|
|
1160
|
-
|
|
1161
|
-
const failing = makeFailing();
|
|
1162
|
-
|
|
1163
|
-
let mut_reached = false;
|
|
1164
|
-
|
|
1165
|
-
const result = Result.safeTry(function* () {
|
|
1166
|
-
const x = yield* Result.safeUnwrap(failing);
|
|
1167
|
-
|
|
1168
|
-
mut_reached = true;
|
|
1169
|
-
|
|
1170
|
-
return Result.ok(x);
|
|
1171
|
-
});
|
|
1172
|
-
|
|
1173
|
-
assert.isFalse(mut_reached);
|
|
1174
|
-
|
|
1175
|
-
assert.deepStrictEqual(result, Result.err('first error'));
|
|
1176
|
-
});
|
|
1177
|
-
|
|
1178
|
-
test('should collect error types from yields and the return', () => {
|
|
1179
|
-
const makeSource = (): Result<number, 'e1'> => Result.ok(1);
|
|
1180
|
-
|
|
1181
|
-
const source = makeSource();
|
|
1182
|
-
|
|
1183
|
-
const result = Result.safeTry(function* () {
|
|
1184
|
-
const x = yield* Result.safeUnwrap(source);
|
|
1185
|
-
|
|
1186
|
-
if (x > 0) {
|
|
1187
|
-
return Result.err('e2');
|
|
1188
|
-
}
|
|
1189
|
-
|
|
1190
|
-
return Result.ok(x);
|
|
1191
|
-
});
|
|
1192
|
-
|
|
1193
|
-
expectType<typeof result, Result<number, 'e1' | 'e2'>>('=');
|
|
1194
|
-
|
|
1195
|
-
assert.deepStrictEqual(result, Result.err('e2'));
|
|
1196
|
-
});
|
|
1197
|
-
|
|
1198
|
-
test('async generator should return a Promise of the body result', async () => {
|
|
1199
|
-
const result = await Result.safeTry(async function* () {
|
|
1200
|
-
const x = yield* Result.safeUnwrap(
|
|
1201
|
-
await Result.fromPromise(Promise.resolve(10)),
|
|
1202
|
-
);
|
|
1203
|
-
|
|
1204
|
-
return Result.ok(x + 1);
|
|
1205
|
-
});
|
|
1206
|
-
|
|
1207
|
-
assert.deepStrictEqual(result, Result.ok(11));
|
|
1208
|
-
});
|
|
1209
|
-
|
|
1210
|
-
test('async generator should short-circuit on Err', async () => {
|
|
1211
|
-
const makeFailing = (): Promise<Result<number, string>> =>
|
|
1212
|
-
Promise.resolve(Result.err('async error'));
|
|
1213
|
-
|
|
1214
|
-
let mut_reached = false;
|
|
1215
|
-
|
|
1216
|
-
const promise = Result.safeTry(async function* () {
|
|
1217
|
-
const x = yield* Result.safeUnwrap(await makeFailing());
|
|
1218
|
-
|
|
1219
|
-
mut_reached = true;
|
|
1220
|
-
|
|
1221
|
-
return Result.ok(x);
|
|
1222
|
-
});
|
|
1223
|
-
|
|
1224
|
-
expectType<typeof promise, Promise<Result<number, string>>>('=');
|
|
1225
|
-
|
|
1226
|
-
const result = await promise;
|
|
1227
|
-
|
|
1228
|
-
assert.isFalse(mut_reached);
|
|
1229
|
-
|
|
1230
|
-
assert.deepStrictEqual(result, Result.err('async error'));
|
|
1231
|
-
});
|
|
1232
|
-
|
|
1233
|
-
test('should run pending `finally` blocks when short-circuiting', () => {
|
|
1234
|
-
// Created through a function so that the const is not flow-narrowed to
|
|
1235
|
-
// the Err side.
|
|
1236
|
-
const makeFailing = (): Result<number, string> => Result.err('boom');
|
|
1237
|
-
|
|
1238
|
-
let mut_cleanedUp = false;
|
|
1239
|
-
|
|
1240
|
-
const result = Result.safeTry(function* () {
|
|
1241
|
-
try {
|
|
1242
|
-
const x = yield* Result.safeUnwrap(makeFailing());
|
|
1243
|
-
|
|
1244
|
-
return Result.ok(x);
|
|
1245
|
-
} finally {
|
|
1246
|
-
mut_cleanedUp = true;
|
|
1247
|
-
}
|
|
1248
|
-
});
|
|
1249
|
-
|
|
1250
|
-
assert.isTrue(mut_cleanedUp);
|
|
1251
|
-
|
|
1252
|
-
assert.deepStrictEqual(result, Result.err('boom'));
|
|
1253
|
-
});
|
|
1254
|
-
|
|
1255
|
-
test('should propagate an exception thrown by the body', () => {
|
|
1256
|
-
expect(() =>
|
|
1257
|
-
Result.safeTry(function* () {
|
|
1258
|
-
const x = yield* Result.safeUnwrap(Result.ok(1));
|
|
1259
|
-
|
|
1260
|
-
throw new Error(`boom: ${x}`);
|
|
1261
|
-
}),
|
|
1262
|
-
).toThrow('boom: 1');
|
|
1263
|
-
});
|
|
1264
|
-
|
|
1265
|
-
test('async generator should reject on an exception thrown by the body', async () => {
|
|
1266
|
-
await expect(
|
|
1267
|
-
Result.safeTry(async function* () {
|
|
1268
|
-
const x = yield* Result.safeUnwrap(
|
|
1269
|
-
await Promise.resolve(Result.ok(1)),
|
|
1270
|
-
);
|
|
1271
|
-
|
|
1272
|
-
throw new Error(`boom: ${x}`);
|
|
1273
|
-
}),
|
|
1274
|
-
).rejects.toThrow('boom: 1');
|
|
1275
|
-
});
|
|
1276
|
-
|
|
1277
|
-
test('should propagate through a delegating helper generator', () => {
|
|
1278
|
-
const makeSource = (): Result<number, 'e1'> => Result.err('e1');
|
|
1279
|
-
|
|
1280
|
-
const doubled = function* (): Generator<Err<'e1'>, number, unknown> {
|
|
1281
|
-
const x = yield* Result.safeUnwrap(makeSource());
|
|
1282
|
-
|
|
1283
|
-
return x * 2;
|
|
1284
|
-
};
|
|
1285
|
-
|
|
1286
|
-
const result = Result.safeTry(function* () {
|
|
1287
|
-
const y = yield* doubled();
|
|
1288
|
-
|
|
1289
|
-
return Result.ok(y);
|
|
1290
|
-
});
|
|
1291
|
-
|
|
1292
|
-
expectType<typeof result, Result<number, 'e1'>>('=');
|
|
1293
|
-
|
|
1294
|
-
assert.deepStrictEqual(result, Result.err('e1'));
|
|
1295
|
-
});
|
|
1296
|
-
|
|
1297
|
-
test('async generator should run pending `finally` blocks too', async () => {
|
|
1298
|
-
const makeFailing = (): Promise<Result<number, string>> =>
|
|
1299
|
-
Promise.resolve(Result.err('boom'));
|
|
1300
|
-
|
|
1301
|
-
let mut_cleanedUp = false;
|
|
1302
|
-
|
|
1303
|
-
const result = await Result.safeTry(async function* () {
|
|
1304
|
-
try {
|
|
1305
|
-
const x = yield* Result.safeUnwrap(await makeFailing());
|
|
1306
|
-
|
|
1307
|
-
return Result.ok(x);
|
|
1308
|
-
} finally {
|
|
1309
|
-
mut_cleanedUp = true;
|
|
1310
|
-
}
|
|
1311
|
-
});
|
|
1312
|
-
|
|
1313
|
-
assert.isTrue(mut_cleanedUp);
|
|
1314
|
-
|
|
1315
|
-
assert.deepStrictEqual(result, Result.err('boom'));
|
|
1316
|
-
});
|
|
1317
|
-
});
|
|
1318
|
-
|
|
1319
|
-
describe('safeUnwrap', () => {
|
|
1320
|
-
test('should return the value without yielding for an Ok', () => {
|
|
1321
|
-
const generator = Result.safeUnwrap(Result.ok(7));
|
|
1322
|
-
|
|
1323
|
-
assert.deepStrictEqual(generator.next(), { done: true, value: 7 });
|
|
1324
|
-
});
|
|
1325
|
-
|
|
1326
|
-
test('should yield the Err itself for an Err', () => {
|
|
1327
|
-
const makeFailing = (): Result<number, string> => Result.err('boom');
|
|
1328
|
-
|
|
1329
|
-
const generator = Result.safeUnwrap(makeFailing());
|
|
1330
|
-
|
|
1331
|
-
assert.deepStrictEqual(generator.next(), {
|
|
1332
|
-
done: false,
|
|
1333
|
-
value: Result.err('boom'),
|
|
1334
|
-
});
|
|
1335
|
-
});
|
|
1336
|
-
|
|
1337
|
-
test('should throw if resumed after yielding an Err', () => {
|
|
1338
|
-
const makeFailing = (): Result<number, string> => Result.err('boom');
|
|
1339
|
-
|
|
1340
|
-
const generator = Result.safeUnwrap(makeFailing());
|
|
1341
|
-
|
|
1342
|
-
generator.next();
|
|
1343
|
-
|
|
1344
|
-
expect(() => generator.next()).toThrow(
|
|
1345
|
-
'it must only be used via `yield*` inside `safeTry()`',
|
|
1346
|
-
);
|
|
1347
|
-
});
|
|
1348
|
-
});
|
|
1349
|
-
});
|