2 lines
5.4 KiB
JavaScript
2 lines
5.4 KiB
JavaScript
var U=class r{constructor(e){this._promise=e;this._promise=e}static fromPromise(e,t){let n=e.then(p=>new s(p)).catch(p=>new i(t(p)));return new r(n)}static fromSafePromise(e){let t=e.then(n=>new s(n));return new r(t)}static fromThrowable(e,t){return(...n)=>{try{return v(e(n))}catch(p){return T(t?t(p):p)}}}async unwrap(){let e=await this._promise;if(e.isErr())throw e.error;return e.value}async match(e,t){let n=await this._promise;return n.isErr()?t(n.error):e(n.value)}map(e){return new r(this._promise.then(t=>t.isErr()?new i(t.error):new s(e(t.value))))}mapAsync(e){return new r(this._promise.then(async t=>t.isErr()?T(t.error):new s(await e(t.value))))}mapErr(e){return new r(this._promise.then(t=>t.isErr()?new i(e(t.error)):new s(t.value)))}mapErrAsync(e){return new r(this._promise.then(async t=>t.isErr()?T(await e(t.error)):new s(t.value)))}andThen(e){return new r(this._promise.then(t=>t.isErr()?T(t.error):e(t.value)))}nullableToOption(){return this.map(e=>e?E(e):f)}flatten(){return new r(this._promise.then(e=>e.flatten()))}flattenOption(e){return new r(this._promise.then(t=>t.flattenOption(e)))}flattenOptionOrDefault(e){return new r(this._promise.then(t=>t.flattenOptionOrDefault(e)))}matchOption(e,t){return new r(this._promise.then(n=>n.matchOption(e,t)))}matchOptionAndFlatten(e,t){return new r(this._promise.then(n=>n.matchOptionAndFlatten(e,t)))}then(e,t){return this._promise.then(e,t)}};function v(r){return new U(Promise.resolve(new s(r)))}function T(r){return new U(Promise.resolve(new i(r)))}var s=class r{constructor(e){this.value=e;this.value=e,Object.defineProperties(this,{tag:{writable:!1,enumerable:!1}})}tag="Ok";isErr(){return!1}ifErr(e){return this}isErrOrNone(){return this.value instanceof o}isOk(){return!0}ifOk(e){return e(this.value),this}unwrap(){return this.value}unwrapOr(e){return this.value}unwrapOrElse(e){return this.value}match(e,t){return e(this.value)}map(e){let t=e(this.value);return new r(t)}mapOption(e){return this.value instanceof o||this.value instanceof l?u(this.value.map(e)):u(E(e(this.value)))}andThen(e){return e(this.value)}mapErr(e){return new r(this.value)}flatten(){return c(this)}flattenOption(e){return this.value instanceof o||this.value instanceof l?this.value.okOrElse(e):new r(this.value)}flattenOptionOr(e){return this.value instanceof o||this.value instanceof l?this.value.unwrapOr(e):new r(this.value)}matchOption(e,t){return this.value instanceof o||this.value instanceof l?u(this.value.match(e,t)):u(e(this.value))}toNullable(){return this.value}toAsync(){return v(this.value)}void(){return u()}},i=class r{constructor(e){this.error=e;this.error=e,Object.defineProperties(this,{tag:{writable:!1,configurable:!1,enumerable:!1}})}tag="Err";isErr(){return!0}ifErr(e){return e(this.error),this}isOk(){return!1}ifOk(e){return this}isErrOrNone(){return!0}unwrap(){let e=`Tried to unwrap error: ${O(this.error)}`;throw new Error(e)}unwrapOr(e){return e}unwrapOrElse(e){return e()}match(e,t){return t(this.error)}map(e){return new r(this.error)}mapErr(e){let t=e(this.error);return new r(t)}mapOption(e){return a(this.error)}andThen(e){return new r(this.error)}flatten(){return c(this)}flattenOption(e){return new r(this.error)}flattenOptionOr(e){return new r(this.error)}matchOption(e,t){return a(this.error)}toNullable(){return null}toAsync(){return T(this.error)}void(){return a(this.error)}};function u(r){return new s(r)}function a(r){return new i(r)}function b(r,e){return(...t)=>{try{let n=r(...t);return u(n)}catch(n){return a(e?e(n):n)}}}function O(r){if(r instanceof Error)return r.message?r.message:"code"in r&&typeof r.code=="string"?r.code:"An unknown error occurred";if(typeof r=="string")return r;if(typeof r=="object"&&r!==null&&"message"in r){let e=r;return typeof e.message=="string"?e.message:String(e.message)}return"An unknown error occurred"}function c(r){let e=r;for(;e instanceof s;)e=e.value;return e}function g(r){let e=JSON.parse(r);if(obj.value)return u(obj.value);if(obj.error)return a(obj.error)}var l=class r{constructor(e){this.value=e;Object.defineProperties(this,{tag:{writable:!1,enumerable:!1}})}tag="Some";isSome(){return!0}ifSome(e){return e(this.value),this}isNone(){return!1}ifNone(e){return this}map(e){return new r(e(this.value))}flatMap(e){return e(this.value)}andThen(e){return e(this.value)}unwrap(){return this.value}unwrapOr(e){return this.value}unwrapOrElse(e){return this.value}or(e){return this}orElse(e){return this}match(e,t){return e(this.value)}toJSON(){return{value:this.value}}toString(){return`Some(${this.value})`}toNullable(){return this.value}toBoolean(){return!0}okOrElse(e){return u(this.value)}},o=class r{tag="None";constructor(){Object.defineProperties(this,{tag:{writable:!1,enumerable:!1}})}isSome(){return!1}ifSome(e){return this}isNone(){return!0}ifNone(e){return e(),this}map(e){return new r}andThen(e){return f}flatMap(e){return new r}unwrap(){throw new Error("Tried to unwrap a non-existent value")}unwrapOr(e){return e}unwrapOrElse(e){return e()}or(e){return e}orElse(e){return e()}match(e,t){return t()}toJSON(){return{_tag:this._tag}}toString(){return"None"}toNullable(){return null}toBoolean(){return!1}okOrElse(e){return a(e())}};function E(r){return new l(r)}var f=new o;function S(r){return r?E(r):f}export{i as Err,o as None,s as Ok,U as ResultAsync,g as ResultFromJSON,l as Some,a as err,T as errAsync,c as flattenResult,S as fromNullableVal,b as fromThrowable,O as getMessageFromError,f as none,u as ok,v as okAsync,E as some};
|