UNPKG

rustlike-ts

Version:

A Rust-like functional utility library for safe and expressive error handling in TypeScript.

2 lines (1 loc) 3.2 kB
"use strict";var E=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var w=Object.prototype.hasOwnProperty;var f=(t,r)=>{for(var e in r)E(t,e,{get:r[e],enumerable:!0})},d=(t,r,e,R)=>{if(r&&typeof r=="object"||typeof r=="function")for(let T of m(r))!w.call(t,T)&&T!==e&&E(t,T,{get:()=>r[T],enumerable:!(R=h(r,T))||R.enumerable});return t};var c=t=>d(E({},"__esModule",{value:!0}),t);var S={};f(S,{Err:()=>n,None:()=>s,Ok:()=>o,OptionError:()=>i,ResultError:()=>a,Some:()=>u,filterMapOption:()=>b,filterMapResult:()=>g,mapOption:()=>k,mapResult:()=>y,matchOption:()=>x,matchResult:()=>U});module.exports=c(S);var i=class extends Error{name="OptionError"},l=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 p}or(r){return this}okOr(r){return o(this.#r)}okOrElse(r){return o(this.#r)}andThen(r){return r(this.#r)}filter(r){return r(this.#r)?this:new p}},p=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 u(t){return new l(t)}var s=new p;var a=class extends Error{name="ResultError"},O=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 u(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}},_=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 u(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 o(t){return new O(t)}function n(t){return new _(t)}function U(t,r){return t.isOk()?r.Ok(t.unwrap()):r.Err(t.unwrapErr())}function x(t,r){return t.isSome()?r.Some(t.unwrap()):r.None()}function k(t,r){return t.map(e=>e.isSome()?u(r(e.unwrap())):s)}function y(t,r){return t.map(e=>e.isOk()?o(r(e.unwrap())):n(e.unwrapErr()))}function b(t,r){return t.map(e=>e.isSome()?r(e.unwrap()):s).filter(e=>e.isSome())}function g(t,r){return t.map(e=>e.isOk()?r(e.unwrap()):n(e.unwrapErr())).filter(e=>e.isOk())}0&&(module.exports={Err,None,Ok,OptionError,ResultError,Some,filterMapOption,filterMapResult,mapOption,mapResult,matchOption,matchResult});