@shirudo/result 1.1.0 → 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 +90 -38
- 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.cjs
CHANGED
|
@@ -1,18 +1,46 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_result = require('./result-
|
|
3
|
-
const require_errors = require('./errors.cjs');
|
|
4
|
-
const
|
|
5
|
-
const
|
|
2
|
+
const require_result = require('./result-DoqQufvR.cjs');
|
|
3
|
+
const require_errors = require('./errors-CVgC7NII.cjs');
|
|
4
|
+
const require_operators = require('./operators.cjs');
|
|
5
|
+
const require_collections = require('./collections.cjs');
|
|
6
6
|
|
|
7
7
|
//#region src/gen.ts
|
|
8
8
|
async function task(makeGenerator, onThrow) {
|
|
9
9
|
const iterator = makeGenerator();
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
const decide = (yielded) => {
|
|
11
|
+
if (!require_result.isResult(yielded)) return {
|
|
12
|
+
kind: "throw",
|
|
13
|
+
error: new require_errors.TaskYieldNotResultError(yielded)
|
|
14
|
+
};
|
|
15
|
+
if (yielded.isOk()) return {
|
|
16
|
+
kind: "continue",
|
|
17
|
+
input: yielded.value
|
|
18
|
+
};
|
|
19
|
+
if (yielded.isErr()) return {
|
|
20
|
+
kind: "return",
|
|
21
|
+
result: yielded
|
|
22
|
+
};
|
|
23
|
+
return {
|
|
24
|
+
kind: "throw",
|
|
25
|
+
error: new require_errors.InvalidResultStateError("task")
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
const abandon = async (pending) => {
|
|
29
|
+
if (typeof iterator.return !== "function") return pending;
|
|
30
|
+
let completion = pending;
|
|
13
31
|
let step = await iterator.return(void 0);
|
|
14
|
-
while (!step.done)
|
|
32
|
+
while (!step.done) {
|
|
33
|
+
const decision = decide(step.value);
|
|
34
|
+
if (decision.kind === "continue") {
|
|
35
|
+
step = await iterator.next(decision.input);
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
completion = decision;
|
|
39
|
+
step = await iterator.return(void 0);
|
|
40
|
+
}
|
|
41
|
+
return completion;
|
|
15
42
|
};
|
|
43
|
+
let input = void 0;
|
|
16
44
|
while (true) {
|
|
17
45
|
let step;
|
|
18
46
|
try {
|
|
@@ -29,36 +57,20 @@ async function task(makeGenerator, onThrow) {
|
|
|
29
57
|
if (!onThrow) throw caught;
|
|
30
58
|
return require_result.err(onThrow(caught));
|
|
31
59
|
}
|
|
32
|
-
const
|
|
33
|
-
if (
|
|
34
|
-
|
|
35
|
-
await runGeneratorReturn();
|
|
36
|
-
} catch (caught) {
|
|
37
|
-
if (!onThrow) throw caught;
|
|
38
|
-
return require_result.err(onThrow(caught));
|
|
39
|
-
}
|
|
40
|
-
throw new require_errors.TaskYieldNotResultError(yielded);
|
|
41
|
-
}
|
|
42
|
-
if (yielded.isOk()) {
|
|
43
|
-
input = yielded.value;
|
|
60
|
+
const decision = decide(step.value);
|
|
61
|
+
if (decision.kind === "continue") {
|
|
62
|
+
input = decision.input;
|
|
44
63
|
continue;
|
|
45
64
|
}
|
|
46
|
-
|
|
47
|
-
try {
|
|
48
|
-
await runGeneratorReturn();
|
|
49
|
-
} catch (caught) {
|
|
50
|
-
if (!onThrow) throw caught;
|
|
51
|
-
return require_result.err(onThrow(caught));
|
|
52
|
-
}
|
|
53
|
-
return yielded;
|
|
54
|
-
}
|
|
65
|
+
let completion;
|
|
55
66
|
try {
|
|
56
|
-
await
|
|
67
|
+
completion = await abandon(decision);
|
|
57
68
|
} catch (caught) {
|
|
58
69
|
if (!onThrow) throw caught;
|
|
59
70
|
return require_result.err(onThrow(caught));
|
|
60
71
|
}
|
|
61
|
-
|
|
72
|
+
if (completion.kind === "throw") throw completion.error;
|
|
73
|
+
return completion.result;
|
|
62
74
|
}
|
|
63
75
|
}
|
|
64
76
|
const gen = task;
|
|
@@ -169,11 +181,29 @@ function expectErr(result, message) {
|
|
|
169
181
|
* by `Result.toSerialized()` and by `JSON.stringify` on a Result.
|
|
170
182
|
*
|
|
171
183
|
* Throws `InvalidResultStateError` for data that is not in that shape.
|
|
184
|
+
*
|
|
185
|
+
* @deprecated Rebuild the Result yourself after validating the payload:
|
|
186
|
+
*
|
|
187
|
+
* ```ts
|
|
188
|
+
* const restored = parsed._tag === 'Ok' ? ok(parsed.value) : err(parsed.error);
|
|
189
|
+
* ```
|
|
190
|
+
*
|
|
191
|
+
* `fromSerialized` only checks the envelope, the `_tag` discriminant plus the
|
|
192
|
+
* absence of an own key of the other state, never the payload itself, while its
|
|
193
|
+
* type parameters claim `T` and `E` for whatever `JSON.parse` returned. It
|
|
194
|
+
* also throws for malformed input at exactly the boundary where a Result
|
|
195
|
+
* should be returned. Validate foreign data with your schema tool and
|
|
196
|
+
* then call `ok`/`err`; the shape is exported as `ResultType<T, E>`. This
|
|
197
|
+
* function will be removed in 2.0.
|
|
172
198
|
*/
|
|
173
199
|
function fromSerialized(data) {
|
|
174
|
-
if (data !== null && typeof data === "object") {
|
|
175
|
-
if (data._tag === "Ok" && "
|
|
176
|
-
if (data._tag === "Err" && "
|
|
200
|
+
if (data !== null && typeof data === "object") try {
|
|
201
|
+
if (data._tag === "Ok" && !Object.hasOwn(data, "error")) return require_result.ok(data.value);
|
|
202
|
+
if (data._tag === "Err" && !Object.hasOwn(data, "value")) return require_result.err(data.error);
|
|
203
|
+
} catch (error) {
|
|
204
|
+
const invalid = new require_errors.InvalidResultStateError("fromSerialized");
|
|
205
|
+
invalid.cause = error;
|
|
206
|
+
throw invalid;
|
|
177
207
|
}
|
|
178
208
|
throw new require_errors.InvalidResultStateError("fromSerialized");
|
|
179
209
|
}
|
|
@@ -282,25 +312,25 @@ exports.TaskYieldNotResultError = require_errors.TaskYieldNotResultError;
|
|
|
282
312
|
exports.UnwrapErrOnOkError = require_errors.UnwrapErrOnOkError;
|
|
283
313
|
exports.UnwrapOnErrError = require_errors.UnwrapOnErrError;
|
|
284
314
|
exports.all = require_result.all;
|
|
285
|
-
exports.and =
|
|
286
|
-
exports.bimap =
|
|
287
|
-
exports.collectAllErrors =
|
|
288
|
-
exports.collectFirstOk =
|
|
289
|
-
exports.collectFirstOkAsync =
|
|
290
|
-
exports.collectFirstOkParallelAsync =
|
|
315
|
+
exports.and = require_operators.and;
|
|
316
|
+
exports.bimap = require_operators.bimap;
|
|
317
|
+
exports.collectAllErrors = require_collections.collectAllErrors;
|
|
318
|
+
exports.collectFirstOk = require_collections.collectFirstOk;
|
|
319
|
+
exports.collectFirstOkAsync = require_collections.collectFirstOkAsync;
|
|
320
|
+
exports.collectFirstOkParallelAsync = require_collections.collectFirstOkParallelAsync;
|
|
291
321
|
exports.combine = require_result.combine;
|
|
292
322
|
exports.contains = contains;
|
|
293
323
|
exports.containsErr = containsErr;
|
|
294
324
|
exports.err = require_result.err;
|
|
295
325
|
exports.expectErr = expectErr;
|
|
296
326
|
exports.expectResult = expectResult;
|
|
297
|
-
exports.filter =
|
|
298
|
-
exports.filterAsync =
|
|
299
|
-
exports.flatMap =
|
|
300
|
-
exports.flatMapAsync =
|
|
301
|
-
exports.flatten =
|
|
302
|
-
exports.fold =
|
|
303
|
-
exports.foldAsync =
|
|
327
|
+
exports.filter = require_operators.filter;
|
|
328
|
+
exports.filterAsync = require_operators.filterAsync;
|
|
329
|
+
exports.flatMap = require_operators.flatMap;
|
|
330
|
+
exports.flatMapAsync = require_operators.flatMapAsync;
|
|
331
|
+
exports.flatten = require_collections.flatten;
|
|
332
|
+
exports.fold = require_operators.fold;
|
|
333
|
+
exports.foldAsync = require_operators.foldAsync;
|
|
304
334
|
exports.fromNullable = require_result.fromNullable;
|
|
305
335
|
exports.fromPromise = require_result.fromPromise;
|
|
306
336
|
exports.fromSerialized = fromSerialized;
|
|
@@ -309,38 +339,38 @@ exports.gen = gen;
|
|
|
309
339
|
exports.isErr = isErr;
|
|
310
340
|
exports.isOk = isOk;
|
|
311
341
|
exports.isResult = require_result.isResult;
|
|
312
|
-
exports.map =
|
|
313
|
-
exports.mapAsync =
|
|
314
|
-
exports.mapBoth =
|
|
315
|
-
exports.mapErr =
|
|
316
|
-
exports.mapErrAsync =
|
|
317
|
-
exports.mapOr =
|
|
318
|
-
exports.mapOrElse =
|
|
319
|
-
exports.match =
|
|
320
|
-
exports.matchAsync =
|
|
342
|
+
exports.map = require_operators.map;
|
|
343
|
+
exports.mapAsync = require_operators.mapAsync;
|
|
344
|
+
exports.mapBoth = require_operators.mapBoth;
|
|
345
|
+
exports.mapErr = require_operators.mapErr;
|
|
346
|
+
exports.mapErrAsync = require_operators.mapErrAsync;
|
|
347
|
+
exports.mapOr = require_operators.mapOr;
|
|
348
|
+
exports.mapOrElse = require_operators.mapOrElse;
|
|
349
|
+
exports.match = require_operators.match;
|
|
350
|
+
exports.matchAsync = require_operators.matchAsync;
|
|
321
351
|
exports.matchTag = require_result.matchTag;
|
|
322
352
|
exports.ok = require_result.ok;
|
|
323
353
|
exports.okIf = require_result.okIf;
|
|
324
354
|
exports.okIfLazy = require_result.okIfLazy;
|
|
325
|
-
exports.or =
|
|
326
|
-
exports.orElse =
|
|
327
|
-
exports.partition =
|
|
328
|
-
exports.recover =
|
|
329
|
-
exports.recoverWith =
|
|
355
|
+
exports.or = require_operators.or;
|
|
356
|
+
exports.orElse = require_operators.orElse;
|
|
357
|
+
exports.partition = require_collections.partition;
|
|
358
|
+
exports.recover = require_operators.recover;
|
|
359
|
+
exports.recoverWith = require_operators.recoverWith;
|
|
330
360
|
exports.sequence = require_result.sequence;
|
|
331
|
-
exports.sequenceRecord =
|
|
332
|
-
exports.swap =
|
|
333
|
-
exports.tap =
|
|
334
|
-
exports.tapAsync =
|
|
361
|
+
exports.sequenceRecord = require_collections.sequenceRecord;
|
|
362
|
+
exports.swap = require_operators.swap;
|
|
363
|
+
exports.tap = require_operators.tap;
|
|
364
|
+
exports.tapAsync = require_operators.tapAsync;
|
|
335
365
|
exports.task = task;
|
|
336
366
|
exports.toNullable = toNullable;
|
|
337
367
|
exports.toPromise = toPromise;
|
|
338
368
|
exports.tryAsync = require_result.tryAsync;
|
|
339
|
-
exports.tryCatch =
|
|
340
|
-
exports.tryCatchAsync =
|
|
369
|
+
exports.tryCatch = require_operators.tryCatch;
|
|
370
|
+
exports.tryCatchAsync = require_operators.tryCatchAsync;
|
|
341
371
|
exports.tryFn = require_result.tryFn;
|
|
342
|
-
exports.tryMap =
|
|
343
|
-
exports.tryMapAsync =
|
|
372
|
+
exports.tryMap = require_operators.tryMap;
|
|
373
|
+
exports.tryMapAsync = require_operators.tryMapAsync;
|
|
344
374
|
exports.unwrap = unwrap;
|
|
345
375
|
exports.unwrapErr = unwrapErr;
|
|
346
376
|
exports.unwrapOr = unwrapOr;
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["err","isResult","ok","TaskYieldNotResultError","InvalidResultStateError","UnwrapOnErrError","InvalidResultStateError","InvalidResultStateError","InvalidResultStateError","InvalidResultStateError","UnwrapErrOnOkError","InvalidResultStateError","ExpectOkError","InvalidResultStateError","ExpectErrError","InvalidResultStateError","ok","err","InvalidResultStateError","InvalidResultStateError","InvalidResultStateError","InvalidResultStateError","InvalidResultStateError","InvalidResultStateError","InvalidResultStateError"],"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,OAAOA,mBAA+D,QAAQ,MAAM,CAAC;EACzF;EAEA,IAAI,KAAK,MACL,IAAI;GACA,MAAM,UAAU,MAAM,KAAK;GAC3B,IAAIC,wBAAS,OAAO,GAChB,OAAO;GAEX,OAAOC,kBAA8D,OAAwB;EACjG,SAAS,QAAQ;GACb,IAAI,CAAC,SAAS,MAAM;GACpB,OAAOF,mBAA+D,QAAQ,MAAM,CAAC;EACzF;EAGJ,MAAM,UAAU,KAAK;EACrB,IAAI,CAACC,wBAAS,OAAO,GAAG;GACpB,IAAI;IACA,MAAM,mBAAmB;GAC7B,SAAS,QAAQ;IACb,IAAI,CAAC,SAAS,MAAM;IACpB,OAAOD,mBAA+D,QAAQ,MAAM,CAAC;GACzF;GACA,MAAM,IAAIG,uCAAwB,OAAO;E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KAAK,GAAG,OAAO;CAC1B,IAAI,OAAO,MAAM,GAAG,OAAO;CAC3B,MAAM,IAAIC,uCAAwB,MAAM;AAC5C;;;;;;;;ACJA,SAAgB,MAAY,QAA2C;CACnE,IAAI,OAAO,MAAM,GAAG,OAAO;CAC3B,IAAI,OAAO,KAAK,GAAG,OAAO;CAC1B,MAAM,IAAIC,uCAAwB,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,IAAIC,uCAAwB,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,IAAIC,uCAAwB,aAAa;AACnD"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["isResult","TaskYieldNotResultError","InvalidResultStateError","err","ok","UnwrapOnErrError","InvalidResultStateError","InvalidResultStateError","InvalidResultStateError","InvalidResultStateError","UnwrapErrOnOkError","InvalidResultStateError","ExpectOkError","InvalidResultStateError","ExpectErrError","InvalidResultStateError","ok","err","InvalidResultStateError","InvalidResultStateError","InvalidResultStateError","InvalidResultStateError","InvalidResultStateError","InvalidResultStateError","InvalidResultStateError"],"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,CAACA,wBAAS,OAAO,GAAG,OAAO;GAAE,MAAM;GAAS,OAAO,IAAIC,uCAAwB,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,IAAIC,uCAAwB,MAAM;EAAE;CACvE;CAQA,MAAM,UAAU,OACZ,YACiF;EACjF,IAAI,OAAO,SAAS,WAAW,YAAY,OAAO;EAClD,IAAI,aA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mB;CACvE,IAAI,OAAO,MAAM,GAAG,OAAO,OAAO,GAAG,OAAO,OAAO,KAAK;CACxD,IAAI,OAAO,KAAK,GAAG,OAAO;CAC1B,MAAM,IAAIC,uCAAwB,aAAa;AACnD"}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { C as ErrMatchBuilder, E as Awaitable, S as AsyncErrorMatchBuilder, T as matchTag, _ as tryFn, a as Ok, b as isResult, c as ResultType, d as fromPromise, f as fromThrowable, g as tryAsync, h as okIfLazy, i as Err, l as err, m as okIf, n as sequence, o as OperatorFunction, p as ok, r as AsyncOperatorFunction, s as Result, t as all, u as fromNullable, v as combine, w as ErrorMatchBuilder, x as AsyncErrMatchBuilder, y as zip } from "./sequence-CmugKPbu.cjs";
|
|
2
|
+
import { collectAllErrors, collectFirstOk, collectFirstOkAsync, collectFirstOkParallelAsync, flatten, partition, sequenceRecord } from "./collections.cjs";
|
|
3
3
|
import { ERR_EXPECT_ERR, ERR_EXPECT_OK, ERR_INVALID_RESULT_STATE, ERR_INVALID_STATE, ERR_MATCH_ERR_HANDLER_NOT_RESULT, ERR_MATCH_ON_OK, ERR_MATCH_TAG_MISSING_HANDLER, ERR_TASK_YIELD_NOT_RESULT, ERR_UNWRAP_ERR_ON_OK, ERR_UNWRAP_ON_ERR, ExpectErrError, ExpectOkError, InvalidResultStateError, MatchErrHandlerNotResultError, MatchOnOkError, MatchTagMissingHandlerError, ResultError, ResultErrorCode, ResultTypeError, TaskYieldNotResultError, UnwrapErrOnOkError, UnwrapOnErrError } from "./errors.cjs";
|
|
4
|
-
import {
|
|
5
|
-
|
|
4
|
+
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.cjs";
|
|
6
5
|
//#region src/gen.d.ts
|
|
7
6
|
type AnyGenerator<Y = unknown, R = unknown, N = unknown> = Generator<Y, R, N> | AsyncGenerator<Y, R, N>;
|
|
8
7
|
type ErrorOfYield<Y> = Y extends Result<any, infer E> ? E : never;
|
|
@@ -11,138 +10,152 @@ type AwaitedReturn<R> = Awaited<R>;
|
|
|
11
10
|
type OkOfReturn<R> = AwaitedReturn<R> extends Result<infer T, any> ? T : AwaitedReturn<R>;
|
|
12
11
|
type ErrOfReturn<R> = AwaitedReturn<R> extends Result<any, infer E> ? E : never;
|
|
13
12
|
/**
|
|
14
|
-
* Generator-based do-notation for `Result`.
|
|
15
|
-
*
|
|
16
|
-
* Usage:
|
|
17
|
-
* ```ts
|
|
18
|
-
* const out = await task(async function* () {
|
|
19
|
-
* const user = yield* await fromPromise(findUser(1));
|
|
20
|
-
* const email = yield* validate(user.email);
|
|
21
|
-
* return email;
|
|
22
|
-
* });
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
13
|
+
* Generator-based do-notation for `Result`.
|
|
14
|
+
*
|
|
15
|
+
* Usage:
|
|
16
|
+
* ```ts
|
|
17
|
+
* const out = await task(async function* () {
|
|
18
|
+
* const user = yield* await fromPromise(findUser(1));
|
|
19
|
+
* const email = yield* validate(user.email);
|
|
20
|
+
* return email;
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
25
24
|
declare function task<const Y, const R>(makeGenerator: () => AnyGenerator<Y, R, unknown>): Promise<Result<OkOfReturn<R>, ErrorOfYield<Y> | ErrOfReturn<R>>>;
|
|
26
25
|
declare function task<const Y, const R, EThrown>(makeGenerator: () => AnyGenerator<Y, R, unknown>, onThrow: OnThrow<EThrown>): Promise<Result<OkOfReturn<R>, ErrorOfYield<Y> | ErrOfReturn<R> | EThrown>>;
|
|
27
26
|
declare const gen: typeof task;
|
|
28
27
|
//#endregion
|
|
29
28
|
//#region src/core/unwrap.d.ts
|
|
30
29
|
/**
|
|
31
|
-
* Returns the value or throws an Error.
|
|
32
|
-
* Corresponds to Rust `unwrap`.
|
|
33
|
-
*/
|
|
30
|
+
* Returns the value or throws an Error.
|
|
31
|
+
* Corresponds to Rust `unwrap`.
|
|
32
|
+
*/
|
|
34
33
|
declare function unwrap<T, E>(result: Result<T, E>): T;
|
|
35
34
|
//#endregion
|
|
36
35
|
//#region src/core/unwrapOr.d.ts
|
|
37
36
|
/**
|
|
38
|
-
* Returns the value or a default value.
|
|
39
|
-
* Pure function alternative to the instance method.
|
|
40
|
-
*/
|
|
37
|
+
* Returns the value or a default value.
|
|
38
|
+
* Pure function alternative to the instance method.
|
|
39
|
+
*/
|
|
41
40
|
declare function unwrapOr<T, E>(result: Result<T, E>, defaultValue: T): T;
|
|
42
41
|
//#endregion
|
|
43
42
|
//#region src/core/unwrapOrElse.d.ts
|
|
44
43
|
/**
|
|
45
|
-
* Returns the value or calculates a default value using a function.
|
|
46
|
-
* Corresponds to Rust `unwrap_or_else`.
|
|
47
|
-
*/
|
|
44
|
+
* Returns the value or calculates a default value using a function.
|
|
45
|
+
* Corresponds to Rust `unwrap_or_else`.
|
|
46
|
+
*/
|
|
48
47
|
declare function unwrapOrElse<T, E>(result: Result<T, E>, fn: (error: E) => T): T;
|
|
49
48
|
//#endregion
|
|
50
49
|
//#region src/core/unwrapOrDefault.d.ts
|
|
51
50
|
/**
|
|
52
|
-
* Alias for `unwrapOr`.
|
|
53
|
-
*
|
|
54
|
-
* @deprecated Use {@link unwrapOr} instead. The name is misleading: Rust's
|
|
55
|
-
* `unwrap_or_default` takes no argument (it uses the type's `Default`),
|
|
56
|
-
* whereas this function requires an explicit default value — which is exactly
|
|
57
|
-
* what `unwrapOr` does.
|
|
58
|
-
*/
|
|
51
|
+
* Alias for `unwrapOr`.
|
|
52
|
+
*
|
|
53
|
+
* @deprecated Use {@link unwrapOr} instead. The name is misleading: Rust's
|
|
54
|
+
* `unwrap_or_default` takes no argument (it uses the type's `Default`),
|
|
55
|
+
* whereas this function requires an explicit default value — which is exactly
|
|
56
|
+
* what `unwrapOr` does.
|
|
57
|
+
*/
|
|
59
58
|
declare function unwrapOrDefault<T, E>(result: Result<T, E>, defaultValue: T): T;
|
|
60
59
|
//#endregion
|
|
61
60
|
//#region src/core/unwrapOrThrow.d.ts
|
|
62
61
|
/**
|
|
63
|
-
* Returns the value or throws the original Err value (not wrapped).
|
|
64
|
-
* Useful to preserve `Error` instances including stack traces.
|
|
65
|
-
*/
|
|
62
|
+
* Returns the value or throws the original Err value (not wrapped).
|
|
63
|
+
* Useful to preserve `Error` instances including stack traces.
|
|
64
|
+
*/
|
|
66
65
|
declare function unwrapOrThrow<T, E>(result: Result<T, E>): T;
|
|
67
66
|
//#endregion
|
|
68
67
|
//#region src/core/unwrapErr.d.ts
|
|
69
68
|
/**
|
|
70
|
-
* Returns the error or throws an Error.
|
|
71
|
-
* Corresponds to Rust `unwrap_err`.
|
|
72
|
-
*/
|
|
69
|
+
* Returns the error or throws an Error.
|
|
70
|
+
* Corresponds to Rust `unwrap_err`.
|
|
71
|
+
*/
|
|
73
72
|
declare function unwrapErr<T, E>(result: Result<T, E>): E;
|
|
74
73
|
//#endregion
|
|
75
74
|
//#region src/core/expectResult.d.ts
|
|
76
75
|
/**
|
|
77
|
-
* Returns the value or throws an Error with a custom message.
|
|
78
|
-
* Corresponds to Rust `expect`.
|
|
79
|
-
*/
|
|
76
|
+
* Returns the value or throws an Error with a custom message.
|
|
77
|
+
* Corresponds to Rust `expect`.
|
|
78
|
+
*/
|
|
80
79
|
declare function expectResult<T, E>(result: Result<T, E>, message: string): T;
|
|
81
80
|
//#endregion
|
|
82
81
|
//#region src/core/expectErr.d.ts
|
|
83
82
|
/**
|
|
84
|
-
* Returns the error or throws an Error with a custom message.
|
|
85
|
-
* Corresponds to Rust `expect_err`.
|
|
86
|
-
*/
|
|
83
|
+
* Returns the error or throws an Error with a custom message.
|
|
84
|
+
* Corresponds to Rust `expect_err`.
|
|
85
|
+
*/
|
|
87
86
|
declare function expectErr<T, E>(result: Result<T, E>, message: string): E;
|
|
88
87
|
//#endregion
|
|
89
88
|
//#region src/core/fromSerialized.d.ts
|
|
90
89
|
/**
|
|
91
|
-
* Rebuilds a `Result` from its serialized discriminated form
|
|
92
|
-
* (`{ _tag: 'Ok', value }` / `{ _tag: 'Err', error }`), the shape produced
|
|
93
|
-
* by `Result.toSerialized()` and by `JSON.stringify` on a Result.
|
|
94
|
-
*
|
|
95
|
-
* Throws `InvalidResultStateError` for data that is not in that shape.
|
|
96
|
-
|
|
90
|
+
* Rebuilds a `Result` from its serialized discriminated form
|
|
91
|
+
* (`{ _tag: 'Ok', value }` / `{ _tag: 'Err', error }`), the shape produced
|
|
92
|
+
* by `Result.toSerialized()` and by `JSON.stringify` on a Result.
|
|
93
|
+
*
|
|
94
|
+
* Throws `InvalidResultStateError` for data that is not in that shape.
|
|
95
|
+
*
|
|
96
|
+
* @deprecated Rebuild the Result yourself after validating the payload:
|
|
97
|
+
*
|
|
98
|
+
* ```ts
|
|
99
|
+
* const restored = parsed._tag === 'Ok' ? ok(parsed.value) : err(parsed.error);
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* `fromSerialized` only checks the envelope, the `_tag` discriminant plus the
|
|
103
|
+
* absence of an own key of the other state, never the payload itself, while its
|
|
104
|
+
* type parameters claim `T` and `E` for whatever `JSON.parse` returned. It
|
|
105
|
+
* also throws for malformed input at exactly the boundary where a Result
|
|
106
|
+
* should be returned. Validate foreign data with your schema tool and
|
|
107
|
+
* then call `ok`/`err`; the shape is exported as `ResultType<T, E>`. This
|
|
108
|
+
* function will be removed in 2.0.
|
|
109
|
+
*/
|
|
97
110
|
declare function fromSerialized<T, E>(data: ResultType<T, E>): Result<T, E>;
|
|
98
111
|
//#endregion
|
|
99
112
|
//#region src/core/toPromise.d.ts
|
|
100
113
|
/**
|
|
101
|
-
* Converts a Result to a Promise.
|
|
102
|
-
* Ok → resolve(value), Err → reject(error)
|
|
103
|
-
*/
|
|
114
|
+
* Converts a Result to a Promise.
|
|
115
|
+
* Ok → resolve(value), Err → reject(error)
|
|
116
|
+
*/
|
|
104
117
|
declare function toPromise<T, E>(result: Result<T, E>): Promise<T>;
|
|
105
118
|
//#endregion
|
|
106
119
|
//#region src/core/toNullable.d.ts
|
|
107
120
|
/**
|
|
108
|
-
* Converts a Result to `T | null`.
|
|
109
|
-
* Ok → value, Err → null
|
|
110
|
-
*/
|
|
121
|
+
* Converts a Result to `T | null`.
|
|
122
|
+
* Ok → value, Err → null
|
|
123
|
+
*/
|
|
111
124
|
declare function toNullable<T, E>(result: Result<T, E>): T | null;
|
|
112
125
|
//#endregion
|
|
113
126
|
//#region src/core/isOk.d.ts
|
|
114
127
|
/**
|
|
115
|
-
* Checks if a Result is Ok.
|
|
116
|
-
* Pure function alternative to the instance method.
|
|
117
|
-
*/
|
|
128
|
+
* Checks if a Result is Ok.
|
|
129
|
+
* Pure function alternative to the instance method.
|
|
130
|
+
*/
|
|
118
131
|
declare function isOk<T, E>(result: Result<T, E>): result is Ok<T, E>;
|
|
119
132
|
//#endregion
|
|
120
133
|
//#region src/core/isErr.d.ts
|
|
121
134
|
/**
|
|
122
|
-
* Checks if a Result is Err.
|
|
123
|
-
* Pure function alternative to the instance method.
|
|
124
|
-
*/
|
|
135
|
+
* Checks if a Result is Err.
|
|
136
|
+
* Pure function alternative to the instance method.
|
|
137
|
+
*/
|
|
125
138
|
declare function isErr<T, E>(result: Result<T, E>): result is Err<T, E>;
|
|
126
139
|
//#endregion
|
|
127
140
|
//#region src/core/contains.d.ts
|
|
128
141
|
/**
|
|
129
|
-
* Checks if the Result contains a specific value.
|
|
130
|
-
* Corresponds to Rust `contains`.
|
|
131
|
-
*
|
|
132
|
-
* Comparison uses `Object.is` (SameValue): `NaN` matches `NaN`, `+0` and
|
|
133
|
-
* `-0` are distinct, and objects are compared by reference.
|
|
134
|
-
*/
|
|
142
|
+
* Checks if the Result contains a specific value.
|
|
143
|
+
* Corresponds to Rust `contains`.
|
|
144
|
+
*
|
|
145
|
+
* Comparison uses `Object.is` (SameValue): `NaN` matches `NaN`, `+0` and
|
|
146
|
+
* `-0` are distinct, and objects are compared by reference.
|
|
147
|
+
*/
|
|
135
148
|
declare function contains<T, E>(result: Result<T, E>, value: T): boolean;
|
|
136
149
|
//#endregion
|
|
137
150
|
//#region src/core/containsErr.d.ts
|
|
138
151
|
/**
|
|
139
|
-
* Checks if the Result contains a specific error.
|
|
140
|
-
* Analogous to `contains` for the Err case.
|
|
141
|
-
*
|
|
142
|
-
* Comparison uses `Object.is` (SameValue): `NaN` matches `NaN`, `+0` and
|
|
143
|
-
* `-0` are distinct, and objects are compared by reference.
|
|
144
|
-
*/
|
|
152
|
+
* Checks if the Result contains a specific error.
|
|
153
|
+
* Analogous to `contains` for the Err case.
|
|
154
|
+
*
|
|
155
|
+
* Comparison uses `Object.is` (SameValue): `NaN` matches `NaN`, `+0` and
|
|
156
|
+
* `-0` are distinct, and objects are compared by reference.
|
|
157
|
+
*/
|
|
145
158
|
declare function containsErr<T, E>(result: Result<T, E>, error: E): boolean;
|
|
146
159
|
//#endregion
|
|
147
|
-
export { AsyncOperatorFunction, type Awaitable, ERR_EXPECT_ERR, ERR_EXPECT_OK, ERR_INVALID_RESULT_STATE, ERR_INVALID_STATE, ERR_MATCH_ERR_HANDLER_NOT_RESULT, ERR_MATCH_ON_OK, ERR_MATCH_TAG_MISSING_HANDLER, ERR_TASK_YIELD_NOT_RESULT, ERR_UNWRAP_ERR_ON_OK, ERR_UNWRAP_ON_ERR, Err, ExpectErrError, ExpectOkError, InvalidResultStateError, MatchErrHandlerNotResultError, MatchOnOkError, MatchTagMissingHandlerError, Ok, OperatorFunction, Result, ResultError, ResultErrorCode, ResultType, ResultTypeError, TaskYieldNotResultError, UnwrapErrOnOkError, UnwrapOnErrError, all, and, bimap, collectAllErrors, collectFirstOk, collectFirstOkAsync, collectFirstOkParallelAsync, combine, contains, containsErr, err, expectErr, expectResult, filter, filterAsync, flatMap, flatMapAsync, flatten, fold, foldAsync, fromNullable, fromPromise, fromSerialized, fromThrowable, gen, isErr, isOk, isResult, map, mapAsync, mapBoth, mapErr, mapErrAsync, mapOr, mapOrElse, match, matchAsync, matchTag, ok, okIf, okIfLazy, or, orElse, partition, recover, recoverWith, sequence, sequenceRecord, swap, tap, tapAsync, task, toNullable, toPromise, tryAsync, tryCatch, tryCatchAsync, tryFn, tryMap, tryMapAsync, unwrap, unwrapErr, unwrapOr, unwrapOrDefault, unwrapOrElse, unwrapOrThrow, zip };
|
|
160
|
+
export { type AsyncErrMatchBuilder, type AsyncErrorMatchBuilder, AsyncOperatorFunction, type Awaitable, ERR_EXPECT_ERR, ERR_EXPECT_OK, ERR_INVALID_RESULT_STATE, ERR_INVALID_STATE, ERR_MATCH_ERR_HANDLER_NOT_RESULT, ERR_MATCH_ON_OK, ERR_MATCH_TAG_MISSING_HANDLER, ERR_TASK_YIELD_NOT_RESULT, ERR_UNWRAP_ERR_ON_OK, ERR_UNWRAP_ON_ERR, Err, type ErrMatchBuilder, type ErrorMatchBuilder, ExpectErrError, ExpectOkError, InvalidResultStateError, MatchErrHandlerNotResultError, MatchOnOkError, MatchTagMissingHandlerError, Ok, OperatorFunction, Result, ResultError, ResultErrorCode, ResultType, ResultTypeError, TaskYieldNotResultError, UnwrapErrOnOkError, UnwrapOnErrError, all, and, bimap, collectAllErrors, collectFirstOk, collectFirstOkAsync, collectFirstOkParallelAsync, combine, contains, containsErr, err, expectErr, expectResult, filter, filterAsync, flatMap, flatMapAsync, flatten, fold, foldAsync, fromNullable, fromPromise, fromSerialized, fromThrowable, gen, isErr, isOk, isResult, map, mapAsync, mapBoth, mapErr, mapErrAsync, mapOr, mapOrElse, match, matchAsync, matchTag, ok, okIf, okIfLazy, or, orElse, partition, recover, recoverWith, sequence, sequenceRecord, swap, tap, tapAsync, task, toNullable, toPromise, tryAsync, tryCatch, tryCatchAsync, tryFn, tryMap, tryMapAsync, unwrap, unwrapErr, unwrapOr, unwrapOrDefault, unwrapOrElse, unwrapOrThrow, zip };
|
|
148
161
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","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"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.cts","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"],"mappings":";;;;;KAKK,aAAa,aAAa,aAAa,eAAe,UAAU,GAAG,GAAG,KAAK,eAAe,GAAG,GAAG;KAEhG,aAAa,KAAK,UAAU,kBAAkB,KAAK;KAEnD,QAAQ,MAAM,mBAAmB;KAEjC,cAAc,KAAK,QAAQ;KAC3B,WAAW,KAAK,cAAc,WAAW,aAAa,UAAU,IAAI,cAAc;KAClF,YAAY,KAAK,cAAc,WAAW,kBAAkB,KAAK;;;;;;;;;;;;;iBAyBhD,WAAW,SAAS,GACtC,qBAAqB,aAAa,GAAG,cACtC,QAAQ,OAAO,WAAW,IAAI,aAAa,KAAK,YAAY;iBACzC,WAAW,SAAS,GAAG,SACzC,qBAAqB,aAAa,GAAG,aACrC,SAAS,QAAQ,WAClB,QAAQ,OAAO,WAAW,IAAI,aAAa,KAAK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