neverthrow
Advanced tools
Comparing version 4.3.1 to 4.4.2
@@ -5,3 +5,3 @@ 'use strict'; | ||
/*! ***************************************************************************** | ||
/****************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
@@ -76,10 +76,14 @@ | ||
function __spread() { | ||
for (var ar = [], i = 0; i < arguments.length; i++) | ||
ar = ar.concat(__read(arguments[i])); | ||
return ar; | ||
function __spreadArray(to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
} | ||
var defaultErrorConfig = { | ||
withStackTrace: false, | ||
withStackTrace: false | ||
}; | ||
@@ -97,6 +101,8 @@ // Custom error object | ||
message: message, | ||
stack: maybeStack, | ||
stack: maybeStack | ||
}; | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
exports.Result = void 0; | ||
(function (Result) { | ||
@@ -118,3 +124,3 @@ /** | ||
try { | ||
var result = fn.apply(void 0, __spread(args)); | ||
var result = fn.apply(void 0, __spreadArray([], __read(args), false)); | ||
return ok(result); | ||
@@ -317,3 +323,3 @@ } | ||
var appendValueToEndOfList = function (value) { return function (list) { return __spread(list, [value]); }; }; | ||
var appendValueToEndOfList = function (value) { return function (list) { return __spreadArray(__spreadArray([], __read(list), false), [value], false); }; }; | ||
/** | ||
@@ -355,7 +361,7 @@ * Short circuits on the FIRST Err value that we find | ||
? acc.isErr() | ||
? err(__spread(acc.error, [result.error])) | ||
? err(__spreadArray(__spreadArray([], __read(acc.error), false), [result.error], false)) | ||
: err([result.error]) | ||
: acc.isErr() | ||
? acc | ||
: ok(__spread(acc.value, [result.value])); | ||
: ok(__spreadArray(__spreadArray([], __read(acc.value), false), [result.value], false)); | ||
}, ok([])); | ||
@@ -362,0 +368,0 @@ }; |
declare class ResultAsync<T, E> implements PromiseLike<Result<T, E>> { | ||
private _promise; | ||
constructor(res: Promise<Result<T, E>>); | ||
static fromSafePromise<T, E>(promise: Promise<T>): ResultAsync<T, E>; | ||
static fromPromise<T, E>(promise: Promise<T>, errorFn: (e: unknown) => E): ResultAsync<T, E>; | ||
static fromSafePromise<T, E>(promise: PromiseLike<T>): ResultAsync<T, E>; | ||
static fromPromise<T, E>(promise: PromiseLike<T>, errorFn: (e: unknown) => E): ResultAsync<T, E>; | ||
map<A>(f: (t: T) => A | Promise<A>): ResultAsync<A, E>; | ||
@@ -7,0 +7,0 @@ mapErr<U>(f: (e: E) => U | Promise<U>): ResultAsync<T, U>; |
@@ -1,2 +0,2 @@ | ||
/*! ***************************************************************************** | ||
/****************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
@@ -71,10 +71,14 @@ | ||
function __spread() { | ||
for (var ar = [], i = 0; i < arguments.length; i++) | ||
ar = ar.concat(__read(arguments[i])); | ||
return ar; | ||
function __spreadArray(to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
} | ||
var defaultErrorConfig = { | ||
withStackTrace: false, | ||
withStackTrace: false | ||
}; | ||
@@ -92,3 +96,3 @@ // Custom error object | ||
message: message, | ||
stack: maybeStack, | ||
stack: maybeStack | ||
}; | ||
@@ -115,3 +119,3 @@ }; | ||
try { | ||
var result = fn.apply(void 0, __spread(args)); | ||
var result = fn.apply(void 0, __spreadArray([], __read(args), false)); | ||
return ok(result); | ||
@@ -314,3 +318,3 @@ } | ||
var appendValueToEndOfList = function (value) { return function (list) { return __spread(list, [value]); }; }; | ||
var appendValueToEndOfList = function (value) { return function (list) { return __spreadArray(__spreadArray([], __read(list), false), [value], false); }; }; | ||
/** | ||
@@ -352,7 +356,7 @@ * Short circuits on the FIRST Err value that we find | ||
? acc.isErr() | ||
? err(__spread(acc.error, [result.error])) | ||
? err(__spreadArray(__spreadArray([], __read(acc.error), false), [result.error], false)) | ||
: err([result.error]) | ||
: acc.isErr() | ||
? acc | ||
: ok(__spread(acc.value, [result.value])); | ||
: ok(__spreadArray(__spreadArray([], __read(acc.value), false), [result.value], false)); | ||
}, ok([])); | ||
@@ -359,0 +363,0 @@ }; |
{ | ||
"name": "neverthrow", | ||
"version": "4.3.1", | ||
"version": "4.4.2", | ||
"description": "Stop throwing errors, and instead return Results!", | ||
@@ -28,21 +28,22 @@ "main": "dist/index.cjs.js", | ||
"devDependencies": { | ||
"@babel/core": "7.15.0", | ||
"@babel/preset-env": "7.15.0", | ||
"@babel/preset-typescript": "7.15.0", | ||
"@types/jest": "26.0.24", | ||
"@babel/core": "7.16.7", | ||
"@babel/preset-env": "7.16.8", | ||
"@babel/preset-typescript": "7.16.7", | ||
"@types/jest": "27.4.1", | ||
"@types/node": "14.17.4", | ||
"@typescript-eslint/eslint-plugin": "4.28.1", | ||
"@typescript-eslint/parser": "4.28.1", | ||
"babel-jest": "27.0.6", | ||
"babel-jest": "27.5.1", | ||
"eslint": "7.30.0", | ||
"eslint-config-prettier": "7.1.0", | ||
"eslint-plugin-prettier": "3.4.0", | ||
"jest": "27.0.6", | ||
"jest": "27.5.1", | ||
"prettier": "2.2.1", | ||
"rollup": "2.52.7", | ||
"rollup": "2.75.6", | ||
"rollup-plugin-dts": "3.0.1", | ||
"rollup-plugin-typescript2": "0.29.0", | ||
"testdouble": "^3.16.3", | ||
"ts-jest": "27.0.3", | ||
"typescript": "4.2.4" | ||
"rollup-plugin-typescript2": "^0.32.1", | ||
"testdouble": "3.16.3", | ||
"ts-jest": "27.1.5", | ||
"tslib": "^2.4.0", | ||
"typescript": "4.7.2" | ||
}, | ||
@@ -49,0 +50,0 @@ "keywords": [ |
@@ -452,2 +452,6 @@ # NeverThrow 🙅 | ||
`match` is like chaining `map` and `mapErr`, with the distinction that with `match` both functions must have the same return type. | ||
The differences between `match` and chaining `map` and `mapErr` are that: | ||
- with `match` both functions must have the same return type `A` | ||
- `match` unwraps the `Result<T, E>` into an `A` (the match functions' return type) | ||
- This makes no difference if you are performing side effects only | ||
@@ -457,24 +461,41 @@ **Example:** | ||
```typescript | ||
const result = computationThatMightFail() | ||
const successCallback = (someNumber: number) => { | ||
console.log('> number is: ', someNumber) | ||
} | ||
const failureCallback = (someFailureValue: string) => { | ||
console.log('> boooooo') | ||
} | ||
// method chaining api | ||
// note that you DONT have to append mapErr | ||
// map/mapErr api | ||
// note that you DON'T have to append mapErr | ||
// after map which means that you are not required to do | ||
// error handling | ||
result.map(successCallback).mapErr(failureCallback) | ||
computationThatMightFail().map(console.log).mapErr(console.error) | ||
// match api | ||
// works exactly the same as above, | ||
// works exactly the same as above since both callbacks | ||
// only perform side effects, | ||
// except, now you HAVE to do error handling :) | ||
myval.match(successCallback, failureCallback) | ||
computationThatMightFail().match(console.log, console.error) | ||
// Returning values | ||
const attempt = computationThatMightFail() | ||
.map((str) => str.toUpperCase()) | ||
.mapErr((err) => `Error: ${err}`) | ||
// `attempt` is of type `Result<string, string>` | ||
const answer = computationThatMightFail().match( | ||
(str) => str.toUpperCase(), | ||
(err) => `Error: ${err}` | ||
) | ||
// `answer` is of type `string` | ||
``` | ||
If you don't use the error parameter in your match callback then `match` is equivalent to chaining `map` with `unwrapOr`: | ||
```ts | ||
const answer = computationThatMightFail().match( | ||
(str) => str.toUpperCase(), | ||
() => 'ComputationError' | ||
) | ||
// `answer` is of type `string` | ||
const answer = computationThatMightFail() | ||
.map((str) => str.toUpperCase()) | ||
.unwrapOr('ComputationError') | ||
``` | ||
[⬆️ Back to top](#toc) | ||
@@ -612,6 +633,7 @@ | ||
Transforms a `Promise<T>` that may throw into a `ResultAsync<T, E>`. | ||
Transforms a `PromiseLike<T>` (that may throw) into a `ResultAsync<T, E>`. | ||
The second argument handles the rejection case of the promise and maps the error from `unknown` into some type `E`. | ||
**Signature:** | ||
@@ -624,3 +646,3 @@ | ||
ResultAsync.fromPromise<T, E>( | ||
promise: Promise<T>, | ||
promise: PromiseLike<T>, | ||
errorHandler: (unknownError: unknown) => E) | ||
@@ -630,2 +652,4 @@ ): ResultAsync<T, E> { ... } | ||
If you are working with `PromiseLike` objects that you **know for a fact** will not throw, then use `fromSafePromise` in order to avoid having to pass a redundant `errorHandler` argument. | ||
**Example**: | ||
@@ -657,3 +681,3 @@ | ||
ResultAsync.fromSafePromise<T, E>( | ||
promise: Promise<T> | ||
promise: PromiseLike<T> | ||
): ResultAsync<T, E> { ... } | ||
@@ -660,0 +684,0 @@ ``` |
76363
942
1111
20