eaux
Version:
A lightweight and functional-style library that provides robust abstractions for handling optional values and handling operations that can either succeed or fail. By making states explicit, it encourages precise and deliberate management of application lo
30 lines (29 loc) • 6.07 kB
JavaScript
var b=Object.defineProperty;var s=(n,t)=>b(n,"name",{value:t,configurable:!0});var A="1.0.7",k={version:A};class u extends Error{static{s(this,"ExpectationError")}constructor(t){super(t),this.name="ExpectationError"}}const e=await(async()=>{let n;try{n=(await import("node:assert/strict")).ok}catch{return()=>{}}return n})(),g=await(async()=>{let n;try{n=(await import("node:util")).inspect}catch{return()=>""}return n})(),w=Symbol.for("nodejs.util.inspect.custom");class a extends Error{static{s(this,"ImproperUnwrapError")}constructor(t){super(t),this.name="ImproperUnwrapError"}}class y extends Error{static{s(this,"UnreachableCodeError")}constructor(t){super(t),this.name="UnreachableCodeError"}}class h{static{s(this,"Maybe")}constructor(){}#t(t){if(this.isSomething()){const r=this.unwrap();return`
Maybe{
Something{
value: ${t(r).split(`
`).map(i=>` ${i}`).join(`
`).trimStart()},
}
}
`.trim()}else{if(this.isNothing())return`
Maybe{
Nothing{}
}
`.trim();throw new y("Reached an unreachable code path")}}and(t){return e(o(t)),this._and(t)}andThen(t){return e(typeof t=="function"),this._andThen(t)}expect(t){return e(typeof t=="string"),this._expect(t)}filter(t){return e(typeof t=="function"),this._filter(t)}getSuccessOr(t){return this._getSuccessOr(t)}inspect(t){return e(typeof t=="function"),this._inspect(t)}isNothing(){return this._isNothing()}isNothingOr(t){return e(typeof t=="function"),this._isNothingOr(t)}isSomething(){return this._isSomething()}isSomethingAnd(t){return e(typeof t=="function"),this._isSomethingAnd(t)}map(t){return e(typeof t=="function"),this._map(t)}or(t){return e(o(t)),this._or(t)}toString(){return this.#t(t=>`${t}`)}unwrap(){return this._unwrap()}[w](){return this.#t(t=>g(t))}}class F extends h{static{s(this,"Nothing")}static#t;static{const t=new this;this.#t=t}static make(){return this.#t}constructor(){super()}_and(){return this}_andThen(){return this}_expect(t){throw new u(t)}_filter(){return this}_getSuccessOr(t){return v(t)}_inspect(){return this}_isNothing(){return!0}_isNothingOr(){return!0}_isSomething(){return!1}_isSomethingAnd(){return!1}_map(){return this}_or(t){return t}_unwrap(){throw new a("Attempted to unwrap a `Nothing` value")}}class f extends h{static{s(this,"Something")}static make(t){return new this(t)}#t;constructor(t){super(),this.#t=t}_and(t){return t}_andThen(t){const r=t(this.#t);return e(o(r)),r}_expect(){return this.#t}_filter(t){const r=t(this.#t);return e(typeof r=="boolean"),r?this:F.make()}_getSuccessOr(){return x(this.#t)}_inspect(t){return t(this.#t),this}_isNothing(){return!1}_isNothingOr(t){const r=t(this.#t);return e(typeof r=="boolean"),r}_isSomething(){return!0}_isSomethingAnd(t){const r=t(this.#t);return e(typeof r=="boolean"),r}_map(t){const r=t(this.#t);return f.make(r)}_or(){return this}_unwrap(){return this.#t}}function o(n){return n instanceof h}s(o,"isMaybe");function p(){return F.make()}s(p,"nothing");function l(n){return f.make(n)}s(l,"something");class _{static{s(this,"Result")}constructor(){}#t(t){if(this.isSuccess()){const r=this.unwrap();return`
Result{
Success{
value: ${t(r).split(`
`).map(i=>` ${i}`).join(`
`).trimStart()},
}
}
`.trim()}else if(this.isFailure()){const r=this.unwrapFailure();return`
Result{
Failure{
error: ${t(r).split(`
`).map(i=>` ${i}`).join(`
`).trimStart()},
}
}
`.trim()}else throw new y("Reached an unreachable code path")}and(t){return e(c(t)),this._and(t)}andThen(t){return e(typeof t=="function"),this._andThen(t)}expect(t){return e(typeof t=="string"),this._expect(t)}expectFailure(t){return e(typeof t=="string"),this._expectFailure(t)}getFailure(){return this._getFailure()}getSuccess(){return this._getSuccess()}inspect(t){return e(typeof t=="function"),this._inspect(t)}inspectFailure(t){return e(typeof t=="function"),this._inspectFailure(t)}isFailure(){return this._isFailure()}isFailureAnd(t){return e(typeof t=="function"),this._isFailureAnd(t)}isSuccess(){return this._isSuccess()}isSuccessAnd(t){return e(typeof t=="function"),this._isSuccessAnd(t)}map(t){return e(typeof t=="function"),this._map(t)}mapFailure(t){return e(typeof t=="function"),this._mapFailure(t)}or(t){return e(c(t)),this._or(t)}toString(){return this.#t(t=>`${t}`)}unwrap(){return this._unwrap()}unwrapFailure(){return this._unwrapFailure()}[w](){return this.#t(t=>g(t))}}class m extends _{static{s(this,"Failure")}static make(t){return new this(t)}#t;constructor(t){super(),this.#t=t}_and(){return this}_andThen(){return this}_expect(t){throw new u(t)}_expectFailure(){return this.#t}_getFailure(){return l(this.#t)}_getSuccess(){return p()}_inspect(){return this}_inspectFailure(t){return t(this.#t),this}_isFailure(){return!0}_isFailureAnd(t){const r=t(this.#t);return e(typeof r=="boolean"),r}_isSuccess(){return!1}_isSuccessAnd(){return!1}_map(){return this}_mapFailure(t){const r=t(this.#t);return e(typeof r<"u"),e(r!==null),m.make(r)}_or(t){return t}_unwrap(){throw new a("Attempted to unwrap a `Failure` value")}_unwrapFailure(){return this.#t}}class d extends _{static{s(this,"Success")}static make(t){return new this(t)}#t;constructor(t){super(),this.#t=t}_and(t){return t}_andThen(t){const r=t(this.#t);return e(c(r)),r}_expect(){return this.#t}_expectFailure(t){throw new u(t)}_getFailure(){return p()}_getSuccess(){return l(this.#t)}_inspect(t){return t(this.#t),this}_inspectFailure(){return this}_isFailure(){return!1}_isFailureAnd(){return!1}_isSuccess(){return!0}_isSuccessAnd(t){const r=t(this.#t);return e(typeof r=="boolean"),r}_map(t){const r=t(this.#t);return d.make(r)}_mapFailure(){return this}_or(){return this}_unwrap(){return this.#t}_unwrapFailure(){throw new a("Attempted to unwrap a `Success` value")}}function c(n){return n instanceof _}s(c,"isResult");function v(n){return e(typeof n<"u"),e(n!==null),m.make(n)}s(v,"failure");function x(n){return d.make(n)}s(x,"success");const N=k.version;export{u as ExpectationError,a as ImproperUnwrapError,N as VERSION,v as failure,o as isMaybe,c as isResult,p as nothing,l as something,x as success};