@praha/byethrow 0.4.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.
@@ -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
+ };
@@ -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.4.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",
@@ -42,7 +42,7 @@
42
42
  ],
43
43
  "devDependencies": {
44
44
  "@rslib/core": "0.10.4",
45
- "eslint": "9.30.0",
45
+ "eslint": "9.30.1",
46
46
  "typedoc": "0.28.7",
47
47
  "typescript": "5.8.3",
48
48
  "vitest": "3.2.4"