@praha/byethrow 0.0.1 → 0.1.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/README.md +1 -1
- package/dist/cjs/exports.cjs +42 -33
- package/dist/cjs/exports.d.ts +1 -0
- package/dist/cjs/functions/and-then.d.ts +2 -0
- package/dist/cjs/functions/and-through.d.ts +2 -0
- package/dist/cjs/functions/bind.d.ts +2 -0
- package/dist/cjs/functions/combine.cjs +89 -0
- package/dist/cjs/functions/combine.d.ts +54 -0
- package/dist/cjs/functions/fail.d.ts +3 -0
- package/dist/cjs/functions/map-error.d.ts +2 -0
- package/dist/cjs/functions/map.d.ts +2 -0
- package/dist/cjs/functions/succeed.d.ts +2 -0
- package/dist/esm/exports.d.ts +1 -0
- package/dist/esm/exports.js +1 -0
- package/dist/esm/functions/and-then.d.ts +2 -0
- package/dist/esm/functions/and-through.d.ts +2 -0
- package/dist/esm/functions/bind.d.ts +2 -0
- package/dist/esm/functions/combine.d.ts +54 -0
- package/dist/esm/functions/combine.js +55 -0
- package/dist/esm/functions/fail.d.ts +3 -0
- package/dist/esm/functions/map-error.d.ts +2 -0
- package/dist/esm/functions/map.d.ts +2 -0
- package/dist/esm/functions/succeed.d.ts +2 -0
- package/package.json +3 -3
package/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
[](https://www.npmjs.com/package/@praha/byethrow)
|
4
4
|
[](https://www.npmjs.com/package/@praha/byethrow)
|
5
|
-
[](https://github.com/praha-inc/byethrow/blob/main/LICENSE)
|
5
|
+
[](https://github.com/praha-inc/byethrow/blob/main/packages/byethrow/LICENSE)
|
6
6
|
[](https://github.com/orgs/praha-inc/followers)
|
7
7
|
|
8
8
|
A lightweight, tree-shakable Result type package with a simple, consistent API designed.
|
package/dist/cjs/exports.cjs
CHANGED
@@ -9,6 +9,9 @@ var __webpack_modules__ = {
|
|
9
9
|
"./functions/bind": function(module) {
|
10
10
|
module.exports = require("./functions/bind.cjs");
|
11
11
|
},
|
12
|
+
"./functions/combine": function(module) {
|
13
|
+
module.exports = require("./functions/combine.cjs");
|
14
|
+
},
|
12
15
|
"./functions/do": function(module) {
|
13
16
|
module.exports = require("./functions/do.cjs");
|
14
17
|
},
|
@@ -113,70 +116,76 @@ var __webpack_exports__ = {};
|
|
113
116
|
return _functions_bind__WEBPACK_IMPORTED_MODULE_3__[key];
|
114
117
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
115
118
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
116
|
-
var
|
119
|
+
var _functions_combine__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__("./functions/combine");
|
120
|
+
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
121
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_combine__WEBPACK_IMPORTED_MODULE_4__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
122
|
+
return _functions_combine__WEBPACK_IMPORTED_MODULE_4__[key];
|
123
|
+
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
124
|
+
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
125
|
+
var _functions_do__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__("./functions/do");
|
117
126
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
118
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
119
|
-
return
|
127
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_do__WEBPACK_IMPORTED_MODULE_5__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
128
|
+
return _functions_do__WEBPACK_IMPORTED_MODULE_5__[key];
|
120
129
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
121
130
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
122
|
-
var
|
131
|
+
var _functions_fail__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__("./functions/fail");
|
123
132
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
124
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
125
|
-
return
|
133
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_fail__WEBPACK_IMPORTED_MODULE_6__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
134
|
+
return _functions_fail__WEBPACK_IMPORTED_MODULE_6__[key];
|
126
135
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
127
136
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
128
|
-
var
|
137
|
+
var _functions_is_failure__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__("./functions/is-failure");
|
129
138
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
130
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
131
|
-
return
|
139
|
+
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) {
|
140
|
+
return _functions_is_failure__WEBPACK_IMPORTED_MODULE_7__[key];
|
132
141
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
133
142
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
134
|
-
var
|
143
|
+
var _functions_is_success__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__("./functions/is-success");
|
135
144
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
136
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
137
|
-
return
|
145
|
+
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) {
|
146
|
+
return _functions_is_success__WEBPACK_IMPORTED_MODULE_8__[key];
|
138
147
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
139
148
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
140
|
-
var
|
149
|
+
var _functions_map__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__("./functions/map");
|
141
150
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
142
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
143
|
-
return
|
151
|
+
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) {
|
152
|
+
return _functions_map__WEBPACK_IMPORTED_MODULE_9__[key];
|
144
153
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
145
154
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
146
|
-
var
|
155
|
+
var _functions_map_error__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__("./functions/map-error");
|
147
156
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
148
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
149
|
-
return
|
157
|
+
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) {
|
158
|
+
return _functions_map_error__WEBPACK_IMPORTED_MODULE_10__[key];
|
150
159
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
151
160
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
152
|
-
var
|
161
|
+
var _functions_pipe__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__("./functions/pipe");
|
153
162
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
154
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
155
|
-
return
|
163
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_pipe__WEBPACK_IMPORTED_MODULE_11__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
164
|
+
return _functions_pipe__WEBPACK_IMPORTED_MODULE_11__[key];
|
156
165
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
157
166
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
158
|
-
var
|
167
|
+
var _functions_succeed__WEBPACK_IMPORTED_MODULE_12__ = __webpack_require__("./functions/succeed");
|
159
168
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
160
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
161
|
-
return
|
169
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_succeed__WEBPACK_IMPORTED_MODULE_12__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
170
|
+
return _functions_succeed__WEBPACK_IMPORTED_MODULE_12__[key];
|
162
171
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
163
172
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
164
|
-
var
|
173
|
+
var _functions_try__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__("./functions/try");
|
165
174
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
166
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
167
|
-
return
|
175
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_try__WEBPACK_IMPORTED_MODULE_13__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
176
|
+
return _functions_try__WEBPACK_IMPORTED_MODULE_13__[key];
|
168
177
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
169
178
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
170
|
-
var
|
179
|
+
var _functions_unwrap__WEBPACK_IMPORTED_MODULE_14__ = __webpack_require__("./functions/unwrap");
|
171
180
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
172
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
173
|
-
return
|
181
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_unwrap__WEBPACK_IMPORTED_MODULE_14__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
182
|
+
return _functions_unwrap__WEBPACK_IMPORTED_MODULE_14__[key];
|
174
183
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
175
184
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
176
|
-
var
|
185
|
+
var _functions_unwrap_error__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__("./functions/unwrap-error");
|
177
186
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
178
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
179
|
-
return
|
187
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_unwrap_error__WEBPACK_IMPORTED_MODULE_15__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
188
|
+
return _functions_unwrap_error__WEBPACK_IMPORTED_MODULE_15__[key];
|
180
189
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
181
190
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
182
191
|
})();
|
package/dist/cjs/exports.d.ts
CHANGED
@@ -2,6 +2,7 @@ export * from './result';
|
|
2
2
|
export * from './functions/and-then';
|
3
3
|
export * from './functions/and-through';
|
4
4
|
export * from './functions/bind';
|
5
|
+
export * from './functions/combine';
|
5
6
|
export * from './functions/do';
|
6
7
|
export * from './functions/fail';
|
7
8
|
export * from './functions/is-failure';
|
@@ -37,6 +37,8 @@ import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '..
|
|
37
37
|
* // result: { type: 'Failure', error: 'error: 3' }
|
38
38
|
* ```
|
39
39
|
*
|
40
|
+
* @see {@link pipe} - It is recommended to use this function with the {@link pipe} function for better readability and composability.
|
41
|
+
*
|
40
42
|
* @category Combinators
|
41
43
|
*/
|
42
44
|
export declare const andThen: {
|
@@ -49,6 +49,8 @@ import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '..
|
|
49
49
|
* // { type: 'Failure', error: 'Must be > 0' }
|
50
50
|
* ```
|
51
51
|
*
|
52
|
+
* @see {@link pipe} - It is recommended to use this function with the {@link pipe} function for better readability and composability.
|
53
|
+
*
|
52
54
|
* @category Combinators
|
53
55
|
*/
|
54
56
|
export declare const andThrough: {
|
@@ -49,6 +49,8 @@ import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '..
|
|
49
49
|
* // { type: 'Failure', error: 'error' }
|
50
50
|
* ```
|
51
51
|
*
|
52
|
+
* @see {@link pipe} - It is recommended to use this function with the {@link pipe} function for better readability and composability.
|
53
|
+
*
|
52
54
|
* @category Combinators
|
53
55
|
*/
|
54
56
|
export declare const bind: {
|
@@ -0,0 +1,89 @@
|
|
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
|
+
combine: ()=>combine
|
28
|
+
});
|
29
|
+
const external_fail_cjs_namespaceObject = require("./fail.cjs");
|
30
|
+
const external_is_failure_cjs_namespaceObject = require("./is-failure.cjs");
|
31
|
+
const external_succeed_cjs_namespaceObject = require("./succeed.cjs");
|
32
|
+
const is_promise_cjs_namespaceObject = require("../internals/helpers/is-promise.cjs");
|
33
|
+
const combine = (value)=>{
|
34
|
+
if (Array.isArray(value)) {
|
35
|
+
const group = (results)=>{
|
36
|
+
const errors = [];
|
37
|
+
const successes = [];
|
38
|
+
for (const result of results)if ((0, external_is_failure_cjs_namespaceObject.isFailure)(result)) errors.push(result.error);
|
39
|
+
else successes.push(result.value);
|
40
|
+
return {
|
41
|
+
errors,
|
42
|
+
successes
|
43
|
+
};
|
44
|
+
};
|
45
|
+
if (value.some(is_promise_cjs_namespaceObject.isPromise)) {
|
46
|
+
const results = value;
|
47
|
+
return Promise.all(results).then((results)=>{
|
48
|
+
const { successes, errors } = group(results);
|
49
|
+
return errors.length > 0 ? (0, external_fail_cjs_namespaceObject.fail)(errors) : (0, external_succeed_cjs_namespaceObject.succeed)(successes);
|
50
|
+
});
|
51
|
+
}
|
52
|
+
const results = value;
|
53
|
+
const { successes, errors } = group(results);
|
54
|
+
return errors.length > 0 ? (0, external_fail_cjs_namespaceObject.fail)(errors) : (0, external_succeed_cjs_namespaceObject.succeed)(successes);
|
55
|
+
}
|
56
|
+
{
|
57
|
+
const group = (entries)=>{
|
58
|
+
const errors = [];
|
59
|
+
const successes = {};
|
60
|
+
for (const [key, result] of entries)if ((0, external_is_failure_cjs_namespaceObject.isFailure)(result)) errors.push(result.error);
|
61
|
+
else successes[key] = result.value;
|
62
|
+
return {
|
63
|
+
errors,
|
64
|
+
successes
|
65
|
+
};
|
66
|
+
};
|
67
|
+
const entries = Object.entries(value);
|
68
|
+
if (entries.some(([, result])=>(0, is_promise_cjs_namespaceObject.isPromise)(result))) {
|
69
|
+
const results = entries;
|
70
|
+
const promises = results.map(async ([key, result])=>[
|
71
|
+
key,
|
72
|
+
await result
|
73
|
+
]);
|
74
|
+
return Promise.all(promises).then((entries)=>{
|
75
|
+
const { successes, errors } = group(entries);
|
76
|
+
return errors.length > 0 ? (0, external_fail_cjs_namespaceObject.fail)(errors) : (0, external_succeed_cjs_namespaceObject.succeed)(successes);
|
77
|
+
});
|
78
|
+
}
|
79
|
+
const { successes, errors } = group(entries);
|
80
|
+
return errors.length > 0 ? (0, external_fail_cjs_namespaceObject.fail)(errors) : (0, external_succeed_cjs_namespaceObject.succeed)(successes);
|
81
|
+
}
|
82
|
+
};
|
83
|
+
exports.combine = __webpack_exports__.combine;
|
84
|
+
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
85
|
+
"combine"
|
86
|
+
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
87
|
+
Object.defineProperty(exports, '__esModule', {
|
88
|
+
value: true
|
89
|
+
});
|
@@ -0,0 +1,54 @@
|
|
1
|
+
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result';
|
2
|
+
/**
|
3
|
+
* Combines multiple {@link Result} or {@link ResultAsync} values into a single result.
|
4
|
+
* If all inputs are {@link Success}, returns a {@link Success} with combined values.
|
5
|
+
* If any input is a {@link Failure}, returns a {@link Failure} with an array of all errors.
|
6
|
+
*
|
7
|
+
* @function
|
8
|
+
* @typeParam X - The input type (object or array of Results).
|
9
|
+
*
|
10
|
+
* @example Object Input
|
11
|
+
* ```ts
|
12
|
+
* import { Result } from '@praha/byethrow';
|
13
|
+
*
|
14
|
+
* const result = Result.combine({
|
15
|
+
* a: Result.succeed(1),
|
16
|
+
* b: Result.succeed('hello'),
|
17
|
+
* });
|
18
|
+
* // { type: 'Success', value: { a: 1, b: 'hello' } }
|
19
|
+
* ```
|
20
|
+
*
|
21
|
+
* @example Array Input
|
22
|
+
* ```ts
|
23
|
+
* import { Result } from '@praha/byethrow';
|
24
|
+
*
|
25
|
+
* const result = Result.combine([
|
26
|
+
* Result.succeed(1),
|
27
|
+
* Result.succeed(2),
|
28
|
+
* Result.succeed(3),
|
29
|
+
* ]);
|
30
|
+
* // { type: 'Success', value: [1, 2, 3] }
|
31
|
+
* ```
|
32
|
+
*
|
33
|
+
* @example With Failures
|
34
|
+
* ```ts
|
35
|
+
* import { Result } from '@praha/byethrow';
|
36
|
+
*
|
37
|
+
* const result = Result.combine([
|
38
|
+
* Result.succeed(1),
|
39
|
+
* Result.fail('error1'),
|
40
|
+
* Result.fail('error2'),
|
41
|
+
* ]);
|
42
|
+
* // { type: 'Failure', error: ['error1', 'error2'] }
|
43
|
+
* ```
|
44
|
+
*
|
45
|
+
* @category Utilities
|
46
|
+
*/
|
47
|
+
export declare const combine: {
|
48
|
+
<X extends Record<string, ResultMaybeAsync<any, any>>>(x: X): ResultFor<X[keyof X], {
|
49
|
+
[K in keyof X]: InferSuccess<X[K]>;
|
50
|
+
}, InferFailure<X[keyof X]>[]>;
|
51
|
+
<const X extends Array<ResultMaybeAsync<any, any>>>(x: X): ResultFor<X[number], {
|
52
|
+
[K in keyof X]: InferSuccess<X[K]>;
|
53
|
+
}, InferFailure<X[number]>[]>;
|
54
|
+
};
|
@@ -30,6 +30,9 @@ import type { ResultFor } from '../result';
|
|
30
30
|
* const result = Result.fail();
|
31
31
|
* // Result.Result<never, void>
|
32
32
|
* ```
|
33
|
+
*
|
34
|
+
* @see {@link combine} - For combining multiple Results into a single Result.
|
35
|
+
*
|
33
36
|
* @category Creators
|
34
37
|
*/
|
35
38
|
export declare const fail: {
|
@@ -29,6 +29,8 @@ import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '..
|
|
29
29
|
* // { type: 'Success', value: 123 }
|
30
30
|
* ```
|
31
31
|
*
|
32
|
+
* @see {@link pipe} - It is recommended to use this function with the {@link pipe} function for better readability and composability.
|
33
|
+
*
|
32
34
|
* @category Combinators
|
33
35
|
*/
|
34
36
|
export declare const mapError: {
|
@@ -29,6 +29,8 @@ import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '..
|
|
29
29
|
* // { type: 'Failure', error: 'error' }
|
30
30
|
* ```
|
31
31
|
*
|
32
|
+
* @see {@link pipe} - It is recommended to use this function with the {@link pipe} function for better readability and composability.
|
33
|
+
*
|
32
34
|
* @category Combinators
|
33
35
|
*/
|
34
36
|
export declare const map: {
|
package/dist/esm/exports.d.ts
CHANGED
@@ -2,6 +2,7 @@ export * from './result';
|
|
2
2
|
export * from './functions/and-then';
|
3
3
|
export * from './functions/and-through';
|
4
4
|
export * from './functions/bind';
|
5
|
+
export * from './functions/combine';
|
5
6
|
export * from './functions/do';
|
6
7
|
export * from './functions/fail';
|
7
8
|
export * from './functions/is-failure';
|
package/dist/esm/exports.js
CHANGED
@@ -2,6 +2,7 @@ export * from "./result.js";
|
|
2
2
|
export * from "./functions/and-then.js";
|
3
3
|
export * from "./functions/and-through.js";
|
4
4
|
export * from "./functions/bind.js";
|
5
|
+
export * from "./functions/combine.js";
|
5
6
|
export * from "./functions/do.js";
|
6
7
|
export * from "./functions/fail.js";
|
7
8
|
export * from "./functions/is-failure.js";
|
@@ -37,6 +37,8 @@ import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '..
|
|
37
37
|
* // result: { type: 'Failure', error: 'error: 3' }
|
38
38
|
* ```
|
39
39
|
*
|
40
|
+
* @see {@link pipe} - It is recommended to use this function with the {@link pipe} function for better readability and composability.
|
41
|
+
*
|
40
42
|
* @category Combinators
|
41
43
|
*/
|
42
44
|
export declare const andThen: {
|
@@ -49,6 +49,8 @@ import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '..
|
|
49
49
|
* // { type: 'Failure', error: 'Must be > 0' }
|
50
50
|
* ```
|
51
51
|
*
|
52
|
+
* @see {@link pipe} - It is recommended to use this function with the {@link pipe} function for better readability and composability.
|
53
|
+
*
|
52
54
|
* @category Combinators
|
53
55
|
*/
|
54
56
|
export declare const andThrough: {
|
@@ -49,6 +49,8 @@ import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '..
|
|
49
49
|
* // { type: 'Failure', error: 'error' }
|
50
50
|
* ```
|
51
51
|
*
|
52
|
+
* @see {@link pipe} - It is recommended to use this function with the {@link pipe} function for better readability and composability.
|
53
|
+
*
|
52
54
|
* @category Combinators
|
53
55
|
*/
|
54
56
|
export declare const bind: {
|
@@ -0,0 +1,54 @@
|
|
1
|
+
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result';
|
2
|
+
/**
|
3
|
+
* Combines multiple {@link Result} or {@link ResultAsync} values into a single result.
|
4
|
+
* If all inputs are {@link Success}, returns a {@link Success} with combined values.
|
5
|
+
* If any input is a {@link Failure}, returns a {@link Failure} with an array of all errors.
|
6
|
+
*
|
7
|
+
* @function
|
8
|
+
* @typeParam X - The input type (object or array of Results).
|
9
|
+
*
|
10
|
+
* @example Object Input
|
11
|
+
* ```ts
|
12
|
+
* import { Result } from '@praha/byethrow';
|
13
|
+
*
|
14
|
+
* const result = Result.combine({
|
15
|
+
* a: Result.succeed(1),
|
16
|
+
* b: Result.succeed('hello'),
|
17
|
+
* });
|
18
|
+
* // { type: 'Success', value: { a: 1, b: 'hello' } }
|
19
|
+
* ```
|
20
|
+
*
|
21
|
+
* @example Array Input
|
22
|
+
* ```ts
|
23
|
+
* import { Result } from '@praha/byethrow';
|
24
|
+
*
|
25
|
+
* const result = Result.combine([
|
26
|
+
* Result.succeed(1),
|
27
|
+
* Result.succeed(2),
|
28
|
+
* Result.succeed(3),
|
29
|
+
* ]);
|
30
|
+
* // { type: 'Success', value: [1, 2, 3] }
|
31
|
+
* ```
|
32
|
+
*
|
33
|
+
* @example With Failures
|
34
|
+
* ```ts
|
35
|
+
* import { Result } from '@praha/byethrow';
|
36
|
+
*
|
37
|
+
* const result = Result.combine([
|
38
|
+
* Result.succeed(1),
|
39
|
+
* Result.fail('error1'),
|
40
|
+
* Result.fail('error2'),
|
41
|
+
* ]);
|
42
|
+
* // { type: 'Failure', error: ['error1', 'error2'] }
|
43
|
+
* ```
|
44
|
+
*
|
45
|
+
* @category Utilities
|
46
|
+
*/
|
47
|
+
export declare const combine: {
|
48
|
+
<X extends Record<string, ResultMaybeAsync<any, any>>>(x: X): ResultFor<X[keyof X], {
|
49
|
+
[K in keyof X]: InferSuccess<X[K]>;
|
50
|
+
}, InferFailure<X[keyof X]>[]>;
|
51
|
+
<const X extends Array<ResultMaybeAsync<any, any>>>(x: X): ResultFor<X[number], {
|
52
|
+
[K in keyof X]: InferSuccess<X[K]>;
|
53
|
+
}, InferFailure<X[number]>[]>;
|
54
|
+
};
|
@@ -0,0 +1,55 @@
|
|
1
|
+
import { fail } from "./fail.js";
|
2
|
+
import { isFailure } from "./is-failure.js";
|
3
|
+
import { succeed } from "./succeed.js";
|
4
|
+
import { isPromise } from "../internals/helpers/is-promise.js";
|
5
|
+
const combine = (value)=>{
|
6
|
+
if (Array.isArray(value)) {
|
7
|
+
const group = (results)=>{
|
8
|
+
const errors = [];
|
9
|
+
const successes = [];
|
10
|
+
for (const result of results)if (isFailure(result)) errors.push(result.error);
|
11
|
+
else successes.push(result.value);
|
12
|
+
return {
|
13
|
+
errors,
|
14
|
+
successes
|
15
|
+
};
|
16
|
+
};
|
17
|
+
if (value.some(isPromise)) {
|
18
|
+
const results = value;
|
19
|
+
return Promise.all(results).then((results)=>{
|
20
|
+
const { successes, errors } = group(results);
|
21
|
+
return errors.length > 0 ? fail(errors) : succeed(successes);
|
22
|
+
});
|
23
|
+
}
|
24
|
+
const results = value;
|
25
|
+
const { successes, errors } = group(results);
|
26
|
+
return errors.length > 0 ? fail(errors) : succeed(successes);
|
27
|
+
}
|
28
|
+
{
|
29
|
+
const group = (entries)=>{
|
30
|
+
const errors = [];
|
31
|
+
const successes = {};
|
32
|
+
for (const [key, result] of entries)if (isFailure(result)) errors.push(result.error);
|
33
|
+
else successes[key] = result.value;
|
34
|
+
return {
|
35
|
+
errors,
|
36
|
+
successes
|
37
|
+
};
|
38
|
+
};
|
39
|
+
const entries = Object.entries(value);
|
40
|
+
if (entries.some(([, result])=>isPromise(result))) {
|
41
|
+
const results = entries;
|
42
|
+
const promises = results.map(async ([key, result])=>[
|
43
|
+
key,
|
44
|
+
await result
|
45
|
+
]);
|
46
|
+
return Promise.all(promises).then((entries)=>{
|
47
|
+
const { successes, errors } = group(entries);
|
48
|
+
return errors.length > 0 ? fail(errors) : succeed(successes);
|
49
|
+
});
|
50
|
+
}
|
51
|
+
const { successes, errors } = group(entries);
|
52
|
+
return errors.length > 0 ? fail(errors) : succeed(successes);
|
53
|
+
}
|
54
|
+
};
|
55
|
+
export { combine };
|
@@ -30,6 +30,9 @@ import type { ResultFor } from '../result';
|
|
30
30
|
* const result = Result.fail();
|
31
31
|
* // Result.Result<never, void>
|
32
32
|
* ```
|
33
|
+
*
|
34
|
+
* @see {@link combine} - For combining multiple Results into a single Result.
|
35
|
+
*
|
33
36
|
* @category Creators
|
34
37
|
*/
|
35
38
|
export declare const fail: {
|
@@ -29,6 +29,8 @@ import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '..
|
|
29
29
|
* // { type: 'Success', value: 123 }
|
30
30
|
* ```
|
31
31
|
*
|
32
|
+
* @see {@link pipe} - It is recommended to use this function with the {@link pipe} function for better readability and composability.
|
33
|
+
*
|
32
34
|
* @category Combinators
|
33
35
|
*/
|
34
36
|
export declare const mapError: {
|
@@ -29,6 +29,8 @@ import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '..
|
|
29
29
|
* // { type: 'Failure', error: 'error' }
|
30
30
|
* ```
|
31
31
|
*
|
32
|
+
* @see {@link pipe} - It is recommended to use this function with the {@link pipe} function for better readability and composability.
|
33
|
+
*
|
32
34
|
* @category Combinators
|
33
35
|
*/
|
34
36
|
export declare const map: {
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@praha/byethrow",
|
3
|
-
"version": "0.0
|
3
|
+
"version": "0.1.0",
|
4
4
|
"description": "A lightweight, tree-shakable Result type package with a simple, consistent API designed",
|
5
5
|
"keywords": [
|
6
6
|
"javascript",
|
@@ -41,11 +41,11 @@
|
|
41
41
|
"README.md"
|
42
42
|
],
|
43
43
|
"devDependencies": {
|
44
|
-
"@rslib/core": "0.10.
|
44
|
+
"@rslib/core": "0.10.1",
|
45
45
|
"eslint": "9.29.0",
|
46
46
|
"typedoc": "0.28.5",
|
47
47
|
"typescript": "5.8.3",
|
48
|
-
"vitest": "3.2.
|
48
|
+
"vitest": "3.2.4"
|
49
49
|
},
|
50
50
|
"publishConfig": {
|
51
51
|
"provenance": true
|