rustlike-ts
Version:
A Rust-like functional utility library for safe and expressive error handling in TypeScript.
2 lines (1 loc) • 2.6 kB
JavaScript
var i=class extends Error{name="OptionError"},p=class{#r;constructor(r){this.#r=r}isSome(){return!0}isNone(){return!1}unwrap(){return this.#r}expect(r){return this.#r}unwrapOr(r){return this.#r}unwrapOrElse(r){return this.#r}map(r){return this.#r=r(this.#r),this}mapOr(r,e){return e(this.#r)}mapOrElse(r,e){return e(this.#r)}and(r){return r.isSome()?r:new o}or(r){return this}okOr(r){return u(this.#r)}okOrElse(r){return u(this.#r)}andThen(r){return r(this.#r)}filter(r){return r(this.#r)?this:new o}},o=class{isSome(){return!1}isNone(){return!0}unwrap(){throw new i("Attempted to unwrap a None value!")}expect(r){throw new i(r)}unwrapOr(r){return r}unwrapOrElse(r){return r()}map(r){return this}mapOr(r,e){return r}mapOrElse(r,e){return r()}and(r){return this}or(r){return r}okOr(r){return n(r)}okOrElse(r){return n(r())}andThen(r){return this}filter(r){return this}};function T(t){return new p(t)}var s=new o;var a=class extends Error{name="ResultError"},E=class{#r;constructor(r){this.#r=r}unwrap(){return this.#r}unwrapErr(){throw new a(`Called unwrapErr() on an Ok value: ${JSON.stringify(this.#r)}`)}unwrapOr(r){return this.#r}unwrapOrElse(r){return this.#r}expect(r){return this.#r}expectErr(r){throw new a(r)}isOk(){return!0}isErr(){return!1}ok(){return T(this.#r)}err(){return s}map(r){return this.#r=r(this.#r),this}mapErr(r){return this}and(r){return r}andThen(r){return r(this.#r)}or(r){return this}orElse(r){return this}},l=class{#r;constructor(r){this.#r=r}unwrap(){throw new a(`Called unwrap() on an Err value: ${JSON.stringify(this.#r)}`)}unwrapErr(){return this.#r}unwrapOr(r){return r}unwrapOrElse(r){return r(this.#r)}expect(r){throw new a(r)}expectErr(r){return this.#r}isOk(){return!1}isErr(){return!0}ok(){return s}err(){return T(this.#r)}map(r){return this}mapErr(r){return this.#r=r(this.#r),this}and(r){return this}andThen(r){return this}or(r){return r}orElse(r){return r(this.#r)}};function u(t){return new E(t)}function n(t){return new l(t)}function f(t,r){return t.isOk()?r.Ok(t.unwrap()):r.Err(t.unwrapErr())}function d(t,r){return t.isSome()?r.Some(t.unwrap()):r.None()}function c(t,r){return t.map(e=>e.isSome()?T(r(e.unwrap())):s)}function U(t,r){return t.map(e=>e.isOk()?u(r(e.unwrap())):n(e.unwrapErr()))}function x(t,r){return t.map(e=>e.isSome()?r(e.unwrap()):s).filter(e=>e.isSome())}function k(t,r){return t.map(e=>e.isOk()?r(e.unwrap()):n(e.unwrapErr())).filter(e=>e.isOk())}export{n as Err,s as None,u as Ok,i as OptionError,a as ResultError,T as Some,x as filterMapOption,k as filterMapResult,c as mapOption,U as mapResult,d as matchOption,f as matchResult};