neverthrow
Advanced tools
Comparing version 4.1.1 to 4.2.0
@@ -358,2 +358,28 @@ 'use strict'; | ||
} | ||
/** | ||
* Give a list of all the errors we find | ||
*/ | ||
var combineResultListWithAllErrors = function (resultList) { | ||
return resultList.reduce(function (acc, result) { | ||
return result.isErr() | ||
? acc.isErr() | ||
? err(__spread(acc.error, [result.error])) | ||
: err([result.error]) | ||
: acc.isErr() | ||
? acc | ||
: ok(__spread(acc.value, [result.value])); | ||
}, ok([])); | ||
}; | ||
var combineResultAsyncListWithAllErrors = function (asyncResultList) { | ||
return ResultAsync.fromSafePromise(Promise.all(asyncResultList)).andThen(combineResultListWithAllErrors); | ||
}; | ||
// eslint-disable-next-line | ||
function combineWithAllErrors(list) { | ||
if (list[0] instanceof ResultAsync) { | ||
return combineResultAsyncListWithAllErrors(list); | ||
} | ||
else { | ||
return combineResultListWithAllErrors(list); | ||
} | ||
} | ||
@@ -364,2 +390,3 @@ exports.Err = Err; | ||
exports.combine = combine; | ||
exports.combineWithAllErrors = combineWithAllErrors; | ||
exports.err = err; | ||
@@ -366,0 +393,0 @@ exports.errAsync = errAsync; |
@@ -90,3 +90,5 @@ interface ErrorConfig { | ||
declare function combine<T extends readonly ResultAsync<unknown, unknown>[]>(asyncResultList: T): ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number]>; | ||
declare function combineWithAllErrors<T extends readonly Result<unknown, unknown>[]>(resultList: T): Result<ExtractOkTypes<T>, ExtractErrTypes<T>[number][]>; | ||
declare function combineWithAllErrors<T extends readonly ResultAsync<unknown, unknown>[]>(asyncResultList: T): ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number][]>; | ||
export { Err, Ok, Result, ResultAsync, combine, err, errAsync, fromPromise, fromSafePromise, fromThrowable, ok, okAsync }; | ||
export { Err, Ok, Result, ResultAsync, combine, combineWithAllErrors, err, errAsync, fromPromise, fromSafePromise, fromThrowable, ok, okAsync }; |
@@ -356,3 +356,29 @@ /*! ***************************************************************************** | ||
} | ||
/** | ||
* Give a list of all the errors we find | ||
*/ | ||
var combineResultListWithAllErrors = function (resultList) { | ||
return resultList.reduce(function (acc, result) { | ||
return result.isErr() | ||
? acc.isErr() | ||
? err(__spread(acc.error, [result.error])) | ||
: err([result.error]) | ||
: acc.isErr() | ||
? acc | ||
: ok(__spread(acc.value, [result.value])); | ||
}, ok([])); | ||
}; | ||
var combineResultAsyncListWithAllErrors = function (asyncResultList) { | ||
return ResultAsync.fromSafePromise(Promise.all(asyncResultList)).andThen(combineResultListWithAllErrors); | ||
}; | ||
// eslint-disable-next-line | ||
function combineWithAllErrors(list) { | ||
if (list[0] instanceof ResultAsync) { | ||
return combineResultAsyncListWithAllErrors(list); | ||
} | ||
else { | ||
return combineResultListWithAllErrors(list); | ||
} | ||
} | ||
export { Err, Ok, Result, ResultAsync, combine, err, errAsync, fromPromise, fromSafePromise, fromThrowable, ok, okAsync }; | ||
export { Err, Ok, Result, ResultAsync, combine, combineWithAllErrors, err, errAsync, fromPromise, fromSafePromise, fromThrowable, ok, okAsync }; |
{ | ||
"name": "neverthrow", | ||
"version": "4.1.1", | ||
"version": "4.2.0", | ||
"description": "Stop throwing errors, and instead return Results!", | ||
@@ -32,7 +32,7 @@ "main": "dist/index.cjs.js", | ||
"@types/jest": "26.0.20", | ||
"@types/node": "14.14.20", | ||
"@types/node": "14.14.34", | ||
"@typescript-eslint/eslint-plugin": "4.12.0", | ||
"@typescript-eslint/parser": "4.12.0", | ||
"babel-jest": "26.6.3", | ||
"eslint": "7.16.0", | ||
"eslint": "7.22.0", | ||
"eslint-config-prettier": "7.1.0", | ||
@@ -39,0 +39,0 @@ "eslint-plugin-prettier": "3.3.0", |
@@ -46,3 +46,3 @@ # NeverThrow 🙅 | ||
- [`ResultAsync.fromSafePromise` (static class method)](#resultasyncfromsafepromise-static-class-method) | ||
- [`ResultAsync.map` (method)](#resultasyncmap-method) | ||
- [`ResultAsync.map` (method)](#resultasyncmap-method-1) | ||
- [`ResultAsync.mapErr` (method)](#resultasyncmaperr-method) | ||
@@ -55,2 +55,3 @@ - [`ResultAsync.unwrapOr` (method)](#resultasyncunwrapor-method) | ||
- [`combine`](#combine) | ||
- [`combineWithAllErrors`](#combineWithAllErrors) | ||
- [`fromThrowable`](#fromThrowable) | ||
@@ -881,3 +882,3 @@ - [`fromPromise`](#fromPromise) | ||
**`combine` works on both heterogeneous and homogeneous lists**. This means that you can have lists that contain different kinds of `Result`s and still be able to combine them. Note that you cannot combine lists that contain both `Result`s **and** `ResultAsync`s. | ||
**`combine` works on both heterogeneous and homogeneous lists**. This means that you can have lists that contain different kinds of `Result`s and still be able to combine them. Note that you cannot combine lists that contain both `Result`s **and** `ResultAsync`s. | ||
@@ -913,2 +914,43 @@ The combine function takes a list of results and returns a single result. If all the results in the list are `Ok`, then the return value will be a `Ok` containing a list of all the individual `Ok` values. | ||
#### `combineWithAllErrors` | ||
Like `combine` but without short-circuiting. Instead of just the first error value, you get a list of all error values of the input result list. | ||
If only some results fail, the new combined error list will only contain the error value of the failed results, meaning that there is no guarantee of the length of the new error list. | ||
Like `combine`, it works for both `Result` and `ResultAsync`. | ||
Function signature: | ||
```typescript | ||
// homogeneous lists | ||
function combineWithAllErrors<T, E>(resultList: Result<T, E>[]): Result<T[], E[]> | ||
// heterogeneous lists | ||
function combineWithAllErrors<T1, T2, E1, E2>(resultList: [ Result<T1, E1>, Result<T2, E2> ]): Result<[ T1, T2 ], (E1 | E2)[]> | ||
function combineWithAllErrors<T1, T2, T3, E1, E2, E3> => Result<[ T1, T2, T3 ], (E1 | E2 | E3)[]> | ||
function combineWithAllErrors<T1, T2, T3, T4, E1, E2, E3, E4> => Result<[ T1, T2, T3, T4 ], (E1 | E2 | E3 | E4)[]> | ||
// ... etc etc ad infinitum | ||
``` | ||
Example usage: | ||
```typescript | ||
const resultList: Result<number, string>[] = [ | ||
ok(123), | ||
err('boooom!'), | ||
ok(456), | ||
err('ahhhhh!'), | ||
] | ||
const result = combineWithAllErrors(resultList) | ||
// result is Err(['boooom!', 'ahhhhh!']) | ||
``` | ||
[⬆️ Back to top](#toc) | ||
--- | ||
#### fromThrowable | ||
@@ -915,0 +957,0 @@ |
67978
7
844
1032