@praha/byethrow 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,6 +18,12 @@ var __webpack_modules__ = {
18
18
  "./functions/fail": function(module) {
19
19
  module.exports = require("./functions/fail.cjs");
20
20
  },
21
+ "./functions/inspect-error": function(module) {
22
+ module.exports = require("./functions/inspect-error.cjs");
23
+ },
24
+ "./functions/inspect": function(module) {
25
+ module.exports = require("./functions/inspect.cjs");
26
+ },
21
27
  "./functions/is-failure": function(module) {
22
28
  module.exports = require("./functions/is-failure.cjs");
23
29
  },
@@ -137,64 +143,76 @@ var __webpack_exports__ = {};
137
143
  return _functions_fail__WEBPACK_IMPORTED_MODULE_6__[key];
138
144
  }).bind(0, __WEBPACK_IMPORT_KEY__);
139
145
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
140
- var _functions_is_failure__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__("./functions/is-failure");
146
+ var _functions_inspect__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__("./functions/inspect");
147
+ var __WEBPACK_REEXPORT_OBJECT__ = {};
148
+ for(var __WEBPACK_IMPORT_KEY__ in _functions_inspect__WEBPACK_IMPORTED_MODULE_7__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
149
+ return _functions_inspect__WEBPACK_IMPORTED_MODULE_7__[key];
150
+ }).bind(0, __WEBPACK_IMPORT_KEY__);
151
+ __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
152
+ var _functions_inspect_error__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__("./functions/inspect-error");
153
+ var __WEBPACK_REEXPORT_OBJECT__ = {};
154
+ for(var __WEBPACK_IMPORT_KEY__ in _functions_inspect_error__WEBPACK_IMPORTED_MODULE_8__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
155
+ return _functions_inspect_error__WEBPACK_IMPORTED_MODULE_8__[key];
156
+ }).bind(0, __WEBPACK_IMPORT_KEY__);
157
+ __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
158
+ var _functions_is_failure__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__("./functions/is-failure");
141
159
  var __WEBPACK_REEXPORT_OBJECT__ = {};
142
- for(var __WEBPACK_IMPORT_KEY__ in _functions_is_failure__WEBPACK_IMPORTED_MODULE_7__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
143
- return _functions_is_failure__WEBPACK_IMPORTED_MODULE_7__[key];
160
+ for(var __WEBPACK_IMPORT_KEY__ in _functions_is_failure__WEBPACK_IMPORTED_MODULE_9__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
161
+ return _functions_is_failure__WEBPACK_IMPORTED_MODULE_9__[key];
144
162
  }).bind(0, __WEBPACK_IMPORT_KEY__);
145
163
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
146
- var _functions_is_success__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__("./functions/is-success");
164
+ var _functions_is_success__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__("./functions/is-success");
147
165
  var __WEBPACK_REEXPORT_OBJECT__ = {};
148
- for(var __WEBPACK_IMPORT_KEY__ in _functions_is_success__WEBPACK_IMPORTED_MODULE_8__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
149
- return _functions_is_success__WEBPACK_IMPORTED_MODULE_8__[key];
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];
150
168
  }).bind(0, __WEBPACK_IMPORT_KEY__);
151
169
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
152
- var _functions_map__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__("./functions/map");
170
+ var _functions_map__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__("./functions/map");
153
171
  var __WEBPACK_REEXPORT_OBJECT__ = {};
154
- for(var __WEBPACK_IMPORT_KEY__ in _functions_map__WEBPACK_IMPORTED_MODULE_9__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
155
- return _functions_map__WEBPACK_IMPORTED_MODULE_9__[key];
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];
156
174
  }).bind(0, __WEBPACK_IMPORT_KEY__);
157
175
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
158
- var _functions_map_error__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__("./functions/map-error");
176
+ var _functions_map_error__WEBPACK_IMPORTED_MODULE_12__ = __webpack_require__("./functions/map-error");
159
177
  var __WEBPACK_REEXPORT_OBJECT__ = {};
160
- for(var __WEBPACK_IMPORT_KEY__ in _functions_map_error__WEBPACK_IMPORTED_MODULE_10__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
161
- return _functions_map_error__WEBPACK_IMPORTED_MODULE_10__[key];
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];
162
180
  }).bind(0, __WEBPACK_IMPORT_KEY__);
163
181
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
164
- var _functions_or_else__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__("./functions/or-else");
182
+ var _functions_or_else__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__("./functions/or-else");
165
183
  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];
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];
168
186
  }).bind(0, __WEBPACK_IMPORT_KEY__);
169
187
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
170
- var _functions_pipe__WEBPACK_IMPORTED_MODULE_12__ = __webpack_require__("./functions/pipe");
188
+ var _functions_pipe__WEBPACK_IMPORTED_MODULE_14__ = __webpack_require__("./functions/pipe");
171
189
  var __WEBPACK_REEXPORT_OBJECT__ = {};
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];
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];
174
192
  }).bind(0, __WEBPACK_IMPORT_KEY__);
175
193
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
176
- var _functions_succeed__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__("./functions/succeed");
194
+ var _functions_succeed__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__("./functions/succeed");
177
195
  var __WEBPACK_REEXPORT_OBJECT__ = {};
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];
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];
180
198
  }).bind(0, __WEBPACK_IMPORT_KEY__);
181
199
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
182
- var _functions_try__WEBPACK_IMPORTED_MODULE_14__ = __webpack_require__("./functions/try");
200
+ var _functions_try__WEBPACK_IMPORTED_MODULE_16__ = __webpack_require__("./functions/try");
183
201
  var __WEBPACK_REEXPORT_OBJECT__ = {};
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];
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];
186
204
  }).bind(0, __WEBPACK_IMPORT_KEY__);
187
205
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
188
- var _functions_unwrap__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__("./functions/unwrap");
206
+ var _functions_unwrap__WEBPACK_IMPORTED_MODULE_17__ = __webpack_require__("./functions/unwrap");
189
207
  var __WEBPACK_REEXPORT_OBJECT__ = {};
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];
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];
192
210
  }).bind(0, __WEBPACK_IMPORT_KEY__);
193
211
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
194
- var _functions_unwrap_error__WEBPACK_IMPORTED_MODULE_16__ = __webpack_require__("./functions/unwrap-error");
212
+ var _functions_unwrap_error__WEBPACK_IMPORTED_MODULE_18__ = __webpack_require__("./functions/unwrap-error");
195
213
  var __WEBPACK_REEXPORT_OBJECT__ = {};
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];
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];
198
216
  }).bind(0, __WEBPACK_IMPORT_KEY__);
199
217
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
200
218
  })();
@@ -5,6 +5,8 @@ export * from './functions/bind';
5
5
  export * from './functions/combine';
6
6
  export * from './functions/do';
7
7
  export * from './functions/fail';
8
+ export * from './functions/inspect';
9
+ export * from './functions/inspect-error';
8
10
  export * from './functions/is-failure';
9
11
  export * from './functions/is-success';
10
12
  export * from './functions/map';
@@ -32,7 +32,7 @@ const andThrough = (fn)=>(result)=>{
32
32
  const apply = (r)=>{
33
33
  if ((0, external_is_failure_cjs_namespaceObject.isFailure)(r)) return r;
34
34
  const next = fn(r.value);
35
- if (next instanceof Promise) return next.then((n)=>{
35
+ if ((0, is_promise_cjs_namespaceObject.isPromise)(next)) return next.then((n)=>{
36
36
  if ((0, external_is_failure_cjs_namespaceObject.isFailure)(n)) return n;
37
37
  return r;
38
38
  });
@@ -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
+ inspectError: ()=>inspectError
28
+ });
29
+ const external_is_failure_cjs_namespaceObject = require("./is-failure.cjs");
30
+ const is_promise_cjs_namespaceObject = require("../internals/helpers/is-promise.cjs");
31
+ const inspectError = (fn)=>(result)=>{
32
+ const apply = (r)=>{
33
+ if ((0, external_is_failure_cjs_namespaceObject.isFailure)(r)) fn(r.error);
34
+ return r;
35
+ };
36
+ return (0, is_promise_cjs_namespaceObject.isPromise)(result) ? result.then(apply) : apply(result);
37
+ };
38
+ exports.inspectError = __webpack_exports__.inspectError;
39
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
40
+ "inspectError"
41
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
42
+ Object.defineProperty(exports, '__esModule', {
43
+ value: true
44
+ });
@@ -0,0 +1,42 @@
1
+ import type { InferFailure, ResultMaybeAsync } from '../result';
2
+ /**
3
+ * Executes a side effect function on the error value of a {@link Result} or {@link ResultAsync},
4
+ * without modifying the original result. This is useful for debugging, logging, or performing
5
+ * other side effects while maintaining the original value and error state.
6
+ *
7
+ * @function
8
+ * @typeParam R1 - The input {@link Result} or {@link ResultAsync}.
9
+ * @typeParam R2 - The return type of the inspection function.
10
+ *
11
+ * @example Failure Case
12
+ * ```ts
13
+ * import { Result } from '@praha/byethrow';
14
+ *
15
+ * const result = Result.pipe(
16
+ * Result.fail('error'),
17
+ * Result.inspectError((error) => console.log('Debug error:', error)),
18
+ * );
19
+ * // Console output: "Debug error: error"
20
+ * // Result: { type: 'Failure', error: 'error' }
21
+ * ```
22
+ *
23
+ * @example Success Case
24
+ * ```ts
25
+ * import { Result } from '@praha/byethrow';
26
+ *
27
+ * const result = Result.pipe(
28
+ * Result.succeed(42),
29
+ * Result.inspectError((error) => console.log('Debug error:', error)),
30
+ * );
31
+ * // No console output
32
+ * // Result: { type: 'Success', value: 42 }
33
+ * ```
34
+ *
35
+ * @see {@link pipe} - It is recommended to use this function with the {@link pipe} function for better readability and composability.
36
+ *
37
+ * @category Combinators
38
+ */
39
+ export declare const inspectError: {
40
+ <R1 extends ResultMaybeAsync<any, any>>(fn: (a: InferFailure<R1>) => unknown): (result: R1) => R1;
41
+ <F extends (a: any) => unknown>(fn: F): <R1 extends ResultMaybeAsync<any, Parameters<F>[0]>>(result: R1) => R1;
42
+ };
@@ -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
+ inspect: ()=>inspect
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 inspect = (fn)=>(result)=>{
32
+ const apply = (r)=>{
33
+ if ((0, external_is_success_cjs_namespaceObject.isSuccess)(r)) fn(r.value);
34
+ return r;
35
+ };
36
+ return (0, is_promise_cjs_namespaceObject.isPromise)(result) ? result.then(apply) : apply(result);
37
+ };
38
+ exports.inspect = __webpack_exports__.inspect;
39
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
40
+ "inspect"
41
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
42
+ Object.defineProperty(exports, '__esModule', {
43
+ value: true
44
+ });
@@ -0,0 +1,42 @@
1
+ import type { InferSuccess, ResultMaybeAsync } from '../result';
2
+ /**
3
+ * Executes a side effect function on the success value of a {@link Result} or {@link ResultAsync},
4
+ * without modifying the original result. This is useful for debugging, logging, or performing
5
+ * other side effects while maintaining the original value and error state.
6
+ *
7
+ * @function
8
+ * @typeParam R1 - The input {@link Result} or {@link ResultAsync}.
9
+ * @typeParam R2 - The return type of the inspection function.
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.inspect((value) => console.log('Debug:', value)),
18
+ * );
19
+ * // Console output: "Debug: 42"
20
+ * // Result: { type: 'Success', value: 42 }
21
+ * ```
22
+ *
23
+ * @example Failure Case
24
+ * ```ts
25
+ * import { Result } from '@praha/byethrow';
26
+ *
27
+ * const result = Result.pipe(
28
+ * Result.fail('error'),
29
+ * Result.inspect((value) => console.log('Debug:', value)),
30
+ * );
31
+ * // No console output
32
+ * // Result: { type: 'Failure', error: 'error' }
33
+ * ```
34
+ *
35
+ * @see {@link pipe} - It is recommended to use this function with the {@link pipe} function for better readability and composability.
36
+ *
37
+ * @category Combinators
38
+ */
39
+ export declare const inspect: {
40
+ <R1 extends ResultMaybeAsync<any, any>>(fn: (a: InferSuccess<R1>) => unknown): (result: R1) => R1;
41
+ <F extends (a: any) => unknown>(fn: F): <R1 extends ResultMaybeAsync<Parameters<F>[0], any>>(result: R1) => R1;
42
+ };
@@ -27,7 +27,12 @@ __webpack_require__.d(__webpack_exports__, {
27
27
  unwrapError: ()=>unwrapError
28
28
  });
29
29
  const external_is_success_cjs_namespaceObject = require("./is-success.cjs");
30
+ const is_promise_cjs_namespaceObject = require("../internals/helpers/is-promise.cjs");
30
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
+ });
31
36
  if ((0, external_is_success_cjs_namespaceObject.isSuccess)(result)) throw result.value;
32
37
  return result.error;
33
38
  };
@@ -1,31 +1,49 @@
1
- import type { Result } from '../result';
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 E - The type of the error value.
9
- * @param result - The {@link Result} to unwrap the error from.
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> = { type: 'Failure', error: 'Something went wrong' };
18
- * const error = Result.unwrapError(result); // 'Something went wrong'
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> = { type: 'Success', value: 123 };
26
- * const error = Result.unwrapError(result); // throws 123
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: <E>(result: Result<unknown, E>) => E;
49
+ export declare const unwrapError: <R extends ResultMaybeAsync<any, any>>(result: R) => true extends HasPromise<R> ? Promise<InferFailure<R>> : InferFailure<R>;
@@ -27,7 +27,12 @@ __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 is_promise_cjs_namespaceObject = require("../internals/helpers/is-promise.cjs");
30
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
+ });
31
36
  if ((0, external_is_failure_cjs_namespaceObject.isFailure)(result)) throw result.error;
32
37
  return result.value;
33
38
  };
@@ -1,20 +1,22 @@
1
- import type { Result } from '../result';
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 T - The type of the success value.
9
- * @param result - The {@link Result} to unwrap.
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> = { type: 'Success', value: 100 };
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> = { type: 'Failure', error: 'Oops' };
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: <T>(result: Result<T, unknown>) => T;
49
+ export declare const unwrap: <R extends ResultMaybeAsync<any, any>>(result: R) => true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>;
@@ -5,6 +5,8 @@ export * from './functions/bind';
5
5
  export * from './functions/combine';
6
6
  export * from './functions/do';
7
7
  export * from './functions/fail';
8
+ export * from './functions/inspect';
9
+ export * from './functions/inspect-error';
8
10
  export * from './functions/is-failure';
9
11
  export * from './functions/is-success';
10
12
  export * from './functions/map';
@@ -5,6 +5,8 @@ export * from "./functions/bind.js";
5
5
  export * from "./functions/combine.js";
6
6
  export * from "./functions/do.js";
7
7
  export * from "./functions/fail.js";
8
+ export * from "./functions/inspect.js";
9
+ export * from "./functions/inspect-error.js";
8
10
  export * from "./functions/is-failure.js";
9
11
  export * from "./functions/is-success.js";
10
12
  export * from "./functions/map.js";
@@ -4,7 +4,7 @@ const andThrough = (fn)=>(result)=>{
4
4
  const apply = (r)=>{
5
5
  if (isFailure(r)) return r;
6
6
  const next = fn(r.value);
7
- if (next instanceof Promise) return next.then((n)=>{
7
+ if (isPromise(next)) return next.then((n)=>{
8
8
  if (isFailure(n)) return n;
9
9
  return r;
10
10
  });
@@ -0,0 +1,42 @@
1
+ import type { InferFailure, ResultMaybeAsync } from '../result';
2
+ /**
3
+ * Executes a side effect function on the error value of a {@link Result} or {@link ResultAsync},
4
+ * without modifying the original result. This is useful for debugging, logging, or performing
5
+ * other side effects while maintaining the original value and error state.
6
+ *
7
+ * @function
8
+ * @typeParam R1 - The input {@link Result} or {@link ResultAsync}.
9
+ * @typeParam R2 - The return type of the inspection function.
10
+ *
11
+ * @example Failure Case
12
+ * ```ts
13
+ * import { Result } from '@praha/byethrow';
14
+ *
15
+ * const result = Result.pipe(
16
+ * Result.fail('error'),
17
+ * Result.inspectError((error) => console.log('Debug error:', error)),
18
+ * );
19
+ * // Console output: "Debug error: error"
20
+ * // Result: { type: 'Failure', error: 'error' }
21
+ * ```
22
+ *
23
+ * @example Success Case
24
+ * ```ts
25
+ * import { Result } from '@praha/byethrow';
26
+ *
27
+ * const result = Result.pipe(
28
+ * Result.succeed(42),
29
+ * Result.inspectError((error) => console.log('Debug error:', error)),
30
+ * );
31
+ * // No console output
32
+ * // Result: { type: 'Success', value: 42 }
33
+ * ```
34
+ *
35
+ * @see {@link pipe} - It is recommended to use this function with the {@link pipe} function for better readability and composability.
36
+ *
37
+ * @category Combinators
38
+ */
39
+ export declare const inspectError: {
40
+ <R1 extends ResultMaybeAsync<any, any>>(fn: (a: InferFailure<R1>) => unknown): (result: R1) => R1;
41
+ <F extends (a: any) => unknown>(fn: F): <R1 extends ResultMaybeAsync<any, Parameters<F>[0]>>(result: R1) => R1;
42
+ };
@@ -0,0 +1,10 @@
1
+ import { isFailure } from "./is-failure.js";
2
+ import { isPromise } from "../internals/helpers/is-promise.js";
3
+ const inspectError = (fn)=>(result)=>{
4
+ const apply = (r)=>{
5
+ if (isFailure(r)) fn(r.error);
6
+ return r;
7
+ };
8
+ return isPromise(result) ? result.then(apply) : apply(result);
9
+ };
10
+ export { inspectError };
@@ -0,0 +1,42 @@
1
+ import type { InferSuccess, ResultMaybeAsync } from '../result';
2
+ /**
3
+ * Executes a side effect function on the success value of a {@link Result} or {@link ResultAsync},
4
+ * without modifying the original result. This is useful for debugging, logging, or performing
5
+ * other side effects while maintaining the original value and error state.
6
+ *
7
+ * @function
8
+ * @typeParam R1 - The input {@link Result} or {@link ResultAsync}.
9
+ * @typeParam R2 - The return type of the inspection function.
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.inspect((value) => console.log('Debug:', value)),
18
+ * );
19
+ * // Console output: "Debug: 42"
20
+ * // Result: { type: 'Success', value: 42 }
21
+ * ```
22
+ *
23
+ * @example Failure Case
24
+ * ```ts
25
+ * import { Result } from '@praha/byethrow';
26
+ *
27
+ * const result = Result.pipe(
28
+ * Result.fail('error'),
29
+ * Result.inspect((value) => console.log('Debug:', value)),
30
+ * );
31
+ * // No console output
32
+ * // Result: { type: 'Failure', error: 'error' }
33
+ * ```
34
+ *
35
+ * @see {@link pipe} - It is recommended to use this function with the {@link pipe} function for better readability and composability.
36
+ *
37
+ * @category Combinators
38
+ */
39
+ export declare const inspect: {
40
+ <R1 extends ResultMaybeAsync<any, any>>(fn: (a: InferSuccess<R1>) => unknown): (result: R1) => R1;
41
+ <F extends (a: any) => unknown>(fn: F): <R1 extends ResultMaybeAsync<Parameters<F>[0], any>>(result: R1) => R1;
42
+ };
@@ -0,0 +1,10 @@
1
+ import { isSuccess } from "./is-success.js";
2
+ import { isPromise } from "../internals/helpers/is-promise.js";
3
+ const inspect = (fn)=>(result)=>{
4
+ const apply = (r)=>{
5
+ if (isSuccess(r)) fn(r.value);
6
+ return r;
7
+ };
8
+ return isPromise(result) ? result.then(apply) : apply(result);
9
+ };
10
+ export { inspect };
@@ -1,31 +1,49 @@
1
- import type { Result } from '../result';
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 E - The type of the error value.
9
- * @param result - The {@link Result} to unwrap the error from.
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> = { type: 'Failure', error: 'Something went wrong' };
18
- * const error = Result.unwrapError(result); // 'Something went wrong'
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> = { type: 'Success', value: 123 };
26
- * const error = Result.unwrapError(result); // throws 123
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: <E>(result: Result<unknown, E>) => E;
49
+ export declare const unwrapError: <R extends ResultMaybeAsync<any, any>>(result: R) => true extends HasPromise<R> ? Promise<InferFailure<R>> : InferFailure<R>;
@@ -1,5 +1,10 @@
1
1
  import { isSuccess } from "./is-success.js";
2
+ import { isPromise } from "../internals/helpers/is-promise.js";
2
3
  const unwrapError = (result)=>{
4
+ if (isPromise(result)) return result.then((r)=>{
5
+ if (isSuccess(r)) throw r.value;
6
+ return r.error;
7
+ });
3
8
  if (isSuccess(result)) throw result.value;
4
9
  return result.error;
5
10
  };
@@ -1,20 +1,22 @@
1
- import type { Result } from '../result';
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 T - The type of the success value.
9
- * @param result - The {@link Result} to unwrap.
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> = { type: 'Success', value: 100 };
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> = { type: 'Failure', error: 'Oops' };
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: <T>(result: Result<T, unknown>) => T;
49
+ export declare const unwrap: <R extends ResultMaybeAsync<any, any>>(result: R) => true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>;
@@ -1,5 +1,10 @@
1
1
  import { isFailure } from "./is-failure.js";
2
+ import { isPromise } from "../internals/helpers/is-promise.js";
2
3
  const unwrap = (result)=>{
4
+ if (isPromise(result)) return result.then((r)=>{
5
+ if (isFailure(r)) throw r.error;
6
+ return r.value;
7
+ });
3
8
  if (isFailure(result)) throw result.error;
4
9
  return result.value;
5
10
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@praha/byethrow",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
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.1",
44
+ "@rslib/core": "0.10.2",
45
45
  "eslint": "9.29.0",
46
46
  "typedoc": "0.28.5",
47
47
  "typescript": "5.8.3",