UNPKG

@rustresult/result

Version:

Rust-like Result and ResultAsync for Javascript

125 lines 5.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RustlikeResultAsync = void 0; const factory_1 = require("./factory"); const RustlikeResult_1 = require("./RustlikeResult"); class RustlikeResultAsync { constructor(promise) { this._promise = Promise.resolve(promise); } isOk() { return this._promise.then((result) => result.isOk()); } isOkAnd(fn) { return this._promise.then((result) => result.isOk() && fn(result.unwrapUnchecked())); } isErr() { return this._promise.then((result) => result.isErr()); } isErrAnd(fn) { return this._promise.then((result) => result.isErr() && fn(result.unwrapErrUnchecked())); } ok() { return this._promise.then((result) => result.ok()); } err() { return this._promise.then((result) => result.err()); } map(op) { return new RustlikeResultAsync(this._promise.then(async (result) => result.isOk() ? (0, factory_1.Ok)(await op(result.unwrapUnchecked())) : (0, factory_1.Err)(result.unwrapErrUnchecked()))); } mapOr(fallback, map) { return this._promise.then((result) => (result.isOk() ? map(result.unwrapUnchecked()) : fallback)); } mapOrElse(fallback, map) { return this._promise.then((result) => result.isOk() ? map(result.unwrapUnchecked()) : fallback(result.unwrapErrUnchecked())); } mapErr(op) { return new RustlikeResultAsync(this._promise.then(async (result) => result.isOk() ? (0, factory_1.Ok)(result.unwrapUnchecked()) : (0, factory_1.Err)(await op(result.unwrapErrUnchecked())))); } inspect(fn) { return new RustlikeResultAsync(this._promise.then(async (result) => { if (result.isOk()) { await fn(result.unwrapUnchecked()); } return result; })); } inspectErr(fn) { return new RustlikeResultAsync(this._promise.then(async (result) => { if (result.isErr()) { await fn(result.unwrapErrUnchecked()); } return result; })); } expect(msg) { return this._promise.then((result) => result.expect(msg)); } unwrap() { return this._promise.then((result) => result.unwrap()); } expectErr(msg) { return this._promise.then((result) => result.expectErr(msg)); } unwrapErr() { return this._promise.then((result) => result.unwrapErr()); } unwrapOr(fallback) { return this._promise.then((result) => result.unwrapOr(fallback)); } unwrapOrElse(op) { return this._promise.then((result) => result.isOk() ? result.unwrapUnchecked() : op(result.unwrapErrUnchecked())); } unwrapUnchecked() { return this._promise.then((result) => result.unwrapUnchecked()); } unwrapErrUnchecked() { return this._promise.then((result) => result.unwrapErrUnchecked()); } and(res) { return new RustlikeResultAsync(this._promise.then((result) => (result.isOk() ? res : (0, factory_1.Err)(result.unwrapErrUnchecked())))); } andThen(op) { return new RustlikeResultAsync(this._promise.then((result) => result.isOk() ? op(result.unwrapUnchecked()) : (0, factory_1.Err)(result.unwrapErrUnchecked()))); } or(res) { return new RustlikeResultAsync(this._promise.then((result) => (result.isOk() ? (0, factory_1.Ok)(result.unwrapUnchecked()) : res))); } orElse(op) { return new RustlikeResultAsync(this._promise.then((result) => result.isOk() ? (0, factory_1.Ok)(result.unwrapUnchecked()) : op(result.unwrapErrUnchecked()))); } transpose() { return this._promise.then((result) => result.transpose()); } static async _equal(self, other) { const isSelfResult = self instanceof RustlikeResult_1.RustlikeResult || self instanceof RustlikeResultAsync; const isOtherResult = other instanceof RustlikeResult_1.RustlikeResult || other instanceof RustlikeResultAsync; if (isSelfResult && isOtherResult) { const _self = await self; const _other = await other; const isOk = _self.isOk(); if (isOk !== _other.isOk()) return false; return isOk ? RustlikeResultAsync._equal(_self.unwrapUnchecked(), _other.unwrapUnchecked()) : RustlikeResultAsync._equal(_self.unwrapErrUnchecked(), _other.unwrapErrUnchecked()); } return self === other || (Number.isNaN(self) && Number.isNaN(other)); } async equal(other) { const _self = await this._promise; const _other = await other; const isOk = _self.isOk(); if (isOk !== _other.isOk()) return false; return isOk ? RustlikeResultAsync._equal(_self.unwrapUnchecked(), _other.unwrapUnchecked()) : RustlikeResultAsync._equal(_self.unwrapErrUnchecked(), _other.unwrapErrUnchecked()); } then(onfulfilled, onrejected) { return this._promise.then(onfulfilled, onrejected); } } exports.RustlikeResultAsync = RustlikeResultAsync; //# sourceMappingURL=RustlikeResultAsync.js.map