ts-data-forge 14.5.0 → 14.6.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/dist/array/impl/array-utils-element-access.mjs +1 -1
- package/dist/array/impl/array-utils-reducing-value.mjs +3 -3
- package/dist/array/impl/array-utils-search.mjs +1 -1
- package/dist/collections/imap-mapped.mjs +1 -1
- package/dist/collections/imap.mjs +1 -1
- package/dist/entry-point.mjs +8 -6
- package/dist/entry-point.mjs.map +1 -1
- package/dist/functional/async-result/async-result.d.mts +16 -0
- package/dist/functional/async-result/async-result.d.mts.map +1 -0
- package/dist/functional/async-result/async-result.mjs +3 -0
- package/dist/functional/async-result/async-result.mjs.map +1 -0
- package/dist/functional/async-result/impl/async-result-flat-map.d.mts +39 -0
- package/dist/functional/async-result/impl/async-result-flat-map.d.mts.map +1 -0
- package/dist/functional/async-result/impl/async-result-flat-map.mjs +25 -0
- package/dist/functional/async-result/impl/async-result-flat-map.mjs.map +1 -0
- package/dist/functional/async-result/impl/async-result-from-promise.d.mts +47 -0
- package/dist/functional/async-result/impl/async-result-from-promise.d.mts.map +1 -0
- package/dist/functional/async-result/impl/async-result-from-promise.mjs +11 -0
- package/dist/functional/async-result/impl/async-result-from-promise.mjs.map +1 -0
- package/dist/functional/async-result/impl/async-result-from-throwable.d.mts +53 -0
- package/dist/functional/async-result/impl/async-result-from-throwable.d.mts.map +1 -0
- package/dist/functional/async-result/impl/async-result-from-throwable.mjs +17 -0
- package/dist/functional/async-result/impl/async-result-from-throwable.mjs.map +1 -0
- package/dist/functional/async-result/impl/async-result-map-err.d.mts +35 -0
- package/dist/functional/async-result/impl/async-result-map-err.d.mts.map +1 -0
- package/dist/functional/async-result/impl/async-result-map-err.mjs +23 -0
- package/dist/functional/async-result/impl/async-result-map-err.mjs.map +1 -0
- package/dist/functional/async-result/impl/async-result-map.d.mts +45 -0
- package/dist/functional/async-result/impl/async-result-map.d.mts.map +1 -0
- package/dist/functional/async-result/impl/async-result-map.mjs +23 -0
- package/dist/functional/async-result/impl/async-result-map.mjs.map +1 -0
- package/dist/functional/async-result/impl/async-result-unwrap-or.d.mts +34 -0
- package/dist/functional/async-result/impl/async-result-unwrap-or.d.mts.map +1 -0
- package/dist/functional/async-result/impl/async-result-unwrap-or.mjs +10 -0
- package/dist/functional/async-result/impl/async-result-unwrap-or.mjs.map +1 -0
- package/dist/functional/async-result/impl/index.d.mts +8 -0
- package/dist/functional/async-result/impl/index.d.mts.map +1 -0
- package/dist/functional/async-result/impl/index.mjs +7 -0
- package/dist/functional/async-result/impl/index.mjs.map +1 -0
- package/dist/functional/async-result/impl/types.d.mts +45 -0
- package/dist/functional/async-result/impl/types.d.mts.map +1 -0
- package/dist/functional/async-result/impl/types.mjs +2 -0
- package/dist/functional/async-result/impl/types.mjs.map +1 -0
- package/dist/functional/async-result/index.d.mts +2 -0
- package/dist/functional/async-result/index.d.mts.map +1 -0
- package/dist/functional/async-result/index.mjs +3 -0
- package/dist/functional/async-result/index.mjs.map +1 -0
- package/dist/functional/index.d.mts +1 -0
- package/dist/functional/index.d.mts.map +1 -1
- package/dist/functional/index.mjs +8 -6
- package/dist/functional/index.mjs.map +1 -1
- package/dist/functional/result/impl/result-from-promise.d.mts +5 -0
- package/dist/functional/result/impl/result-from-promise.d.mts.map +1 -1
- package/dist/functional/result/impl/result-from-promise.mjs.map +1 -1
- package/dist/json/json.mjs +2 -2
- package/dist/number/refined-number-utils.mjs +1 -1
- package/dist/object/object.mjs +1 -1
- package/package.json +7 -7
- package/src/functional/async-result/async-result.mts +17 -0
- package/src/functional/async-result/impl/async-result-flat-map.mts +99 -0
- package/src/functional/async-result/impl/async-result-from-promise.mts +69 -0
- package/src/functional/async-result/impl/async-result-from-throwable.mts +78 -0
- package/src/functional/async-result/impl/async-result-map-err.mts +84 -0
- package/src/functional/async-result/impl/async-result-map.mts +94 -0
- package/src/functional/async-result/impl/async-result-unwrap-or.mts +42 -0
- package/src/functional/async-result/impl/index.mts +7 -0
- package/src/functional/async-result/impl/types.mts +51 -0
- package/src/functional/async-result/index.mts +1 -0
- package/src/functional/async-result.test.mts +290 -0
- package/src/functional/index.mts +1 -0
- package/src/functional/result/impl/result-from-promise.mts +5 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { none } from '../../functional/optional/impl/optional-none.mjs';
|
|
2
2
|
import { some } from '../../functional/optional/impl/optional-some.mjs';
|
|
3
|
-
import { pipe } from '../../functional/pipe.mjs';
|
|
4
3
|
import '@sindresorhus/is';
|
|
4
|
+
import { pipe } from '../../functional/pipe.mjs';
|
|
5
5
|
|
|
6
6
|
function at(...args) {
|
|
7
7
|
switch (args.length) {
|
|
@@ -3,11 +3,11 @@ import '../../collections/imap-mapped.mjs';
|
|
|
3
3
|
import { IMap } from '../../collections/imap.mjs';
|
|
4
4
|
import '../../collections/iset-mapped.mjs';
|
|
5
5
|
import '../../collections/iset.mjs';
|
|
6
|
-
import { isString, isUndefined } from '../../guard/is-type.mjs';
|
|
7
|
-
import { none } from '../../functional/optional/impl/optional-none.mjs';
|
|
8
|
-
import { some } from '../../functional/optional/impl/optional-some.mjs';
|
|
9
6
|
import { err } from '../../functional/result/impl/result-err.mjs';
|
|
10
7
|
import { ok } from '../../functional/result/impl/result-ok.mjs';
|
|
8
|
+
import { none } from '../../functional/optional/impl/optional-none.mjs';
|
|
9
|
+
import { some } from '../../functional/optional/impl/optional-some.mjs';
|
|
10
|
+
import { isString, isUndefined } from '../../guard/is-type.mjs';
|
|
11
11
|
import { unknownToString } from '../../others/unknown-to-string.mjs';
|
|
12
12
|
import '../../number/branded-types/finite-number.mjs';
|
|
13
13
|
import '../../number/branded-types/int.mjs';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { none } from '../../functional/optional/impl/optional-none.mjs';
|
|
2
2
|
import { some } from '../../functional/optional/impl/optional-some.mjs';
|
|
3
|
-
import { pipe } from '../../functional/pipe.mjs';
|
|
4
3
|
import '@sindresorhus/is';
|
|
4
|
+
import { pipe } from '../../functional/pipe.mjs';
|
|
5
5
|
import '../../number/branded-types/finite-number.mjs';
|
|
6
6
|
import '../../number/branded-types/int.mjs';
|
|
7
7
|
import '../../number/branded-types/int16.mjs';
|
|
@@ -2,9 +2,9 @@ import { isSome } from '../functional/optional/impl/optional-is-some.mjs';
|
|
|
2
2
|
import { none } from '../functional/optional/impl/optional-none.mjs';
|
|
3
3
|
import { some } from '../functional/optional/impl/optional-some.mjs';
|
|
4
4
|
import { isNone } from '../functional/optional/impl/optional-is-none.mjs';
|
|
5
|
-
import { pipe } from '../functional/pipe.mjs';
|
|
6
5
|
import '@sindresorhus/is';
|
|
7
6
|
import { tp } from '../others/tuple.mjs';
|
|
7
|
+
import { pipe } from '../functional/pipe.mjs';
|
|
8
8
|
import '../number/branded-types/finite-number.mjs';
|
|
9
9
|
import '../number/branded-types/int.mjs';
|
|
10
10
|
import '../number/branded-types/int16.mjs';
|
|
@@ -2,10 +2,10 @@ import { isSome } from '../functional/optional/impl/optional-is-some.mjs';
|
|
|
2
2
|
import { none } from '../functional/optional/impl/optional-none.mjs';
|
|
3
3
|
import { some } from '../functional/optional/impl/optional-some.mjs';
|
|
4
4
|
import { isNone } from '../functional/optional/impl/optional-is-none.mjs';
|
|
5
|
-
import { pipe } from '../functional/pipe.mjs';
|
|
6
5
|
import '@sindresorhus/is';
|
|
7
6
|
import { tp } from '../others/tuple.mjs';
|
|
8
7
|
import { unknownToString } from '../others/unknown-to-string.mjs';
|
|
8
|
+
import { pipe } from '../functional/pipe.mjs';
|
|
9
9
|
import '../number/branded-types/finite-number.mjs';
|
|
10
10
|
import '../number/branded-types/int.mjs';
|
|
11
11
|
import '../number/branded-types/int16.mjs';
|
package/dist/entry-point.mjs
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import * as index from './array/impl/index.mjs';
|
|
2
2
|
export { index as Arr };
|
|
3
|
+
import * as index$1 from './functional/async-result/impl/index.mjs';
|
|
4
|
+
export { index$1 as AsyncResult };
|
|
3
5
|
export { match } from './functional/match.mjs';
|
|
4
|
-
import * as index$
|
|
5
|
-
export { index$
|
|
6
|
+
import * as index$2 from './functional/optional/impl/index.mjs';
|
|
7
|
+
export { index$2 as Optional };
|
|
6
8
|
export { pipe } from './functional/pipe.mjs';
|
|
7
|
-
import * as index$
|
|
8
|
-
export { index$
|
|
9
|
-
import * as index$
|
|
10
|
-
export { index$
|
|
9
|
+
import * as index$3 from './functional/result/impl/index.mjs';
|
|
10
|
+
export { index$3 as Result };
|
|
11
|
+
import * as index$4 from './functional/ternary-result/impl/index.mjs';
|
|
12
|
+
export { index$4 as TernaryResult };
|
|
11
13
|
export { IMapMapped } from './collections/imap-mapped.mjs';
|
|
12
14
|
export { IMap } from './collections/imap.mjs';
|
|
13
15
|
export { ISetMapped } from './collections/iset-mapped.mjs';
|
package/dist/entry-point.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entry-point.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"entry-point.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type Result } from '../result/index.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* Represents an asynchronous computation that can either succeed (`Ok`) or
|
|
4
|
+
* fail (`Err`) — a `Promise` that resolves to a `Result<S, E>`.
|
|
5
|
+
*
|
|
6
|
+
* Unlike a raw `Promise`, an `AsyncResult` never rejects for expected
|
|
7
|
+
* failures: errors are carried in the `Err` channel with a concrete type `E`,
|
|
8
|
+
* so they can be handled with the same combinators as a synchronous
|
|
9
|
+
* `Result`.
|
|
10
|
+
*
|
|
11
|
+
* @template S The type of the success value.
|
|
12
|
+
* @template E The type of the error value.
|
|
13
|
+
*/
|
|
14
|
+
export type AsyncResult<S, E> = Promise<Result<S, E>>;
|
|
15
|
+
export * as AsyncResult from './impl/index.mjs';
|
|
16
|
+
//# sourceMappingURL=async-result.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-result.d.mts","sourceRoot":"","sources":["../../../src/functional/async-result/async-result.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAElD;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,EAAE,CAAC,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAEtD,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-result.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type UnknownResult } from '../../result/index.mjs';
|
|
2
|
+
import { type AsyncResult } from '../async-result.mjs';
|
|
3
|
+
import { type UnknownAsyncResult, type UnwrapErr, type UnwrapOk } from './types.mjs';
|
|
4
|
+
/**
|
|
5
|
+
* Applies a function that returns an `AsyncResult` or a `Result` to the
|
|
6
|
+
* success value of an `AsyncResult`. If the input resolves to `Err`, the
|
|
7
|
+
* original `Err` is passed through and the function is not called. This is
|
|
8
|
+
* the monadic bind operation for `AsyncResult`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* const parseNumber = (input: string): Result<number, string> => {
|
|
14
|
+
* const num = Number.parseInt(input, 10);
|
|
15
|
+
*
|
|
16
|
+
* return Number.isNaN(num) ? Result.err('not a number') : Result.ok(num);
|
|
17
|
+
* };
|
|
18
|
+
*
|
|
19
|
+
* const input = AsyncResult.fromPromise(
|
|
20
|
+
* Promise.resolve('42'),
|
|
21
|
+
* () => 'failed',
|
|
22
|
+
* );
|
|
23
|
+
*
|
|
24
|
+
* const parsed = await AsyncResult.flatMap(input, parseNumber);
|
|
25
|
+
*
|
|
26
|
+
* assert.deepStrictEqual(parsed, Result.ok(42));
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @template S The type of the success value of the input.
|
|
30
|
+
* @template E The type of the error value of the input.
|
|
31
|
+
* @template R2 The `AsyncResult` or `Result` type returned by the function.
|
|
32
|
+
* @param asyncResult The `AsyncResult` to flat map.
|
|
33
|
+
* @param flatMapFn The function to apply that returns an `AsyncResult` or a
|
|
34
|
+
* `Result`.
|
|
35
|
+
* @returns The result of applying the function, or the original `Err`.
|
|
36
|
+
*/
|
|
37
|
+
export declare function flatMap<S, E, R2 extends UnknownAsyncResult | UnknownResult>(asyncResult: AsyncResult<S, E>, flatMapFn: (value: S) => R2): AsyncResult<UnwrapOk<R2>, E | UnwrapErr<R2>>;
|
|
38
|
+
export declare function flatMap<S, R2 extends UnknownAsyncResult | UnknownResult>(flatMapFn: (value: S) => R2): <E>(asyncResult: AsyncResult<S, E>) => AsyncResult<UnwrapOk<R2>, E | UnwrapErr<R2>>;
|
|
39
|
+
//# sourceMappingURL=async-result-flat-map.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-result-flat-map.d.mts","sourceRoot":"","sources":["../../../../src/functional/async-result/impl/async-result-flat-map.mts"],"names":[],"mappings":"AAAA,OAAO,EAAU,KAAK,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACpE,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EACL,KAAK,kBAAkB,EACvB,KAAK,SAAS,EACd,KAAK,QAAQ,EACd,MAAM,aAAa,CAAC;AAErB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,SAAS,kBAAkB,GAAG,aAAa,EACzE,WAAW,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,EAC9B,SAAS,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,GAC1B,WAAW,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC;AAWhD,wBAAgB,OAAO,CAAC,CAAC,EAAE,EAAE,SAAS,kBAAkB,GAAG,aAAa,EACtE,SAAS,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,GAC1B,CAAC,CAAC,EACH,WAAW,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,KAC3B,WAAW,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { isErr } from '../../result/impl/result-is-err.mjs';
|
|
2
|
+
import '@sindresorhus/is';
|
|
3
|
+
|
|
4
|
+
function flatMap(...args) {
|
|
5
|
+
switch (args.length) {
|
|
6
|
+
case 2: {
|
|
7
|
+
const [asyncResult, flatMapFn] = args;
|
|
8
|
+
return flatMapImpl(asyncResult, flatMapFn);
|
|
9
|
+
}
|
|
10
|
+
case 1: {
|
|
11
|
+
const [flatMapFn] = args;
|
|
12
|
+
return (asyncResult) => flatMapImpl(asyncResult, flatMapFn);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const flatMapImpl = async (asyncResult, flatMapFn) => {
|
|
17
|
+
const result = await asyncResult;
|
|
18
|
+
return isErr(result) ? result : (
|
|
19
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
20
|
+
await flatMapFn(result.value)
|
|
21
|
+
);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export { flatMap };
|
|
25
|
+
//# sourceMappingURL=async-result-flat-map.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-result-flat-map.mjs","sources":["../../../../src/functional/async-result/impl/async-result-flat-map.mts"],"sourcesContent":["import { Result, type UnknownResult } from '../../result/index.mjs';\nimport { type AsyncResult } from '../async-result.mjs';\nimport {\n type UnknownAsyncResult,\n type UnwrapErr,\n type UnwrapOk,\n} from './types.mjs';\n\n/**\n * Applies a function that returns an `AsyncResult` or a `Result` to the\n * success value of an `AsyncResult`. If the input resolves to `Err`, the\n * original `Err` is passed through and the function is not called. This is\n * the monadic bind operation for `AsyncResult`.\n *\n * @example\n *\n * ```ts\n * const parseNumber = (input: string): Result<number, string> => {\n * const num = Number.parseInt(input, 10);\n *\n * return Number.isNaN(num) ? Result.err('not a number') : Result.ok(num);\n * };\n *\n * const input = AsyncResult.fromPromise(\n * Promise.resolve('42'),\n * () => 'failed',\n * );\n *\n * const parsed = await AsyncResult.flatMap(input, parseNumber);\n *\n * assert.deepStrictEqual(parsed, Result.ok(42));\n * ```\n *\n * @template S The type of the success value of the input.\n * @template E The type of the error value of the input.\n * @template R2 The `AsyncResult` or `Result` type returned by the function.\n * @param asyncResult The `AsyncResult` to flat map.\n * @param flatMapFn The function to apply that returns an `AsyncResult` or a\n * `Result`.\n * @returns The result of applying the function, or the original `Err`.\n */\nexport function flatMap<S, E, R2 extends UnknownAsyncResult | UnknownResult>(\n asyncResult: AsyncResult<S, E>,\n flatMapFn: (value: S) => R2,\n): AsyncResult<UnwrapOk<R2>, E | UnwrapErr<R2>>;\n\n// Curried version\n//\n// The function's return type is captured as a whole (`R2`) and unwrapped with\n// `UnwrapOk` / `UnwrapErr`, instead of being written as\n// `AsyncResult<S2, E2> | Result<S2, E2>` with separate `S2` / `E2` type\n// parameters. With the union form, inference for `E2` collects a candidate\n// from matching `Ok<S2>` against `Err<E2>` (both have a `value` property), so\n// `E2` ends up as the success type and the call fails to type-check even for\n// a function annotated as returning `Result<S2, E2>`.\nexport function flatMap<S, R2 extends UnknownAsyncResult | UnknownResult>(\n flatMapFn: (value: S) => R2,\n): <E>(\n asyncResult: AsyncResult<S, E>,\n) => AsyncResult<UnwrapOk<R2>, E | UnwrapErr<R2>>;\n\nexport function flatMap<S, E, R2 extends UnknownAsyncResult | UnknownResult>(\n ...args:\n | readonly [asyncResult: AsyncResult<S, E>, flatMapFn: (value: S) => R2]\n | readonly [flatMapFn: (value: S) => R2]\n):\n | AsyncResult<UnwrapOk<R2>, E | UnwrapErr<R2>>\n | ((\n asyncResult: AsyncResult<S, E>,\n ) => AsyncResult<UnwrapOk<R2>, E | UnwrapErr<R2>>) {\n switch (args.length) {\n case 2: {\n // Direct version: first argument is asyncResult\n const [asyncResult, flatMapFn] = args;\n\n return flatMapImpl(asyncResult, flatMapFn);\n }\n\n case 1: {\n // Curried version\n const [flatMapFn] = args;\n\n return (asyncResult: AsyncResult<S, E>) =>\n flatMapImpl(asyncResult, flatMapFn);\n }\n }\n}\n\nconst flatMapImpl = async <S, E, R2 extends UnknownAsyncResult | UnknownResult>(\n asyncResult: AsyncResult<S, E>,\n flatMapFn: (value: S) => R2,\n): AsyncResult<UnwrapOk<R2>, E | UnwrapErr<R2>> => {\n const result = await asyncResult;\n\n return Result.isErr(result)\n ? result\n : // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n ((await flatMapFn(result.value)) as Result<UnwrapOk<R2>, UnwrapErr<R2>>);\n};\n"],"names":["Result.isErr"],"mappings":";;;AA6DO,SAAS,WACX,IAAA,EAOkD;AACrD,EAAA,QAAQ,KAAK,MAAA;AAAQ,IACnB,KAAK,CAAA,EAAG;AAEN,MAAA,MAAM,CAAC,WAAA,EAAa,SAAS,CAAA,GAAI,IAAA;AAEjC,MAAA,OAAO,WAAA,CAAY,aAAa,SAAS,CAAA;AAAA,IAC3C;AAAA,IAEA,KAAK,CAAA,EAAG;AAEN,MAAA,MAAM,CAAC,SAAS,CAAA,GAAI,IAAA;AAEpB,MAAA,OAAO,CAAC,WAAA,KACN,WAAA,CAAY,WAAA,EAAa,SAAS,CAAA;AAAA,IACtC;AAAA;AAEJ;AAEA,MAAM,WAAA,GAAc,OAClB,WAAA,EACA,SAAA,KACiD;AACjD,EAAA,MAAM,SAAS,MAAM,WAAA;AAErB,EAAA,OAAOA,KAAO,CAAM,MAAM,CAAA,GACtB,MAAA;AAAA;AAAA,IAEE,MAAM,SAAA,CAAU,MAAA,CAAO,KAAK;AAAA,GAAA;AACpC,CAAA;;;;"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type AsyncResult } from '../async-result.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* Converts a `Promise` into an `AsyncResult`. If the input Promise resolves,
|
|
4
|
+
* the `AsyncResult` resolves to `Ok` with the resolved value. If the input
|
|
5
|
+
* Promise rejects, the rejection reason is passed through `mapError` and the
|
|
6
|
+
* `AsyncResult` resolves to `Err` with the mapped error.
|
|
7
|
+
*
|
|
8
|
+
* Passing `mapError` is what gives the error channel a concrete type `E`.
|
|
9
|
+
* Omitting it carries the rejection reason through untouched, leaving the
|
|
10
|
+
* error channel as `unknown` — the same contract as `Result.fromPromise`.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* const resolved = await AsyncResult.fromPromise(
|
|
16
|
+
* Promise.resolve('ok'),
|
|
17
|
+
* (error) => `failed: ${String(error)}`,
|
|
18
|
+
* );
|
|
19
|
+
*
|
|
20
|
+
* const rejected = await AsyncResult.fromPromise(
|
|
21
|
+
* Promise.reject(new Error('boom')),
|
|
22
|
+
* (error) => (isError(error) ? error.message : 'unknown'),
|
|
23
|
+
* );
|
|
24
|
+
*
|
|
25
|
+
* assert.deepStrictEqual(resolved, Result.ok('ok'));
|
|
26
|
+
*
|
|
27
|
+
* assert.deepStrictEqual(rejected, Result.err('boom'));
|
|
28
|
+
*
|
|
29
|
+
* // Without `mapError`, the rejection reason is carried as-is.
|
|
30
|
+
* const untyped = await AsyncResult.fromPromise(
|
|
31
|
+
* Promise.reject(new Error('boom')),
|
|
32
|
+
* );
|
|
33
|
+
*
|
|
34
|
+
* assert.isTrue(Result.isErr(untyped));
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @template S The type of the success value.
|
|
38
|
+
* @template E The type of the error value.
|
|
39
|
+
* @param promise The Promise to convert.
|
|
40
|
+
* @param mapError The function applied to the rejection reason to produce the
|
|
41
|
+
* error value. Omit it to carry the rejection reason as `unknown`.
|
|
42
|
+
* @returns An `AsyncResult<S, E>` that never rejects for the input Promise's
|
|
43
|
+
* failure.
|
|
44
|
+
*/
|
|
45
|
+
export declare function fromPromise<S>(promise: Promise<S>): AsyncResult<S, unknown>;
|
|
46
|
+
export declare function fromPromise<S, E>(promise: Promise<S>, mapError: (error: unknown) => E): AsyncResult<S, E>;
|
|
47
|
+
//# sourceMappingURL=async-result-from-promise.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-result-from-promise.d.mts","sourceRoot":"","sources":["../../../../src/functional/async-result/impl/async-result-from-promise.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAEvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;AAE7E,wBAAgB,WAAW,CAAC,CAAC,EAAE,CAAC,EAC9B,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EACnB,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,CAAC,GAC9B,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { err } from '../../result/impl/result-err.mjs';
|
|
2
|
+
import { ok } from '../../result/impl/result-ok.mjs';
|
|
3
|
+
import '@sindresorhus/is';
|
|
4
|
+
|
|
5
|
+
function fromPromise(promise, mapError) {
|
|
6
|
+
return mapError === void 0 ? fromPromiseImpl(promise, (error) => error) : fromPromiseImpl(promise, mapError);
|
|
7
|
+
}
|
|
8
|
+
const fromPromiseImpl = (promise, mapError) => promise.then((value) => ok(value)).catch((error) => err(mapError(error)));
|
|
9
|
+
|
|
10
|
+
export { fromPromise };
|
|
11
|
+
//# sourceMappingURL=async-result-from-promise.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-result-from-promise.mjs","sources":["../../../../src/functional/async-result/impl/async-result-from-promise.mts"],"sourcesContent":["import { Result } from '../../result/index.mjs';\nimport { type AsyncResult } from '../async-result.mjs';\n\n/**\n * Converts a `Promise` into an `AsyncResult`. If the input Promise resolves,\n * the `AsyncResult` resolves to `Ok` with the resolved value. If the input\n * Promise rejects, the rejection reason is passed through `mapError` and the\n * `AsyncResult` resolves to `Err` with the mapped error.\n *\n * Passing `mapError` is what gives the error channel a concrete type `E`.\n * Omitting it carries the rejection reason through untouched, leaving the\n * error channel as `unknown` — the same contract as `Result.fromPromise`.\n *\n * @example\n *\n * ```ts\n * const resolved = await AsyncResult.fromPromise(\n * Promise.resolve('ok'),\n * (error) => `failed: ${String(error)}`,\n * );\n *\n * const rejected = await AsyncResult.fromPromise(\n * Promise.reject(new Error('boom')),\n * (error) => (isError(error) ? error.message : 'unknown'),\n * );\n *\n * assert.deepStrictEqual(resolved, Result.ok('ok'));\n *\n * assert.deepStrictEqual(rejected, Result.err('boom'));\n *\n * // Without `mapError`, the rejection reason is carried as-is.\n * const untyped = await AsyncResult.fromPromise(\n * Promise.reject(new Error('boom')),\n * );\n *\n * assert.isTrue(Result.isErr(untyped));\n * ```\n *\n * @template S The type of the success value.\n * @template E The type of the error value.\n * @param promise The Promise to convert.\n * @param mapError The function applied to the rejection reason to produce the\n * error value. Omit it to carry the rejection reason as `unknown`.\n * @returns An `AsyncResult<S, E>` that never rejects for the input Promise's\n * failure.\n */\nexport function fromPromise<S>(promise: Promise<S>): AsyncResult<S, unknown>;\n\nexport function fromPromise<S, E>(\n promise: Promise<S>,\n mapError: (error: unknown) => E,\n): AsyncResult<S, E>;\n\nexport function fromPromise<S, E>(\n promise: Promise<S>,\n mapError?: (error: unknown) => E,\n): AsyncResult<S, E> | AsyncResult<S, unknown> {\n return mapError === undefined\n ? fromPromiseImpl(promise, (error: unknown) => error)\n : fromPromiseImpl(promise, mapError);\n}\n\nconst fromPromiseImpl = <S, E>(\n promise: Promise<S>,\n mapError: (error: unknown) => E,\n): AsyncResult<S, E> =>\n promise\n .then((value) => Result.ok(value))\n .catch((error: unknown) => Result.err(mapError(error)));\n"],"names":["Result.ok","Result.err"],"mappings":";;;;AAqDO,SAAS,WAAA,CACd,SACA,QAAA,EAC6C;AAC7C,EAAA,OAAO,QAAA,KAAa,MAAA,GAChB,eAAA,CAAgB,OAAA,EAAS,CAAC,UAAmB,KAAK,CAAA,GAClD,eAAA,CAAgB,OAAA,EAAS,QAAQ,CAAA;AACvC;AAEA,MAAM,eAAA,GAAkB,CACtB,OAAA,EACA,QAAA,KAEA,QACG,IAAA,CAAK,CAAC,UAAUA,EAAO,CAAG,KAAK,CAAC,CAAA,CAChC,MAAM,CAAC,KAAA,KAAmBC,GAAO,CAAI,QAAA,CAAS,KAAK,CAAC,CAAC,CAAA;;;;"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type AsyncResult } from '../async-result.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* Wraps a function returning a `Promise` in an `AsyncResult`. Both a
|
|
4
|
+
* synchronous `throw` while calling `fn` and an asynchronous rejection of the
|
|
5
|
+
* returned Promise are caught, passed through `mapError`, and carried as
|
|
6
|
+
* `Err`.
|
|
7
|
+
*
|
|
8
|
+
* Passing `mapError` is what gives the error channel a concrete type `E`.
|
|
9
|
+
* Omitting it carries the thrown value through untouched, leaving the error
|
|
10
|
+
* channel as `unknown`.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* const success = await AsyncResult.fromThrowable(
|
|
16
|
+
* () => Promise.resolve(2),
|
|
17
|
+
* (error) => `failed: ${String(error)}`,
|
|
18
|
+
* );
|
|
19
|
+
*
|
|
20
|
+
* const failure = await AsyncResult.fromThrowable(
|
|
21
|
+
* (): Promise<number> => {
|
|
22
|
+
* throw new Error('boom');
|
|
23
|
+
* },
|
|
24
|
+
* (error) => (isError(error) ? error.message : 'unknown'),
|
|
25
|
+
* );
|
|
26
|
+
*
|
|
27
|
+
* assert.deepStrictEqual(success, Result.ok(2));
|
|
28
|
+
*
|
|
29
|
+
* assert.deepStrictEqual(failure, Result.err('boom'));
|
|
30
|
+
*
|
|
31
|
+
* // Without `mapError`, the thrown value is carried as-is.
|
|
32
|
+
* const untyped = await AsyncResult.fromThrowable(
|
|
33
|
+
* (): Promise<number> => {
|
|
34
|
+
* throw new Error('boom');
|
|
35
|
+
* },
|
|
36
|
+
* );
|
|
37
|
+
*
|
|
38
|
+
* assert.isTrue(Result.isErr(untyped));
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @template S The type of the success value.
|
|
42
|
+
* @template E The type of the error value.
|
|
43
|
+
* @param fn The function to execute; it may throw synchronously or return a
|
|
44
|
+
* rejecting Promise.
|
|
45
|
+
* @param mapError The function applied to the thrown value or rejection
|
|
46
|
+
* reason to produce the error value. Omit it to carry the thrown value as
|
|
47
|
+
* `unknown`.
|
|
48
|
+
* @returns An `AsyncResult<S, E>` containing either the resolved value or the
|
|
49
|
+
* mapped error.
|
|
50
|
+
*/
|
|
51
|
+
export declare function fromThrowable<S>(fn: () => Promise<S>): AsyncResult<S, unknown>;
|
|
52
|
+
export declare function fromThrowable<S, E>(fn: () => Promise<S>, mapError: (error: unknown) => E): AsyncResult<S, E>;
|
|
53
|
+
//# sourceMappingURL=async-result-from-throwable.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-result-from-throwable.d.mts","sourceRoot":"","sources":["../../../../src/functional/async-result/impl/async-result-from-throwable.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAEvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;AAEhF,wBAAgB,aAAa,CAAC,CAAC,EAAE,CAAC,EAChC,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,CAAC,GAC9B,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { err } from '../../result/impl/result-err.mjs';
|
|
2
|
+
import { ok } from '../../result/impl/result-ok.mjs';
|
|
3
|
+
import '@sindresorhus/is';
|
|
4
|
+
|
|
5
|
+
function fromThrowable(fn, mapError) {
|
|
6
|
+
return mapError === void 0 ? fromThrowableImpl(fn, (error) => error) : fromThrowableImpl(fn, mapError);
|
|
7
|
+
}
|
|
8
|
+
const fromThrowableImpl = async (fn, mapError) => {
|
|
9
|
+
try {
|
|
10
|
+
return ok(await fn());
|
|
11
|
+
} catch (error) {
|
|
12
|
+
return err(mapError(error));
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export { fromThrowable };
|
|
17
|
+
//# sourceMappingURL=async-result-from-throwable.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-result-from-throwable.mjs","sources":["../../../../src/functional/async-result/impl/async-result-from-throwable.mts"],"sourcesContent":["import { Result } from '../../result/index.mjs';\nimport { type AsyncResult } from '../async-result.mjs';\n\n/**\n * Wraps a function returning a `Promise` in an `AsyncResult`. Both a\n * synchronous `throw` while calling `fn` and an asynchronous rejection of the\n * returned Promise are caught, passed through `mapError`, and carried as\n * `Err`.\n *\n * Passing `mapError` is what gives the error channel a concrete type `E`.\n * Omitting it carries the thrown value through untouched, leaving the error\n * channel as `unknown`.\n *\n * @example\n *\n * ```ts\n * const success = await AsyncResult.fromThrowable(\n * () => Promise.resolve(2),\n * (error) => `failed: ${String(error)}`,\n * );\n *\n * const failure = await AsyncResult.fromThrowable(\n * (): Promise<number> => {\n * throw new Error('boom');\n * },\n * (error) => (isError(error) ? error.message : 'unknown'),\n * );\n *\n * assert.deepStrictEqual(success, Result.ok(2));\n *\n * assert.deepStrictEqual(failure, Result.err('boom'));\n *\n * // Without `mapError`, the thrown value is carried as-is.\n * const untyped = await AsyncResult.fromThrowable(\n * (): Promise<number> => {\n * throw new Error('boom');\n * },\n * );\n *\n * assert.isTrue(Result.isErr(untyped));\n * ```\n *\n * @template S The type of the success value.\n * @template E The type of the error value.\n * @param fn The function to execute; it may throw synchronously or return a\n * rejecting Promise.\n * @param mapError The function applied to the thrown value or rejection\n * reason to produce the error value. Omit it to carry the thrown value as\n * `unknown`.\n * @returns An `AsyncResult<S, E>` containing either the resolved value or the\n * mapped error.\n */\nexport function fromThrowable<S>(fn: () => Promise<S>): AsyncResult<S, unknown>;\n\nexport function fromThrowable<S, E>(\n fn: () => Promise<S>,\n mapError: (error: unknown) => E,\n): AsyncResult<S, E>;\n\nexport function fromThrowable<S, E>(\n fn: () => Promise<S>,\n mapError?: (error: unknown) => E,\n): AsyncResult<S, E> | AsyncResult<S, unknown> {\n return mapError === undefined\n ? fromThrowableImpl(fn, (error: unknown) => error)\n : fromThrowableImpl(fn, mapError);\n}\n\nconst fromThrowableImpl = async <S, E>(\n fn: () => Promise<S>,\n mapError: (error: unknown) => E,\n): AsyncResult<S, E> => {\n try {\n return Result.ok(await fn());\n } catch (error) {\n return Result.err(mapError(error));\n }\n};\n"],"names":["Result.ok","Result.err"],"mappings":";;;;AA2DO,SAAS,aAAA,CACd,IACA,QAAA,EAC6C;AAC7C,EAAA,OAAO,QAAA,KAAa,MAAA,GAChB,iBAAA,CAAkB,EAAA,EAAI,CAAC,UAAmB,KAAK,CAAA,GAC/C,iBAAA,CAAkB,EAAA,EAAI,QAAQ,CAAA;AACpC;AAEA,MAAM,iBAAA,GAAoB,OACxB,EAAA,EACA,QAAA,KACsB;AACtB,EAAA,IAAI;AACF,IAAA,OAAOA,EAAO,CAAG,MAAM,EAAA,EAAI,CAAA;AAAA,EAC7B,SAAS,KAAA,EAAO;AACd,IAAA,OAAOC,GAAO,CAAI,QAAA,CAAS,KAAK,CAAC,CAAA;AAAA,EACnC;AACF,CAAA;;;;"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type AsyncResult } from '../async-result.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* Maps an `AsyncResult<S, E>` to `AsyncResult<S, E2>` by applying a function
|
|
4
|
+
* to the error value. The mapping function may be synchronous or return a
|
|
5
|
+
* `Promise`; both are handled transparently. If the `AsyncResult` resolves to
|
|
6
|
+
* `Ok`, the original `Ok` is passed through and the function is not called.
|
|
7
|
+
*
|
|
8
|
+
* If `mapFn` throws or returns a rejecting Promise, the rejection propagates
|
|
9
|
+
* as-is — it is **not** caught and converted into an `Err`.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* const errValue = AsyncResult.fromPromise(
|
|
15
|
+
* Promise.reject(new Error('boom')),
|
|
16
|
+
* () => 'failure',
|
|
17
|
+
* );
|
|
18
|
+
*
|
|
19
|
+
* const uppercased = await AsyncResult.mapErr(errValue, (error) =>
|
|
20
|
+
* error.toUpperCase(),
|
|
21
|
+
* );
|
|
22
|
+
*
|
|
23
|
+
* assert.deepStrictEqual(uppercased, Result.err('FAILURE'));
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @template S The type of the success value.
|
|
27
|
+
* @template E The type of the error value of the input.
|
|
28
|
+
* @template E2 The type of the error value returned by the mapping function.
|
|
29
|
+
* @param asyncResult The `AsyncResult` to map.
|
|
30
|
+
* @param mapFn The function to apply to the error value if present.
|
|
31
|
+
* @returns A new `AsyncResult<S, E2>`.
|
|
32
|
+
*/
|
|
33
|
+
export declare function mapErr<S, E, E2>(asyncResult: AsyncResult<S, E>, mapFn: (error: E) => E2 | Promise<E2>): AsyncResult<S, E2>;
|
|
34
|
+
export declare function mapErr<E, E2>(mapFn: (error: E) => E2 | Promise<E2>): <S>(asyncResult: AsyncResult<S, E>) => AsyncResult<S, E2>;
|
|
35
|
+
//# sourceMappingURL=async-result-map-err.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-result-map-err.d.mts","sourceRoot":"","sources":["../../../../src/functional/async-result/impl/async-result-map-err.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAEvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,EAC7B,WAAW,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,EAC9B,KAAK,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,GAAG,OAAO,CAAC,EAAE,CAAC,GACpC,WAAW,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AAQtB,wBAAgB,MAAM,CAAC,CAAC,EAAE,EAAE,EAC1B,KAAK,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,GAAG,OAAO,CAAC,EAAE,CAAC,GACpC,CAAC,CAAC,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,WAAW,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { err } from '../../result/impl/result-err.mjs';
|
|
2
|
+
import { isOk } from '../../result/impl/result-is-ok.mjs';
|
|
3
|
+
import '@sindresorhus/is';
|
|
4
|
+
|
|
5
|
+
function mapErr(...args) {
|
|
6
|
+
switch (args.length) {
|
|
7
|
+
case 2: {
|
|
8
|
+
const [asyncResult, mapFn] = args;
|
|
9
|
+
return mapErrImpl(asyncResult, mapFn);
|
|
10
|
+
}
|
|
11
|
+
case 1: {
|
|
12
|
+
const [mapFn] = args;
|
|
13
|
+
return (asyncResult) => mapErrImpl(asyncResult, mapFn);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const mapErrImpl = async (asyncResult, mapFn) => {
|
|
18
|
+
const result = await asyncResult;
|
|
19
|
+
return isOk(result) ? result : err(await mapFn(result.value));
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export { mapErr };
|
|
23
|
+
//# sourceMappingURL=async-result-map-err.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-result-map-err.mjs","sources":["../../../../src/functional/async-result/impl/async-result-map-err.mts"],"sourcesContent":["import { Result } from '../../result/index.mjs';\nimport { type AsyncResult } from '../async-result.mjs';\n\n/**\n * Maps an `AsyncResult<S, E>` to `AsyncResult<S, E2>` by applying a function\n * to the error value. The mapping function may be synchronous or return a\n * `Promise`; both are handled transparently. If the `AsyncResult` resolves to\n * `Ok`, the original `Ok` is passed through and the function is not called.\n *\n * If `mapFn` throws or returns a rejecting Promise, the rejection propagates\n * as-is — it is **not** caught and converted into an `Err`.\n *\n * @example\n *\n * ```ts\n * const errValue = AsyncResult.fromPromise(\n * Promise.reject(new Error('boom')),\n * () => 'failure',\n * );\n *\n * const uppercased = await AsyncResult.mapErr(errValue, (error) =>\n * error.toUpperCase(),\n * );\n *\n * assert.deepStrictEqual(uppercased, Result.err('FAILURE'));\n * ```\n *\n * @template S The type of the success value.\n * @template E The type of the error value of the input.\n * @template E2 The type of the error value returned by the mapping function.\n * @param asyncResult The `AsyncResult` to map.\n * @param mapFn The function to apply to the error value if present.\n * @returns A new `AsyncResult<S, E2>`.\n */\nexport function mapErr<S, E, E2>(\n asyncResult: AsyncResult<S, E>,\n mapFn: (error: E) => E2 | Promise<E2>,\n): AsyncResult<S, E2>;\n\n// Curried version\n//\n// Unlike `Result.mapErr`, the returned function can simply be generic over\n// `S`: an `AsyncResult` is a `Promise`, not a discriminated union, so there\n// is no literal-narrowing problem that would require the whole-result\n// generic trick used by the synchronous combinators.\nexport function mapErr<E, E2>(\n mapFn: (error: E) => E2 | Promise<E2>,\n): <S>(asyncResult: AsyncResult<S, E>) => AsyncResult<S, E2>;\n\nexport function mapErr<S, E, E2>(\n ...args:\n | readonly [\n asyncResult: AsyncResult<S, E>,\n mapFn: (error: E) => E2 | Promise<E2>,\n ]\n | readonly [mapFn: (error: E) => E2 | Promise<E2>]\n):\n | AsyncResult<S, E2>\n | ((asyncResult: AsyncResult<S, E>) => AsyncResult<S, E2>) {\n switch (args.length) {\n case 2: {\n // Direct version: first argument is asyncResult\n const [asyncResult, mapFn] = args;\n\n return mapErrImpl(asyncResult, mapFn);\n }\n\n case 1: {\n // Curried version: first argument is mapping function\n const [mapFn] = args;\n\n return (asyncResult: AsyncResult<S, E>) => mapErrImpl(asyncResult, mapFn);\n }\n }\n}\n\nconst mapErrImpl = async <S, E, E2>(\n asyncResult: AsyncResult<S, E>,\n mapFn: (error: E) => E2 | Promise<E2>,\n): AsyncResult<S, E2> => {\n const result = await asyncResult;\n\n return Result.isOk(result) ? result : Result.err(await mapFn(result.value));\n};\n"],"names":["Result.isOk","Result.err"],"mappings":";;;;AAiDO,SAAS,UACX,IAAA,EAQwD;AAC3D,EAAA,QAAQ,KAAK,MAAA;AAAQ,IACnB,KAAK,CAAA,EAAG;AAEN,MAAA,MAAM,CAAC,WAAA,EAAa,KAAK,CAAA,GAAI,IAAA;AAE7B,MAAA,OAAO,UAAA,CAAW,aAAa,KAAK,CAAA;AAAA,IACtC;AAAA,IAEA,KAAK,CAAA,EAAG;AAEN,MAAA,MAAM,CAAC,KAAK,CAAA,GAAI,IAAA;AAEhB,MAAA,OAAO,CAAC,WAAA,KAAmC,UAAA,CAAW,WAAA,EAAa,KAAK,CAAA;AAAA,IAC1E;AAAA;AAEJ;AAEA,MAAM,UAAA,GAAa,OACjB,WAAA,EACA,KAAA,KACuB;AACvB,EAAA,MAAM,SAAS,MAAM,WAAA;AAErB,EAAA,OAAOA,IAAO,CAAK,MAAM,CAAA,GAAI,MAAA,GAASC,GAAO,CAAI,MAAM,KAAA,CAAM,MAAA,CAAO,KAAK,CAAC,CAAA;AAC5E,CAAA;;;;"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type AsyncResult } from '../async-result.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* Maps an `AsyncResult<S, E>` to `AsyncResult<S2, E>` by applying a function
|
|
4
|
+
* to the success value. The mapping function may be synchronous or return a
|
|
5
|
+
* `Promise`; both are handled transparently. If the `AsyncResult` resolves to
|
|
6
|
+
* `Err`, the original `Err` is passed through and the function is not called.
|
|
7
|
+
*
|
|
8
|
+
* If `mapFn` throws or returns a rejecting Promise, the rejection propagates
|
|
9
|
+
* as-is — it is **not** caught and converted into an `Err`. `map` performs no
|
|
10
|
+
* implicit error conversion; write a fallible step as
|
|
11
|
+
* `AsyncResult.flatMap` combined with `AsyncResult.fromThrowable` instead, so
|
|
12
|
+
* that the error type stays explicit.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* const okValue = AsyncResult.fromPromise(
|
|
18
|
+
* Promise.resolve(5),
|
|
19
|
+
* () => 'failed',
|
|
20
|
+
* );
|
|
21
|
+
*
|
|
22
|
+
* const doubled = await AsyncResult.map(okValue, (value) => value * 2);
|
|
23
|
+
*
|
|
24
|
+
* assert.deepStrictEqual(doubled, Result.ok(10));
|
|
25
|
+
*
|
|
26
|
+
* const double = AsyncResult.map((value: number) => value * 2);
|
|
27
|
+
*
|
|
28
|
+
* const curried = await double(
|
|
29
|
+
* AsyncResult.fromPromise(Promise.resolve(21), () => 'failed'),
|
|
30
|
+
* );
|
|
31
|
+
*
|
|
32
|
+
* assert.deepStrictEqual(curried, Result.ok(42));
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @template S The type of the success value of the input.
|
|
36
|
+
* @template S2 The type of the success value returned by the mapping
|
|
37
|
+
* function.
|
|
38
|
+
* @template E The type of the error value.
|
|
39
|
+
* @param asyncResult The `AsyncResult` to map.
|
|
40
|
+
* @param mapFn The function to apply to the success value if present.
|
|
41
|
+
* @returns A new `AsyncResult<S2, E>`.
|
|
42
|
+
*/
|
|
43
|
+
export declare function map<S, S2, E>(asyncResult: AsyncResult<S, E>, mapFn: (value: S) => Promise<S2> | S2): AsyncResult<S2, E>;
|
|
44
|
+
export declare function map<S, S2>(mapFn: (value: S) => Promise<S2> | S2): <E>(asyncResult: AsyncResult<S, E>) => AsyncResult<S2, E>;
|
|
45
|
+
//# sourceMappingURL=async-result-map.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-result-map.d.mts","sourceRoot":"","sources":["../../../../src/functional/async-result/impl/async-result-map.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAEvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,wBAAgB,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAC1B,WAAW,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,EAC9B,KAAK,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,GACpC,WAAW,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;AAQtB,wBAAgB,GAAG,CAAC,CAAC,EAAE,EAAE,EACvB,KAAK,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,GACpC,CAAC,CAAC,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,WAAW,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { isErr } from '../../result/impl/result-is-err.mjs';
|
|
2
|
+
import { ok } from '../../result/impl/result-ok.mjs';
|
|
3
|
+
import '@sindresorhus/is';
|
|
4
|
+
|
|
5
|
+
function map(...args) {
|
|
6
|
+
switch (args.length) {
|
|
7
|
+
case 2: {
|
|
8
|
+
const [asyncResult, mapFn] = args;
|
|
9
|
+
return mapImpl(asyncResult, mapFn);
|
|
10
|
+
}
|
|
11
|
+
case 1: {
|
|
12
|
+
const [mapFn] = args;
|
|
13
|
+
return (asyncResult) => mapImpl(asyncResult, mapFn);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const mapImpl = async (asyncResult, mapFn) => {
|
|
18
|
+
const result = await asyncResult;
|
|
19
|
+
return isErr(result) ? result : ok(await mapFn(result.value));
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export { map };
|
|
23
|
+
//# sourceMappingURL=async-result-map.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-result-map.mjs","sources":["../../../../src/functional/async-result/impl/async-result-map.mts"],"sourcesContent":["import { Result } from '../../result/index.mjs';\nimport { type AsyncResult } from '../async-result.mjs';\n\n/**\n * Maps an `AsyncResult<S, E>` to `AsyncResult<S2, E>` by applying a function\n * to the success value. The mapping function may be synchronous or return a\n * `Promise`; both are handled transparently. If the `AsyncResult` resolves to\n * `Err`, the original `Err` is passed through and the function is not called.\n *\n * If `mapFn` throws or returns a rejecting Promise, the rejection propagates\n * as-is — it is **not** caught and converted into an `Err`. `map` performs no\n * implicit error conversion; write a fallible step as\n * `AsyncResult.flatMap` combined with `AsyncResult.fromThrowable` instead, so\n * that the error type stays explicit.\n *\n * @example\n *\n * ```ts\n * const okValue = AsyncResult.fromPromise(\n * Promise.resolve(5),\n * () => 'failed',\n * );\n *\n * const doubled = await AsyncResult.map(okValue, (value) => value * 2);\n *\n * assert.deepStrictEqual(doubled, Result.ok(10));\n *\n * const double = AsyncResult.map((value: number) => value * 2);\n *\n * const curried = await double(\n * AsyncResult.fromPromise(Promise.resolve(21), () => 'failed'),\n * );\n *\n * assert.deepStrictEqual(curried, Result.ok(42));\n * ```\n *\n * @template S The type of the success value of the input.\n * @template S2 The type of the success value returned by the mapping\n * function.\n * @template E The type of the error value.\n * @param asyncResult The `AsyncResult` to map.\n * @param mapFn The function to apply to the success value if present.\n * @returns A new `AsyncResult<S2, E>`.\n */\nexport function map<S, S2, E>(\n asyncResult: AsyncResult<S, E>,\n mapFn: (value: S) => Promise<S2> | S2,\n): AsyncResult<S2, E>;\n\n// Curried version\n//\n// Unlike `Result.map`, the returned function can simply be generic over `E`:\n// an `AsyncResult` is a `Promise`, not a discriminated union, so there is no\n// literal-narrowing problem that would require the whole-result generic\n// trick used by the synchronous combinators.\nexport function map<S, S2>(\n mapFn: (value: S) => Promise<S2> | S2,\n): <E>(asyncResult: AsyncResult<S, E>) => AsyncResult<S2, E>;\n\nexport function map<S, S2, E>(\n ...args:\n | readonly [\n asyncResult: AsyncResult<S, E>,\n mapFn: (value: S) => Promise<S2> | S2,\n ]\n | readonly [mapFn: (value: S) => Promise<S2> | S2]\n):\n | AsyncResult<S2, E>\n | ((asyncResult: AsyncResult<S, E>) => AsyncResult<S2, E>) {\n switch (args.length) {\n case 2: {\n // Direct version: first argument is asyncResult\n const [asyncResult, mapFn] = args;\n\n return mapImpl(asyncResult, mapFn);\n }\n\n case 1: {\n // Curried version\n const [mapFn] = args;\n\n return (asyncResult: AsyncResult<S, E>) => mapImpl(asyncResult, mapFn);\n }\n }\n}\n\nconst mapImpl = async <S, S2, E>(\n asyncResult: AsyncResult<S, E>,\n mapFn: (value: S) => Promise<S2> | S2,\n): AsyncResult<S2, E> => {\n const result = await asyncResult;\n\n return Result.isErr(result) ? result : Result.ok(await mapFn(result.value));\n};\n"],"names":["Result.isErr","Result.ok"],"mappings":";;;;AA2DO,SAAS,OACX,IAAA,EAQwD;AAC3D,EAAA,QAAQ,KAAK,MAAA;AAAQ,IACnB,KAAK,CAAA,EAAG;AAEN,MAAA,MAAM,CAAC,WAAA,EAAa,KAAK,CAAA,GAAI,IAAA;AAE7B,MAAA,OAAO,OAAA,CAAQ,aAAa,KAAK,CAAA;AAAA,IACnC;AAAA,IAEA,KAAK,CAAA,EAAG;AAEN,MAAA,MAAM,CAAC,KAAK,CAAA,GAAI,IAAA;AAEhB,MAAA,OAAO,CAAC,WAAA,KAAmC,OAAA,CAAQ,WAAA,EAAa,KAAK,CAAA;AAAA,IACvE;AAAA;AAEJ;AAEA,MAAM,OAAA,GAAU,OACd,WAAA,EACA,KAAA,KACuB;AACvB,EAAA,MAAM,SAAS,MAAM,WAAA;AAErB,EAAA,OAAOA,KAAO,CAAM,MAAM,CAAA,GAAI,MAAA,GAASC,EAAO,CAAG,MAAM,KAAA,CAAM,MAAA,CAAO,KAAK,CAAC,CAAA;AAC5E,CAAA;;;;"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type AsyncResult } from '../async-result.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* Unwraps an `AsyncResult`, resolving to the success value or a default value
|
|
4
|
+
* if it resolves to `Err`.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* const okValue = AsyncResult.fromPromise(
|
|
10
|
+
* Promise.resolve(10),
|
|
11
|
+
* () => 'failed',
|
|
12
|
+
* );
|
|
13
|
+
*
|
|
14
|
+
* const errValue = AsyncResult.fromPromise(
|
|
15
|
+
* Promise.reject(new Error('boom')),
|
|
16
|
+
* () => 'failed',
|
|
17
|
+
* );
|
|
18
|
+
*
|
|
19
|
+
* assert.isTrue((await AsyncResult.unwrapOr(okValue, 0)) === 10);
|
|
20
|
+
*
|
|
21
|
+
* assert.isTrue((await AsyncResult.unwrapOr(errValue, 0)) === 0);
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @template S The type of the success value.
|
|
25
|
+
* @template E The type of the error value.
|
|
26
|
+
* @template D The type of the default value.
|
|
27
|
+
* @param asyncResult The `AsyncResult` to unwrap.
|
|
28
|
+
* @param defaultValue The value to resolve to if `asyncResult` resolves to
|
|
29
|
+
* `Err`.
|
|
30
|
+
* @returns A Promise of the success value if `Ok`, otherwise of
|
|
31
|
+
* `defaultValue`.
|
|
32
|
+
*/
|
|
33
|
+
export declare const unwrapOr: <S, E, D>(asyncResult: AsyncResult<S, E>, defaultValue: D) => Promise<D | S>;
|
|
34
|
+
//# sourceMappingURL=async-result-unwrap-or.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-result-unwrap-or.d.mts","sourceRoot":"","sources":["../../../../src/functional/async-result/impl/async-result-unwrap-or.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAEvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,eAAO,MAAM,QAAQ,GAAU,CAAC,EAAE,CAAC,EAAE,CAAC,eACvB,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,gBAChB,CAAC,KACd,OAAO,CAAC,CAAC,GAAG,CAAC,CAIf,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { isOk } from '../../result/impl/result-is-ok.mjs';
|
|
2
|
+
import '@sindresorhus/is';
|
|
3
|
+
|
|
4
|
+
const unwrapOr = async (asyncResult, defaultValue) => {
|
|
5
|
+
const result = await asyncResult;
|
|
6
|
+
return isOk(result) ? result.value : defaultValue;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export { unwrapOr };
|
|
10
|
+
//# sourceMappingURL=async-result-unwrap-or.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-result-unwrap-or.mjs","sources":["../../../../src/functional/async-result/impl/async-result-unwrap-or.mts"],"sourcesContent":["import { Result } from '../../result/index.mjs';\nimport { type AsyncResult } from '../async-result.mjs';\n\n/**\n * Unwraps an `AsyncResult`, resolving to the success value or a default value\n * if it resolves to `Err`.\n *\n * @example\n *\n * ```ts\n * const okValue = AsyncResult.fromPromise(\n * Promise.resolve(10),\n * () => 'failed',\n * );\n *\n * const errValue = AsyncResult.fromPromise(\n * Promise.reject(new Error('boom')),\n * () => 'failed',\n * );\n *\n * assert.isTrue((await AsyncResult.unwrapOr(okValue, 0)) === 10);\n *\n * assert.isTrue((await AsyncResult.unwrapOr(errValue, 0)) === 0);\n * ```\n *\n * @template S The type of the success value.\n * @template E The type of the error value.\n * @template D The type of the default value.\n * @param asyncResult The `AsyncResult` to unwrap.\n * @param defaultValue The value to resolve to if `asyncResult` resolves to\n * `Err`.\n * @returns A Promise of the success value if `Ok`, otherwise of\n * `defaultValue`.\n */\nexport const unwrapOr = async <S, E, D>(\n asyncResult: AsyncResult<S, E>,\n defaultValue: D,\n): Promise<D | S> => {\n const result = await asyncResult;\n\n return Result.isOk(result) ? result.value : defaultValue;\n};\n"],"names":["Result.isOk"],"mappings":";;;AAkCO,MAAM,QAAA,GAAW,OACtB,WAAA,EACA,YAAA,KACmB;AACnB,EAAA,MAAM,SAAS,MAAM,WAAA;AAErB,EAAA,OAAOA,IAAO,CAAK,MAAM,CAAA,GAAI,OAAO,KAAA,GAAQ,YAAA;AAC9C;;;;"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './async-result-flat-map.mjs';
|
|
2
|
+
export * from './async-result-from-promise.mjs';
|
|
3
|
+
export * from './async-result-from-throwable.mjs';
|
|
4
|
+
export * from './async-result-map-err.mjs';
|
|
5
|
+
export * from './async-result-map.mjs';
|
|
6
|
+
export * from './async-result-unwrap-or.mjs';
|
|
7
|
+
export * from './types.mjs';
|
|
8
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../../src/functional/async-result/impl/index.mts"],"names":[],"mappings":"AAAA,cAAc,6BAA6B,CAAC;AAC5C,cAAc,iCAAiC,CAAC;AAChD,cAAc,mCAAmC,CAAC;AAClD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,wBAAwB,CAAC;AACvC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { flatMap } from './async-result-flat-map.mjs';
|
|
2
|
+
export { fromPromise } from './async-result-from-promise.mjs';
|
|
3
|
+
export { fromThrowable } from './async-result-from-throwable.mjs';
|
|
4
|
+
export { mapErr } from './async-result-map-err.mjs';
|
|
5
|
+
export { map } from './async-result-map.mjs';
|
|
6
|
+
export { unwrapOr } from './async-result-unwrap-or.mjs';
|
|
7
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;"}
|