@shirudo/result 1.1.1 → 1.2.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 +6 -2
- package/dist/collections.cjs +181 -9
- package/dist/collections.cjs.map +1 -0
- package/dist/collections.d.cts +101 -3
- package/dist/collections.d.cts.map +1 -0
- package/dist/collections.d.mts +101 -3
- package/dist/collections.d.mts.map +1 -0
- package/dist/collections.mjs +175 -3
- package/dist/collections.mjs.map +1 -0
- package/dist/errors-CVgC7NII.cjs +278 -0
- package/dist/errors-CVgC7NII.cjs.map +1 -0
- package/dist/errors-MuakEcnM.mjs +146 -0
- package/dist/errors-MuakEcnM.mjs.map +1 -0
- package/dist/errors.cjs +22 -130
- package/dist/errors.d.cts +9 -9
- package/dist/errors.d.cts.map +1 -1
- package/dist/errors.d.mts +9 -9
- package/dist/errors.d.mts.map +1 -1
- package/dist/errors.mjs +2 -109
- package/dist/index.cjs +99 -69
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +87 -74
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +87 -74
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +64 -34
- package/dist/index.mjs.map +1 -1
- package/dist/operators.cjs +412 -31
- package/dist/operators.cjs.map +1 -0
- package/dist/operators.d.cts +211 -3
- package/dist/operators.d.cts.map +1 -0
- package/dist/operators.d.mts +211 -3
- package/dist/operators.d.mts.map +1 -0
- package/dist/operators.mjs +384 -3
- package/dist/operators.mjs.map +1 -0
- package/dist/{result-q9Na2bGa.mjs → result-DQCpkuOJ.mjs} +36 -17
- package/dist/result-DQCpkuOJ.mjs.map +1 -0
- package/dist/{result-BBOGxvSH.cjs → result-DoqQufvR.cjs} +36 -17
- package/dist/result-DoqQufvR.cjs.map +1 -0
- package/dist/{sequence-DDwePRLd.d.cts → sequence-CmugKPbu.d.cts} +105 -84
- package/dist/sequence-CmugKPbu.d.cts.map +1 -0
- package/dist/{sequence-DDwePRLd.d.mts → sequence-CmugKPbu.d.mts} +105 -84
- package/dist/sequence-CmugKPbu.d.mts.map +1 -0
- package/package.json +20 -12
- package/dist/errors.cjs.map +0 -1
- package/dist/errors.mjs.map +0 -1
- package/dist/flatten-B_XIkaOK.cjs +0 -208
- package/dist/flatten-B_XIkaOK.cjs.map +0 -1
- package/dist/flatten-C7D6Kttf.d.mts +0 -81
- package/dist/flatten-C7D6Kttf.d.mts.map +0 -1
- package/dist/flatten-ChTL5BfA.mjs +0 -167
- package/dist/flatten-ChTL5BfA.mjs.map +0 -1
- package/dist/flatten-D0K8UcQH.d.cts +0 -81
- package/dist/flatten-D0K8UcQH.d.cts.map +0 -1
- package/dist/result-BBOGxvSH.cjs.map +0 -1
- package/dist/result-q9Na2bGa.mjs.map +0 -1
- package/dist/sequence-DDwePRLd.d.cts.map +0 -1
- package/dist/sequence-DDwePRLd.d.mts.map +0 -1
- package/dist/swap-BnyMBdD_.cjs +0 -552
- package/dist/swap-BnyMBdD_.cjs.map +0 -1
- package/dist/swap-CFD2g1cB.mjs +0 -385
- package/dist/swap-CFD2g1cB.mjs.map +0 -1
- package/dist/swap-CrHQZIax.d.cts +0 -212
- package/dist/swap-CrHQZIax.d.cts.map +0 -1
- package/dist/swap-DT4LdRFV.d.mts +0 -212
- package/dist/swap-DT4LdRFV.d.mts.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,17 +1,45 @@
|
|
|
1
|
-
import { _ as matchTag, a as fromNullable, c as ok, d as tryAsync, f as tryFn, g as sequence, h as all, i as err, l as okIf, m as zip, n as Ok, o as fromPromise, p as combine, r as Result, s as fromThrowable, t as Err, u as okIfLazy, v as isResult } from "./result-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { _ as matchTag, a as fromNullable, c as ok, d as tryAsync, f as tryFn, g as sequence, h as all, i as err, l as okIf, m as zip, n as Ok, o as fromPromise, p as combine, r as Result, s as fromThrowable, t as Err, u as okIfLazy, v as isResult } from "./result-DQCpkuOJ.mjs";
|
|
2
|
+
import { _ as ResultError, a as ERR_MATCH_ERR_HANDLER_NOT_RESULT, b as UnwrapErrOnOkError, c as ERR_TASK_YIELD_NOT_RESULT, d as ExpectErrError, f as ExpectOkError, g as MatchTagMissingHandlerError, h as MatchOnOkError, i as ERR_INVALID_STATE, l as ERR_UNWRAP_ERR_ON_OK, m as MatchErrHandlerNotResultError, n as ERR_EXPECT_OK, o as ERR_MATCH_ON_OK, p as InvalidResultStateError, r as ERR_INVALID_RESULT_STATE, s as ERR_MATCH_TAG_MISSING_HANDLER, t as ERR_EXPECT_ERR, u as ERR_UNWRAP_ON_ERR, v as ResultTypeError, x as UnwrapOnErrError, y as TaskYieldNotResultError } from "./errors-MuakEcnM.mjs";
|
|
3
|
+
import { and, bimap, filter, filterAsync, flatMap, flatMapAsync, fold, foldAsync, map, mapAsync, mapBoth, mapErr, mapErrAsync, mapOr, mapOrElse, match, matchAsync, or, orElse, recover, recoverWith, swap, tap, tapAsync, tryCatch, tryCatchAsync, tryMap, tryMapAsync } from "./operators.mjs";
|
|
4
|
+
import { collectAllErrors, collectFirstOk, collectFirstOkAsync, collectFirstOkParallelAsync, flatten, partition, sequenceRecord } from "./collections.mjs";
|
|
5
5
|
|
|
6
6
|
//#region src/gen.ts
|
|
7
7
|
async function task(makeGenerator, onThrow) {
|
|
8
8
|
const iterator = makeGenerator();
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
const decide = (yielded) => {
|
|
10
|
+
if (!isResult(yielded)) return {
|
|
11
|
+
kind: "throw",
|
|
12
|
+
error: new TaskYieldNotResultError(yielded)
|
|
13
|
+
};
|
|
14
|
+
if (yielded.isOk()) return {
|
|
15
|
+
kind: "continue",
|
|
16
|
+
input: yielded.value
|
|
17
|
+
};
|
|
18
|
+
if (yielded.isErr()) return {
|
|
19
|
+
kind: "return",
|
|
20
|
+
result: yielded
|
|
21
|
+
};
|
|
22
|
+
return {
|
|
23
|
+
kind: "throw",
|
|
24
|
+
error: new InvalidResultStateError("task")
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
const abandon = async (pending) => {
|
|
28
|
+
if (typeof iterator.return !== "function") return pending;
|
|
29
|
+
let completion = pending;
|
|
12
30
|
let step = await iterator.return(void 0);
|
|
13
|
-
while (!step.done)
|
|
31
|
+
while (!step.done) {
|
|
32
|
+
const decision = decide(step.value);
|
|
33
|
+
if (decision.kind === "continue") {
|
|
34
|
+
step = await iterator.next(decision.input);
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
completion = decision;
|
|
38
|
+
step = await iterator.return(void 0);
|
|
39
|
+
}
|
|
40
|
+
return completion;
|
|
14
41
|
};
|
|
42
|
+
let input = void 0;
|
|
15
43
|
while (true) {
|
|
16
44
|
let step;
|
|
17
45
|
try {
|
|
@@ -28,36 +56,20 @@ async function task(makeGenerator, onThrow) {
|
|
|
28
56
|
if (!onThrow) throw caught;
|
|
29
57
|
return err(onThrow(caught));
|
|
30
58
|
}
|
|
31
|
-
const
|
|
32
|
-
if (
|
|
33
|
-
|
|
34
|
-
await runGeneratorReturn();
|
|
35
|
-
} catch (caught) {
|
|
36
|
-
if (!onThrow) throw caught;
|
|
37
|
-
return err(onThrow(caught));
|
|
38
|
-
}
|
|
39
|
-
throw new TaskYieldNotResultError(yielded);
|
|
40
|
-
}
|
|
41
|
-
if (yielded.isOk()) {
|
|
42
|
-
input = yielded.value;
|
|
59
|
+
const decision = decide(step.value);
|
|
60
|
+
if (decision.kind === "continue") {
|
|
61
|
+
input = decision.input;
|
|
43
62
|
continue;
|
|
44
63
|
}
|
|
45
|
-
|
|
46
|
-
try {
|
|
47
|
-
await runGeneratorReturn();
|
|
48
|
-
} catch (caught) {
|
|
49
|
-
if (!onThrow) throw caught;
|
|
50
|
-
return err(onThrow(caught));
|
|
51
|
-
}
|
|
52
|
-
return yielded;
|
|
53
|
-
}
|
|
64
|
+
let completion;
|
|
54
65
|
try {
|
|
55
|
-
await
|
|
66
|
+
completion = await abandon(decision);
|
|
56
67
|
} catch (caught) {
|
|
57
68
|
if (!onThrow) throw caught;
|
|
58
69
|
return err(onThrow(caught));
|
|
59
70
|
}
|
|
60
|
-
|
|
71
|
+
if (completion.kind === "throw") throw completion.error;
|
|
72
|
+
return completion.result;
|
|
61
73
|
}
|
|
62
74
|
}
|
|
63
75
|
const gen = task;
|
|
@@ -168,11 +180,29 @@ function expectErr(result, message) {
|
|
|
168
180
|
* by `Result.toSerialized()` and by `JSON.stringify` on a Result.
|
|
169
181
|
*
|
|
170
182
|
* Throws `InvalidResultStateError` for data that is not in that shape.
|
|
183
|
+
*
|
|
184
|
+
* @deprecated Rebuild the Result yourself after validating the payload:
|
|
185
|
+
*
|
|
186
|
+
* ```ts
|
|
187
|
+
* const restored = parsed._tag === 'Ok' ? ok(parsed.value) : err(parsed.error);
|
|
188
|
+
* ```
|
|
189
|
+
*
|
|
190
|
+
* `fromSerialized` only checks the envelope, the `_tag` discriminant plus the
|
|
191
|
+
* absence of an own key of the other state, never the payload itself, while its
|
|
192
|
+
* type parameters claim `T` and `E` for whatever `JSON.parse` returned. It
|
|
193
|
+
* also throws for malformed input at exactly the boundary where a Result
|
|
194
|
+
* should be returned. Validate foreign data with your schema tool and
|
|
195
|
+
* then call `ok`/`err`; the shape is exported as `ResultType<T, E>`. This
|
|
196
|
+
* function will be removed in 2.0.
|
|
171
197
|
*/
|
|
172
198
|
function fromSerialized(data) {
|
|
173
|
-
if (data !== null && typeof data === "object") {
|
|
174
|
-
if (data._tag === "Ok" && "
|
|
175
|
-
if (data._tag === "Err" && "
|
|
199
|
+
if (data !== null && typeof data === "object") try {
|
|
200
|
+
if (data._tag === "Ok" && !Object.hasOwn(data, "error")) return ok(data.value);
|
|
201
|
+
if (data._tag === "Err" && !Object.hasOwn(data, "value")) return err(data.error);
|
|
202
|
+
} catch (error) {
|
|
203
|
+
const invalid = new InvalidResultStateError("fromSerialized");
|
|
204
|
+
invalid.cause = error;
|
|
205
|
+
throw invalid;
|
|
176
206
|
}
|
|
177
207
|
throw new InvalidResultStateError("fromSerialized");
|
|
178
208
|
}
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/gen.ts","../src/core/unwrap.ts","../src/core/unwrapOr.ts","../src/core/unwrapOrElse.ts","../src/core/unwrapOrDefault.ts","../src/core/unwrapOrThrow.ts","../src/core/unwrapErr.ts","../src/core/expectResult.ts","../src/core/expectErr.ts","../src/core/fromSerialized.ts","../src/core/toPromise.ts","../src/core/toNullable.ts","../src/core/isOk.ts","../src/core/isErr.ts","../src/core/contains.ts","../src/core/containsErr.ts"],"sourcesContent":["import type { Result } from './core/result';\nimport { err, ok } from './core/result';\nimport { isResult } from './core/isResult';\nimport { InvalidResultStateError, TaskYieldNotResultError } from './errors';\n\ntype AnyGenerator<Y = unknown, R = unknown, N = unknown> = Generator<Y, R, N> | AsyncGenerator<Y, R, N>;\n\ntype ErrorOfYield<Y> = Y extends Result<any, infer E> ? E : never;\n\ntype OnThrow<E> = (error: unknown) => E;\n\ntype AwaitedReturn<R> = Awaited<R>;\ntype OkOfReturn<R> = AwaitedReturn<R> extends Result<infer T, any> ? T : AwaitedReturn<R>;\ntype ErrOfReturn<R> = AwaitedReturn<R> extends Result<any, infer E> ? E : never;\n\n/**\n * Generator-based do-notation for `Result`.\n *\n * Usage:\n * ```ts\n * const out = await task(async function* () {\n * const user = yield* await fromPromise(findUser(1));\n * const email = yield* validate(user.email);\n * return email;\n * });\n * ```\n */\nexport async function task<const Y, const R>(\n makeGenerator: () => AnyGenerator<Y, R, unknown>\n): Promise<Result<OkOfReturn<R>, ErrorOfYield<Y> | ErrOfReturn<R>>>;\nexport async function task<const Y, const R, EThrown>(\n makeGenerator: () => AnyGenerator<Y, R, unknown>,\n onThrow: OnThrow<EThrown>\n): Promise<Result<OkOfReturn<R>, ErrorOfYield<Y> | ErrOfReturn<R> | EThrown>>;\nexport async function task<const Y, const R, EThrown>(\n makeGenerator: () => AnyGenerator<Y, R, unknown>,\n onThrow?: OnThrow<EThrown>\n): Promise<Result<OkOfReturn<R>, ErrorOfYield<Y> | ErrOfReturn<R> | EThrown>> {\n const iterator = makeGenerator();\n let input: unknown = undefined;\n\n // Resume the suspended generator as if it returned, so its `finally`\n // blocks run before we abort the workflow (short-circuit or error path).\n const runGeneratorReturn = async (): Promise<void> => {\n if (typeof iterator.return !== 'function') return;\n // A `yield` inside a `finally` block suspends the generator again\n // ({done: false}). Resume with next() so the remaining cleanup code\n // still runs — calling return() again would abort the rest of that\n // finally block. The pending return completion ends the generator\n // once all finally blocks have finished.\n let step = await iterator.return(undefined as unknown as R);\n while (!step.done) {\n step = await iterator.next(undefined);\n }\n };\n\n while (true) {\n let step: IteratorResult<Y, R>;\n try {\n step = await iterator.next(input);\n } catch (caught) {\n if (!onThrow) throw caught;\n return err<ErrorOfYield<Y> | ErrOfReturn<R> | EThrown, OkOfReturn<R>>(onThrow(caught));\n }\n\n if (step.done) {\n try {\n const awaited = await step.value;\n if (isResult(awaited)) {\n return awaited as Result<OkOfReturn<R>, ErrorOfYield<Y> | ErrOfReturn<R> | EThrown>;\n }\n return ok<OkOfReturn<R>, ErrorOfYield<Y> | ErrOfReturn<R> | EThrown>(awaited as OkOfReturn<R>);\n } catch (caught) {\n if (!onThrow) throw caught;\n return err<ErrorOfYield<Y> | ErrOfReturn<R> | EThrown, OkOfReturn<R>>(onThrow(caught));\n }\n }\n\n const yielded = step.value as unknown;\n if (!isResult(yielded)) {\n try {\n await runGeneratorReturn();\n } catch (caught) {\n if (!onThrow) throw caught;\n return err<ErrorOfYield<Y> | ErrOfReturn<R> | EThrown, OkOfReturn<R>>(onThrow(caught));\n }\n throw new TaskYieldNotResultError(yielded);\n }\n\n if (yielded.isOk()) {\n input = yielded.value;\n continue;\n }\n\n if (yielded.isErr()) {\n try {\n await runGeneratorReturn();\n } catch (caught) {\n if (!onThrow) throw caught;\n return err<ErrorOfYield<Y> | ErrOfReturn<R> | EThrown, OkOfReturn<R>>(onThrow(caught));\n }\n\n return yielded as Result<OkOfReturn<R>, ErrorOfYield<Y> | ErrOfReturn<R> | EThrown>;\n }\n\n try {\n await runGeneratorReturn();\n } catch (caught) {\n if (!onThrow) throw caught;\n return err<ErrorOfYield<Y> | ErrOfReturn<R> | EThrown, OkOfReturn<R>>(onThrow(caught));\n }\n throw new InvalidResultStateError('task');\n }\n}\n\nexport const gen: typeof task = task;\n","import type { Result } from './result';\nimport { InvalidResultStateError, UnwrapOnErrError } from '../errors';\n\n/**\n * Returns the value or throws an Error.\n * Corresponds to Rust `unwrap`.\n */\nexport function unwrap<T, E>(result: Result<T, E>): T {\n if (result.isOk()) {\n return result.value;\n }\n if (result.isErr()) throw new UnwrapOnErrError(result.error);\n throw new InvalidResultStateError('unwrap');\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Returns the value or a default value.\n * Pure function alternative to the instance method.\n */\nexport function unwrapOr<T, E>(result: Result<T, E>, defaultValue: T): T {\n if (result.isOk()) return result.value;\n if (result.isErr()) return defaultValue;\n throw new InvalidResultStateError('unwrapOr');\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Returns the value or calculates a default value using a function.\n * Corresponds to Rust `unwrap_or_else`.\n */\nexport function unwrapOrElse<T, E>(result: Result<T, E>, fn: (error: E) => T): T {\n if (result.isOk()) return result.value;\n if (result.isErr()) return fn(result.error);\n throw new InvalidResultStateError('unwrapOrElse');\n}\n","import type { Result } from './result';\nimport { unwrapOr } from './unwrapOr';\n\n/**\n * Alias for `unwrapOr`.\n *\n * @deprecated Use {@link unwrapOr} instead. The name is misleading: Rust's\n * `unwrap_or_default` takes no argument (it uses the type's `Default`),\n * whereas this function requires an explicit default value — which is exactly\n * what `unwrapOr` does.\n */\nexport function unwrapOrDefault<T, E>(result: Result<T, E>, defaultValue: T): T {\n return unwrapOr(result, defaultValue);\n}\n\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Returns the value or throws the original Err value (not wrapped).\n * Useful to preserve `Error` instances including stack traces.\n */\nexport function unwrapOrThrow<T, E>(result: Result<T, E>): T {\n if (result.isOk()) return result.value;\n if (result.isErr()) throw result.error;\n throw new InvalidResultStateError('unwrapOrThrow');\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError, UnwrapErrOnOkError } from '../errors';\n\n/**\n * Returns the error or throws an Error.\n * Corresponds to Rust `unwrap_err`.\n */\nexport function unwrapErr<T, E>(result: Result<T, E>): E {\n if (result.isErr()) {\n return result.error;\n }\n if (result.isOk()) throw new UnwrapErrOnOkError(result.value);\n throw new InvalidResultStateError('unwrapErr');\n}\n","import type { Result } from './result';\nimport { ExpectOkError, InvalidResultStateError } from '../errors';\n\n/**\n * Returns the value or throws an Error with a custom message.\n * Corresponds to Rust `expect`.\n */\nexport function expectResult<T, E>(result: Result<T, E>, message: string): T {\n if (result.isOk()) {\n return result.value;\n }\n if (result.isErr()) {\n throw new ExpectOkError(message, result.error);\n }\n throw new InvalidResultStateError('expectResult');\n}\n","import type { Result } from './result';\nimport { ExpectErrError, InvalidResultStateError } from '../errors';\n\n/**\n * Returns the error or throws an Error with a custom message.\n * Corresponds to Rust `expect_err`.\n */\nexport function expectErr<T, E>(result: Result<T, E>, message: string): E {\n if (result.isErr()) {\n return result.error;\n }\n if (result.isOk()) {\n throw new ExpectErrError(message, result.value);\n }\n throw new InvalidResultStateError('expectErr');\n}\n","import type { Result, ResultType } from './result';\nimport { err, ok } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Rebuilds a `Result` from its serialized discriminated form\n * (`{ _tag: 'Ok', value }` / `{ _tag: 'Err', error }`), the shape produced\n * by `Result.toSerialized()` and by `JSON.stringify` on a Result.\n *\n * Throws `InvalidResultStateError` for data that is not in that shape.\n */\nexport function fromSerialized<T, E>(data: ResultType<T, E>): Result<T, E> {\n if (data !== null && typeof data === 'object') {\n if (data._tag === 'Ok' && 'value' in data) return ok<T, E>(data.value);\n if (data._tag === 'Err' && 'error' in data) return err<E, T>(data.error);\n }\n throw new InvalidResultStateError('fromSerialized');\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Converts a Result to a Promise.\n * Ok → resolve(value), Err → reject(error)\n */\nexport function toPromise<T, E>(result: Result<T, E>): Promise<T> {\n if (result.isOk()) {\n return Promise.resolve(result.value);\n }\n if (result.isErr()) return Promise.reject(result.error);\n throw new InvalidResultStateError('toPromise');\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Converts a Result to `T | null`.\n * Ok → value, Err → null\n */\nexport function toNullable<T, E>(result: Result<T, E>): T | null {\n if (result.isOk()) return result.value;\n if (result.isErr()) return null;\n throw new InvalidResultStateError('toNullable');\n}\n","import type { Ok, Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Checks if a Result is Ok.\n * Pure function alternative to the instance method.\n */\nexport function isOk<T, E>(result: Result<T, E>): result is Ok<T, E> {\n if (result.isOk()) return true;\n if (result.isErr()) return false;\n throw new InvalidResultStateError('isOk');\n}\n","import type { Err, Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Checks if a Result is Err.\n * Pure function alternative to the instance method.\n */\nexport function isErr<T, E>(result: Result<T, E>): result is Err<T, E> {\n if (result.isErr()) return true;\n if (result.isOk()) return false;\n throw new InvalidResultStateError('isErr');\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Checks if the Result contains a specific value.\n * Corresponds to Rust `contains`.\n *\n * Comparison uses `Object.is` (SameValue): `NaN` matches `NaN`, `+0` and\n * `-0` are distinct, and objects are compared by reference.\n */\nexport function contains<T, E>(result: Result<T, E>, value: T): boolean {\n if (result.isOk()) return Object.is(result.value, value);\n if (result.isErr()) return false;\n throw new InvalidResultStateError('contains');\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Checks if the Result contains a specific error.\n * Analogous to `contains` for the Err case.\n *\n * Comparison uses `Object.is` (SameValue): `NaN` matches `NaN`, `+0` and\n * `-0` are distinct, and objects are compared by reference.\n */\nexport function containsErr<T, E>(result: Result<T, E>, error: E): boolean {\n if (result.isErr()) return Object.is(result.error, error);\n if (result.isOk()) return false;\n throw new InvalidResultStateError('containsErr');\n}\n"],"mappings":";;;;;;AAkCA,eAAsB,KAClB,eACA,SAC0E;CAC1E,MAAM,WAAW,cAAc;CAC/B,IAAI,QAAiB;CAIrB,MAAM,qBAAqB,YAA2B;EAClD,IAAI,OAAO,SAAS,WAAW,YAAY;EAM3C,IAAI,OAAO,MAAM,SAAS,OAAO,MAAyB;EAC1D,OAAO,CAAC,KAAK,MACT,OAAO,MAAM,SAAS,KAAK,MAAS;CAE5C;CAEA,OAAO,MAAM;EACT,IAAI;EACJ,IAAI;GACA,OAAO,MAAM,SAAS,KAAK,KAAK;EACpC,SAAS,QAAQ;GACb,IAAI,CAAC,SAAS,MAAM;GACpB,OAAO,IAA+D,QAAQ,MAAM,CAAC;EACzF;EAEA,IAAI,KAAK,MACL,IAAI;GACA,MAAM,UAAU,MAAM,KAAK;GAC3B,IAAI,SAAS,OAAO,GAChB,OAAO;GAEX,OAAO,GAA8D,OAAwB;EACjG,SAAS,QAAQ;GACb,IAAI,CAAC,SAAS,MAAM;GACpB,OAAO,IAA+D,QAAQ,MAAM,CAAC;EACzF;EAGJ,MAAM,UAAU,KAAK;EACrB,IAAI,CAAC,SAAS,OAAO,GAAG;GACpB,IAAI;IACA,MAAM,mBAAmB;GAC7B,SAAS,QAAQ;IACb,IAAI,CAAC,SAAS,MAAM;IACpB,OAAO,IAA+D,QAAQ,MAAM,CAAC;GACzF;GACA,MAAM,IAAI,wBAAwB,OAAO;EAC7C;EAEA,IAAI,QAAQ,KAAK,GAAG;GAChB,QAAQ,QAAQ;GAChB;EACJ;EAEA,IAAI,QAAQ,MAAM,GAAG;GACjB,IAAI;IACA,MAAM,mBAAmB;G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AAO;CAC3B,IAAI,OAAO,KAAK,GAAG,OAAO;CAC1B,MAAM,IAAI,wBAAwB,OAAO;AAC7C;;;;;;;;;;;ACDA,SAAgB,SAAe,QAAsB,OAAmB;CACpE,IAAI,OAAO,KAAK,GAAG,OAAO,OAAO,GAAG,OAAO,OAAO,KAAK;CACvD,IAAI,OAAO,MAAM,GAAG,OAAO;CAC3B,MAAM,IAAI,wBAAwB,UAAU;AAChD;;;;;;;;;;;ACJA,SAAgB,YAAkB,QAAsB,OAAmB;CACvE,IAAI,OAAO,MAAM,GAAG,OAAO,OAAO,GAAG,OAAO,OAAO,KAAK;CACxD,IAAI,OAAO,KAAK,GAAG,OAAO;CAC1B,MAAM,IAAI,wBAAwB,aAAa;AACnD"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/gen.ts","../src/core/unwrap.ts","../src/core/unwrapOr.ts","../src/core/unwrapOrElse.ts","../src/core/unwrapOrDefault.ts","../src/core/unwrapOrThrow.ts","../src/core/unwrapErr.ts","../src/core/expectResult.ts","../src/core/expectErr.ts","../src/core/fromSerialized.ts","../src/core/toPromise.ts","../src/core/toNullable.ts","../src/core/isOk.ts","../src/core/isErr.ts","../src/core/contains.ts","../src/core/containsErr.ts"],"sourcesContent":["import type { Result } from './core/result';\nimport { err, ok } from './core/result';\nimport { isResult } from './core/isResult';\nimport { InvalidResultStateError, TaskYieldNotResultError } from './errors';\n\ntype AnyGenerator<Y = unknown, R = unknown, N = unknown> = Generator<Y, R, N> | AsyncGenerator<Y, R, N>;\n\ntype ErrorOfYield<Y> = Y extends Result<any, infer E> ? E : never;\n\ntype OnThrow<E> = (error: unknown) => E;\n\ntype AwaitedReturn<R> = Awaited<R>;\ntype OkOfReturn<R> = AwaitedReturn<R> extends Result<infer T, any> ? T : AwaitedReturn<R>;\ntype ErrOfReturn<R> = AwaitedReturn<R> extends Result<any, infer E> ? E : never;\n\n/**\n * What the runner does with a yielded value: resume the generator with the\n * Ok value, or end the workflow with a Result or with a programmer error.\n */\ntype Decision<T, E> =\n | { readonly kind: 'continue'; readonly input: unknown }\n | { readonly kind: 'return'; readonly result: Result<T, E> }\n | { readonly kind: 'throw'; readonly error: unknown };\n\ntype Completion<T, E> = Exclude<Decision<T, E>, { kind: 'continue' }>;\n\n/**\n * Generator-based do-notation for `Result`.\n *\n * Usage:\n * ```ts\n * const out = await task(async function* () {\n * const user = yield* await fromPromise(findUser(1));\n * const email = yield* validate(user.email);\n * return email;\n * });\n * ```\n */\nexport async function task<const Y, const R>(\n makeGenerator: () => AnyGenerator<Y, R, unknown>\n): Promise<Result<OkOfReturn<R>, ErrorOfYield<Y> | ErrOfReturn<R>>>;\nexport async function task<const Y, const R, EThrown>(\n makeGenerator: () => AnyGenerator<Y, R, unknown>,\n onThrow: OnThrow<EThrown>\n): Promise<Result<OkOfReturn<R>, ErrorOfYield<Y> | ErrOfReturn<R> | EThrown>>;\nexport async function task<const Y, const R, EThrown>(\n makeGenerator: () => AnyGenerator<Y, R, unknown>,\n onThrow?: OnThrow<EThrown>\n): Promise<Result<OkOfReturn<R>, ErrorOfYield<Y> | ErrOfReturn<R> | EThrown>> {\n type Out = Result<OkOfReturn<R>, ErrorOfYield<Y> | ErrOfReturn<R> | EThrown>;\n\n const iterator = makeGenerator();\n\n const decide = (yielded: unknown): Decision<OkOfReturn<R>, ErrorOfYield<Y> | ErrOfReturn<R> | EThrown> => {\n if (!isResult(yielded)) return { kind: 'throw', error: new TaskYieldNotResultError(yielded) };\n if (yielded.isOk()) return { kind: 'continue', input: yielded.value };\n if (yielded.isErr()) return { kind: 'return', result: yielded as Out };\n return { kind: 'throw', error: new InvalidResultStateError('task') };\n };\n\n // Resume the suspended generator as if it returned, so its `finally`\n // blocks run before the workflow ends. Those blocks follow the same\n // yield protocol as the body: an Ok sends its value back, and an Err or\n // a non-Result replaces the pending completion and skips the rest of\n // that block, exactly like a `throw` inside `finally` replaces a pending\n // exception. Outer `finally` blocks still run.\n const abandon = async (\n pending: Completion<OkOfReturn<R>, ErrorOfYield<Y> | ErrOfReturn<R> | EThrown>\n ): Promise<Completion<OkOfReturn<R>, ErrorOfYield<Y> | ErrOfReturn<R> | EThrown>> => {\n if (typeof iterator.return !== 'function') return pending;\n let completion = pending;\n let step = await iterator.return(undefined as unknown as R);\n while (!step.done) {\n const decision = decide(step.value);\n if (decision.kind === 'continue') {\n step = await iterator.next(decision.input);\n continue;\n }\n completion = decision;\n step = await iterator.return(undefined as unknown as R);\n }\n return completion;\n };\n\n let input: unknown = undefined;\n\n while (true) {\n let step: IteratorResult<Y, R>;\n try {\n step = await iterator.next(input);\n } catch (caught) {\n if (!onThrow) throw caught;\n return err<ErrorOfYield<Y> | ErrOfReturn<R> | EThrown, OkOfReturn<R>>(onThrow(caught));\n }\n\n if (step.done) {\n try {\n const awaited = await step.value;\n if (isResult(awaited)) return awaited as Out;\n return ok<OkOfReturn<R>, ErrorOfYield<Y> | ErrOfReturn<R> | EThrown>(awaited as OkOfReturn<R>);\n } catch (caught) {\n if (!onThrow) throw caught;\n return err<ErrorOfYield<Y> | ErrOfReturn<R> | EThrown, OkOfReturn<R>>(onThrow(caught));\n }\n }\n\n const decision = decide(step.value);\n if (decision.kind === 'continue') {\n input = decision.input;\n continue;\n }\n\n let completion: Completion<OkOfReturn<R>, ErrorOfYield<Y> | ErrOfReturn<R> | EThrown>;\n try {\n completion = await abandon(decision);\n } catch (caught) {\n if (!onThrow) throw caught;\n return err<ErrorOfYield<Y> | ErrOfReturn<R> | EThrown, OkOfReturn<R>>(onThrow(caught));\n }\n if (completion.kind === 'throw') throw completion.error;\n return completion.result;\n }\n}\n\nexport const gen: typeof task = task;\n","import type { Result } from './result';\nimport { InvalidResultStateError, UnwrapOnErrError } from '../errors';\n\n/**\n * Returns the value or throws an Error.\n * Corresponds to Rust `unwrap`.\n */\nexport function unwrap<T, E>(result: Result<T, E>): T {\n if (result.isOk()) {\n return result.value;\n }\n if (result.isErr()) throw new UnwrapOnErrError(result.error);\n throw new InvalidResultStateError('unwrap');\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Returns the value or a default value.\n * Pure function alternative to the instance method.\n */\nexport function unwrapOr<T, E>(result: Result<T, E>, defaultValue: T): T {\n if (result.isOk()) return result.value;\n if (result.isErr()) return defaultValue;\n throw new InvalidResultStateError('unwrapOr');\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Returns the value or calculates a default value using a function.\n * Corresponds to Rust `unwrap_or_else`.\n */\nexport function unwrapOrElse<T, E>(result: Result<T, E>, fn: (error: E) => T): T {\n if (result.isOk()) return result.value;\n if (result.isErr()) return fn(result.error);\n throw new InvalidResultStateError('unwrapOrElse');\n}\n","import type { Result } from './result';\nimport { unwrapOr } from './unwrapOr';\n\n/**\n * Alias for `unwrapOr`.\n *\n * @deprecated Use {@link unwrapOr} instead. The name is misleading: Rust's\n * `unwrap_or_default` takes no argument (it uses the type's `Default`),\n * whereas this function requires an explicit default value — which is exactly\n * what `unwrapOr` does.\n */\nexport function unwrapOrDefault<T, E>(result: Result<T, E>, defaultValue: T): T {\n return unwrapOr(result, defaultValue);\n}\n\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Returns the value or throws the original Err value (not wrapped).\n * Useful to preserve `Error` instances including stack traces.\n */\nexport function unwrapOrThrow<T, E>(result: Result<T, E>): T {\n if (result.isOk()) return result.value;\n if (result.isErr()) throw result.error;\n throw new InvalidResultStateError('unwrapOrThrow');\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError, UnwrapErrOnOkError } from '../errors';\n\n/**\n * Returns the error or throws an Error.\n * Corresponds to Rust `unwrap_err`.\n */\nexport function unwrapErr<T, E>(result: Result<T, E>): E {\n if (result.isErr()) {\n return result.error;\n }\n if (result.isOk()) throw new UnwrapErrOnOkError(result.value);\n throw new InvalidResultStateError('unwrapErr');\n}\n","import type { Result } from './result';\nimport { ExpectOkError, InvalidResultStateError } from '../errors';\n\n/**\n * Returns the value or throws an Error with a custom message.\n * Corresponds to Rust `expect`.\n */\nexport function expectResult<T, E>(result: Result<T, E>, message: string): T {\n if (result.isOk()) {\n return result.value;\n }\n if (result.isErr()) {\n throw new ExpectOkError(message, result.error);\n }\n throw new InvalidResultStateError('expectResult');\n}\n","import type { Result } from './result';\nimport { ExpectErrError, InvalidResultStateError } from '../errors';\n\n/**\n * Returns the error or throws an Error with a custom message.\n * Corresponds to Rust `expect_err`.\n */\nexport function expectErr<T, E>(result: Result<T, E>, message: string): E {\n if (result.isErr()) {\n return result.error;\n }\n if (result.isOk()) {\n throw new ExpectErrError(message, result.value);\n }\n throw new InvalidResultStateError('expectErr');\n}\n","import type { Result, ResultType } from './result';\nimport { err, ok } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Rebuilds a `Result` from its serialized discriminated form\n * (`{ _tag: 'Ok', value }` / `{ _tag: 'Err', error }`), the shape produced\n * by `Result.toSerialized()` and by `JSON.stringify` on a Result.\n *\n * Throws `InvalidResultStateError` for data that is not in that shape.\n *\n * @deprecated Rebuild the Result yourself after validating the payload:\n *\n * ```ts\n * const restored = parsed._tag === 'Ok' ? ok(parsed.value) : err(parsed.error);\n * ```\n *\n * `fromSerialized` only checks the envelope, the `_tag` discriminant plus the\n * absence of an own key of the other state, never the payload itself, while its\n * type parameters claim `T` and `E` for whatever `JSON.parse` returned. It\n * also throws for malformed input at exactly the boundary where a Result\n * should be returned. Validate foreign data with your schema tool and\n * then call `ok`/`err`; the shape is exported as `ResultType<T, E>`. This\n * function will be removed in 2.0.\n */\nexport function fromSerialized<T, E>(data: ResultType<T, E>): Result<T, E> {\n if (data !== null && typeof data === 'object') {\n // Reading the envelope runs code of the payload: `Object.hasOwn` calls\n // the `getOwnPropertyDescriptor` trap of a Proxy, and the discriminant\n // can be a getter. A payload that throws there is not a serialized\n // Result, and the coded error of this library says so.\n try {\n // The key is absent only where `T` or `E` admits `undefined`, see `ResultType`.\n if (data._tag === 'Ok' && !Object.hasOwn(data, 'error')) return ok<T, E>(data.value as T);\n if (data._tag === 'Err' && !Object.hasOwn(data, 'value')) return err<E, T>(data.error as E);\n } catch (error) {\n const invalid = new InvalidResultStateError('fromSerialized');\n invalid.cause = error;\n throw invalid;\n }\n }\n throw new InvalidResultStateError('fromSerialized');\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Converts a Result to a Promise.\n * Ok → resolve(value), Err → reject(error)\n */\nexport function toPromise<T, E>(result: Result<T, E>): Promise<T> {\n if (result.isOk()) {\n return Promise.resolve(result.value);\n }\n if (result.isErr()) return Promise.reject(result.error);\n throw new InvalidResultStateError('toPromise');\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Converts a Result to `T | null`.\n * Ok → value, Err → null\n */\nexport function toNullable<T, E>(result: Result<T, E>): T | null {\n if (result.isOk()) return result.value;\n if (result.isErr()) return null;\n throw new InvalidResultStateError('toNullable');\n}\n","import type { Ok, Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Checks if a Result is Ok.\n * Pure function alternative to the instance method.\n */\nexport function isOk<T, E>(result: Result<T, E>): result is Ok<T, E> {\n if (result.isOk()) return true;\n if (result.isErr()) return false;\n throw new InvalidResultStateError('isOk');\n}\n","import type { Err, Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Checks if a Result is Err.\n * Pure function alternative to the instance method.\n */\nexport function isErr<T, E>(result: Result<T, E>): result is Err<T, E> {\n if (result.isErr()) return true;\n if (result.isOk()) return false;\n throw new InvalidResultStateError('isErr');\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Checks if the Result contains a specific value.\n * Corresponds to Rust `contains`.\n *\n * Comparison uses `Object.is` (SameValue): `NaN` matches `NaN`, `+0` and\n * `-0` are distinct, and objects are compared by reference.\n */\nexport function contains<T, E>(result: Result<T, E>, value: T): boolean {\n if (result.isOk()) return Object.is(result.value, value);\n if (result.isErr()) return false;\n throw new InvalidResultStateError('contains');\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Checks if the Result contains a specific error.\n * Analogous to `contains` for the Err case.\n *\n * Comparison uses `Object.is` (SameValue): `NaN` matches `NaN`, `+0` and\n * `-0` are distinct, and objects are compared by reference.\n */\nexport function containsErr<T, E>(result: Result<T, E>, error: E): boolean {\n if (result.isErr()) return Object.is(result.error, error);\n if (result.isOk()) return false;\n throw new InvalidResultStateError('containsErr');\n}\n"],"mappings":";;;;;;AA6CA,eAAsB,KAClB,eACA,SAC0E;CAG1E,MAAM,WAAW,cAAc;CAE/B,MAAM,UAAU,YAA0F;EACtG,IAAI,CAAC,SAAS,OAAO,GAAG,OAAO;GAAE,MAAM;GAAS,OAAO,IAAI,wBAAwB,OAAO;EAAE;EAC5F,IAAI,QAAQ,KAAK,GAAG,OAAO;GAAE,MAAM;GAAY,OAAO,QAAQ;EAAM;EACpE,IAAI,QAAQ,MAAM,GAAG,OAAO;GAAE,MAAM;GAAU,QAAQ;EAAe;EACrE,OAAO;GAAE,MAAM;GAAS,OAAO,IAAI,wBAAwB,MAAM;EAAE;CACvE;CAQA,MAAM,UAAU,OACZ,YACiF;EACjF,IAAI,OAAO,SAAS,WAAW,YAAY,OAAO;EAClD,IAAI,aAAa;EACjB,IAAI,OAAO,MAAM,SAAS,OAAO,MAAyB;EAC1D,OAAO,CAAC,KAAK,MAAM;GACf,MAAM,WAAW,OAAO,KAAK,KAAK;GAClC,IAAI,SAAS,SAAS,YAAY;IAC9B,OAAO,MAAM,SAAS,KAAK,SAAS,KAAK;IACzC;GACJ;GACA,aAAa;GACb,OAAO,MAAM,SAAS,OAAO,MAAyB;EAC1D;EACA,OAAO;CACX;CAEA,IAAI,QAAiB;CAErB,OAAO,MAAM;EACT,IAAI;EACJ,IAAI;GACA,OAAO,MAAM,SAAS,KAAK,KAAK;EACpC,SAAS,QAAQ;GACb,IAAI,CAAC,SAAS,MAAM;GACpB,OAAO,IAA+D,Q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AAG,OAAO,QAAQ,OAAO,OAAO,KAAK;CACtD,MAAM,IAAI,wBAAwB,WAAW;AACjD;;;;;;;;ACNA,SAAgB,WAAiB,QAAgC;CAC7D,IAAI,OAAO,KAAK,GAAG,OAAO,OAAO;CACjC,IAAI,OAAO,MAAM,GAAG,OAAO;CAC3B,MAAM,IAAI,wBAAwB,YAAY;AAClD;;;;;;;;ACJA,SAAgB,KAAW,QAA0C;CACjE,IAAI,OAAO,KAAK,GAAG,OAAO;CAC1B,IAAI,OAAO,MAAM,GAAG,OAAO;CAC3B,MAAM,IAAI,wBAAwB,MAAM;AAC5C;;;;;;;;ACJA,SAAgB,MAAY,QAA2C;CACnE,IAAI,OAAO,MAAM,GAAG,OAAO;CAC3B,IAAI,OAAO,KAAK,GAAG,OAAO;CAC1B,MAAM,IAAI,wBAAwB,OAAO;AAC7C;;;;;;;;;;;ACDA,SAAgB,SAAe,QAAsB,OAAmB;CACpE,IAAI,OAAO,KAAK,GAAG,OAAO,OAAO,GAAG,OAAO,OAAO,KAAK;CACvD,IAAI,OAAO,MAAM,GAAG,OAAO;CAC3B,MAAM,IAAI,wBAAwB,UAAU;AAChD;;;;;;;;;;;ACJA,SAAgB,YAAkB,QAAsB,OAAmB;CACvE,IAAI,OAAO,MAAM,GAAG,OAAO,OAAO,GAAG,OAAO,OAAO,KAAK;CACxD,IAAI,OAAO,KAAK,GAAG,OAAO;CAC1B,MAAM,IAAI,wBAAwB,aAAa;AACnD"}
|
package/dist/operators.cjs
CHANGED
|
@@ -1,34 +1,415 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_result = require('./result-
|
|
3
|
-
const
|
|
2
|
+
const require_result = require('./result-DoqQufvR.cjs');
|
|
3
|
+
const require_errors = require('./errors-CVgC7NII.cjs');
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
//#region src/core/map.ts
|
|
6
|
+
/**
|
|
7
|
+
* Transforms the value (Ok case).
|
|
8
|
+
* Corresponds to Rust `map`.
|
|
9
|
+
*/
|
|
10
|
+
function map(project) {
|
|
11
|
+
return (source) => {
|
|
12
|
+
if (source.isOk()) return require_result.ok(project(source.value));
|
|
13
|
+
if (source.isErr()) return source;
|
|
14
|
+
throw new require_errors.InvalidResultStateError("map");
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/core/mapErr.ts
|
|
20
|
+
/**
|
|
21
|
+
* Transforms the error (Err case).
|
|
22
|
+
* Corresponds to Rust `map_err`.
|
|
23
|
+
*/
|
|
24
|
+
function mapErr(project) {
|
|
25
|
+
return (source) => {
|
|
26
|
+
if (source.isErr()) return require_result.err(project(source.error));
|
|
27
|
+
if (source.isOk()) return source;
|
|
28
|
+
throw new require_errors.InvalidResultStateError("mapErr");
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/core/mapBoth.ts
|
|
34
|
+
/**
|
|
35
|
+
* Transforms both the Ok value and the Err error.
|
|
36
|
+
* Corresponds to FP `bimap` / `mapBoth`.
|
|
37
|
+
*/
|
|
38
|
+
function mapBoth(mapOk, mapErr) {
|
|
39
|
+
return (source) => {
|
|
40
|
+
if (source.isOk()) return require_result.ok(mapOk(source.value));
|
|
41
|
+
if (source.isErr()) return require_result.err(mapErr(source.error));
|
|
42
|
+
throw new require_errors.InvalidResultStateError("mapBoth");
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Alias for `mapBoth`.
|
|
47
|
+
*/
|
|
48
|
+
const bimap = mapBoth;
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
//#region src/core/flatMap.ts
|
|
52
|
+
/**
|
|
53
|
+
* Chains another operation that returns a Result.
|
|
54
|
+
* Corresponds to Rust `and_then` or JS `flatMap`.
|
|
55
|
+
*/
|
|
56
|
+
function flatMap(project) {
|
|
57
|
+
return (source) => {
|
|
58
|
+
if (source.isOk()) return project(source.value);
|
|
59
|
+
if (source.isErr()) return source;
|
|
60
|
+
throw new require_errors.InvalidResultStateError("flatMap");
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/core/tap.ts
|
|
66
|
+
/**
|
|
67
|
+
* Executes a side effect (logging, debugging) without changing the Result.
|
|
68
|
+
* Corresponds to Rust `inspect` / `inspect_err`.
|
|
69
|
+
*/
|
|
70
|
+
function tap(observer) {
|
|
71
|
+
return (source) => {
|
|
72
|
+
if (source.isOk()) {
|
|
73
|
+
if (observer.ok) observer.ok(source.value);
|
|
74
|
+
return source;
|
|
75
|
+
}
|
|
76
|
+
if (source.isErr()) {
|
|
77
|
+
if (observer.err) observer.err(source.error);
|
|
78
|
+
return source;
|
|
79
|
+
}
|
|
80
|
+
throw new require_errors.InvalidResultStateError("tap");
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region src/core/filter.ts
|
|
86
|
+
/**
|
|
87
|
+
* Checks a condition. If false, the Result becomes Err.
|
|
88
|
+
* Corresponds to Rust `filter` (partially).
|
|
89
|
+
*/
|
|
90
|
+
function filter(predicate, errorFn) {
|
|
91
|
+
return (source) => {
|
|
92
|
+
if (source.isOk()) {
|
|
93
|
+
if (predicate(source.value)) return source;
|
|
94
|
+
return require_result.err(errorFn());
|
|
95
|
+
}
|
|
96
|
+
if (source.isErr()) return source;
|
|
97
|
+
throw new require_errors.InvalidResultStateError("filter");
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
//#endregion
|
|
102
|
+
//#region src/core/fold.ts
|
|
103
|
+
/**
|
|
104
|
+
* Folds the Result into a single value by applying one of two functions.
|
|
105
|
+
* The end of the pipe.
|
|
106
|
+
*
|
|
107
|
+
* This is the pipe operator equivalent of the `.fold()` instance method.
|
|
108
|
+
*/
|
|
109
|
+
function fold(handlers) {
|
|
110
|
+
return (source) => {
|
|
111
|
+
if (source.isOk()) return handlers.ok(source.value);
|
|
112
|
+
if (source.isErr()) return handlers.err(source.error);
|
|
113
|
+
throw new require_errors.InvalidResultStateError("fold");
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
//#endregion
|
|
118
|
+
//#region src/core/tryCatch.ts
|
|
119
|
+
function tryCatch(fn, errorMapper) {
|
|
120
|
+
return (source) => {
|
|
121
|
+
if (source.isErr()) return source;
|
|
122
|
+
try {
|
|
123
|
+
return require_result.ok(fn());
|
|
124
|
+
} catch (error) {
|
|
125
|
+
return require_result.err(errorMapper ? errorMapper(error) : error);
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
//#endregion
|
|
131
|
+
//#region src/core/tryMap.ts
|
|
132
|
+
function tryMap(project, errorMapper) {
|
|
133
|
+
return (source) => {
|
|
134
|
+
if (source.isErr()) return source;
|
|
135
|
+
if (!source.isOk()) throw new require_errors.InvalidResultStateError("tryMap");
|
|
136
|
+
try {
|
|
137
|
+
return require_result.ok(project(source.value));
|
|
138
|
+
} catch (error) {
|
|
139
|
+
return require_result.err(errorMapper ? errorMapper(error) : error);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
//#endregion
|
|
145
|
+
//#region src/core/recover.ts
|
|
146
|
+
/**
|
|
147
|
+
* Recover: converts Err to Ok(defaultValue).
|
|
148
|
+
* Result is guaranteed to be Ok → Error type becomes `never`.
|
|
149
|
+
*/
|
|
150
|
+
function recover(defaultValue) {
|
|
151
|
+
return (source) => {
|
|
152
|
+
if (source.isOk()) return source;
|
|
153
|
+
if (source.isErr()) return require_result.ok(defaultValue);
|
|
154
|
+
throw new require_errors.InvalidResultStateError("recover");
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Like `recover`, but calculates the default value based on the error.
|
|
159
|
+
*/
|
|
160
|
+
function recoverWith(fn) {
|
|
161
|
+
return (source) => {
|
|
162
|
+
if (source.isOk()) return source;
|
|
163
|
+
if (source.isErr()) return require_result.ok(fn(source.error));
|
|
164
|
+
throw new require_errors.InvalidResultStateError("recoverWith");
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
//#endregion
|
|
169
|
+
//#region src/core/match.ts
|
|
170
|
+
/**
|
|
171
|
+
* Resolves the Result. The end of the pipe.
|
|
172
|
+
* Corresponds to Rust `match`.
|
|
173
|
+
*/
|
|
174
|
+
function match(handlers) {
|
|
175
|
+
return (source) => {
|
|
176
|
+
if (source.isOk()) return handlers.ok(source.value);
|
|
177
|
+
if (source.isErr()) return handlers.err(source.error);
|
|
178
|
+
throw new require_errors.InvalidResultStateError("match");
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
//#endregion
|
|
183
|
+
//#region src/core/mapAsync.ts
|
|
184
|
+
/**
|
|
185
|
+
* Async version of map.
|
|
186
|
+
*/
|
|
187
|
+
function mapAsync(project) {
|
|
188
|
+
return async (source) => {
|
|
189
|
+
if (source.isOk()) return require_result.ok(await project(source.value));
|
|
190
|
+
if (source.isErr()) return source;
|
|
191
|
+
throw new require_errors.InvalidResultStateError("mapAsync");
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
//#endregion
|
|
196
|
+
//#region src/core/mapErrAsync.ts
|
|
197
|
+
/**
|
|
198
|
+
* Async version of mapErr.
|
|
199
|
+
*/
|
|
200
|
+
function mapErrAsync(project) {
|
|
201
|
+
return async (source) => {
|
|
202
|
+
if (source.isErr()) return require_result.err(await project(source.error));
|
|
203
|
+
if (source.isOk()) return source;
|
|
204
|
+
throw new require_errors.InvalidResultStateError("mapErrAsync");
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
//#endregion
|
|
209
|
+
//#region src/core/flatMapAsync.ts
|
|
210
|
+
/**
|
|
211
|
+
* Async version of flatMap.
|
|
212
|
+
*/
|
|
213
|
+
function flatMapAsync(project) {
|
|
214
|
+
return async (source) => {
|
|
215
|
+
if (source.isOk()) return await project(source.value);
|
|
216
|
+
if (source.isErr()) return source;
|
|
217
|
+
throw new require_errors.InvalidResultStateError("flatMapAsync");
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
//#endregion
|
|
222
|
+
//#region src/core/tapAsync.ts
|
|
223
|
+
/**
|
|
224
|
+
* Async version of tap.
|
|
225
|
+
*/
|
|
226
|
+
function tapAsync(observer) {
|
|
227
|
+
return async (source) => {
|
|
228
|
+
if (source.isOk()) {
|
|
229
|
+
if (observer.ok) await observer.ok(source.value);
|
|
230
|
+
return source;
|
|
231
|
+
}
|
|
232
|
+
if (source.isErr()) {
|
|
233
|
+
if (observer.err) await observer.err(source.error);
|
|
234
|
+
return source;
|
|
235
|
+
}
|
|
236
|
+
throw new require_errors.InvalidResultStateError("tapAsync");
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
//#endregion
|
|
241
|
+
//#region src/core/filterAsync.ts
|
|
242
|
+
/**
|
|
243
|
+
* Async version of filter.
|
|
244
|
+
*/
|
|
245
|
+
function filterAsync(predicate, errorFn) {
|
|
246
|
+
return async (source) => {
|
|
247
|
+
if (source.isOk()) {
|
|
248
|
+
if (await predicate(source.value)) return source;
|
|
249
|
+
return require_result.err(await errorFn());
|
|
250
|
+
}
|
|
251
|
+
if (source.isErr()) return source;
|
|
252
|
+
throw new require_errors.InvalidResultStateError("filterAsync");
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
//#endregion
|
|
257
|
+
//#region src/core/foldAsync.ts
|
|
258
|
+
/**
|
|
259
|
+
* Async version of `fold`. Folds the Result into a single value asynchronously.
|
|
260
|
+
* The end of the pipe.
|
|
261
|
+
*
|
|
262
|
+
* This is the async pipe operator equivalent of the `.fold()` instance method.
|
|
263
|
+
*/
|
|
264
|
+
function foldAsync(handlers) {
|
|
265
|
+
return async (source) => {
|
|
266
|
+
if (source.isOk()) return await handlers.ok(source.value);
|
|
267
|
+
if (source.isErr()) return await handlers.err(source.error);
|
|
268
|
+
throw new require_errors.InvalidResultStateError("foldAsync");
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
//#endregion
|
|
273
|
+
//#region src/core/tryCatchAsync.ts
|
|
274
|
+
function tryCatchAsync(fn, errorMapper) {
|
|
275
|
+
return async (source) => {
|
|
276
|
+
if (source.isErr()) return source;
|
|
277
|
+
try {
|
|
278
|
+
return require_result.ok(await fn());
|
|
279
|
+
} catch (error) {
|
|
280
|
+
return require_result.err(errorMapper ? errorMapper(error) : error);
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
//#endregion
|
|
286
|
+
//#region src/core/tryMapAsync.ts
|
|
287
|
+
function tryMapAsync(project, errorMapper) {
|
|
288
|
+
return async (source) => {
|
|
289
|
+
if (source.isErr()) return source;
|
|
290
|
+
if (!source.isOk()) throw new require_errors.InvalidResultStateError("tryMapAsync");
|
|
291
|
+
try {
|
|
292
|
+
return require_result.ok(await project(source.value));
|
|
293
|
+
} catch (error) {
|
|
294
|
+
return require_result.err(errorMapper ? errorMapper(error) : error);
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
//#endregion
|
|
300
|
+
//#region src/core/matchAsync.ts
|
|
301
|
+
/**
|
|
302
|
+
* Async version of match.
|
|
303
|
+
*/
|
|
304
|
+
function matchAsync(handlers) {
|
|
305
|
+
return async (source) => {
|
|
306
|
+
if (source.isOk()) return await handlers.ok(source.value);
|
|
307
|
+
if (source.isErr()) return await handlers.err(source.error);
|
|
308
|
+
throw new require_errors.InvalidResultStateError("matchAsync");
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
//#endregion
|
|
313
|
+
//#region src/core/and.ts
|
|
314
|
+
/**
|
|
315
|
+
* Combines two Results. Returns the second one only if the first is Ok.
|
|
316
|
+
* Corresponds to Rust `and`.
|
|
317
|
+
*/
|
|
318
|
+
function and(result, other) {
|
|
319
|
+
if (result.isOk()) return other;
|
|
320
|
+
if (result.isErr()) return result;
|
|
321
|
+
throw new require_errors.InvalidResultStateError("and");
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
//#endregion
|
|
325
|
+
//#region src/core/or.ts
|
|
326
|
+
/**
|
|
327
|
+
* Fallback to another Result if the first is Err.
|
|
328
|
+
* Corresponds to Rust `or`.
|
|
329
|
+
*/
|
|
330
|
+
function or(result, other) {
|
|
331
|
+
if (result.isOk()) return result;
|
|
332
|
+
if (result.isErr()) return other;
|
|
333
|
+
throw new require_errors.InvalidResultStateError("or");
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
//#endregion
|
|
337
|
+
//#region src/core/orElse.ts
|
|
338
|
+
/**
|
|
339
|
+
* Fallback with a function that returns a Result.
|
|
340
|
+
* Corresponds to Rust `or_else`.
|
|
341
|
+
*/
|
|
342
|
+
function orElse(result, fn) {
|
|
343
|
+
if (result.isOk()) return result;
|
|
344
|
+
if (result.isErr()) return fn(result.error);
|
|
345
|
+
throw new require_errors.InvalidResultStateError("orElse");
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
//#endregion
|
|
349
|
+
//#region src/core/mapOr.ts
|
|
350
|
+
/**
|
|
351
|
+
* Transforms the value or returns a default value.
|
|
352
|
+
* Corresponds to Rust `map_or`.
|
|
353
|
+
*/
|
|
354
|
+
function mapOr(result, defaultValue, fn) {
|
|
355
|
+
if (result.isOk()) return fn(result.value);
|
|
356
|
+
if (result.isErr()) return defaultValue;
|
|
357
|
+
throw new require_errors.InvalidResultStateError("mapOr");
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
//#endregion
|
|
361
|
+
//#region src/core/mapOrElse.ts
|
|
362
|
+
/**
|
|
363
|
+
* Transforms the value or calculates a default value using a function.
|
|
364
|
+
* Corresponds to Rust `map_or_else`.
|
|
365
|
+
*/
|
|
366
|
+
function mapOrElse(result, defaultFn, fn) {
|
|
367
|
+
if (result.isOk()) return fn(result.value);
|
|
368
|
+
if (result.isErr()) return defaultFn(result.error);
|
|
369
|
+
throw new require_errors.InvalidResultStateError("mapOrElse");
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
//#endregion
|
|
373
|
+
//#region src/core/swap.ts
|
|
374
|
+
/**
|
|
375
|
+
* Swaps Ok and Err.
|
|
376
|
+
* Result<T, E> → Result<E, T>
|
|
377
|
+
*/
|
|
378
|
+
function swap(result) {
|
|
379
|
+
if (result.isOk()) return require_result.err(result.value);
|
|
380
|
+
if (result.isErr()) return require_result.ok(result.error);
|
|
381
|
+
throw new require_errors.InvalidResultStateError("swap");
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
//#endregion
|
|
385
|
+
exports.and = and;
|
|
386
|
+
exports.bimap = bimap;
|
|
7
387
|
exports.combine = require_result.combine;
|
|
8
|
-
exports.filter =
|
|
9
|
-
exports.filterAsync =
|
|
10
|
-
exports.flatMap =
|
|
11
|
-
exports.flatMapAsync =
|
|
12
|
-
exports.fold =
|
|
13
|
-
exports.foldAsync =
|
|
14
|
-
exports.map =
|
|
15
|
-
exports.mapAsync =
|
|
16
|
-
exports.mapBoth =
|
|
17
|
-
exports.mapErr =
|
|
18
|
-
exports.mapErrAsync =
|
|
19
|
-
exports.mapOr =
|
|
20
|
-
exports.mapOrElse =
|
|
21
|
-
exports.match =
|
|
22
|
-
exports.matchAsync =
|
|
23
|
-
exports.or =
|
|
24
|
-
exports.orElse =
|
|
25
|
-
exports.recover =
|
|
26
|
-
exports.recoverWith =
|
|
27
|
-
exports.swap =
|
|
28
|
-
exports.tap =
|
|
29
|
-
exports.tapAsync =
|
|
30
|
-
exports.tryCatch =
|
|
31
|
-
exports.tryCatchAsync =
|
|
32
|
-
exports.tryMap =
|
|
33
|
-
exports.tryMapAsync =
|
|
34
|
-
exports.zip = require_result.zip;
|
|
388
|
+
exports.filter = filter;
|
|
389
|
+
exports.filterAsync = filterAsync;
|
|
390
|
+
exports.flatMap = flatMap;
|
|
391
|
+
exports.flatMapAsync = flatMapAsync;
|
|
392
|
+
exports.fold = fold;
|
|
393
|
+
exports.foldAsync = foldAsync;
|
|
394
|
+
exports.map = map;
|
|
395
|
+
exports.mapAsync = mapAsync;
|
|
396
|
+
exports.mapBoth = mapBoth;
|
|
397
|
+
exports.mapErr = mapErr;
|
|
398
|
+
exports.mapErrAsync = mapErrAsync;
|
|
399
|
+
exports.mapOr = mapOr;
|
|
400
|
+
exports.mapOrElse = mapOrElse;
|
|
401
|
+
exports.match = match;
|
|
402
|
+
exports.matchAsync = matchAsync;
|
|
403
|
+
exports.or = or;
|
|
404
|
+
exports.orElse = orElse;
|
|
405
|
+
exports.recover = recover;
|
|
406
|
+
exports.recoverWith = recoverWith;
|
|
407
|
+
exports.swap = swap;
|
|
408
|
+
exports.tap = tap;
|
|
409
|
+
exports.tapAsync = tapAsync;
|
|
410
|
+
exports.tryCatch = tryCatch;
|
|
411
|
+
exports.tryCatchAsync = tryCatchAsync;
|
|
412
|
+
exports.tryMap = tryMap;
|
|
413
|
+
exports.tryMapAsync = tryMapAsync;
|
|
414
|
+
exports.zip = require_result.zip;
|
|
415
|
+
//# sourceMappingURL=operators.cjs.map
|