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
package/src/functional/pipe.mts
DELETED
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
import { expectType } from '../expect-type.mjs';
|
|
2
|
-
import { Optional, type UnknownOptional } from './optional/index.mjs';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Creates a new pipe object that allows for chaining operations on a value.
|
|
6
|
-
*
|
|
7
|
-
* This function provides a fluent interface for applying transformations to
|
|
8
|
-
* values, with intelligent method selection based on the input type:
|
|
9
|
-
*
|
|
10
|
-
* - For `Optional` values: Provides `mapOptional` for safe Optional
|
|
11
|
-
* transformations
|
|
12
|
-
* - For other values: Provides `mapNullable` for null-safe transformations
|
|
13
|
-
* - All types get the basic `map` method for general transformations
|
|
14
|
-
*
|
|
15
|
-
* The pipe maintains type safety throughout the chain, automatically selecting
|
|
16
|
-
* the appropriate overload based on the current value type.
|
|
17
|
-
*
|
|
18
|
-
* @template A The type of the initial value to wrap in a pipe.
|
|
19
|
-
* @param a The initial value to wrap in a pipe.
|
|
20
|
-
* @returns A pipe object with chaining methods appropriate for the value type.
|
|
21
|
-
*/
|
|
22
|
-
export const pipe = <const A,>(a: A): Pipe<A> =>
|
|
23
|
-
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
24
|
-
({
|
|
25
|
-
value: a,
|
|
26
|
-
map: (fn) => pipe(fn(a)),
|
|
27
|
-
mapNullable: (fn) => pipe(a == null ? undefined : fn(a)),
|
|
28
|
-
|
|
29
|
-
...(Optional.isOptional(a)
|
|
30
|
-
? {
|
|
31
|
-
mapOptional: (fn) => pipe(Optional.map(a, fn)),
|
|
32
|
-
}
|
|
33
|
-
: {}),
|
|
34
|
-
}) satisfies PipeImpl<A> as unknown as Pipe<A>;
|
|
35
|
-
|
|
36
|
-
// `PipeBase<A>` is always present; the conditional only *adds* `mapOptional`
|
|
37
|
-
// for Optional values. Two subtleties motivate this shape:
|
|
38
|
-
//
|
|
39
|
-
// 1. The check is `[A] extends [UnknownOptional]` (a 1-tuple), not the bare
|
|
40
|
-
// `A extends UnknownOptional`. A bare check distributes over unions, so a
|
|
41
|
-
// piped union value such as `Result<D, E> | undefined` would expand into
|
|
42
|
-
// `Pipe<Ok<D>> | Pipe<Err<E>> | Pipe<undefined>`. Calling a chained method
|
|
43
|
-
// on that union of pipe objects forces TypeScript to synthesize a merged
|
|
44
|
-
// call signature, during which a branded member like `Ok<D>` is widened to
|
|
45
|
-
// its constraint (`Ok<number>`) — losing the brand `D`.
|
|
46
|
-
//
|
|
47
|
-
// 2. `value: A` lives on `PipeBase<A>`, *outside* the conditional. If `value`
|
|
48
|
-
// flowed through the conditional, then for a not-yet-resolved generic `A`
|
|
49
|
-
// the deferred true branch narrows `A` to `A & UnknownOptional`, so
|
|
50
|
-
// `.value` would surface as the noisy `(A & UnknownOptional) | A`. Keeping
|
|
51
|
-
// it on the unconditional base yields a clean `A`.
|
|
52
|
-
export type Pipe<A> = PipeBase<A> &
|
|
53
|
-
PipeMapNullable<A> &
|
|
54
|
-
([A] extends [UnknownOptional] ? PipeMapOptional<A> : unknown);
|
|
55
|
-
|
|
56
|
-
// NOTE: 以下では 'typing when used with generics' test で型エラーが出る。
|
|
57
|
-
// 詳細は documents/reports/pipe-typing.md の 「mapNullable を条件付きで含めるのがうまくいかない理由」を参照。
|
|
58
|
-
// export type Pipe<A> = PipeBase<A> &
|
|
59
|
-
// ([A] extends [UnknownOptional] ? PipeMapOptional<A> : unknown) &
|
|
60
|
-
// ([undefined] extends [A] ? PipeMapNullable<A> : unknown) &
|
|
61
|
-
// ([null] extends [A] ? PipeMapNullable<A> : unknown);
|
|
62
|
-
|
|
63
|
-
expectType<Optional<number>, UnknownOptional>('<=');
|
|
64
|
-
|
|
65
|
-
expectType<number | undefined, undefined>('>=');
|
|
66
|
-
|
|
67
|
-
expectType<keyof Pipe<number | undefined>, 'value' | 'map' | 'mapNullable'>(
|
|
68
|
-
'=',
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
expectType<
|
|
72
|
-
keyof Pipe<Optional<number>>,
|
|
73
|
-
'value' | 'map' | 'mapNullable' | 'mapOptional'
|
|
74
|
-
>('=');
|
|
75
|
-
|
|
76
|
-
expectType<
|
|
77
|
-
keyof Pipe<Optional<number | undefined>>,
|
|
78
|
-
'value' | 'map' | 'mapNullable' | 'mapOptional'
|
|
79
|
-
>('=');
|
|
80
|
-
|
|
81
|
-
expectType<
|
|
82
|
-
keyof Pipe<Optional<number> | undefined>,
|
|
83
|
-
'value' | 'map' | 'mapNullable'
|
|
84
|
-
>('=');
|
|
85
|
-
|
|
86
|
-
type PipeBase<A> = Readonly<{
|
|
87
|
-
/** The current value being piped. */
|
|
88
|
-
value: A;
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Maps the current value to a new value using the provided function.
|
|
92
|
-
*
|
|
93
|
-
* @example
|
|
94
|
-
*
|
|
95
|
-
* ```ts
|
|
96
|
-
* const result = pipe(2)
|
|
97
|
-
* .map((value) => value * 3)
|
|
98
|
-
* .map((value) => `value: ${value}`);
|
|
99
|
-
*
|
|
100
|
-
* assert.deepStrictEqual(result.value, 'value: 6');
|
|
101
|
-
* ```
|
|
102
|
-
*
|
|
103
|
-
* @template B The type of the new value.
|
|
104
|
-
* @param fn Function to transform the current value.
|
|
105
|
-
* @returns A new pipe containing the transformed value.
|
|
106
|
-
*/
|
|
107
|
-
map: <B>(fn: (a: A) => B) => Pipe<B>;
|
|
108
|
-
}>;
|
|
109
|
-
|
|
110
|
-
type PipeMapNullable<A> = Readonly<{
|
|
111
|
-
/**
|
|
112
|
-
* Maps the current value only if it's not null or undefined. If the current
|
|
113
|
-
* value is null/undefined, the transformation is skipped and undefined is
|
|
114
|
-
* propagated through the pipe.
|
|
115
|
-
*
|
|
116
|
-
* @example
|
|
117
|
-
*
|
|
118
|
-
* ```ts
|
|
119
|
-
* const result = pipe<string | undefined>('hello')
|
|
120
|
-
* .mapNullable((value) => value.toUpperCase())
|
|
121
|
-
* .mapNullable((value) => value.slice(0, 2));
|
|
122
|
-
*
|
|
123
|
-
* assert.deepStrictEqual(result.value, 'HE');
|
|
124
|
-
*
|
|
125
|
-
* const empty = pipe<string | undefined>(undefined).mapNullable((value) =>
|
|
126
|
-
* value.toUpperCase(),
|
|
127
|
-
* );
|
|
128
|
-
*
|
|
129
|
-
* assert.isTrue(empty.value === undefined);
|
|
130
|
-
* ```
|
|
131
|
-
*
|
|
132
|
-
* @template B The type of the transformed value.
|
|
133
|
-
* @param fn Function to transform the non-null value.
|
|
134
|
-
* @returns A new pipe containing the transformed value or undefined.
|
|
135
|
-
*/
|
|
136
|
-
mapNullable: <B>(fn: (a: NonNullable<A>) => B) => Pipe<B | undefined>;
|
|
137
|
-
}>;
|
|
138
|
-
|
|
139
|
-
type PipeMapOptional<A extends UnknownOptional> = Readonly<{
|
|
140
|
-
/**
|
|
141
|
-
* Maps the value inside an Optional using Optional.map semantics. If the
|
|
142
|
-
* Optional is None, the transformation is skipped and None is propagated.
|
|
143
|
-
* If the Optional is Some, the transformation is applied to the inner
|
|
144
|
-
* value.
|
|
145
|
-
*
|
|
146
|
-
* @example
|
|
147
|
-
*
|
|
148
|
-
* ```ts
|
|
149
|
-
* const result = pipe(Optional.some(10))
|
|
150
|
-
* .mapOptional((value) => value * 2)
|
|
151
|
-
* .mapOptional((value) => value + 5);
|
|
152
|
-
*
|
|
153
|
-
* assert.deepStrictEqual(result.value, Optional.some(25));
|
|
154
|
-
*
|
|
155
|
-
* const empty = pipe(Optional.none as Optional<number>).mapOptional(
|
|
156
|
-
* (value) => value * 2,
|
|
157
|
-
* );
|
|
158
|
-
*
|
|
159
|
-
* assert.isTrue(Optional.isNone(empty.value));
|
|
160
|
-
* ```
|
|
161
|
-
*
|
|
162
|
-
* @template B The type of the transformed inner value.
|
|
163
|
-
* @param fn Function to transform the inner value of the Optional.
|
|
164
|
-
* @returns A new pipe containing an Optional with the transformed value.
|
|
165
|
-
*/
|
|
166
|
-
mapOptional: <B>(fn: (a: Optional.Unwrap<A>) => B) => Pipe<Optional<B>>;
|
|
167
|
-
}>;
|
|
168
|
-
|
|
169
|
-
/** @internal */
|
|
170
|
-
type Cast<T, U> = T & U;
|
|
171
|
-
|
|
172
|
-
/** @internal */
|
|
173
|
-
type PipeImpl<A> = Readonly<{
|
|
174
|
-
value: A;
|
|
175
|
-
map: <B>(fn: (a: A) => B) => PipeImpl<B>;
|
|
176
|
-
mapNullable?: <B>(fn: (a: NonNullable<A>) => B) => PipeImpl<B | undefined>;
|
|
177
|
-
mapOptional?: <B>(
|
|
178
|
-
fn: (a: Optional.Unwrap<Cast<A, UnknownOptional>>) => B,
|
|
179
|
-
) => PipeImpl<Optional<B>>;
|
|
180
|
-
}>;
|
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import { expectType } from '../expect-type.mjs';
|
|
2
|
-
import { Num } from '../number/index.mjs';
|
|
3
|
-
import { type None, type Some } from '../types.mjs';
|
|
4
|
-
import { Optional } from './optional/index.mjs';
|
|
5
|
-
import { pipe } from './pipe.mjs';
|
|
6
|
-
import { Result } from './result/index.mjs';
|
|
7
|
-
|
|
8
|
-
describe(pipe, () => {
|
|
9
|
-
test('basic pipe operations', () => {
|
|
10
|
-
const result = pipe(5)
|
|
11
|
-
.map((x) => x * 2)
|
|
12
|
-
.map((x) => x + 1).value;
|
|
13
|
-
|
|
14
|
-
expect(result).toBe(11);
|
|
15
|
-
|
|
16
|
-
expectType<typeof result, number>('=');
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
test('pipe with string operations', () => {
|
|
20
|
-
const result = pipe('hello')
|
|
21
|
-
.map((s) => s.toUpperCase())
|
|
22
|
-
.map((s) => `${s}!`).value;
|
|
23
|
-
|
|
24
|
-
expect(result).toBe('HELLO!');
|
|
25
|
-
|
|
26
|
-
expectType<typeof result, string>('=');
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
test('pipe with array operations', () => {
|
|
30
|
-
const result = pipe([1, 2, 3])
|
|
31
|
-
.map((arr) => arr.map((x) => x * 2))
|
|
32
|
-
.map((arr) => arr.length).value;
|
|
33
|
-
|
|
34
|
-
expect(result).toBe(3);
|
|
35
|
-
|
|
36
|
-
expectType<typeof result, number>('=');
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test('mapNullable with non-null value', () => {
|
|
40
|
-
const s = 5 as number | null;
|
|
41
|
-
|
|
42
|
-
const result = pipe(s).mapNullable((x) => x * 2).value;
|
|
43
|
-
|
|
44
|
-
expect(result).toBe(10);
|
|
45
|
-
|
|
46
|
-
expectType<typeof result, number | undefined>('=');
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test('mapNullable with null value', () => {
|
|
50
|
-
const result = pipe(null as number | null).mapNullable((x) => x * 2).value;
|
|
51
|
-
|
|
52
|
-
expect(result).toBeUndefined();
|
|
53
|
-
|
|
54
|
-
expectType<typeof result, number | undefined>('=');
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
test('mapNullable with undefined value', () => {
|
|
58
|
-
const result = pipe(undefined as number | undefined).mapNullable(
|
|
59
|
-
(x) => x * 2,
|
|
60
|
-
).value;
|
|
61
|
-
|
|
62
|
-
expect(result).toBeUndefined();
|
|
63
|
-
|
|
64
|
-
expectType<typeof result, number | undefined>('=');
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
test('mapOptional with Some value', () => {
|
|
68
|
-
const optional = Optional.some(42);
|
|
69
|
-
|
|
70
|
-
const result = pipe(optional).mapOptional((x) => x * 2).value;
|
|
71
|
-
|
|
72
|
-
assert.isTrue(Optional.isSome(result));
|
|
73
|
-
|
|
74
|
-
if (Optional.isSome(result)) {
|
|
75
|
-
expect(result.value).toBe(84);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
expectType<typeof result, Some<number>>('=');
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
test('mapOptional with None value', () => {
|
|
82
|
-
const optional: Optional<number> = Optional.none;
|
|
83
|
-
|
|
84
|
-
const result = pipe(optional).mapOptional((x) => x * 2).value;
|
|
85
|
-
|
|
86
|
-
assert.isTrue(Optional.isNone(result));
|
|
87
|
-
|
|
88
|
-
expectType<typeof result, None>('=');
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
test('chaining multiple operations', () => {
|
|
92
|
-
const result = pipe('5')
|
|
93
|
-
.map((s) => Result.unwrapOkOr(Num.safeParseInt(s), Number.NaN))
|
|
94
|
-
.map((n) => n * 3)
|
|
95
|
-
.map((n) => n.toString())
|
|
96
|
-
.map((s) => `${s} items`).value;
|
|
97
|
-
|
|
98
|
-
expect(result).toBe('15 items');
|
|
99
|
-
|
|
100
|
-
expectType<typeof result, string>('=');
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
test('typing when used with Result', () => {
|
|
104
|
-
const validate = <D,>(_u: unknown): Result<D, unknown> =>
|
|
105
|
-
Result.err(undefined);
|
|
106
|
-
|
|
107
|
-
const decodeBranded = (
|
|
108
|
-
input: string,
|
|
109
|
-
parse: (s: string) => number | undefined,
|
|
110
|
-
): number | undefined => {
|
|
111
|
-
const v = pipe(input)
|
|
112
|
-
.map(parse)
|
|
113
|
-
.mapNullable(validate<number>)
|
|
114
|
-
.mapNullable((res) => Result.unwrapOk(res)).value;
|
|
115
|
-
|
|
116
|
-
expectType<typeof v, number | undefined>('=');
|
|
117
|
-
|
|
118
|
-
return v;
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
expect(decodeBranded('', () => undefined)).toBeUndefined();
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
test('typing when used with generics', () => {
|
|
125
|
-
const validate = <D,>(_u: unknown): Result<D, unknown> =>
|
|
126
|
-
Result.err(undefined);
|
|
127
|
-
|
|
128
|
-
// Regression: a generic brand `D` must survive the whole chain.
|
|
129
|
-
//
|
|
130
|
-
// The trigger is a `.map` whose result is a branded *union* such as
|
|
131
|
-
// `D | undefined`. If `Pipe<A>` distributes over unions, that step expands
|
|
132
|
-
// into `Pipe<D> | Pipe<undefined>`; calling the next method on the union of
|
|
133
|
-
// pipe objects then widens `D` to its constraint `number`, so `.value`
|
|
134
|
-
// collapses to `number | undefined` instead of `D | undefined`.
|
|
135
|
-
//
|
|
136
|
-
// The explicit `D | undefined` return annotation is itself the assertion:
|
|
137
|
-
// were the brand lost, `.value` would be `number | undefined` and the
|
|
138
|
-
// body would fail to compile.
|
|
139
|
-
const decodeBranded = <D extends number>(
|
|
140
|
-
input: string,
|
|
141
|
-
parse: (s: string) => D | undefined,
|
|
142
|
-
): D | undefined => {
|
|
143
|
-
const v = pipe(input)
|
|
144
|
-
.map(parse)
|
|
145
|
-
.mapNullable(validate<D>)
|
|
146
|
-
.mapNullable((res) => Result.unwrapOk(res)).value;
|
|
147
|
-
|
|
148
|
-
expectType<typeof v, D | undefined>('=');
|
|
149
|
-
|
|
150
|
-
return v;
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
expect(decodeBranded('', () => undefined)).toBeUndefined();
|
|
154
|
-
});
|
|
155
|
-
});
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
export * from './result-err.mjs';
|
|
2
|
-
export * from './result-expect-to-be.mjs';
|
|
3
|
-
export * from './result-flat-map.mjs';
|
|
4
|
-
export * from './result-fold.mjs';
|
|
5
|
-
export * from './result-from-optional.mjs';
|
|
6
|
-
export * from './result-from-promise.mjs';
|
|
7
|
-
export * from './result-from-throwable.mjs';
|
|
8
|
-
export * from './result-is-err.mjs';
|
|
9
|
-
export * from './result-is-ok.mjs';
|
|
10
|
-
export * from './result-is-result.mjs';
|
|
11
|
-
export * from './result-map-err.mjs';
|
|
12
|
-
export * from './result-map.mjs';
|
|
13
|
-
export * from './result-match.mjs';
|
|
14
|
-
export * from './result-ok.mjs';
|
|
15
|
-
export * from './result-or-else.mjs';
|
|
16
|
-
export * from './result-safe-try.mjs';
|
|
17
|
-
export * from './result-safe-unwrap.mjs';
|
|
18
|
-
export * from './result-swap.mjs';
|
|
19
|
-
export * from './result-to-optional.mjs';
|
|
20
|
-
export * from './result-unwrap-err-or.mjs';
|
|
21
|
-
export * from './result-unwrap-err-throw.mjs';
|
|
22
|
-
export * from './result-unwrap-err.mjs';
|
|
23
|
-
export * from './result-unwrap-ok-or.mjs';
|
|
24
|
-
export * from './result-unwrap-ok.mjs';
|
|
25
|
-
export * from './result-unwrap-throw.mjs';
|
|
26
|
-
export * from './result-zip.mjs';
|
|
27
|
-
export * from './types.mjs';
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { type Err } from '../../../types.mjs';
|
|
2
|
-
import { ErrTypeTagName } from './tag.mjs';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Creates a `Result.Err` containing the given error value.
|
|
6
|
-
*
|
|
7
|
-
* Use this constructor when an operation fails and you want to wrap the error
|
|
8
|
-
* information 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 E The type of the error value.
|
|
26
|
-
* @param value The error value.
|
|
27
|
-
* @returns A `Result.Err<E>` containing the value.
|
|
28
|
-
*/
|
|
29
|
-
export const err = <const E,>(value: E): Err<E> =>
|
|
30
|
-
({
|
|
31
|
-
$$tag: ErrTypeTagName,
|
|
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 { unwrapOk } from './result-unwrap-ok.mjs';
|
|
4
|
-
import { type UnwrapOk } from './types.mjs';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Unwraps a `Result`, returning the success value or throwing an error with
|
|
8
|
-
* the provided message.
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
*
|
|
12
|
-
* ```ts
|
|
13
|
-
* const okValue = Result.ok('data');
|
|
14
|
-
*
|
|
15
|
-
* assert.isTrue(Result.expectToBe(okValue, 'should have value') === 'data');
|
|
16
|
-
*
|
|
17
|
-
* const expectResult = Result.expectToBe('missing result');
|
|
18
|
-
*
|
|
19
|
-
* const throwTest = (): void => {
|
|
20
|
-
* expectResult(Result.err('boom'));
|
|
21
|
-
* };
|
|
22
|
-
*
|
|
23
|
-
* assert.throws(throwTest, /missing result/u);
|
|
24
|
-
*
|
|
25
|
-
* assert.isTrue(expectResult(Result.ok('value')) === 'value');
|
|
26
|
-
* ```
|
|
27
|
-
*
|
|
28
|
-
* @template R The `UnknownResult` type to unwrap.
|
|
29
|
-
* @param result The `Result` to unwrap.
|
|
30
|
-
* @param message The error message to throw if the `Result` is `Result.Err`.
|
|
31
|
-
* @returns The success value if `Result.Ok`.
|
|
32
|
-
* @throws Error with the provided message if the `Result` is `Result.Err`.
|
|
33
|
-
*/
|
|
34
|
-
export function expectToBe<R extends UnknownResult>(
|
|
35
|
-
result: R,
|
|
36
|
-
message: string,
|
|
37
|
-
): UnwrapOk<R>;
|
|
38
|
-
|
|
39
|
-
// Curried version
|
|
40
|
-
export function expectToBe(
|
|
41
|
-
message: string,
|
|
42
|
-
): <R extends UnknownResult>(result: R) => UnwrapOk<R>;
|
|
43
|
-
|
|
44
|
-
export function expectToBe<R extends UnknownResult>(
|
|
45
|
-
...args: readonly [result: R, message: string] | readonly [message: string]
|
|
46
|
-
): UnwrapOk<R> | ((result: R) => UnwrapOk<R>) {
|
|
47
|
-
switch (args.length) {
|
|
48
|
-
case 2: {
|
|
49
|
-
// Direct version: first argument is result
|
|
50
|
-
const [result, message] = args;
|
|
51
|
-
|
|
52
|
-
return expectToBeImpl(result, message);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
case 1: {
|
|
56
|
-
// Curried version: first argument is message
|
|
57
|
-
const [message] = args;
|
|
58
|
-
|
|
59
|
-
return (result: R): UnwrapOk<R> => expectToBeImpl(result, message);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const expectToBeImpl = <R extends UnknownResult>(
|
|
65
|
-
result: R,
|
|
66
|
-
message: string,
|
|
67
|
-
): UnwrapOk<R> => {
|
|
68
|
-
if (isOk(result)) {
|
|
69
|
-
return unwrapOk(result);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
throw new Error(message);
|
|
73
|
-
};
|
|
@@ -1,88 +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 { type UnwrapErr, type UnwrapOk } from './types.mjs';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Applies a function that returns a `Result` to the success value of a
|
|
8
|
-
* `Result`. If the input is `Err`, returns the original `Err`. This is the
|
|
9
|
-
* monadic bind operation for `Result`.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
*
|
|
13
|
-
* ```ts
|
|
14
|
-
* const parseNumber = (input: string): Result<number, string> => {
|
|
15
|
-
* const num = Num.safeParseInt(input);
|
|
16
|
-
*
|
|
17
|
-
* return Result.mapErr(num, () => 'not a number');
|
|
18
|
-
* };
|
|
19
|
-
*
|
|
20
|
-
* const parsed = Result.flatMap(Result.ok('42'), parseNumber);
|
|
21
|
-
*
|
|
22
|
-
* const failure = Result.flatMap(Result.ok('abc'), parseNumber);
|
|
23
|
-
*
|
|
24
|
-
* const passthrough = Result.flatMap(Result.err('fail'), parseNumber);
|
|
25
|
-
*
|
|
26
|
-
* assert.deepStrictEqual(parsed, Result.ok(42));
|
|
27
|
-
*
|
|
28
|
-
* assert.deepStrictEqual(failure, Result.err('not a number'));
|
|
29
|
-
*
|
|
30
|
-
* assert.deepStrictEqual(passthrough, Result.err('fail'));
|
|
31
|
-
*
|
|
32
|
-
* const parseThenDouble = Result.flatMap((input: string) =>
|
|
33
|
-
* Result.map(parseNumber(input), (value) => value * 2),
|
|
34
|
-
* );
|
|
35
|
-
*
|
|
36
|
-
* assert.deepStrictEqual(parseThenDouble(Result.ok('10')), Result.ok(20));
|
|
37
|
-
* ```
|
|
38
|
-
*
|
|
39
|
-
* @template R The input `UnknownResult` type.
|
|
40
|
-
* @template S2 The success type of the `Result` returned by the function.
|
|
41
|
-
* @template E2 The error type of the `Result` returned by the function.
|
|
42
|
-
* @param result The `Result` to flat map.
|
|
43
|
-
* @param flatMapFn The function to apply that returns a `Result`.
|
|
44
|
-
* @returns The result of applying the function, or the original `Err`.
|
|
45
|
-
*/
|
|
46
|
-
export function flatMap<R extends UnknownResult, S2, E2>(
|
|
47
|
-
result: R,
|
|
48
|
-
flatMapFn: (value: UnwrapOk<R>) => Result<S2, E2>,
|
|
49
|
-
): Result<S2, E2 | UnwrapErr<R>>;
|
|
50
|
-
|
|
51
|
-
// Curried version
|
|
52
|
-
export function flatMap<S, S2, E2>(
|
|
53
|
-
flatMapFn: (value: S) => Result<S2, E2>,
|
|
54
|
-
): <E>(result: Result<S, E>) => Result<S2, E | E2>;
|
|
55
|
-
|
|
56
|
-
export function flatMap<R extends UnknownResult, S2, E2>(
|
|
57
|
-
...args:
|
|
58
|
-
| readonly [result: R, flatMapFn: (value: UnwrapOk<R>) => Result<S2, E2>]
|
|
59
|
-
| readonly [flatMapFn: (value: UnwrapOk<R>) => Result<S2, E2>]
|
|
60
|
-
):
|
|
61
|
-
| Result<S2, E2 | UnwrapErr<R>>
|
|
62
|
-
| ((result: R) => Result<S2, UnwrapErr<R> | E2>) {
|
|
63
|
-
switch (args.length) {
|
|
64
|
-
case 2: {
|
|
65
|
-
// Direct version: first argument is result
|
|
66
|
-
const [result, flatMapFn] = args;
|
|
67
|
-
|
|
68
|
-
return flatMapImpl(result, flatMapFn);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
case 1: {
|
|
72
|
-
// Curried version
|
|
73
|
-
const [flatMapFn] = args;
|
|
74
|
-
|
|
75
|
-
return (result: R) => flatMapImpl(result, flatMapFn);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const flatMapImpl = <R extends UnknownResult, S2, E2>(
|
|
81
|
-
result: R,
|
|
82
|
-
flatMapFn: (value: UnwrapOk<R>) => Result<S2, E2>,
|
|
83
|
-
): Result<S2, E2 | UnwrapErr<R>> =>
|
|
84
|
-
isErr(result)
|
|
85
|
-
? // eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
86
|
-
(result as Err<UnwrapErr<R>>)
|
|
87
|
-
: // eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
88
|
-
flatMapFn(result.value as UnwrapOk<R>);
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { type Result, type UnknownResult } from '../result.mjs';
|
|
2
|
-
import { err } from './result-err.mjs';
|
|
3
|
-
import { isOk } from './result-is-ok.mjs';
|
|
4
|
-
import { ok } from './result-ok.mjs';
|
|
5
|
-
import { type UnwrapErr, type UnwrapOk } from './types.mjs';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Applies one of two functions depending on whether the `Result` is `Ok` or
|
|
9
|
-
* `Err`.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
*
|
|
13
|
-
* ```ts
|
|
14
|
-
* const okValue = Result.ok(2);
|
|
15
|
-
*
|
|
16
|
-
* const errValue = Result.err('bad');
|
|
17
|
-
*
|
|
18
|
-
* const foldedOk = Result.fold(
|
|
19
|
-
* okValue,
|
|
20
|
-
* (value) => value * 2,
|
|
21
|
-
* (error) => error,
|
|
22
|
-
* );
|
|
23
|
-
*
|
|
24
|
-
* const foldedErr = Result.fold(
|
|
25
|
-
* errValue,
|
|
26
|
-
* (value: number) => value * 2,
|
|
27
|
-
* (error) => error.toUpperCase(),
|
|
28
|
-
* );
|
|
29
|
-
*
|
|
30
|
-
* assert.deepStrictEqual(foldedOk, Result.ok(4));
|
|
31
|
-
*
|
|
32
|
-
* assert.deepStrictEqual(foldedErr, Result.err('BAD'));
|
|
33
|
-
*
|
|
34
|
-
* const foldNumbers = Result.fold(
|
|
35
|
-
* (value: number) => value * 3,
|
|
36
|
-
* (error: string) => error.length,
|
|
37
|
-
* );
|
|
38
|
-
*
|
|
39
|
-
* assert.deepStrictEqual(foldNumbers(Result.ok(3)), Result.ok(9));
|
|
40
|
-
*
|
|
41
|
-
* assert.deepStrictEqual(foldNumbers(Result.err('oops')), Result.err(4));
|
|
42
|
-
* ```
|
|
43
|
-
*
|
|
44
|
-
* @template R The input `UnknownResult` type.
|
|
45
|
-
* @template S2 The type of the success value returned by `mapFn`.
|
|
46
|
-
* @template E2 The type of the error value returned by `mapErrFn`.
|
|
47
|
-
* @param result The `Result` to fold.
|
|
48
|
-
* @param mapFn The function to apply if `result` is `Ok`.
|
|
49
|
-
* @param mapErrFn The function to apply if `result` is `Err`.
|
|
50
|
-
* @returns A new `Result<S2, E2>` based on the applied function.
|
|
51
|
-
*/
|
|
52
|
-
export function fold<R extends UnknownResult, S2, E2>(
|
|
53
|
-
result: R,
|
|
54
|
-
mapFn: (value: UnwrapOk<R>) => S2,
|
|
55
|
-
mapErrFn: (error: UnwrapErr<R>) => E2,
|
|
56
|
-
): Result<S2, E2>;
|
|
57
|
-
|
|
58
|
-
// Curried version
|
|
59
|
-
export function fold<S, E, S2, E2>(
|
|
60
|
-
mapFn: (value: S) => S2,
|
|
61
|
-
mapErrFn: (error: E) => E2,
|
|
62
|
-
): (result: Result<S, E>) => Result<S2, E2>;
|
|
63
|
-
|
|
64
|
-
export function fold<R extends UnknownResult, S2, E2>(
|
|
65
|
-
...args:
|
|
66
|
-
| readonly [
|
|
67
|
-
result: R,
|
|
68
|
-
mapFn: (value: UnwrapOk<R>) => S2,
|
|
69
|
-
mapErrFn: (error: UnwrapErr<R>) => E2,
|
|
70
|
-
]
|
|
71
|
-
| readonly [
|
|
72
|
-
mapFn: (value: UnwrapOk<R>) => S2,
|
|
73
|
-
mapErrFn: (error: UnwrapErr<R>) => E2,
|
|
74
|
-
]
|
|
75
|
-
): Result<S2, E2> | ((result: R) => Result<S2, E2>) {
|
|
76
|
-
switch (args.length) {
|
|
77
|
-
case 3: {
|
|
78
|
-
// Direct version: first argument is result
|
|
79
|
-
const [result, mapFn, mapErrFn] = args;
|
|
80
|
-
|
|
81
|
-
return foldImpl(result, mapFn, mapErrFn);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
case 2: {
|
|
85
|
-
// Curried version
|
|
86
|
-
const [mapFn, mapErrFn] = args;
|
|
87
|
-
|
|
88
|
-
return (result: R) => foldImpl(result, mapFn, mapErrFn);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const foldImpl = <R extends UnknownResult, S2, E2>(
|
|
94
|
-
result: R,
|
|
95
|
-
mapFn: (value: UnwrapOk<R>) => S2,
|
|
96
|
-
mapErrFn: (error: UnwrapErr<R>) => E2,
|
|
97
|
-
): Result<S2, E2> =>
|
|
98
|
-
isOk(result)
|
|
99
|
-
? // eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
100
|
-
ok(mapFn(result.value as UnwrapOk<R>))
|
|
101
|
-
: // eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
102
|
-
err(mapErrFn(result.value as UnwrapErr<R>));
|