1 | /**
|
2 | Tools for working easily with `Maybe` and `Result` *together*... but which do
|
3 | not *require* you to use both. If they were in the `true-myth/maybe` or
|
4 | `true-myth/result` modules, then importing either would always include the
|
5 | other. While that is not usually a concern with bundlers, it *is* an issue
|
6 | when using dynamic imports or otherwise doing runtime resolution in a browser
|
7 | or similar environment.
|
8 |
|
9 | The flip side of that is: importing from *this* module *does* require access
|
10 | to both `Maybe` and `Result` modules.
|
11 |
|
12 | @module
|
13 | */
|
14 | import Result from './result.js';
|
15 | import Maybe from './maybe.js';
|
16 | /**
|
17 | Transposes a {@linkcode Result} of a {@linkcode Maybe} into a `Maybe` of a
|
18 | `Result`.
|
19 |
|
20 | | Input | Output |
|
21 | | ------------- | -------------- |
|
22 | | `Ok(Just(T))` | `Just(Ok(T))` |
|
23 | | `Err(E)` | `Just(Err(E))` |
|
24 | | `Ok(Nothing)` | `Nothing` |
|
25 |
|
26 | @param result a `Result<Maybe<T>, E>` to transform to a `Maybe<Result<T, E>>`.
|
27 | */
|
28 | export declare function transposeResult<T, E>(result: Result<Maybe<T>, E>): Maybe<Result<T, E>>;
|
29 | /**
|
30 | Convert a {@linkcode Result} to a {@linkcode Maybe}.
|
31 |
|
32 | The converted type will be {@linkcode Maybe~Just Just} if the `Result` is
|
33 | {@linkcode Result~Ok Ok} or {@linkcode Maybe~Nothing Nothing} if the `Result`
|
34 | is {@linkcode Result~Err Err}; the wrapped error value will be discarded.
|
35 |
|
36 | @param result The `Result` to convert to a `Maybe`
|
37 | @returns `Just` the value in `result` if it is `Ok`; otherwise `Nothing`
|
38 | */
|
39 | export declare function toMaybe<T extends {}>(result: Result<T, unknown>): Maybe<T>;
|
40 | /**
|
41 | Transform a {@linkcode Maybe~Maybe Maybe} into a {@linkcode Result~Result Result}.
|
42 |
|
43 | If the `Maybe` is a {@linkcode Maybe~Just Just}, its value will be wrapped in
|
44 | the {@linkcode Result~Ok Ok} variant; if it is a
|
45 | {@linkcode Maybe~Nothing Nothing} the `errValue` will be wrapped in the
|
46 | {@linkcode Result~Err Err} variant.
|
47 |
|
48 | @param errValue A value to wrap in an `Err` if `maybe` is a `Nothing`.
|
49 | @param maybe The `Maybe` to convert to a `Result`.
|
50 | */
|
51 | export declare function fromMaybe<T, E>(errValue: E, maybe: Maybe<T>): Result<T, E>;
|
52 | export declare function fromMaybe<T, E>(errValue: E): (maybe: Maybe<T>) => Result<T, E>;
|
53 | /**
|
54 | Transposes a {@linkcode Maybe} of a {@linkcode Result} into a `Result` of a
|
55 | `Maybe`.
|
56 |
|
57 | | Input | Output |
|
58 | | -------------- | ------------- |
|
59 | | `Just(Ok(T))` | `Ok(Just(T))` |
|
60 | | `Just(Err(E))` | `Err(E)` |
|
61 | | `Nothing` | `Ok(Nothing)` |
|
62 |
|
63 | @param maybe a `Maybe<Result<T, E>>` to transform to a `Result<Maybe<T>, E>>`.
|
64 | */
|
65 | export declare function transposeMaybe<T extends {}, E>(maybe: Maybe<Result<T, E>>): Result<Maybe<T>, E>;
|
66 | /**
|
67 | Transform the {@linkcode Maybe} into a {@linkcode Result}, using the wrapped
|
68 | value as the `Ok` value if `Just`; otherwise using the supplied `error` value
|
69 | for `Err`.
|
70 |
|
71 | @template T The wrapped value.
|
72 | @template E The error type to in the `Result`.
|
73 | @param error The error value to use if the `Maybe` is `Nothing`.
|
74 | @param maybe The `Maybe` instance to convert.
|
75 | @returns A `Result` containing the value wrapped in `maybe` in an `Ok`, or
|
76 | `error` in an `Err`.
|
77 | */
|
78 | export declare function toOkOrErr<T, E>(error: E, maybe: Maybe<T>): Result<T, E>;
|
79 | export declare function toOkOrErr<T, E>(error: E): (maybe: Maybe<T>) => Result<T, E>;
|
80 | /**
|
81 | Transform the {@linkcode Maybe} into a {@linkcode Result}, using the wrapped
|
82 | value as the `Ok` value if `Just`; otherwise using `elseFn` to generate `Err`.
|
83 |
|
84 | @template T The wrapped value.
|
85 | @template E The error type to in the `Result`.
|
86 | @param elseFn The function which generates an error of type `E`.
|
87 | @param maybe The `Maybe` instance to convert.
|
88 | @returns A `Result` containing the value wrapped in `maybe` in an `Ok`, or
|
89 | the value generated by `elseFn` in an `Err`.
|
90 | */
|
91 | export declare function toOkOrElseErr<T, E>(elseFn: () => E, maybe: Maybe<T>): Result<T, E>;
|
92 | export declare function toOkOrElseErr<T, E>(elseFn: () => E): (maybe: Maybe<T>) => Result<T, E>;
|
93 | /**
|
94 | Construct a {@linkcode Maybe~Maybe Maybe<T>} from a
|
95 | {@linkcode Result~Result Result<T, E>}.
|
96 |
|
97 | If the `Result` is an `Ok`, wrap its value in `Just`. If the `Result` is an
|
98 | `Err`, throw away the wrapped `E` and transform to a
|
99 | {@linkcode Maybe~Nothing Nothing}.
|
100 |
|
101 | @template T The type of the value wrapped in a `Result.Ok` and in the `Just`
|
102 | of the resulting `Maybe`.
|
103 | @param result The `Result` to construct a `Maybe` from.
|
104 | @returns `Just` if `result` was `Ok` or `Nothing` if it was `Err`.
|
105 | */
|
106 | export declare function fromResult<T extends {}>(result: Result<T, unknown>): Maybe<T>;
|
107 | //# sourceMappingURL=toolbelt.d.ts.map |
\ | No newline at end of file |