@praha/byethrow 0.2.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/exports.cjs +57 -30
- package/dist/cjs/exports.d.ts +3 -0
- package/dist/cjs/functions/and-through.cjs +1 -1
- package/dist/cjs/functions/inspect-error.cjs +44 -0
- package/dist/cjs/functions/inspect-error.d.ts +42 -0
- package/dist/cjs/functions/inspect.cjs +44 -0
- package/dist/cjs/functions/inspect.d.ts +42 -0
- package/dist/cjs/functions/is-result.cjs +36 -0
- package/dist/cjs/functions/is-result.d.ts +24 -0
- package/dist/cjs/functions/unwrap-error.cjs +2 -1
- package/dist/cjs/functions/unwrap.cjs +2 -1
- package/dist/esm/exports.d.ts +3 -0
- package/dist/esm/exports.js +3 -0
- package/dist/esm/functions/and-through.js +1 -1
- package/dist/esm/functions/inspect-error.d.ts +42 -0
- package/dist/esm/functions/inspect-error.js +10 -0
- package/dist/esm/functions/inspect.d.ts +42 -0
- package/dist/esm/functions/inspect.js +10 -0
- package/dist/esm/functions/is-result.d.ts +24 -0
- package/dist/esm/functions/is-result.js +2 -0
- package/dist/esm/functions/unwrap-error.js +2 -1
- package/dist/esm/functions/unwrap.js +2 -1
- package/package.json +4 -4
package/dist/cjs/exports.cjs
CHANGED
@@ -18,9 +18,18 @@ 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
|
},
|
30
|
+
"./functions/is-result": function(module) {
|
31
|
+
module.exports = require("./functions/is-result.cjs");
|
32
|
+
},
|
24
33
|
"./functions/is-success": function(module) {
|
25
34
|
module.exports = require("./functions/is-success.cjs");
|
26
35
|
},
|
@@ -137,64 +146,82 @@ var __webpack_exports__ = {};
|
|
137
146
|
return _functions_fail__WEBPACK_IMPORTED_MODULE_6__[key];
|
138
147
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
139
148
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
140
|
-
var
|
149
|
+
var _functions_inspect__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__("./functions/inspect");
|
150
|
+
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
151
|
+
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) {
|
152
|
+
return _functions_inspect__WEBPACK_IMPORTED_MODULE_7__[key];
|
153
|
+
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
154
|
+
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
155
|
+
var _functions_inspect_error__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__("./functions/inspect-error");
|
156
|
+
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
157
|
+
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) {
|
158
|
+
return _functions_inspect_error__WEBPACK_IMPORTED_MODULE_8__[key];
|
159
|
+
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
160
|
+
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
161
|
+
var _functions_is_failure__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__("./functions/is-failure");
|
162
|
+
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
163
|
+
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) {
|
164
|
+
return _functions_is_failure__WEBPACK_IMPORTED_MODULE_9__[key];
|
165
|
+
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
166
|
+
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
167
|
+
var _functions_is_result__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__("./functions/is-result");
|
141
168
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
142
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
143
|
-
return
|
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];
|
144
171
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
145
172
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
146
|
-
var
|
173
|
+
var _functions_is_success__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__("./functions/is-success");
|
147
174
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
148
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
149
|
-
return
|
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];
|
150
177
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
151
178
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
152
|
-
var
|
179
|
+
var _functions_map__WEBPACK_IMPORTED_MODULE_12__ = __webpack_require__("./functions/map");
|
153
180
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
154
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
155
|
-
return
|
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];
|
156
183
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
157
184
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
158
|
-
var
|
185
|
+
var _functions_map_error__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__("./functions/map-error");
|
159
186
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
160
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
161
|
-
return
|
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];
|
162
189
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
163
190
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
164
|
-
var
|
191
|
+
var _functions_or_else__WEBPACK_IMPORTED_MODULE_14__ = __webpack_require__("./functions/or-else");
|
165
192
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
166
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
167
|
-
return
|
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];
|
168
195
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
169
196
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
170
|
-
var
|
197
|
+
var _functions_pipe__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__("./functions/pipe");
|
171
198
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
172
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
173
|
-
return
|
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];
|
174
201
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
175
202
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
176
|
-
var
|
203
|
+
var _functions_succeed__WEBPACK_IMPORTED_MODULE_16__ = __webpack_require__("./functions/succeed");
|
177
204
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
178
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
179
|
-
return
|
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];
|
180
207
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
181
208
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
182
|
-
var
|
209
|
+
var _functions_try__WEBPACK_IMPORTED_MODULE_17__ = __webpack_require__("./functions/try");
|
183
210
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
184
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
185
|
-
return
|
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];
|
186
213
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
187
214
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
188
|
-
var
|
215
|
+
var _functions_unwrap__WEBPACK_IMPORTED_MODULE_18__ = __webpack_require__("./functions/unwrap");
|
189
216
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
190
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
191
|
-
return
|
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];
|
192
219
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
193
220
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
194
|
-
var
|
221
|
+
var _functions_unwrap_error__WEBPACK_IMPORTED_MODULE_19__ = __webpack_require__("./functions/unwrap-error");
|
195
222
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
196
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
197
|
-
return
|
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];
|
198
225
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
199
226
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
200
227
|
})();
|
package/dist/cjs/exports.d.ts
CHANGED
@@ -5,7 +5,10 @@ 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';
|
11
|
+
export * from './functions/is-result';
|
9
12
|
export * from './functions/is-success';
|
10
13
|
export * from './functions/map';
|
11
14
|
export * from './functions/map-error';
|
@@ -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 (
|
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
|
+
};
|
@@ -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>;
|
@@ -27,8 +27,9 @@ __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)=>{
|
31
|
-
if (
|
32
|
+
if ((0, is_promise_cjs_namespaceObject.isPromise)(result)) return result.then((r)=>{
|
32
33
|
if ((0, external_is_success_cjs_namespaceObject.isSuccess)(r)) throw r.value;
|
33
34
|
return r.error;
|
34
35
|
});
|
@@ -27,8 +27,9 @@ __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)=>{
|
31
|
-
if (
|
32
|
+
if ((0, is_promise_cjs_namespaceObject.isPromise)(result)) return result.then((r)=>{
|
32
33
|
if ((0, external_is_failure_cjs_namespaceObject.isFailure)(r)) throw r.error;
|
33
34
|
return r.value;
|
34
35
|
});
|
package/dist/esm/exports.d.ts
CHANGED
@@ -5,7 +5,10 @@ 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';
|
11
|
+
export * from './functions/is-result';
|
9
12
|
export * from './functions/is-success';
|
10
13
|
export * from './functions/map';
|
11
14
|
export * from './functions/map-error';
|
package/dist/esm/exports.js
CHANGED
@@ -5,7 +5,10 @@ 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";
|
11
|
+
export * from "./functions/is-result.js";
|
9
12
|
export * from "./functions/is-success.js";
|
10
13
|
export * from "./functions/map.js";
|
11
14
|
export * from "./functions/map-error.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
|
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 };
|
@@ -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>;
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import { isSuccess } from "./is-success.js";
|
2
|
+
import { isPromise } from "../internals/helpers/is-promise.js";
|
2
3
|
const unwrapError = (result)=>{
|
3
|
-
if (result
|
4
|
+
if (isPromise(result)) return result.then((r)=>{
|
4
5
|
if (isSuccess(r)) throw r.value;
|
5
6
|
return r.error;
|
6
7
|
});
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import { isFailure } from "./is-failure.js";
|
2
|
+
import { isPromise } from "../internals/helpers/is-promise.js";
|
2
3
|
const unwrap = (result)=>{
|
3
|
-
if (result
|
4
|
+
if (isPromise(result)) return result.then((r)=>{
|
4
5
|
if (isFailure(r)) throw r.error;
|
5
6
|
return r.value;
|
6
7
|
});
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@praha/byethrow",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.4.0",
|
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.
|
45
|
-
"eslint": "9.
|
46
|
-
"typedoc": "0.28.
|
44
|
+
"@rslib/core": "0.10.4",
|
45
|
+
"eslint": "9.30.0",
|
46
|
+
"typedoc": "0.28.7",
|
47
47
|
"typescript": "5.8.3",
|
48
48
|
"vitest": "3.2.4"
|
49
49
|
},
|