@praha/byethrow 0.3.0 → 0.4.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.
@@ -27,6 +27,9 @@ var __webpack_modules__ = {
27
27
  "./functions/is-failure": function(module) {
28
28
  module.exports = require("./functions/is-failure.cjs");
29
29
  },
30
+ "./functions/is-result": function(module) {
31
+ module.exports = require("./functions/is-result.cjs");
32
+ },
30
33
  "./functions/is-success": function(module) {
31
34
  module.exports = require("./functions/is-success.cjs");
32
35
  },
@@ -161,58 +164,64 @@ var __webpack_exports__ = {};
161
164
  return _functions_is_failure__WEBPACK_IMPORTED_MODULE_9__[key];
162
165
  }).bind(0, __WEBPACK_IMPORT_KEY__);
163
166
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
164
- var _functions_is_success__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__("./functions/is-success");
167
+ var _functions_is_result__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__("./functions/is-result");
168
+ var __WEBPACK_REEXPORT_OBJECT__ = {};
169
+ for(var __WEBPACK_IMPORT_KEY__ in _functions_is_result__WEBPACK_IMPORTED_MODULE_10__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
170
+ return _functions_is_result__WEBPACK_IMPORTED_MODULE_10__[key];
171
+ }).bind(0, __WEBPACK_IMPORT_KEY__);
172
+ __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
173
+ var _functions_is_success__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__("./functions/is-success");
165
174
  var __WEBPACK_REEXPORT_OBJECT__ = {};
166
- for(var __WEBPACK_IMPORT_KEY__ in _functions_is_success__WEBPACK_IMPORTED_MODULE_10__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
167
- return _functions_is_success__WEBPACK_IMPORTED_MODULE_10__[key];
175
+ for(var __WEBPACK_IMPORT_KEY__ in _functions_is_success__WEBPACK_IMPORTED_MODULE_11__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
176
+ return _functions_is_success__WEBPACK_IMPORTED_MODULE_11__[key];
168
177
  }).bind(0, __WEBPACK_IMPORT_KEY__);
169
178
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
170
- var _functions_map__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__("./functions/map");
179
+ var _functions_map__WEBPACK_IMPORTED_MODULE_12__ = __webpack_require__("./functions/map");
171
180
  var __WEBPACK_REEXPORT_OBJECT__ = {};
172
- for(var __WEBPACK_IMPORT_KEY__ in _functions_map__WEBPACK_IMPORTED_MODULE_11__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
173
- return _functions_map__WEBPACK_IMPORTED_MODULE_11__[key];
181
+ for(var __WEBPACK_IMPORT_KEY__ in _functions_map__WEBPACK_IMPORTED_MODULE_12__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
182
+ return _functions_map__WEBPACK_IMPORTED_MODULE_12__[key];
174
183
  }).bind(0, __WEBPACK_IMPORT_KEY__);
175
184
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
176
- var _functions_map_error__WEBPACK_IMPORTED_MODULE_12__ = __webpack_require__("./functions/map-error");
185
+ var _functions_map_error__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__("./functions/map-error");
177
186
  var __WEBPACK_REEXPORT_OBJECT__ = {};
178
- for(var __WEBPACK_IMPORT_KEY__ in _functions_map_error__WEBPACK_IMPORTED_MODULE_12__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
179
- return _functions_map_error__WEBPACK_IMPORTED_MODULE_12__[key];
187
+ for(var __WEBPACK_IMPORT_KEY__ in _functions_map_error__WEBPACK_IMPORTED_MODULE_13__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
188
+ return _functions_map_error__WEBPACK_IMPORTED_MODULE_13__[key];
180
189
  }).bind(0, __WEBPACK_IMPORT_KEY__);
181
190
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
182
- var _functions_or_else__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__("./functions/or-else");
191
+ var _functions_or_else__WEBPACK_IMPORTED_MODULE_14__ = __webpack_require__("./functions/or-else");
183
192
  var __WEBPACK_REEXPORT_OBJECT__ = {};
184
- for(var __WEBPACK_IMPORT_KEY__ in _functions_or_else__WEBPACK_IMPORTED_MODULE_13__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
185
- return _functions_or_else__WEBPACK_IMPORTED_MODULE_13__[key];
193
+ for(var __WEBPACK_IMPORT_KEY__ in _functions_or_else__WEBPACK_IMPORTED_MODULE_14__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
194
+ return _functions_or_else__WEBPACK_IMPORTED_MODULE_14__[key];
186
195
  }).bind(0, __WEBPACK_IMPORT_KEY__);
187
196
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
188
- var _functions_pipe__WEBPACK_IMPORTED_MODULE_14__ = __webpack_require__("./functions/pipe");
197
+ var _functions_pipe__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__("./functions/pipe");
189
198
  var __WEBPACK_REEXPORT_OBJECT__ = {};
190
- for(var __WEBPACK_IMPORT_KEY__ in _functions_pipe__WEBPACK_IMPORTED_MODULE_14__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
191
- return _functions_pipe__WEBPACK_IMPORTED_MODULE_14__[key];
199
+ for(var __WEBPACK_IMPORT_KEY__ in _functions_pipe__WEBPACK_IMPORTED_MODULE_15__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
200
+ return _functions_pipe__WEBPACK_IMPORTED_MODULE_15__[key];
192
201
  }).bind(0, __WEBPACK_IMPORT_KEY__);
193
202
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
194
- var _functions_succeed__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__("./functions/succeed");
203
+ var _functions_succeed__WEBPACK_IMPORTED_MODULE_16__ = __webpack_require__("./functions/succeed");
195
204
  var __WEBPACK_REEXPORT_OBJECT__ = {};
196
- for(var __WEBPACK_IMPORT_KEY__ in _functions_succeed__WEBPACK_IMPORTED_MODULE_15__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
197
- return _functions_succeed__WEBPACK_IMPORTED_MODULE_15__[key];
205
+ for(var __WEBPACK_IMPORT_KEY__ in _functions_succeed__WEBPACK_IMPORTED_MODULE_16__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
206
+ return _functions_succeed__WEBPACK_IMPORTED_MODULE_16__[key];
198
207
  }).bind(0, __WEBPACK_IMPORT_KEY__);
199
208
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
200
- var _functions_try__WEBPACK_IMPORTED_MODULE_16__ = __webpack_require__("./functions/try");
209
+ var _functions_try__WEBPACK_IMPORTED_MODULE_17__ = __webpack_require__("./functions/try");
201
210
  var __WEBPACK_REEXPORT_OBJECT__ = {};
202
- for(var __WEBPACK_IMPORT_KEY__ in _functions_try__WEBPACK_IMPORTED_MODULE_16__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
203
- return _functions_try__WEBPACK_IMPORTED_MODULE_16__[key];
211
+ for(var __WEBPACK_IMPORT_KEY__ in _functions_try__WEBPACK_IMPORTED_MODULE_17__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
212
+ return _functions_try__WEBPACK_IMPORTED_MODULE_17__[key];
204
213
  }).bind(0, __WEBPACK_IMPORT_KEY__);
205
214
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
206
- var _functions_unwrap__WEBPACK_IMPORTED_MODULE_17__ = __webpack_require__("./functions/unwrap");
215
+ var _functions_unwrap__WEBPACK_IMPORTED_MODULE_18__ = __webpack_require__("./functions/unwrap");
207
216
  var __WEBPACK_REEXPORT_OBJECT__ = {};
208
- for(var __WEBPACK_IMPORT_KEY__ in _functions_unwrap__WEBPACK_IMPORTED_MODULE_17__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
209
- return _functions_unwrap__WEBPACK_IMPORTED_MODULE_17__[key];
217
+ for(var __WEBPACK_IMPORT_KEY__ in _functions_unwrap__WEBPACK_IMPORTED_MODULE_18__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
218
+ return _functions_unwrap__WEBPACK_IMPORTED_MODULE_18__[key];
210
219
  }).bind(0, __WEBPACK_IMPORT_KEY__);
211
220
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
212
- var _functions_unwrap_error__WEBPACK_IMPORTED_MODULE_18__ = __webpack_require__("./functions/unwrap-error");
221
+ var _functions_unwrap_error__WEBPACK_IMPORTED_MODULE_19__ = __webpack_require__("./functions/unwrap-error");
213
222
  var __WEBPACK_REEXPORT_OBJECT__ = {};
214
- for(var __WEBPACK_IMPORT_KEY__ in _functions_unwrap_error__WEBPACK_IMPORTED_MODULE_18__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
215
- return _functions_unwrap_error__WEBPACK_IMPORTED_MODULE_18__[key];
223
+ for(var __WEBPACK_IMPORT_KEY__ in _functions_unwrap_error__WEBPACK_IMPORTED_MODULE_19__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
224
+ return _functions_unwrap_error__WEBPACK_IMPORTED_MODULE_19__[key];
216
225
  }).bind(0, __WEBPACK_IMPORT_KEY__);
217
226
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
218
227
  })();
@@ -8,6 +8,7 @@ export * from './functions/fail';
8
8
  export * from './functions/inspect';
9
9
  export * from './functions/inspect-error';
10
10
  export * from './functions/is-failure';
11
+ export * from './functions/is-result';
11
12
  export * from './functions/is-success';
12
13
  export * from './functions/map';
13
14
  export * from './functions/map-error';
@@ -0,0 +1,36 @@
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
+ isResult: ()=>isResult
28
+ });
29
+ const isResult = (result)=>'object' == typeof result && null !== result && 'type' in result && ('Success' === result.type && 'value' in result || 'Failure' === result.type && 'error' in result);
30
+ exports.isResult = __webpack_exports__.isResult;
31
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
32
+ "isResult"
33
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
34
+ Object.defineProperty(exports, '__esModule', {
35
+ value: true
36
+ });
@@ -0,0 +1,24 @@
1
+ import type { Result } from '../result';
2
+ /**
3
+ * Type guard to check if a value is a {@link Result}.
4
+ *
5
+ * @function
6
+ * @typeParam T - The type of the success value.
7
+ * @typeParam E - The type of the error value.
8
+ * @param result - The value to check.
9
+ * @returns `true` if the value is a {@link Result}, otherwise `false`.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { Result } from '@praha/byethrow';
14
+ *
15
+ * const value: unknown = { type: 'Success', value: 42 };
16
+ * if (Result.isResult(value)) {
17
+ * // value is now typed as Result<unknown, unknown>
18
+ * console.log(value.type); // 'Success' or 'Failure'
19
+ * }
20
+ * ```
21
+ *
22
+ * @category Type Guards
23
+ */
24
+ export declare const isResult: <T, E>(result: unknown) => result is Result<T, E>;
@@ -29,7 +29,12 @@ __webpack_require__.d(__webpack_exports__, {
29
29
  const external_fail_cjs_namespaceObject = require("./fail.cjs");
30
30
  const external_succeed_cjs_namespaceObject = require("./succeed.cjs");
31
31
  const is_promise_cjs_namespaceObject = require("../internals/helpers/is-promise.cjs");
32
- const try_ = (options)=>(...args)=>{
32
+ const try_ = (options)=>{
33
+ if ((0, is_promise_cjs_namespaceObject.isPromise)(options.try)) {
34
+ if ('safe' in options && options.safe) return (0, external_succeed_cjs_namespaceObject.succeed)(options.try);
35
+ return options.try.then((value)=>(0, external_succeed_cjs_namespaceObject.succeed)(value), (error)=>(0, external_fail_cjs_namespaceObject.fail)(options.catch(error)));
36
+ }
37
+ return (...args)=>{
33
38
  try {
34
39
  const output = options.try(...args);
35
40
  if ((0, is_promise_cjs_namespaceObject.isPromise)(output)) {
@@ -43,6 +48,7 @@ const try_ = (options)=>(...args)=>{
43
48
  return (0, external_fail_cjs_namespaceObject.fail)(options.catch(error));
44
49
  }
45
50
  };
51
+ };
46
52
  exports["try"] = __webpack_exports__["try"];
47
53
  for(var __webpack_i__ in __webpack_exports__)if (-1 === [
48
54
  "try"
@@ -1,15 +1,39 @@
1
1
  import type { Result, ResultAsync } from '../result';
2
2
  /**
3
- * Wraps a function execution (sync or async) in a {@link Result} or {@link ResultAsync} type,
3
+ * Wraps a function execution (sync or async) or a Promise in a {@link Result} or {@link ResultAsync} type,
4
4
  * capturing errors and returning them in a structured way.
5
5
  *
6
6
  * You can use either a custom `catch` handler or rely on the `safe: true` option
7
7
  * to assume the function cannot throw.
8
8
  *
9
9
  * @function
10
- * @typeParam T - The function type to execute (sync or async).
10
+ * @typeParam T - The function type to execute (sync or async) or a Promise type.
11
11
  * @typeParam E - The error type to return if `catch` is used.
12
12
  *
13
+ * @example Promise Try-Catch
14
+ * ```ts
15
+ * import { Result } from '@praha/byethrow';
16
+ *
17
+ * const result = Result.try({
18
+ * try: fetch('/api/data'),
19
+ * catch: (error) => new Error('Fetch failed', { cause: error }),
20
+ * });
21
+ *
22
+ * // result is ResultAsync<Response, Error>
23
+ * ```
24
+ *
25
+ * @example Promise Safe
26
+ * ```ts
27
+ * import { Result } from '@praha/byethrow';
28
+ *
29
+ * const result = Result.try({
30
+ * safe: true,
31
+ * try: Promise.resolve('ok'),
32
+ * });
33
+ *
34
+ * // result is ResultAsync<string, never>
35
+ * ```
36
+ *
13
37
  * @example Sync Try-Catch
14
38
  * ```ts
15
39
  * import { Result } from '@praha/byethrow';
@@ -37,7 +61,7 @@ import type { Result, ResultAsync } from '../result';
37
61
  * const result = fn(1); // Result.Result<number, never>
38
62
  * ```
39
63
  *
40
- * @example Async Try-Catch
64
+ * @example Async Function Try-Catch
41
65
  * ```ts
42
66
  * import { Result } from '@praha/byethrow';
43
67
  *
@@ -49,7 +73,7 @@ import type { Result, ResultAsync } from '../result';
49
73
  * const result = await fn('abc'); // Result.ResultAsync<Response, Error>
50
74
  * ```
51
75
  *
52
- * @example Async Safe
76
+ * @example Async Function Safe
53
77
  * ```ts
54
78
  * import { Result } from '@praha/byethrow';
55
79
  *
@@ -64,6 +88,14 @@ import type { Result, ResultAsync } from '../result';
64
88
  * @category Creators
65
89
  */
66
90
  declare const try_: {
91
+ <T extends Promise<any>, E>(options: {
92
+ try: T;
93
+ catch: (error: unknown) => E;
94
+ }): ResultAsync<Awaited<T>, E>;
95
+ <T extends Promise<any>>(options: {
96
+ try: T;
97
+ safe: true;
98
+ }): ResultAsync<Awaited<T>, never>;
67
99
  <T extends (...args: readonly any[]) => Promise<any>, E>(options: {
68
100
  try: T;
69
101
  catch: (error: unknown) => E;
@@ -26,15 +26,36 @@ __webpack_require__.r(__webpack_exports__);
26
26
  __webpack_require__.d(__webpack_exports__, {
27
27
  unwrapError: ()=>unwrapError
28
28
  });
29
+ const external_is_result_cjs_namespaceObject = require("./is-result.cjs");
29
30
  const external_is_success_cjs_namespaceObject = require("./is-success.cjs");
30
31
  const is_promise_cjs_namespaceObject = require("../internals/helpers/is-promise.cjs");
31
- const unwrapError = (result)=>{
32
- if ((0, is_promise_cjs_namespaceObject.isPromise)(result)) return result.then((r)=>{
33
- if ((0, external_is_success_cjs_namespaceObject.isSuccess)(r)) throw r.value;
34
- return r.error;
35
- });
36
- if ((0, external_is_success_cjs_namespaceObject.isSuccess)(result)) throw result.value;
37
- return result.error;
32
+ const unwrapError = (...args)=>{
33
+ const firstArgument = args[0];
34
+ if ((0, external_is_result_cjs_namespaceObject.isResult)(firstArgument) || (0, is_promise_cjs_namespaceObject.isPromise)(firstArgument)) {
35
+ const result = firstArgument;
36
+ const hasDefault = 2 === args.length;
37
+ const defaultValue = hasDefault ? args[1] : void 0;
38
+ const apply = (r)=>{
39
+ if ((0, external_is_success_cjs_namespaceObject.isSuccess)(r)) {
40
+ if (hasDefault) return defaultValue;
41
+ throw new Error(String(r.value));
42
+ }
43
+ return r.error;
44
+ };
45
+ return (0, is_promise_cjs_namespaceObject.isPromise)(result) ? result.then(apply) : apply(result);
46
+ }
47
+ const hasDefault = 1 === args.length;
48
+ const defaultValue = hasDefault ? args[0] : void 0;
49
+ return (result)=>{
50
+ const apply = (r)=>{
51
+ if ((0, external_is_success_cjs_namespaceObject.isSuccess)(r)) {
52
+ if (hasDefault) return defaultValue;
53
+ throw new Error(String(r.value));
54
+ }
55
+ return r.error;
56
+ };
57
+ return (0, is_promise_cjs_namespaceObject.isPromise)(result) ? result.then(apply) : apply(result);
58
+ };
38
59
  };
39
60
  exports.unwrapError = __webpack_exports__.unwrapError;
40
61
  for(var __webpack_i__ in __webpack_exports__)if (-1 === [
@@ -1,18 +1,15 @@
1
1
  import type { HasPromise } from '../internals/types/has-promise';
2
2
  import type { InferFailure, ResultMaybeAsync } from '../result';
3
3
  /**
4
- * Extracts the error value from a {@link Result}, {@link ResultAsync}.
4
+ * Extracts the error value from a {@link Result} or {@link ResultAsync}.
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
+ * If the input is a {@link Success}, it will throw the success value or return the default value if provided.
8
7
  *
9
8
  * @function
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.
13
- * @throws The success value if the result is a {@link Success}.
9
+ * @typeParam R - The input {@link Result} or {@link ResultAsync}.
10
+ * @typeParam T - The default value type (optional).
14
11
  *
15
- * @example Failure Case
12
+ * @example Failure Case (without default)
16
13
  * ```ts
17
14
  * import { Result } from '@praha/byethrow';
18
15
  *
@@ -20,30 +17,35 @@ import type { InferFailure, ResultMaybeAsync } from '../result';
20
17
  * const error = Result.unwrapError(result); // 'Oops'
21
18
  * ```
22
19
  *
23
- * @example Success Case
20
+ * @example Failure Case (with default)
24
21
  * ```ts
25
22
  * import { Result } from '@praha/byethrow';
26
23
  *
27
- * const result: Result.Result<number, string> = Result.succeed(100);
28
- * const error = Result.unwrapError(result); // throws 100
24
+ * const result: Result.Result<number, string> = Result.fail('Oops');
25
+ * const error = Result.unwrapError(result, 'default'); // 'Oops'
29
26
  * ```
30
27
  *
31
- * @example Async Failure Case
28
+ * @example Success Case (without default)
32
29
  * ```ts
33
30
  * import { Result } from '@praha/byethrow';
34
31
  *
35
- * const result: Result.Result<number, string> = Result,fail(Promise.resolve('Oops'));
36
- * const error = Result.unwrapError(result); // 'Oops'
32
+ * const result: Result.Result<number, string> = Result.succeed(100);
33
+ * const error = Result.unwrapError(result); // throws 100
37
34
  * ```
38
35
  *
39
- * @example Async Success Case
36
+ * @example Success Case (with default)
40
37
  * ```ts
41
38
  * import { Result } from '@praha/byethrow';
42
39
  *
43
- * const result: Result.Result<number, string> = Result.succeed(Promise.resolve(100));
44
- * const error = Result.unwrapError(result); // throws 100
40
+ * const result: Result.Result<number, string> = Result.succeed(100);
41
+ * const error = Result.unwrapError(result, 0); // 0
45
42
  * ```
46
43
  *
47
44
  * @category Unwraps
48
45
  */
49
- export declare const unwrapError: <R extends ResultMaybeAsync<any, any>>(result: R) => true extends HasPromise<R> ? Promise<InferFailure<R>> : InferFailure<R>;
46
+ export declare const unwrapError: {
47
+ <R extends ResultMaybeAsync<any, any>>(result: R): true extends HasPromise<R> ? Promise<InferFailure<R>> : InferFailure<R>;
48
+ <R extends ResultMaybeAsync<any, any>, T>(result: R, defaultValue: T): true extends HasPromise<R> ? Promise<InferFailure<R> | T> : InferFailure<R> | T;
49
+ <R extends ResultMaybeAsync<any, any>>(): (result: R) => true extends HasPromise<R> ? Promise<InferFailure<R>> : InferFailure<R>;
50
+ <R extends ResultMaybeAsync<any, any>, T>(defaultValue: T): (result: R) => true extends HasPromise<R> ? Promise<InferFailure<R> | T> : InferFailure<R> | T;
51
+ };
@@ -27,14 +27,35 @@ __webpack_require__.d(__webpack_exports__, {
27
27
  unwrap: ()=>unwrap
28
28
  });
29
29
  const external_is_failure_cjs_namespaceObject = require("./is-failure.cjs");
30
+ const external_is_result_cjs_namespaceObject = require("./is-result.cjs");
30
31
  const is_promise_cjs_namespaceObject = require("../internals/helpers/is-promise.cjs");
31
- const unwrap = (result)=>{
32
- if ((0, is_promise_cjs_namespaceObject.isPromise)(result)) return result.then((r)=>{
33
- if ((0, external_is_failure_cjs_namespaceObject.isFailure)(r)) throw r.error;
34
- return r.value;
35
- });
36
- if ((0, external_is_failure_cjs_namespaceObject.isFailure)(result)) throw result.error;
37
- return result.value;
32
+ const unwrap = (...args)=>{
33
+ const firstArgument = args[0];
34
+ if ((0, external_is_result_cjs_namespaceObject.isResult)(firstArgument) || (0, is_promise_cjs_namespaceObject.isPromise)(firstArgument)) {
35
+ const result = firstArgument;
36
+ const hasDefault = 2 === args.length;
37
+ const defaultValue = hasDefault ? args[1] : void 0;
38
+ const apply = (r)=>{
39
+ if ((0, external_is_failure_cjs_namespaceObject.isFailure)(r)) {
40
+ if (hasDefault) return defaultValue;
41
+ throw new Error(String(r.error));
42
+ }
43
+ return r.value;
44
+ };
45
+ return (0, is_promise_cjs_namespaceObject.isPromise)(result) ? result.then(apply) : apply(result);
46
+ }
47
+ const hasDefault = 1 === args.length;
48
+ const defaultValue = hasDefault ? args[0] : void 0;
49
+ return (result)=>{
50
+ const apply = (r)=>{
51
+ if ((0, external_is_failure_cjs_namespaceObject.isFailure)(r)) {
52
+ if (hasDefault) return defaultValue;
53
+ throw new Error(String(r.error));
54
+ }
55
+ return r.value;
56
+ };
57
+ return (0, is_promise_cjs_namespaceObject.isPromise)(result) ? result.then(apply) : apply(result);
58
+ };
38
59
  };
39
60
  exports.unwrap = __webpack_exports__.unwrap;
40
61
  for(var __webpack_i__ in __webpack_exports__)if (-1 === [
@@ -1,49 +1,51 @@
1
1
  import type { HasPromise } from '../internals/types/has-promise';
2
2
  import type { InferSuccess, ResultMaybeAsync } from '../result';
3
3
  /**
4
- * Extracts the success value from a {@link Result}, {@link ResultAsync}.
4
+ * Extracts the success value from a {@link Result} or {@link ResultAsync}.
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
+ * If the input is a {@link Failure}, it will throw the error or return the default value if provided.
8
7
  *
9
8
  * @function
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.
13
- * @throws The error value if the result is a {@link Failure}.
9
+ * @typeParam R - The input {@link Result} or {@link ResultAsync}.
10
+ * @typeParam T - The default value type (optional).
14
11
  *
15
- * @example Success Case
12
+ * @example Success Case (without default)
16
13
  * ```ts
17
14
  * import { Result } from '@praha/byethrow';
18
15
  *
19
- * const result: Result.Result<number, string> = Result.succeed(100);
20
- * const value = Result.unwrap(result); // 100
16
+ * const result = Result.succeed(42);
17
+ * const value = Result.unwrap(result); // 42
21
18
  * ```
22
19
  *
23
- * @example Failure Case
20
+ * @example Success Case (with default)
24
21
  * ```ts
25
22
  * import { Result } from '@praha/byethrow';
26
23
  *
27
- * const result: Result.Result<number, string> = Result.fail('Oops');
28
- * const value = Result.unwrap(result); // throws 'Oops'
24
+ * const result = Result.succeed(42);
25
+ * const value = Result.unwrap(result, 0); // 42
29
26
  * ```
30
27
  *
31
- * @example Async Success Case
28
+ * @example Failure Case (without default) - throws error
32
29
  * ```ts
33
30
  * import { Result } from '@praha/byethrow';
34
31
  *
35
- * const result: Result.ResultAsync<number, string> = Result.succeed(Promise.resolve(100));
36
- * const value = await Result.unwrap(result); // 100
32
+ * const result = Result.fail('error');
33
+ * Result.unwrap(result); // throws 'error'
37
34
  * ```
38
35
  *
39
- * @example Async Failure Case
36
+ * @example Failure Case (with default)
40
37
  * ```ts
41
38
  * import { Result } from '@praha/byethrow';
42
39
  *
43
- * const result: Result.ResultAsync<number, string> = Result.fail(Promise.resolve('Oops'));
44
- * const value = await Result.unwrap(result); // throws 'Oops'
40
+ * const result = Result.fail('error');
41
+ * const value = Result.unwrap(result, 0); // 0
45
42
  * ```
46
43
  *
47
44
  * @category Unwraps
48
45
  */
49
- export declare const unwrap: <R extends ResultMaybeAsync<any, any>>(result: R) => true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>;
46
+ export declare const unwrap: {
47
+ <R extends ResultMaybeAsync<any, any>>(result: R): true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>;
48
+ <R extends ResultMaybeAsync<any, any>, T>(result: R, defaultValue: T): true extends HasPromise<R> ? Promise<InferSuccess<R> | T> : InferSuccess<R> | T;
49
+ <R extends ResultMaybeAsync<any, any>>(): (result: R) => true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>;
50
+ <R extends ResultMaybeAsync<any, any>, T>(defaultValue: T): (result: R) => true extends HasPromise<R> ? Promise<InferSuccess<R> | T> : InferSuccess<R> | T;
51
+ };
@@ -8,6 +8,7 @@ export * from './functions/fail';
8
8
  export * from './functions/inspect';
9
9
  export * from './functions/inspect-error';
10
10
  export * from './functions/is-failure';
11
+ export * from './functions/is-result';
11
12
  export * from './functions/is-success';
12
13
  export * from './functions/map';
13
14
  export * from './functions/map-error';
@@ -8,6 +8,7 @@ export * from "./functions/fail.js";
8
8
  export * from "./functions/inspect.js";
9
9
  export * from "./functions/inspect-error.js";
10
10
  export * from "./functions/is-failure.js";
11
+ export * from "./functions/is-result.js";
11
12
  export * from "./functions/is-success.js";
12
13
  export * from "./functions/map.js";
13
14
  export * from "./functions/map-error.js";
@@ -0,0 +1,24 @@
1
+ import type { Result } from '../result';
2
+ /**
3
+ * Type guard to check if a value is a {@link Result}.
4
+ *
5
+ * @function
6
+ * @typeParam T - The type of the success value.
7
+ * @typeParam E - The type of the error value.
8
+ * @param result - The value to check.
9
+ * @returns `true` if the value is a {@link Result}, otherwise `false`.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { Result } from '@praha/byethrow';
14
+ *
15
+ * const value: unknown = { type: 'Success', value: 42 };
16
+ * if (Result.isResult(value)) {
17
+ * // value is now typed as Result<unknown, unknown>
18
+ * console.log(value.type); // 'Success' or 'Failure'
19
+ * }
20
+ * ```
21
+ *
22
+ * @category Type Guards
23
+ */
24
+ export declare const isResult: <T, E>(result: unknown) => result is Result<T, E>;
@@ -0,0 +1,2 @@
1
+ const isResult = (result)=>'object' == typeof result && null !== result && 'type' in result && ('Success' === result.type && 'value' in result || 'Failure' === result.type && 'error' in result);
2
+ export { isResult };
@@ -1,15 +1,39 @@
1
1
  import type { Result, ResultAsync } from '../result';
2
2
  /**
3
- * Wraps a function execution (sync or async) in a {@link Result} or {@link ResultAsync} type,
3
+ * Wraps a function execution (sync or async) or a Promise in a {@link Result} or {@link ResultAsync} type,
4
4
  * capturing errors and returning them in a structured way.
5
5
  *
6
6
  * You can use either a custom `catch` handler or rely on the `safe: true` option
7
7
  * to assume the function cannot throw.
8
8
  *
9
9
  * @function
10
- * @typeParam T - The function type to execute (sync or async).
10
+ * @typeParam T - The function type to execute (sync or async) or a Promise type.
11
11
  * @typeParam E - The error type to return if `catch` is used.
12
12
  *
13
+ * @example Promise Try-Catch
14
+ * ```ts
15
+ * import { Result } from '@praha/byethrow';
16
+ *
17
+ * const result = Result.try({
18
+ * try: fetch('/api/data'),
19
+ * catch: (error) => new Error('Fetch failed', { cause: error }),
20
+ * });
21
+ *
22
+ * // result is ResultAsync<Response, Error>
23
+ * ```
24
+ *
25
+ * @example Promise Safe
26
+ * ```ts
27
+ * import { Result } from '@praha/byethrow';
28
+ *
29
+ * const result = Result.try({
30
+ * safe: true,
31
+ * try: Promise.resolve('ok'),
32
+ * });
33
+ *
34
+ * // result is ResultAsync<string, never>
35
+ * ```
36
+ *
13
37
  * @example Sync Try-Catch
14
38
  * ```ts
15
39
  * import { Result } from '@praha/byethrow';
@@ -37,7 +61,7 @@ import type { Result, ResultAsync } from '../result';
37
61
  * const result = fn(1); // Result.Result<number, never>
38
62
  * ```
39
63
  *
40
- * @example Async Try-Catch
64
+ * @example Async Function Try-Catch
41
65
  * ```ts
42
66
  * import { Result } from '@praha/byethrow';
43
67
  *
@@ -49,7 +73,7 @@ import type { Result, ResultAsync } from '../result';
49
73
  * const result = await fn('abc'); // Result.ResultAsync<Response, Error>
50
74
  * ```
51
75
  *
52
- * @example Async Safe
76
+ * @example Async Function Safe
53
77
  * ```ts
54
78
  * import { Result } from '@praha/byethrow';
55
79
  *
@@ -64,6 +88,14 @@ import type { Result, ResultAsync } from '../result';
64
88
  * @category Creators
65
89
  */
66
90
  declare const try_: {
91
+ <T extends Promise<any>, E>(options: {
92
+ try: T;
93
+ catch: (error: unknown) => E;
94
+ }): ResultAsync<Awaited<T>, E>;
95
+ <T extends Promise<any>>(options: {
96
+ try: T;
97
+ safe: true;
98
+ }): ResultAsync<Awaited<T>, never>;
67
99
  <T extends (...args: readonly any[]) => Promise<any>, E>(options: {
68
100
  try: T;
69
101
  catch: (error: unknown) => E;
@@ -1,7 +1,12 @@
1
1
  import { fail } from "./fail.js";
2
2
  import { succeed } from "./succeed.js";
3
3
  import { isPromise } from "../internals/helpers/is-promise.js";
4
- const try_ = (options)=>(...args)=>{
4
+ const try_ = (options)=>{
5
+ if (isPromise(options.try)) {
6
+ if ('safe' in options && options.safe) return succeed(options.try);
7
+ return options.try.then((value)=>succeed(value), (error)=>fail(options.catch(error)));
8
+ }
9
+ return (...args)=>{
5
10
  try {
6
11
  const output = options.try(...args);
7
12
  if (isPromise(output)) {
@@ -15,4 +20,5 @@ const try_ = (options)=>(...args)=>{
15
20
  return fail(options.catch(error));
16
21
  }
17
22
  };
23
+ };
18
24
  export { try_ as try };
@@ -1,18 +1,15 @@
1
1
  import type { HasPromise } from '../internals/types/has-promise';
2
2
  import type { InferFailure, ResultMaybeAsync } from '../result';
3
3
  /**
4
- * Extracts the error value from a {@link Result}, {@link ResultAsync}.
4
+ * Extracts the error value from a {@link Result} or {@link ResultAsync}.
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
+ * If the input is a {@link Success}, it will throw the success value or return the default value if provided.
8
7
  *
9
8
  * @function
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.
13
- * @throws The success value if the result is a {@link Success}.
9
+ * @typeParam R - The input {@link Result} or {@link ResultAsync}.
10
+ * @typeParam T - The default value type (optional).
14
11
  *
15
- * @example Failure Case
12
+ * @example Failure Case (without default)
16
13
  * ```ts
17
14
  * import { Result } from '@praha/byethrow';
18
15
  *
@@ -20,30 +17,35 @@ import type { InferFailure, ResultMaybeAsync } from '../result';
20
17
  * const error = Result.unwrapError(result); // 'Oops'
21
18
  * ```
22
19
  *
23
- * @example Success Case
20
+ * @example Failure Case (with default)
24
21
  * ```ts
25
22
  * import { Result } from '@praha/byethrow';
26
23
  *
27
- * const result: Result.Result<number, string> = Result.succeed(100);
28
- * const error = Result.unwrapError(result); // throws 100
24
+ * const result: Result.Result<number, string> = Result.fail('Oops');
25
+ * const error = Result.unwrapError(result, 'default'); // 'Oops'
29
26
  * ```
30
27
  *
31
- * @example Async Failure Case
28
+ * @example Success Case (without default)
32
29
  * ```ts
33
30
  * import { Result } from '@praha/byethrow';
34
31
  *
35
- * const result: Result.Result<number, string> = Result,fail(Promise.resolve('Oops'));
36
- * const error = Result.unwrapError(result); // 'Oops'
32
+ * const result: Result.Result<number, string> = Result.succeed(100);
33
+ * const error = Result.unwrapError(result); // throws 100
37
34
  * ```
38
35
  *
39
- * @example Async Success Case
36
+ * @example Success Case (with default)
40
37
  * ```ts
41
38
  * import { Result } from '@praha/byethrow';
42
39
  *
43
- * const result: Result.Result<number, string> = Result.succeed(Promise.resolve(100));
44
- * const error = Result.unwrapError(result); // throws 100
40
+ * const result: Result.Result<number, string> = Result.succeed(100);
41
+ * const error = Result.unwrapError(result, 0); // 0
45
42
  * ```
46
43
  *
47
44
  * @category Unwraps
48
45
  */
49
- export declare const unwrapError: <R extends ResultMaybeAsync<any, any>>(result: R) => true extends HasPromise<R> ? Promise<InferFailure<R>> : InferFailure<R>;
46
+ export declare const unwrapError: {
47
+ <R extends ResultMaybeAsync<any, any>>(result: R): true extends HasPromise<R> ? Promise<InferFailure<R>> : InferFailure<R>;
48
+ <R extends ResultMaybeAsync<any, any>, T>(result: R, defaultValue: T): true extends HasPromise<R> ? Promise<InferFailure<R> | T> : InferFailure<R> | T;
49
+ <R extends ResultMaybeAsync<any, any>>(): (result: R) => true extends HasPromise<R> ? Promise<InferFailure<R>> : InferFailure<R>;
50
+ <R extends ResultMaybeAsync<any, any>, T>(defaultValue: T): (result: R) => true extends HasPromise<R> ? Promise<InferFailure<R> | T> : InferFailure<R> | T;
51
+ };
@@ -1,11 +1,32 @@
1
+ import { isResult } from "./is-result.js";
1
2
  import { isSuccess } from "./is-success.js";
2
3
  import { isPromise } from "../internals/helpers/is-promise.js";
3
- const unwrapError = (result)=>{
4
- if (isPromise(result)) return result.then((r)=>{
5
- if (isSuccess(r)) throw r.value;
6
- return r.error;
7
- });
8
- if (isSuccess(result)) throw result.value;
9
- return result.error;
4
+ const unwrapError = (...args)=>{
5
+ const firstArgument = args[0];
6
+ if (isResult(firstArgument) || isPromise(firstArgument)) {
7
+ const result = firstArgument;
8
+ const hasDefault = 2 === args.length;
9
+ const defaultValue = hasDefault ? args[1] : void 0;
10
+ const apply = (r)=>{
11
+ if (isSuccess(r)) {
12
+ if (hasDefault) return defaultValue;
13
+ throw new Error(String(r.value));
14
+ }
15
+ return r.error;
16
+ };
17
+ return isPromise(result) ? result.then(apply) : apply(result);
18
+ }
19
+ const hasDefault = 1 === args.length;
20
+ const defaultValue = hasDefault ? args[0] : void 0;
21
+ return (result)=>{
22
+ const apply = (r)=>{
23
+ if (isSuccess(r)) {
24
+ if (hasDefault) return defaultValue;
25
+ throw new Error(String(r.value));
26
+ }
27
+ return r.error;
28
+ };
29
+ return isPromise(result) ? result.then(apply) : apply(result);
30
+ };
10
31
  };
11
32
  export { unwrapError };
@@ -1,49 +1,51 @@
1
1
  import type { HasPromise } from '../internals/types/has-promise';
2
2
  import type { InferSuccess, ResultMaybeAsync } from '../result';
3
3
  /**
4
- * Extracts the success value from a {@link Result}, {@link ResultAsync}.
4
+ * Extracts the success value from a {@link Result} or {@link ResultAsync}.
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
+ * If the input is a {@link Failure}, it will throw the error or return the default value if provided.
8
7
  *
9
8
  * @function
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.
13
- * @throws The error value if the result is a {@link Failure}.
9
+ * @typeParam R - The input {@link Result} or {@link ResultAsync}.
10
+ * @typeParam T - The default value type (optional).
14
11
  *
15
- * @example Success Case
12
+ * @example Success Case (without default)
16
13
  * ```ts
17
14
  * import { Result } from '@praha/byethrow';
18
15
  *
19
- * const result: Result.Result<number, string> = Result.succeed(100);
20
- * const value = Result.unwrap(result); // 100
16
+ * const result = Result.succeed(42);
17
+ * const value = Result.unwrap(result); // 42
21
18
  * ```
22
19
  *
23
- * @example Failure Case
20
+ * @example Success Case (with default)
24
21
  * ```ts
25
22
  * import { Result } from '@praha/byethrow';
26
23
  *
27
- * const result: Result.Result<number, string> = Result.fail('Oops');
28
- * const value = Result.unwrap(result); // throws 'Oops'
24
+ * const result = Result.succeed(42);
25
+ * const value = Result.unwrap(result, 0); // 42
29
26
  * ```
30
27
  *
31
- * @example Async Success Case
28
+ * @example Failure Case (without default) - throws error
32
29
  * ```ts
33
30
  * import { Result } from '@praha/byethrow';
34
31
  *
35
- * const result: Result.ResultAsync<number, string> = Result.succeed(Promise.resolve(100));
36
- * const value = await Result.unwrap(result); // 100
32
+ * const result = Result.fail('error');
33
+ * Result.unwrap(result); // throws 'error'
37
34
  * ```
38
35
  *
39
- * @example Async Failure Case
36
+ * @example Failure Case (with default)
40
37
  * ```ts
41
38
  * import { Result } from '@praha/byethrow';
42
39
  *
43
- * const result: Result.ResultAsync<number, string> = Result.fail(Promise.resolve('Oops'));
44
- * const value = await Result.unwrap(result); // throws 'Oops'
40
+ * const result = Result.fail('error');
41
+ * const value = Result.unwrap(result, 0); // 0
45
42
  * ```
46
43
  *
47
44
  * @category Unwraps
48
45
  */
49
- export declare const unwrap: <R extends ResultMaybeAsync<any, any>>(result: R) => true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>;
46
+ export declare const unwrap: {
47
+ <R extends ResultMaybeAsync<any, any>>(result: R): true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>;
48
+ <R extends ResultMaybeAsync<any, any>, T>(result: R, defaultValue: T): true extends HasPromise<R> ? Promise<InferSuccess<R> | T> : InferSuccess<R> | T;
49
+ <R extends ResultMaybeAsync<any, any>>(): (result: R) => true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>;
50
+ <R extends ResultMaybeAsync<any, any>, T>(defaultValue: T): (result: R) => true extends HasPromise<R> ? Promise<InferSuccess<R> | T> : InferSuccess<R> | T;
51
+ };
@@ -1,11 +1,32 @@
1
1
  import { isFailure } from "./is-failure.js";
2
+ import { isResult } from "./is-result.js";
2
3
  import { isPromise } from "../internals/helpers/is-promise.js";
3
- const unwrap = (result)=>{
4
- if (isPromise(result)) return result.then((r)=>{
5
- if (isFailure(r)) throw r.error;
6
- return r.value;
7
- });
8
- if (isFailure(result)) throw result.error;
9
- return result.value;
4
+ const unwrap = (...args)=>{
5
+ const firstArgument = args[0];
6
+ if (isResult(firstArgument) || isPromise(firstArgument)) {
7
+ const result = firstArgument;
8
+ const hasDefault = 2 === args.length;
9
+ const defaultValue = hasDefault ? args[1] : void 0;
10
+ const apply = (r)=>{
11
+ if (isFailure(r)) {
12
+ if (hasDefault) return defaultValue;
13
+ throw new Error(String(r.error));
14
+ }
15
+ return r.value;
16
+ };
17
+ return isPromise(result) ? result.then(apply) : apply(result);
18
+ }
19
+ const hasDefault = 1 === args.length;
20
+ const defaultValue = hasDefault ? args[0] : void 0;
21
+ return (result)=>{
22
+ const apply = (r)=>{
23
+ if (isFailure(r)) {
24
+ if (hasDefault) return defaultValue;
25
+ throw new Error(String(r.error));
26
+ }
27
+ return r.value;
28
+ };
29
+ return isPromise(result) ? result.then(apply) : apply(result);
30
+ };
10
31
  };
11
32
  export { unwrap };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@praha/byethrow",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "description": "A lightweight, tree-shakable Result type package with a simple, consistent API designed",
5
5
  "keywords": [
6
6
  "javascript",
@@ -41,9 +41,9 @@
41
41
  "README.md"
42
42
  ],
43
43
  "devDependencies": {
44
- "@rslib/core": "0.10.2",
45
- "eslint": "9.29.0",
46
- "typedoc": "0.28.5",
44
+ "@rslib/core": "0.10.4",
45
+ "eslint": "9.30.1",
46
+ "typedoc": "0.28.7",
47
47
  "typescript": "5.8.3",
48
48
  "vitest": "3.2.4"
49
49
  },