neverthrow
Advanced tools
Comparing version
@@ -47,2 +47,44 @@ 'use strict'; | ||
function __values(o) { | ||
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; | ||
if (m) return m.call(o); | ||
if (o && typeof o.length === "number") return { | ||
next: function () { | ||
if (o && i >= o.length) o = void 0; | ||
return { value: o && o[i++], done: !o }; | ||
} | ||
}; | ||
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); | ||
} | ||
function __await(v) { | ||
return this instanceof __await ? (this.v = v, this) : new __await(v); | ||
} | ||
function __asyncGenerator(thisArg, _arguments, generator) { | ||
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); | ||
var g = generator.apply(thisArg, _arguments || []), i, q = []; | ||
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; | ||
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } | ||
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } | ||
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } | ||
function fulfill(value) { resume("next", value); } | ||
function reject(value) { resume("throw", value); } | ||
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } | ||
} | ||
function __asyncDelegator(o) { | ||
var i, p; | ||
return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i; | ||
function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; } | ||
} | ||
function __asyncValues(o) { | ||
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); | ||
var m = o[Symbol.asyncIterator], i; | ||
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); | ||
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } | ||
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } | ||
} | ||
class ResultAsync { | ||
@@ -109,2 +151,10 @@ constructor(res) { | ||
} | ||
/** | ||
* Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`. | ||
*/ | ||
safeUnwrap() { | ||
return __asyncGenerator(this, arguments, function* safeUnwrap_1() { | ||
return yield __await(yield __await(yield* __asyncDelegator(__asyncValues(yield __await(this._promise.then((res) => res.safeUnwrap())))))); | ||
}); | ||
} | ||
// Makes ResultAsync implement PromiseLike<Result> | ||
@@ -181,2 +231,9 @@ then(successCallback, failureCallback) { | ||
const err = (err) => new Err(err); | ||
function safeTry(body) { | ||
const n = body().next(); | ||
if (n instanceof Promise) { | ||
return n.then((r) => r.value); | ||
} | ||
return n.value; | ||
} | ||
class Ok { | ||
@@ -221,2 +278,9 @@ constructor(value) { | ||
} | ||
safeUnwrap() { | ||
const value = this.value; | ||
/* eslint-disable-next-line require-yield */ | ||
return (function* () { | ||
return value; | ||
})(); | ||
} | ||
_unsafeUnwrap(_) { | ||
@@ -268,2 +332,9 @@ return this.value; | ||
} | ||
safeUnwrap() { | ||
const error = this.error; | ||
return (function* () { | ||
yield err(error); | ||
throw new Error('Do not use this generator out of `safeTry`'); | ||
})(); | ||
} | ||
_unsafeUnwrap(config) { | ||
@@ -289,1 +360,2 @@ throw createNeverThrowError('Called `_unsafeUnwrap` on an Err', this, config); | ||
exports.okAsync = okAsync; | ||
exports.safeTry = safeTry; |
@@ -24,2 +24,6 @@ interface ErrorConfig { | ||
unwrapOr<A>(t: A): Promise<T | A>; | ||
/** | ||
* Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`. | ||
*/ | ||
safeUnwrap(): AsyncGenerator<Err<never, E>, T>; | ||
then<A, B>(successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>; | ||
@@ -72,2 +76,28 @@ } | ||
declare const err: <T = never, E = unknown>(err: E) => Err<T, E>; | ||
/** | ||
* Evaluates the given generator to a Result returned or an Err yielded from it, | ||
* whichever comes first. | ||
* | ||
* This function, in combination with `Result.safeUnwrap()`, is intended to emulate | ||
* Rust's ? operator. | ||
* See `/tests/safeTry.test.ts` for examples. | ||
* | ||
* @param body - What is evaluated. In body, `yield* result.safeUnwrap()` works as | ||
* Rust's `result?` expression. | ||
* @returns The first occurence of either an yielded Err or a returned Result. | ||
*/ | ||
declare function safeTry<T, E>(body: () => Generator<Err<never, E>, Result<T, E>>): Result<T, E>; | ||
/** | ||
* Evaluates the given generator to a Result returned or an Err yielded from it, | ||
* whichever comes first. | ||
* | ||
* This function, in combination with `Result.safeUnwrap()`, is intended to emulate | ||
* Rust's ? operator. | ||
* See `/tests/safeTry.test.ts` for examples. | ||
* | ||
* @param body - What is evaluated. In body, `yield* result.safeUnwrap()` and | ||
* `yield* resultAsync.safeUnwrap()` work as Rust's `result?` expression. | ||
* @returns The first occurence of either an yielded Err or a returned Result. | ||
*/ | ||
declare function safeTry<T, E>(body: () => AsyncGenerator<Err<never, E>, Result<T, E>>): Promise<Result<T, E>>; | ||
interface IResult<T, E> { | ||
@@ -168,2 +198,6 @@ /** | ||
/** | ||
* Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`. | ||
*/ | ||
safeUnwrap(): Generator<Err<never, E>, T>; | ||
/** | ||
* **This method is unsafe, and should only be used in a test environments** | ||
@@ -201,2 +235,3 @@ * | ||
match<A>(ok: (t: T) => A, _err: (e: E) => A): A; | ||
safeUnwrap(): Generator<Err<never, E>, T>; | ||
_unsafeUnwrap(_?: ErrorConfig): T; | ||
@@ -220,2 +255,3 @@ _unsafeUnwrapErr(config?: ErrorConfig): E; | ||
match<A>(_ok: (t: T) => A, err: (e: E) => A): A; | ||
safeUnwrap(): Generator<Err<never, E>, T>; | ||
_unsafeUnwrap(config?: ErrorConfig): T; | ||
@@ -306,2 +342,2 @@ _unsafeUnwrapErr(_?: ErrorConfig): E; | ||
export { Err, Ok, Result, ResultAsync, err, errAsync, fromPromise, fromSafePromise, fromThrowable, ok, okAsync }; | ||
export { Err, Ok, Result, ResultAsync, err, errAsync, fromPromise, fromSafePromise, fromThrowable, ok, okAsync, safeTry }; |
@@ -43,2 +43,44 @@ const defaultErrorConfig = { | ||
function __values(o) { | ||
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; | ||
if (m) return m.call(o); | ||
if (o && typeof o.length === "number") return { | ||
next: function () { | ||
if (o && i >= o.length) o = void 0; | ||
return { value: o && o[i++], done: !o }; | ||
} | ||
}; | ||
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); | ||
} | ||
function __await(v) { | ||
return this instanceof __await ? (this.v = v, this) : new __await(v); | ||
} | ||
function __asyncGenerator(thisArg, _arguments, generator) { | ||
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); | ||
var g = generator.apply(thisArg, _arguments || []), i, q = []; | ||
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; | ||
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } | ||
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } | ||
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } | ||
function fulfill(value) { resume("next", value); } | ||
function reject(value) { resume("throw", value); } | ||
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } | ||
} | ||
function __asyncDelegator(o) { | ||
var i, p; | ||
return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i; | ||
function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; } | ||
} | ||
function __asyncValues(o) { | ||
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); | ||
var m = o[Symbol.asyncIterator], i; | ||
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); | ||
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } | ||
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } | ||
} | ||
class ResultAsync { | ||
@@ -105,2 +147,10 @@ constructor(res) { | ||
} | ||
/** | ||
* Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`. | ||
*/ | ||
safeUnwrap() { | ||
return __asyncGenerator(this, arguments, function* safeUnwrap_1() { | ||
return yield __await(yield __await(yield* __asyncDelegator(__asyncValues(yield __await(this._promise.then((res) => res.safeUnwrap())))))); | ||
}); | ||
} | ||
// Makes ResultAsync implement PromiseLike<Result> | ||
@@ -177,2 +227,9 @@ then(successCallback, failureCallback) { | ||
const err = (err) => new Err(err); | ||
function safeTry(body) { | ||
const n = body().next(); | ||
if (n instanceof Promise) { | ||
return n.then((r) => r.value); | ||
} | ||
return n.value; | ||
} | ||
class Ok { | ||
@@ -217,2 +274,9 @@ constructor(value) { | ||
} | ||
safeUnwrap() { | ||
const value = this.value; | ||
/* eslint-disable-next-line require-yield */ | ||
return (function* () { | ||
return value; | ||
})(); | ||
} | ||
_unsafeUnwrap(_) { | ||
@@ -264,2 +328,9 @@ return this.value; | ||
} | ||
safeUnwrap() { | ||
const error = this.error; | ||
return (function* () { | ||
yield err(error); | ||
throw new Error('Do not use this generator out of `safeTry`'); | ||
})(); | ||
} | ||
_unsafeUnwrap(config) { | ||
@@ -275,2 +346,2 @@ throw createNeverThrowError('Called `_unsafeUnwrap` on an Err', this, config); | ||
export { Err, Ok, Result, ResultAsync, err, errAsync, fromPromise, fromSafePromise, fromThrowable, ok, okAsync }; | ||
export { Err, Ok, Result, ResultAsync, err, errAsync, fromPromise, fromSafePromise, fromThrowable, ok, okAsync, safeTry }; |
{ | ||
"name": "neverthrow", | ||
"version": "6.0.1-0", | ||
"version": "6.1.0", | ||
"description": "Stop throwing errors, and instead return Results!", | ||
@@ -8,2 +8,5 @@ "main": "dist/index.cjs.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
@@ -10,0 +13,0 @@ "test": "jest && npm run test-types", |
@@ -5,5 +5,2 @@ # NeverThrow 🙅 | ||
[](https://bundlephobia.com/result?p=neverthrow) | ||
## Description | ||
@@ -45,2 +42,3 @@ | ||
- [`Result.combineWithAllErrors` (static class method)](#resultcombinewithallerrors-static-class-method) | ||
- [`Result.safeUnwrap()`](#resultsafeunwrap) | ||
+ [Asynchronous API (`ResultAsync`)](#asynchronous-api-resultasync) | ||
@@ -59,2 +57,3 @@ - [`okAsync`](#okasync) | ||
- [`ResultAsync.combineWithAllErrors` (static class method)](#resultasynccombinewithallerrors-static-class-method) | ||
- [`ResultAsync.safeUnwrap()`](#resultasyncsafeunwrap) | ||
+ [Utilities](#utilities) | ||
@@ -64,2 +63,3 @@ - [`fromThrowable`](#fromthrowable) | ||
- [`fromSafePromise`](#fromsafepromise) | ||
- [`safeTry`](#safetry) | ||
+ [Testing](#testing) | ||
@@ -121,2 +121,3 @@ * [A note on the Package Name](#a-note-on-the-package-name) | ||
fromSafePromise, | ||
safeTry, | ||
} from 'neverthrow' | ||
@@ -667,4 +668,13 @@ ``` | ||
[⬆️ Back to top](#toc) | ||
#### `Result.safeUnwrap()` | ||
**⚠️ You must use `.safeUnwrap` in a generator context with `safeTry`** | ||
Allows for unwrapping a `Result` or returning an `Err` implicitly, thereby reducing boilerplate. | ||
[⬆️ Back to top](#toc) | ||
--- | ||
@@ -1137,3 +1147,10 @@ | ||
#### `ResultAsync.safeUnwrap()` | ||
**⚠️ You must use `.safeUnwrap` in a generator context with `safeTry`** | ||
Allows for unwrapping a `Result` or returning an `Err` implicitly, thereby reducing boilerplate. | ||
[⬆️ Back to top](#toc) | ||
--- | ||
@@ -1164,2 +1181,11 @@ | ||
#### `safeTry` | ||
Used to implicityly return errors and reduce boilerplate. | ||
See https://github.com/supermacro/neverthrow/pull/448 and https://github.com/supermacro/neverthrow/issues/444 | ||
[⬆️ Back to top](#toc) | ||
--- | ||
@@ -1166,0 +1192,0 @@ |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
83243
10.6%1007
20.17%0
-100%1232
2.16%6
-14.29%