@cmtlyt/base
Version:
2 lines (1 loc) • 4.16 kB
JavaScript
"use strict";const array=require("./base.BHKgYhd5.cjs");require("ms");const e=new WeakMap;function getState(s){return e.get(s)}function setState(s,t){const r=getState(s);e.set(s,{...r,...t})}class Functor{constructor(t){setState(this,{value:t})}valueOf(){return getState(this).value}inspect(){return`${this.name} { ${this.valueOf()} }`}join=this.valueOf}let a$1=class extends Functor{name="Container";map(t){return container(t(this.valueOf()))}flatMap(t){return container(t(this.valueOf()).valueOf())}};function container(s){return new a$1(s)}class a extends Functor{name="Right";map(t){return right(t(this.valueOf()))}flatMap(t){return right(t(this.valueOf()).valueOf())}}function right(s){return new a(s)}let u$1=class extends Functor{name="Left";map(t){return this}flatMap(t){return this}};function left(s){return new u$1(s)}class f extends Functor{name="Either";map(t){try{return either(t(this.valueOf()))}catch(r){return left(r)}}flatMap(t){try{return either(t(this.valueOf()).valueOf())}catch(r){return left(r)}}}function either(s){return new f(s)}class n extends Functor{name="IO";map(t){return io((...r)=>t(this.valueOf()(...r)))}flatMap(t){return io((...r)=>t(this.valueOf()(...r)).valueOf())}run(...t){return this.valueOf()(...t)}}function io(s){return new n(s)}class u extends Functor{name="Maybe";isNothing(){return typeof this.valueOf()>"u"||this.valueOf()===null}map(t){return this.isNothing()?maybe(null):maybe(t(this.valueOf()))}flatMap(t){return this.isNothing()?maybe(null):maybe(t(this.valueOf()).valueOf())}}function maybe(s){return new u(s)}function isPromise(s){return(s||!1)&&typeof s.then=="function"}const isFile=array.cacheByReturn(()=>array.isInIframe()?(array.warning("iframe \u4E2D\u65E0\u6CD5\u6B63\u786E\u5224\u65AD!!!"),!1):File?File.prototype?s=>Object.prototype.isPrototypeOf.call(File.prototype,s):s=>s instanceof File:!1),isBlob=array.cacheByReturn(()=>array.isInIframe()?(array.warning("iframe \u4E2D\u65E0\u6CD5\u6B63\u786E\u5224\u65AD!!!"),!1):Blob?Blob.prototype?s=>Object.prototype.isPrototypeOf.call(Blob.prototype,s):s=>s instanceof Blob:!1);function isObject(s){return typeof s=="object"&&s!==null}function isPlainObject(s){return array.getType(s)==="object"}class p extends Functor{name="Task";constructor(t){super(t),setState(this,{value:t})}async#t(t,r,i){const c=i===array.INTERNAL_EMPTY?r:i;try{const o=t(c);isPromise(o)?o.then(r.resolve,r.reject):i!==array.INTERNAL_EMPTY&&r.resolve(o)}catch(o){r.reject(o)}}map(t){return task(r=>{const i=this.valueOf(),c={reject:r.reject,resolve:o=>{this.#t(t,r,o)}};this.#t(i,c,array.INTERNAL_EMPTY)})}flatMap(t){return this.map(r=>t(r).valueOf())}then(t,r){const{promise:i,resolve:c,reject:o}=array.withResolvers(),v={onResolved:l=>{try{c(t?t(l):l)}catch(h){o(h)}},onRejected:l=>{try{r?c(r(l)):o(l)}catch(h){o(h)}}};return this.listen(v).run(),i}catch(t){return this.then(void 0,t)}#e(){return{resolve:t=>{setState(this,{result:t,status:"finished"}),this.#r()},reject:t=>{setState(this,{error:t,status:"finished"}),this.#s()}}}run(){const{status:t}=getState(this);if(t)return this;setState(this,{status:"running"});const r=this.#e();return this.#t(this.valueOf(),r,array.INTERNAL_EMPTY),this}#s(){const{listen:t,error:r}=getState(this);t&&(r&&t.onRejecteds.forEach(i=>i(r)),setState(this,{listen:void 0,error:void 0}))}#r(){const{listen:t,result:r}=getState(this);t&&(r&&t.onResolveds.forEach(i=>i(r)),setState(this,{listen:void 0}))}listen(t){const r=getState(this);let{listen:i,result:c,error:o,status:v}=r;const{onRejected:l,onResolved:h}=t;if(v==="finished"){if(o){if(!l)throw o;l(o),setState(this,{error:void 0})}return c&&h&&h(c),this}return i||(i={onResolveds:[],onRejecteds:[]}),h&&i.onResolveds.push(h),l&&i.onRejecteds.push(l),setState(this,{listen:i}),this}}function task(s){return new p(s)}const index={__proto__:null,container,either,io,left,maybe,right,task};exports.container=container,exports.either=either,exports.index=index,exports.io=io,exports.isBlob=isBlob,exports.isFile=isFile,exports.isObject=isObject,exports.isPlainObject=isPlainObject,exports.isPromise=isPromise,exports.left=left,exports.maybe=maybe,exports.right=right,exports.task=task;