@praha/byethrow 0.1.0 → 0.2.1
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/cjs/exports.cjs +24 -15
- package/dist/cjs/exports.d.ts +1 -0
- package/dist/cjs/functions/or-else.cjs +44 -0
- package/dist/cjs/functions/or-else.d.ts +47 -0
- package/dist/cjs/functions/unwrap-error.cjs +4 -0
- package/dist/cjs/functions/unwrap-error.d.ts +28 -10
- package/dist/cjs/functions/unwrap.cjs +4 -0
- package/dist/cjs/functions/unwrap.d.ts +26 -8
- package/dist/esm/exports.d.ts +1 -0
- package/dist/esm/exports.js +1 -0
- package/dist/esm/functions/or-else.d.ts +47 -0
- package/dist/esm/functions/or-else.js +10 -0
- package/dist/esm/functions/unwrap-error.d.ts +28 -10
- package/dist/esm/functions/unwrap-error.js +4 -0
- package/dist/esm/functions/unwrap.d.ts +26 -8
- package/dist/esm/functions/unwrap.js +4 -0
- package/package.json +2 -2
package/dist/cjs/exports.cjs
CHANGED
@@ -30,6 +30,9 @@ var __webpack_modules__ = {
|
|
30
30
|
"./functions/map": function(module) {
|
31
31
|
module.exports = require("./functions/map.cjs");
|
32
32
|
},
|
33
|
+
"./functions/or-else": function(module) {
|
34
|
+
module.exports = require("./functions/or-else.cjs");
|
35
|
+
},
|
33
36
|
"./functions/pipe": function(module) {
|
34
37
|
module.exports = require("./functions/pipe.cjs");
|
35
38
|
},
|
@@ -158,34 +161,40 @@ var __webpack_exports__ = {};
|
|
158
161
|
return _functions_map_error__WEBPACK_IMPORTED_MODULE_10__[key];
|
159
162
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
160
163
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
161
|
-
var
|
164
|
+
var _functions_or_else__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__("./functions/or-else");
|
165
|
+
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
166
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_or_else__WEBPACK_IMPORTED_MODULE_11__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
167
|
+
return _functions_or_else__WEBPACK_IMPORTED_MODULE_11__[key];
|
168
|
+
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
169
|
+
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
170
|
+
var _functions_pipe__WEBPACK_IMPORTED_MODULE_12__ = __webpack_require__("./functions/pipe");
|
162
171
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
163
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
164
|
-
return
|
172
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_pipe__WEBPACK_IMPORTED_MODULE_12__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
173
|
+
return _functions_pipe__WEBPACK_IMPORTED_MODULE_12__[key];
|
165
174
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
166
175
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
167
|
-
var
|
176
|
+
var _functions_succeed__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__("./functions/succeed");
|
168
177
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
169
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
170
|
-
return
|
178
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_succeed__WEBPACK_IMPORTED_MODULE_13__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
179
|
+
return _functions_succeed__WEBPACK_IMPORTED_MODULE_13__[key];
|
171
180
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
172
181
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
173
|
-
var
|
182
|
+
var _functions_try__WEBPACK_IMPORTED_MODULE_14__ = __webpack_require__("./functions/try");
|
174
183
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
175
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
176
|
-
return
|
184
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_try__WEBPACK_IMPORTED_MODULE_14__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
185
|
+
return _functions_try__WEBPACK_IMPORTED_MODULE_14__[key];
|
177
186
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
178
187
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
179
|
-
var
|
188
|
+
var _functions_unwrap__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__("./functions/unwrap");
|
180
189
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
181
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
182
|
-
return
|
190
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_unwrap__WEBPACK_IMPORTED_MODULE_15__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
191
|
+
return _functions_unwrap__WEBPACK_IMPORTED_MODULE_15__[key];
|
183
192
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
184
193
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
185
|
-
var
|
194
|
+
var _functions_unwrap_error__WEBPACK_IMPORTED_MODULE_16__ = __webpack_require__("./functions/unwrap-error");
|
186
195
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
187
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
188
|
-
return
|
196
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_unwrap_error__WEBPACK_IMPORTED_MODULE_16__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
197
|
+
return _functions_unwrap_error__WEBPACK_IMPORTED_MODULE_16__[key];
|
189
198
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
190
199
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
191
200
|
})();
|
package/dist/cjs/exports.d.ts
CHANGED
@@ -9,6 +9,7 @@ export * from './functions/is-failure';
|
|
9
9
|
export * from './functions/is-success';
|
10
10
|
export * from './functions/map';
|
11
11
|
export * from './functions/map-error';
|
12
|
+
export * from './functions/or-else';
|
12
13
|
export * from './functions/pipe';
|
13
14
|
export * from './functions/succeed';
|
14
15
|
export * from './functions/try';
|
@@ -0,0 +1,44 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __webpack_require__ = {};
|
3
|
+
(()=>{
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
6
|
+
enumerable: true,
|
7
|
+
get: definition[key]
|
8
|
+
});
|
9
|
+
};
|
10
|
+
})();
|
11
|
+
(()=>{
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
13
|
+
})();
|
14
|
+
(()=>{
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
16
|
+
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
17
|
+
value: 'Module'
|
18
|
+
});
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
20
|
+
value: true
|
21
|
+
});
|
22
|
+
};
|
23
|
+
})();
|
24
|
+
var __webpack_exports__ = {};
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
27
|
+
orElse: ()=>orElse
|
28
|
+
});
|
29
|
+
const external_is_success_cjs_namespaceObject = require("./is-success.cjs");
|
30
|
+
const is_promise_cjs_namespaceObject = require("../internals/helpers/is-promise.cjs");
|
31
|
+
const orElse = (fn)=>(result)=>{
|
32
|
+
const apply = (r)=>{
|
33
|
+
if ((0, external_is_success_cjs_namespaceObject.isSuccess)(r)) return r;
|
34
|
+
return fn(r.error);
|
35
|
+
};
|
36
|
+
return (0, is_promise_cjs_namespaceObject.isPromise)(result) ? result.then(apply) : apply(result);
|
37
|
+
};
|
38
|
+
exports.orElse = __webpack_exports__.orElse;
|
39
|
+
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
40
|
+
"orElse"
|
41
|
+
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
42
|
+
Object.defineProperty(exports, '__esModule', {
|
43
|
+
value: true
|
44
|
+
});
|
@@ -0,0 +1,47 @@
|
|
1
|
+
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result';
|
2
|
+
/**
|
3
|
+
* Chains the next computation using the error value of a {@link Result} or {@link ResultAsync}.
|
4
|
+
* If the original result is a {@link Success}, it is returned unchanged.
|
5
|
+
* Otherwise, the provided function is called, and its result is returned as-is.
|
6
|
+
*
|
7
|
+
* @function
|
8
|
+
* @typeParam R1 - The input {@link Result} or {@link ResultAsync}.
|
9
|
+
* @typeParam R2 - The result type returned by `fn`.
|
10
|
+
*
|
11
|
+
* @example Success Case
|
12
|
+
* ```ts
|
13
|
+
* import { Result } from '@praha/byethrow';
|
14
|
+
*
|
15
|
+
* const result = Result.pipe(
|
16
|
+
* Result.succeed(42),
|
17
|
+
* Result.orElse((error) => Result.succeed(0)),
|
18
|
+
* );
|
19
|
+
* // { type: 'Success', value: 42 }
|
20
|
+
* ```
|
21
|
+
*
|
22
|
+
* @example Failure Case (function returns a Success)
|
23
|
+
* ```ts
|
24
|
+
* const result = Result.pipe(
|
25
|
+
* Result.fail('original error'),
|
26
|
+
* Result.orElse((error) => Result.succeed('default value')),
|
27
|
+
* );
|
28
|
+
* // result: { type: 'Success', value: 'default value' }
|
29
|
+
* ```
|
30
|
+
*
|
31
|
+
* @example Failure Case (function returns a Failure)
|
32
|
+
* ```ts
|
33
|
+
* const result = Result.pipe(
|
34
|
+
* Result.fail('original error'),
|
35
|
+
* Result.orElse((error) => Result.fail('new error: ' + error)),
|
36
|
+
* );
|
37
|
+
* // result: { type: 'Failure', error: 'new error: original error' }
|
38
|
+
* ```
|
39
|
+
*
|
40
|
+
* @see {@link pipe} - It is recommended to use this function with the {@link pipe} function for better readability and composability.
|
41
|
+
*
|
42
|
+
* @category Combinators
|
43
|
+
*/
|
44
|
+
export declare const orElse: {
|
45
|
+
<R1 extends ResultMaybeAsync<any, any>, R2 extends ResultMaybeAsync<any, any>>(fn: (a: InferFailure<R1>) => R2): (result: R1) => ResultFor<R1 | R2, InferSuccess<R1> | InferSuccess<R2>, InferFailure<R2>>;
|
46
|
+
<F extends (a: any) => ResultMaybeAsync<any, any>>(fn: F): <R1 extends ResultMaybeAsync<any, Parameters<F>[0]>>(result: R1) => ResultFor<R1 | ReturnType<F>, InferSuccess<R1> | InferSuccess<F>, InferFailure<F>>;
|
47
|
+
};
|
@@ -28,6 +28,10 @@ __webpack_require__.d(__webpack_exports__, {
|
|
28
28
|
});
|
29
29
|
const external_is_success_cjs_namespaceObject = require("./is-success.cjs");
|
30
30
|
const unwrapError = (result)=>{
|
31
|
+
if (result instanceof Promise) return result.then((r)=>{
|
32
|
+
if ((0, external_is_success_cjs_namespaceObject.isSuccess)(r)) throw r.value;
|
33
|
+
return r.error;
|
34
|
+
});
|
31
35
|
if ((0, external_is_success_cjs_namespaceObject.isSuccess)(result)) throw result.value;
|
32
36
|
return result.error;
|
33
37
|
};
|
@@ -1,31 +1,49 @@
|
|
1
|
-
import type {
|
1
|
+
import type { HasPromise } from '../internals/types/has-promise';
|
2
|
+
import type { InferFailure, ResultMaybeAsync } from '../result';
|
2
3
|
/**
|
3
|
-
* Extracts the error value from a {@link Result}.
|
4
|
+
* Extracts the error value from a {@link Result}, {@link ResultAsync}.
|
4
5
|
*
|
5
6
|
* If the result is a {@link Success}, it throws the success value (this is rare but symmetric to `unwrap`).
|
7
|
+
* For {@link ResultAsync}, it returns a Promise that resolves to the error value or rejects with the success value.
|
6
8
|
*
|
7
9
|
* @function
|
8
|
-
* @typeParam
|
9
|
-
* @param result - The {@link Result}
|
10
|
-
* @returns The error value.
|
10
|
+
* @typeParam R - The type of the result to unwrap.
|
11
|
+
* @param result - The {@link Result}, {@link ResultAsync}.
|
12
|
+
* @returns The error value, or a Promise of the error value for async results.
|
11
13
|
* @throws The success value if the result is a {@link Success}.
|
12
14
|
*
|
13
15
|
* @example Failure Case
|
14
16
|
* ```ts
|
15
17
|
* import { Result } from '@praha/byethrow';
|
16
18
|
*
|
17
|
-
* const result: Result.Result<number, string> =
|
18
|
-
* const error = Result.unwrapError(result); // '
|
19
|
+
* const result: Result.Result<number, string> = Result.fail('Oops');
|
20
|
+
* const error = Result.unwrapError(result); // 'Oops'
|
19
21
|
* ```
|
20
22
|
*
|
21
23
|
* @example Success Case
|
22
24
|
* ```ts
|
23
25
|
* import { Result } from '@praha/byethrow';
|
24
26
|
*
|
25
|
-
* const result: Result.Result<number, string> =
|
26
|
-
* const error = Result.unwrapError(result); // throws
|
27
|
+
* const result: Result.Result<number, string> = Result.succeed(100);
|
28
|
+
* const error = Result.unwrapError(result); // throws 100
|
29
|
+
* ```
|
30
|
+
*
|
31
|
+
* @example Async Failure Case
|
32
|
+
* ```ts
|
33
|
+
* import { Result } from '@praha/byethrow';
|
34
|
+
*
|
35
|
+
* const result: Result.Result<number, string> = Result,fail(Promise.resolve('Oops'));
|
36
|
+
* const error = Result.unwrapError(result); // 'Oops'
|
37
|
+
* ```
|
38
|
+
*
|
39
|
+
* @example Async Success Case
|
40
|
+
* ```ts
|
41
|
+
* import { Result } from '@praha/byethrow';
|
42
|
+
*
|
43
|
+
* const result: Result.Result<number, string> = Result.succeed(Promise.resolve(100));
|
44
|
+
* const error = Result.unwrapError(result); // throws 100
|
27
45
|
* ```
|
28
46
|
*
|
29
47
|
* @category Unwraps
|
30
48
|
*/
|
31
|
-
export declare const unwrapError: <
|
49
|
+
export declare const unwrapError: <R extends ResultMaybeAsync<any, any>>(result: R) => true extends HasPromise<R> ? Promise<InferFailure<R>> : InferFailure<R>;
|
@@ -28,6 +28,10 @@ __webpack_require__.d(__webpack_exports__, {
|
|
28
28
|
});
|
29
29
|
const external_is_failure_cjs_namespaceObject = require("./is-failure.cjs");
|
30
30
|
const unwrap = (result)=>{
|
31
|
+
if (result instanceof Promise) return result.then((r)=>{
|
32
|
+
if ((0, external_is_failure_cjs_namespaceObject.isFailure)(r)) throw r.error;
|
33
|
+
return r.value;
|
34
|
+
});
|
31
35
|
if ((0, external_is_failure_cjs_namespaceObject.isFailure)(result)) throw result.error;
|
32
36
|
return result.value;
|
33
37
|
};
|
@@ -1,20 +1,22 @@
|
|
1
|
-
import type {
|
1
|
+
import type { HasPromise } from '../internals/types/has-promise';
|
2
|
+
import type { InferSuccess, ResultMaybeAsync } from '../result';
|
2
3
|
/**
|
3
|
-
* Extracts the success value from a {@link Result}.
|
4
|
+
* Extracts the success value from a {@link Result}, {@link ResultAsync}.
|
4
5
|
*
|
5
6
|
* If the result is a {@link Failure}, it throws the contained error.
|
7
|
+
* For {@link ResultAsync}, it returns a Promise that resolves to the success value or rejects with the error.
|
6
8
|
*
|
7
9
|
* @function
|
8
|
-
* @typeParam
|
9
|
-
* @param result - The {@link Result}
|
10
|
-
* @returns The success value.
|
10
|
+
* @typeParam R - The type of the result to unwrap.
|
11
|
+
* @param result - The {@link Result}, {@link ResultAsync}.
|
12
|
+
* @returns The success value, or a Promise of the success value for async results.
|
11
13
|
* @throws The error value if the result is a {@link Failure}.
|
12
14
|
*
|
13
15
|
* @example Success Case
|
14
16
|
* ```ts
|
15
17
|
* import { Result } from '@praha/byethrow';
|
16
18
|
*
|
17
|
-
* const result: Result.Result<number, string> =
|
19
|
+
* const result: Result.Result<number, string> = Result.succeed(100);
|
18
20
|
* const value = Result.unwrap(result); // 100
|
19
21
|
* ```
|
20
22
|
*
|
@@ -22,10 +24,26 @@ import type { Result } from '../result';
|
|
22
24
|
* ```ts
|
23
25
|
* import { Result } from '@praha/byethrow';
|
24
26
|
*
|
25
|
-
* const result: Result.Result<number, string> =
|
27
|
+
* const result: Result.Result<number, string> = Result.fail('Oops');
|
26
28
|
* const value = Result.unwrap(result); // throws 'Oops'
|
27
29
|
* ```
|
28
30
|
*
|
31
|
+
* @example Async Success Case
|
32
|
+
* ```ts
|
33
|
+
* import { Result } from '@praha/byethrow';
|
34
|
+
*
|
35
|
+
* const result: Result.ResultAsync<number, string> = Result.succeed(Promise.resolve(100));
|
36
|
+
* const value = await Result.unwrap(result); // 100
|
37
|
+
* ```
|
38
|
+
*
|
39
|
+
* @example Async Failure Case
|
40
|
+
* ```ts
|
41
|
+
* import { Result } from '@praha/byethrow';
|
42
|
+
*
|
43
|
+
* const result: Result.ResultAsync<number, string> = Result.fail(Promise.resolve('Oops'));
|
44
|
+
* const value = await Result.unwrap(result); // throws 'Oops'
|
45
|
+
* ```
|
46
|
+
*
|
29
47
|
* @category Unwraps
|
30
48
|
*/
|
31
|
-
export declare const unwrap: <
|
49
|
+
export declare const unwrap: <R extends ResultMaybeAsync<any, any>>(result: R) => true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>;
|
package/dist/esm/exports.d.ts
CHANGED
@@ -9,6 +9,7 @@ export * from './functions/is-failure';
|
|
9
9
|
export * from './functions/is-success';
|
10
10
|
export * from './functions/map';
|
11
11
|
export * from './functions/map-error';
|
12
|
+
export * from './functions/or-else';
|
12
13
|
export * from './functions/pipe';
|
13
14
|
export * from './functions/succeed';
|
14
15
|
export * from './functions/try';
|
package/dist/esm/exports.js
CHANGED
@@ -9,6 +9,7 @@ export * from "./functions/is-failure.js";
|
|
9
9
|
export * from "./functions/is-success.js";
|
10
10
|
export * from "./functions/map.js";
|
11
11
|
export * from "./functions/map-error.js";
|
12
|
+
export * from "./functions/or-else.js";
|
12
13
|
export * from "./functions/pipe.js";
|
13
14
|
export * from "./functions/succeed.js";
|
14
15
|
export * from "./functions/try.js";
|
@@ -0,0 +1,47 @@
|
|
1
|
+
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result';
|
2
|
+
/**
|
3
|
+
* Chains the next computation using the error value of a {@link Result} or {@link ResultAsync}.
|
4
|
+
* If the original result is a {@link Success}, it is returned unchanged.
|
5
|
+
* Otherwise, the provided function is called, and its result is returned as-is.
|
6
|
+
*
|
7
|
+
* @function
|
8
|
+
* @typeParam R1 - The input {@link Result} or {@link ResultAsync}.
|
9
|
+
* @typeParam R2 - The result type returned by `fn`.
|
10
|
+
*
|
11
|
+
* @example Success Case
|
12
|
+
* ```ts
|
13
|
+
* import { Result } from '@praha/byethrow';
|
14
|
+
*
|
15
|
+
* const result = Result.pipe(
|
16
|
+
* Result.succeed(42),
|
17
|
+
* Result.orElse((error) => Result.succeed(0)),
|
18
|
+
* );
|
19
|
+
* // { type: 'Success', value: 42 }
|
20
|
+
* ```
|
21
|
+
*
|
22
|
+
* @example Failure Case (function returns a Success)
|
23
|
+
* ```ts
|
24
|
+
* const result = Result.pipe(
|
25
|
+
* Result.fail('original error'),
|
26
|
+
* Result.orElse((error) => Result.succeed('default value')),
|
27
|
+
* );
|
28
|
+
* // result: { type: 'Success', value: 'default value' }
|
29
|
+
* ```
|
30
|
+
*
|
31
|
+
* @example Failure Case (function returns a Failure)
|
32
|
+
* ```ts
|
33
|
+
* const result = Result.pipe(
|
34
|
+
* Result.fail('original error'),
|
35
|
+
* Result.orElse((error) => Result.fail('new error: ' + error)),
|
36
|
+
* );
|
37
|
+
* // result: { type: 'Failure', error: 'new error: original error' }
|
38
|
+
* ```
|
39
|
+
*
|
40
|
+
* @see {@link pipe} - It is recommended to use this function with the {@link pipe} function for better readability and composability.
|
41
|
+
*
|
42
|
+
* @category Combinators
|
43
|
+
*/
|
44
|
+
export declare const orElse: {
|
45
|
+
<R1 extends ResultMaybeAsync<any, any>, R2 extends ResultMaybeAsync<any, any>>(fn: (a: InferFailure<R1>) => R2): (result: R1) => ResultFor<R1 | R2, InferSuccess<R1> | InferSuccess<R2>, InferFailure<R2>>;
|
46
|
+
<F extends (a: any) => ResultMaybeAsync<any, any>>(fn: F): <R1 extends ResultMaybeAsync<any, Parameters<F>[0]>>(result: R1) => ResultFor<R1 | ReturnType<F>, InferSuccess<R1> | InferSuccess<F>, InferFailure<F>>;
|
47
|
+
};
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { isSuccess } from "./is-success.js";
|
2
|
+
import { isPromise } from "../internals/helpers/is-promise.js";
|
3
|
+
const orElse = (fn)=>(result)=>{
|
4
|
+
const apply = (r)=>{
|
5
|
+
if (isSuccess(r)) return r;
|
6
|
+
return fn(r.error);
|
7
|
+
};
|
8
|
+
return isPromise(result) ? result.then(apply) : apply(result);
|
9
|
+
};
|
10
|
+
export { orElse };
|
@@ -1,31 +1,49 @@
|
|
1
|
-
import type {
|
1
|
+
import type { HasPromise } from '../internals/types/has-promise';
|
2
|
+
import type { InferFailure, ResultMaybeAsync } from '../result';
|
2
3
|
/**
|
3
|
-
* Extracts the error value from a {@link Result}.
|
4
|
+
* Extracts the error value from a {@link Result}, {@link ResultAsync}.
|
4
5
|
*
|
5
6
|
* If the result is a {@link Success}, it throws the success value (this is rare but symmetric to `unwrap`).
|
7
|
+
* For {@link ResultAsync}, it returns a Promise that resolves to the error value or rejects with the success value.
|
6
8
|
*
|
7
9
|
* @function
|
8
|
-
* @typeParam
|
9
|
-
* @param result - The {@link Result}
|
10
|
-
* @returns The error value.
|
10
|
+
* @typeParam R - The type of the result to unwrap.
|
11
|
+
* @param result - The {@link Result}, {@link ResultAsync}.
|
12
|
+
* @returns The error value, or a Promise of the error value for async results.
|
11
13
|
* @throws The success value if the result is a {@link Success}.
|
12
14
|
*
|
13
15
|
* @example Failure Case
|
14
16
|
* ```ts
|
15
17
|
* import { Result } from '@praha/byethrow';
|
16
18
|
*
|
17
|
-
* const result: Result.Result<number, string> =
|
18
|
-
* const error = Result.unwrapError(result); // '
|
19
|
+
* const result: Result.Result<number, string> = Result.fail('Oops');
|
20
|
+
* const error = Result.unwrapError(result); // 'Oops'
|
19
21
|
* ```
|
20
22
|
*
|
21
23
|
* @example Success Case
|
22
24
|
* ```ts
|
23
25
|
* import { Result } from '@praha/byethrow';
|
24
26
|
*
|
25
|
-
* const result: Result.Result<number, string> =
|
26
|
-
* const error = Result.unwrapError(result); // throws
|
27
|
+
* const result: Result.Result<number, string> = Result.succeed(100);
|
28
|
+
* const error = Result.unwrapError(result); // throws 100
|
29
|
+
* ```
|
30
|
+
*
|
31
|
+
* @example Async Failure Case
|
32
|
+
* ```ts
|
33
|
+
* import { Result } from '@praha/byethrow';
|
34
|
+
*
|
35
|
+
* const result: Result.Result<number, string> = Result,fail(Promise.resolve('Oops'));
|
36
|
+
* const error = Result.unwrapError(result); // 'Oops'
|
37
|
+
* ```
|
38
|
+
*
|
39
|
+
* @example Async Success Case
|
40
|
+
* ```ts
|
41
|
+
* import { Result } from '@praha/byethrow';
|
42
|
+
*
|
43
|
+
* const result: Result.Result<number, string> = Result.succeed(Promise.resolve(100));
|
44
|
+
* const error = Result.unwrapError(result); // throws 100
|
27
45
|
* ```
|
28
46
|
*
|
29
47
|
* @category Unwraps
|
30
48
|
*/
|
31
|
-
export declare const unwrapError: <
|
49
|
+
export declare const unwrapError: <R extends ResultMaybeAsync<any, any>>(result: R) => true extends HasPromise<R> ? Promise<InferFailure<R>> : InferFailure<R>;
|
@@ -1,5 +1,9 @@
|
|
1
1
|
import { isSuccess } from "./is-success.js";
|
2
2
|
const unwrapError = (result)=>{
|
3
|
+
if (result instanceof Promise) return result.then((r)=>{
|
4
|
+
if (isSuccess(r)) throw r.value;
|
5
|
+
return r.error;
|
6
|
+
});
|
3
7
|
if (isSuccess(result)) throw result.value;
|
4
8
|
return result.error;
|
5
9
|
};
|
@@ -1,20 +1,22 @@
|
|
1
|
-
import type {
|
1
|
+
import type { HasPromise } from '../internals/types/has-promise';
|
2
|
+
import type { InferSuccess, ResultMaybeAsync } from '../result';
|
2
3
|
/**
|
3
|
-
* Extracts the success value from a {@link Result}.
|
4
|
+
* Extracts the success value from a {@link Result}, {@link ResultAsync}.
|
4
5
|
*
|
5
6
|
* If the result is a {@link Failure}, it throws the contained error.
|
7
|
+
* For {@link ResultAsync}, it returns a Promise that resolves to the success value or rejects with the error.
|
6
8
|
*
|
7
9
|
* @function
|
8
|
-
* @typeParam
|
9
|
-
* @param result - The {@link Result}
|
10
|
-
* @returns The success value.
|
10
|
+
* @typeParam R - The type of the result to unwrap.
|
11
|
+
* @param result - The {@link Result}, {@link ResultAsync}.
|
12
|
+
* @returns The success value, or a Promise of the success value for async results.
|
11
13
|
* @throws The error value if the result is a {@link Failure}.
|
12
14
|
*
|
13
15
|
* @example Success Case
|
14
16
|
* ```ts
|
15
17
|
* import { Result } from '@praha/byethrow';
|
16
18
|
*
|
17
|
-
* const result: Result.Result<number, string> =
|
19
|
+
* const result: Result.Result<number, string> = Result.succeed(100);
|
18
20
|
* const value = Result.unwrap(result); // 100
|
19
21
|
* ```
|
20
22
|
*
|
@@ -22,10 +24,26 @@ import type { Result } from '../result';
|
|
22
24
|
* ```ts
|
23
25
|
* import { Result } from '@praha/byethrow';
|
24
26
|
*
|
25
|
-
* const result: Result.Result<number, string> =
|
27
|
+
* const result: Result.Result<number, string> = Result.fail('Oops');
|
26
28
|
* const value = Result.unwrap(result); // throws 'Oops'
|
27
29
|
* ```
|
28
30
|
*
|
31
|
+
* @example Async Success Case
|
32
|
+
* ```ts
|
33
|
+
* import { Result } from '@praha/byethrow';
|
34
|
+
*
|
35
|
+
* const result: Result.ResultAsync<number, string> = Result.succeed(Promise.resolve(100));
|
36
|
+
* const value = await Result.unwrap(result); // 100
|
37
|
+
* ```
|
38
|
+
*
|
39
|
+
* @example Async Failure Case
|
40
|
+
* ```ts
|
41
|
+
* import { Result } from '@praha/byethrow';
|
42
|
+
*
|
43
|
+
* const result: Result.ResultAsync<number, string> = Result.fail(Promise.resolve('Oops'));
|
44
|
+
* const value = await Result.unwrap(result); // throws 'Oops'
|
45
|
+
* ```
|
46
|
+
*
|
29
47
|
* @category Unwraps
|
30
48
|
*/
|
31
|
-
export declare const unwrap: <
|
49
|
+
export declare const unwrap: <R extends ResultMaybeAsync<any, any>>(result: R) => true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>;
|
@@ -1,5 +1,9 @@
|
|
1
1
|
import { isFailure } from "./is-failure.js";
|
2
2
|
const unwrap = (result)=>{
|
3
|
+
if (result instanceof Promise) return result.then((r)=>{
|
4
|
+
if (isFailure(r)) throw r.error;
|
5
|
+
return r.value;
|
6
|
+
});
|
3
7
|
if (isFailure(result)) throw result.error;
|
4
8
|
return result.value;
|
5
9
|
};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@praha/byethrow",
|
3
|
-
"version": "0.1
|
3
|
+
"version": "0.2.1",
|
4
4
|
"description": "A lightweight, tree-shakable Result type package with a simple, consistent API designed",
|
5
5
|
"keywords": [
|
6
6
|
"javascript",
|
@@ -41,7 +41,7 @@
|
|
41
41
|
"README.md"
|
42
42
|
],
|
43
43
|
"devDependencies": {
|
44
|
-
"@rslib/core": "0.10.
|
44
|
+
"@rslib/core": "0.10.2",
|
45
45
|
"eslint": "9.29.0",
|
46
46
|
"typedoc": "0.28.5",
|
47
47
|
"typescript": "5.8.3",
|