2 lines
4.5 KiB
JavaScript
2 lines
4.5 KiB
JavaScript
import{E as o,r as h,l as P,M,n as u,j as c}from"./Either.DDEPhGsy.js";var f;const l={liftEither(i){if(i.isRight())return Promise.resolve(i.extract());throw i.extract()},fromPromise(i){return i.then(l.liftEither)},throwE(i){throw i}};class y{constructor(t){this.runPromise=t,this[f]="EitherAsync",this["fantasy-land/chain"]=this.chain,this["fantasy-land/alt"]=this.alt,this.then=(r,e)=>this.run().then(r,e)}leftOrDefault(t){return this.run().then(r=>r.leftOrDefault(t))}orDefault(t){return this.run().then(r=>r.orDefault(t))}join(){return n(async t=>{const r=await this;if(r.isRight()){const e=await r.extract();return t.liftEither(e)}return t.liftEither(r)})}ap(t){return n(async r=>{const e=await t;if(e.isRight()){const a=await this.run();return a.isRight()?e.extract()(a.extract()):r.liftEither(a)}return r.liftEither(e)})}alt(t){return n(async r=>{const e=await this.run();if(e.isRight())return e.extract();{const a=await t;return r.liftEither(a)}})}extend(t){return n(async r=>{const e=await this.run();if(e.isRight()){const a=n.liftEither(e);return r.liftEither(h(t(a)))}return r.liftEither(e)})}async run(){try{return h(await this.runPromise(l))}catch(t){return P(t)}}bimap(t,r){return n(async e=>{const a=await this.run();try{return await e.liftEither(a.bimap(t,r))}catch(E){throw await E}})}map(t){return n(r=>this.runPromise(r).then(t))}mapLeft(t){return n(async r=>{try{return await this.runPromise(r)}catch(e){throw await t(e)}})}chain(t){return n(async r=>{const e=await this.runPromise(r);return r.fromPromise(t(e))})}chainLeft(t){return n(async r=>{try{return await this.runPromise(r)}catch(e){return r.fromPromise(t(e))}})}toMaybeAsync(){return s(async({liftMaybe:t})=>{const r=await this.run();return t(r.toMaybe())})}swap(){return n(async t=>{const r=await this.run();return r.isRight()&&t.throwE(r.extract()),t.liftEither(h(r.extract()))})}ifLeft(t){return n(async r=>{const e=await this.run();return e.ifLeft(t),r.liftEither(e)})}ifRight(t){return n(async r=>{const e=await this.run();return e.ifRight(t),r.liftEither(e)})}void(){return this.map(t=>{})}caseOf(t){return this.run().then(r=>r.caseOf(t))}finally(t){return n(({fromPromise:r})=>r(this.run().finally(t)))}}f=Symbol.toStringTag;const n=Object.assign(i=>new y(i),{fromPromise:i=>n(({fromPromise:t})=>t(i())),liftEither:i=>n(({liftEither:t})=>t(i)),lefts:i=>Promise.all(i.map(t=>t.run())).then(o.lefts),rights:i=>Promise.all(i.map(t=>t.run())).then(o.rights),sequence:i=>n(async t=>{let r=[];for await(const e of i){if(e.isLeft())return t.liftEither(e);r.push(e.extract())}return t.liftEither(h(r))}),all:i=>n.fromPromise(async()=>Promise.all(i).then(o.sequence))});y.prototype.constructor=n;var m;const w={liftMaybe(i){if(i.isJust())return Promise.resolve(i.extract());throw u},fromPromise(i){return i.then(w.liftMaybe)}};class b{constructor(t){this.runPromise=t,this[m]="MaybeAsync",this["fantasy-land/chain"]=this.chain,this["fantasy-land/filter"]=this.filter,this["fantasy-land/alt"]=this.alt}orDefault(t){return this.run().then(r=>r.orDefault(t))}join(){return s(async t=>{const r=await this.run();if(r.isJust()){const e=await r.extract();return t.liftMaybe(e)}return t.liftMaybe(u)})}ap(t){return s(async r=>{const e=await t;if(e.isJust()){const a=await this.run();return a.isJust()?e.extract()(a.extract()):r.liftMaybe(u)}return r.liftMaybe(u)})}alt(t){return s(async r=>{const e=await this.run();if(e.isJust())return e.extract();{const a=await t;return r.liftMaybe(a)}})}extend(t){return s(async r=>{const e=await this.run();if(e.isJust()){const a=s.liftMaybe(e);return r.liftMaybe(c(t(a)))}return r.liftMaybe(u)})}filter(t){return s(async r=>{const e=await this.run();return r.liftMaybe(e.filter(t))})}async run(){try{return c(await this.runPromise(w))}catch{return u}}map(t){return s(r=>this.runPromise(r).then(t))}chain(t){return s(async r=>{const e=await this.runPromise(r);return r.fromPromise(t(e))})}toEitherAsync(t){return n(async({liftEither:r})=>{const e=await this.run();return r(e.toEither(t))})}ifJust(t){return s(async r=>{const e=await this.run();return e.ifJust(t),r.liftMaybe(e)})}ifNothing(t){return s(async r=>{const e=await this.run();return e.ifNothing(t),r.liftMaybe(e)})}void(){return this.map(t=>{})}caseOf(t){return this.run().then(r=>r.caseOf(t))}finally(t){return s(({fromPromise:r})=>r(this.run().finally(t)))}then(t,r){return this.run().then(t,r)}}m=Symbol.toStringTag;const s=Object.assign(i=>new b(i),{catMaybes:i=>Promise.all(i).then(M.catMaybes),fromPromise:i=>s(({fromPromise:t})=>t(i())),liftMaybe:i=>s(({liftMaybe:t})=>t(i))});b.prototype.constructor=s;export{n as E};
|
|
//# sourceMappingURL=MaybeAsync.BkJailCK.js.map
|