@byloth/core
Version:
An unopinionated collection of useful functions and classes that I use widely in all my projects. 🔧
7 lines (4 loc) • 33 kB
JavaScript
;var tt=Object.defineProperty;var nt=(i,n,e)=>n in i?tt(i,n,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[n]=e;var a=(i,n,e)=>nt(i,typeof n!="symbol"?n+"":n,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const P=typeof window<"u"&&typeof window.document<"u";var z;const st=typeof process<"u"&&!!((z=process.versions)!=null&&z.node);var $;const rt=typeof self=="object"&&(($=self.constructor)==null?void 0:$.name)==="DedicatedWorkerGlobalScope";var D,V;class c extends(V=Error,D=Symbol.toStringTag,V){constructor(e,t,s="Exception"){super(e);a(this,D,"Exception");this.cause=t,this.name=s,t&&(t instanceof Error?this.stack+=`
Caused by ${t.stack}`:this.stack+=`
Caused by ${t}`)}static FromUnknown(e){if(e instanceof c)return e;if(e instanceof Error){const t=new c(e.message);return t.stack=e.stack,t.name=e.name,t}return new c(`${e}`)}}var B,J;class g extends(J=c,B=Symbol.toStringTag,J){constructor(e,t,s="FatalErrorException"){e===void 0&&(e="The program has encountered an unrecoverable error and cannot continue as expected. Please, try again later. If the problem persists, contact the support team.");super(e,t,s);a(this,B,"FatalErrorException")}}var L,Y;class Ke extends(Y=g,L=Symbol.toStringTag,Y){constructor(e,t,s="NotImplementedException"){e===void 0&&(e="This feature isn't implemented yet. Please, try again later.");super(e,t,s);a(this,L,"NotImplementedException")}}var G,K;class C extends(K=c,G=Symbol.toStringTag,K){constructor(e,t,s="FileException"){super(e,t,s);a(this,G,"FileException")}}var Q,H;class it extends(H=C,Q=Symbol.toStringTag,H){constructor(e,t,s="FileExistsException"){super(e,t,s);a(this,Q,"FileExistsException")}}var X,Z;class ot extends(Z=C,X=Symbol.toStringTag,Z){constructor(e,t,s="FileNotFoundException"){super(e,t,s);a(this,X,"FileNotFoundException")}}var W,U;class _ extends(U=c,W=Symbol.toStringTag,U){constructor(e,t,s="KeyException"){super(e,t,s);a(this,W,"KeyException")}}var ee,te;class at extends(te=c,ee=Symbol.toStringTag,te){constructor(e,t,s="NetworkException"){super(e,t,s);a(this,ee,"NetworkException")}}var ne,se;class lt extends(se=c,ne=Symbol.toStringTag,se){constructor(e,t,s="PermissionException"){super(e,t,s);a(this,ne,"PermissionException")}}var re,ie;class E extends(ie=c,re=Symbol.toStringTag,ie){constructor(e,t,s="ReferenceException"){super(e,t,s);a(this,re,"ReferenceException")}}var oe,ae;class m extends(ae=c,oe=Symbol.toStringTag,ae){constructor(e,t,s="RuntimeException"){super(e,t,s);a(this,oe,"RuntimeException")}}var le,ue;class Qe extends(ue=m,le=Symbol.toStringTag,ue){constructor(e,t,s="EnvironmentException"){super(e,t,s);a(this,le,"EnvironmentException")}}var ce,he;class He extends(he=c,ce=Symbol.toStringTag,he){constructor(e,t,s="TimeoutException"){super(e,t,s);a(this,ce,"TimeoutException")}}var fe,de;class ut extends(de=c,fe=Symbol.toStringTag,de){constructor(e,t,s="TypeException"){super(e,t,s);a(this,fe,"TypeException")}}var me,we;class f extends(we=c,me=Symbol.toStringTag,we){constructor(e,t,s="ValueException"){super(e,t,s);a(this,me,"ValueException")}}var pe,_e;class b extends(_e=f,pe=Symbol.toStringTag,_e){constructor(e,t,s="RangeException"){super(e,t,s);a(this,pe,"RangeException")}}var ye;class u{constructor(n){a(this,"_iterator");a(this,ye,"SmartIterator");n instanceof Function?this._iterator=n():Symbol.iterator in n?this._iterator=n[Symbol.iterator]():this._iterator=n}every(n){let e=0;for(;;){const t=this._iterator.next();if(t.done)return!0;if(!n(t.value,e))return!1;e+=1}}some(n){let e=0;for(;;){const t=this._iterator.next();if(t.done)return!1;if(n(t.value,e))return!0;e+=1}}filter(n){const e=this._iterator;return new u(function*(){let t=0;for(;;){const s=e.next();if(s.done)return s.value;n(s.value,t)&&(yield s.value),t+=1}})}map(n){const e=this._iterator;return new u(function*(){let t=0;for(;;){const s=e.next();if(s.done)return s.value;yield n(s.value,t),t+=1}})}reduce(n,e){let t=0,s=e;if(s===void 0){const r=this._iterator.next();if(r.done)throw new f("Cannot reduce an empty iterator without an initial value.");s=r.value,t+=1}for(;;){const r=this._iterator.next();if(r.done)return s;s=n(s,r.value,t),t+=1}}flatMap(n){const e=this._iterator;return new u(function*(){let t=0;for(;;){const s=e.next();if(s.done)return s.value;const r=n(s.value,t);if(r instanceof Array)for(const o of r)yield o;else yield r;t+=1}})}drop(n){const e=this._iterator;return new u(function*(){let t=0;for(;t<n;){if(e.next().done)return;t+=1}for(;;){const s=e.next();if(s.done)return s.value;yield s.value}})}take(n){const e=this._iterator;return new u(function*(){let t=0;for(;t<n;){const s=e.next();if(s.done)return s.value;yield s.value,t+=1}})}find(n){let e=0;for(;;){const t=this._iterator.next();if(t.done)return;if(n(t.value,e))return t.value;e+=1}}enumerate(){return this.map((n,e)=>[e,n])}unique(){const n=this._iterator;return new u(function*(){const e=new Set;for(;;){const t=n.next();if(t.done)return t.value;e.has(t.value)||(e.add(t.value),yield t.value)}})}count(){let n=0;for(;;){if(this._iterator.next().done)return n;n+=1}}forEach(n){let e=0;for(;;){const t=this._iterator.next();if(t.done)return;n(t.value,e),e+=1}}next(...n){return this._iterator.next(...n)}return(n){return this._iterator.return?this._iterator.return(n):{done:!0,value:n}}throw(n){if(this._iterator.throw)return this._iterator.throw(n);throw n}groupBy(n){return new x(this.map((e,t)=>[n(e,t),e]))}toArray(){return Array.from(this)}[(ye=Symbol.toStringTag,Symbol.iterator)](){return this}}var be;be=Symbol.toStringTag;const y=class y{constructor(n){a(this,"_elements");a(this,be,"ReducedIterator");this._elements=new u(n)}every(n){for(const[e,[t,s]]of this._elements.enumerate())if(!n(t,s,e))return!1;return!0}some(n){for(const[e,[t,s]]of this._elements.enumerate())if(n(t,s,e))return!0;return!1}filter(n){const e=this._elements.enumerate();return new y(function*(){for(const[t,[s,r]]of e)n(s,r,t)&&(yield[s,r])})}map(n){const e=this._elements.enumerate();return new y(function*(){for(const[t,[s,r]]of e)yield[s,n(s,r,t)]})}reduce(n,e){let t=0,s=e;if(s===void 0){const r=this._elements.next();if(r.done)throw new f("Cannot reduce an empty iterator without an initial value.");s=r.value[1],t+=1}for(const[r,o]of this._elements)s=n(r,s,o,t),t+=1;return s}flatMap(n){const e=this._elements.enumerate();return new x(function*(){for(const[t,[s,r]]of e){const o=n(s,r,t);if(o instanceof Array)for(const l of o)yield[s,l];else yield[s,o]}})}drop(n){const e=this._elements.enumerate();return new y(function*(){for(const[t,[s,r]]of e)t>=n&&(yield[s,r])})}take(n){const e=this._elements.enumerate();return new y(function*(){for(const[t,[s,r]]of e){if(t>=n)break;yield[s,r]}})}find(n){for(const[e,[t,s]]of this._elements.enumerate())if(n(t,s,e))return s}enumerate(){return this.map((n,e,t)=>[t,e])}unique(){const n=this._elements;return new y(function*(){const e=new Set;for(const[t,s]of n)e.has(s)||(e.add(s),yield[t,s])})}count(){let n=0;for(const e of this._elements)n+=1;return n}forEach(n){for(const[e,[t,s]]of this._elements.enumerate())n(t,s,e)}reorganizeBy(n){const e=this._elements.enumerate();return new x(function*(){for(const[t,[s,r]]of e)yield[n(s,r,t),r]})}keys(){const n=this._elements;return new u(function*(){for(const[e]of n)yield e})}entries(){return this._elements}values(){const n=this._elements;return new u(function*(){for(const[e,t]of n)yield t})}toArray(){return Array.from(this.values())}toMap(){return new Map(this.entries())}toObject(){return Object.fromEntries(this.entries())}};let h=y;var ge;ge=Symbol.toStringTag;const w=class w{constructor(n){a(this,"_elements");a(this,ge,"AggregatedAsyncIterator");this._elements=new d(n)}async every(n){const e=new Map;for await(const[t,s]of this._elements){const[r,o]=e.get(t)??[0,!0];o&&e.set(t,[r+1,await n(t,s,r)])}return new h(function*(){for(const[t,[s,r]]of e)yield[t,r]})}async some(n){const e=new Map;for await(const[t,s]of this._elements){const[r,o]=e.get(t)??[0,!1];o||e.set(t,[r+1,await n(t,s,r)])}return new h(function*(){for(const[t,[s,r]]of e)yield[t,r]})}filter(n){const e=this._elements;return new w(async function*(){const t=new Map;for await(const[s,r]of e){const o=t.get(s)??0;await n(s,r,o)&&(yield[s,r]),t.set(s,o+1)}})}map(n){const e=this._elements;return new w(async function*(){const t=new Map;for await(const[s,r]of e){const o=t.get(s)??0;yield[s,await n(s,r,o)],t.set(s,o+1)}})}async reduce(n,e){const t=new Map;for await(const[s,r]of this._elements){let o,l;if(t.has(s))[o,l]=t.get(s);else if(e!==void 0)o=0,e instanceof Function?l=await e(s):l=await e;else{t.set(s,[0,r]);continue}t.set(s,[o+1,await n(s,l,r,o)])}return new h(function*(){for(const[s,[r,o]]of t)yield[s,o]})}flatMap(n){const e=this._elements;return new w(async function*(){const t=new Map;for await(const[s,r]of e){const o=t.get(s)??0,l=await n(s,r,o);if(l instanceof Array)for(const k of l)yield[s,k];else yield[s,l];t.set(s,o+1)}})}drop(n){const e=this._elements;return new w(async function*(){const t=new Map;for await(const[s,r]of e){const o=t.get(s)??0;if(o<n){t.set(s,o+1);continue}yield[s,r]}})}take(n){const e=this._elements;return new w(async function*(){const t=new Map;for await(const[s,r]of e){const o=t.get(s)??0;o>=n||(yield[s,r],t.set(s,o+1))}})}async find(n){const e=new Map;for await(const[t,s]of this._elements){let[r,o]=e.get(t)??[0,void 0];o===void 0&&(await n(t,s,r)&&(o=s),e.set(t,[r+1,o]))}return new h(function*(){for(const[t,[s,r]]of e)yield[t,r]})}enumerate(){return this.map((n,e,t)=>[t,e])}unique(){const n=this._elements;return new w(async function*(){const e=new Map;for await(const[t,s]of n){const r=e.get(t)??new Set;r.has(s)||(r.add(s),e.set(t,r),yield[t,s])}})}async count(){const n=new Map;for await(const[e]of this._elements){const t=n.get(e)??0;n.set(e,t+1)}return new h(function*(){for(const[e,t]of n)yield[e,t]})}async forEach(n){const e=new Map;for await(const[t,s]of this._elements){const r=e.get(t)??0;await n(t,s,r),e.set(t,r+1)}}reorganizeBy(n){const e=this._elements;return new w(async function*(){const t=new Map;for await(const[s,r]of e){const o=t.get(s)??0;yield[await n(s,r,o),r],t.set(s,o+1)}})}keys(){const n=this._elements;return new d(async function*(){const e=new Set;for await(const[t]of n)e.has(t)||(e.add(t),yield t)})}entries(){return this._elements}values(){const n=this._elements;return new d(async function*(){for await(const[e,t]of n)yield t})}async toArray(){const n=await this.toMap();return Array.from(n.values())}async toMap(){const n=new Map;for await(const[e,t]of this._elements){const s=n.get(e)??[];s.push(t),n.set(e,s)}return n}async toObject(){const n={};for await(const[e,t]of this._elements){const s=n[e]??[];s.push(t),n[e]=s}return n}};let T=w;var xe;class d{constructor(n){a(this,"_iterator");a(this,xe,"SmartAsyncIterator");if(n instanceof Function){const e=n();Symbol.asyncIterator in e?this._iterator=e:this._iterator=async function*(){let t=[];for(;;){const s=e.next(...t);if(s.done)return s.value;t=[yield s.value]}}()}else if(Symbol.asyncIterator in n)this._iterator=n[Symbol.asyncIterator]();else if(Symbol.iterator in n){const e=n[Symbol.iterator]();this._iterator=async function*(){for(;;){const t=e.next();if(t.done)return t.value;yield t.value}}()}else this._iterator=async function*(){let e=[];for(;;){const t=await n.next(...e);if(t.done)return t.value;e=[yield t.value]}}()}async every(n){let e=0;for(;;){const t=await this._iterator.next();if(t.done)return!0;if(!await n(t.value,e))return!1;e+=1}}async some(n){let e=0;for(;;){const t=await this._iterator.next();if(t.done)return!1;if(await n(t.value,e))return!0;e+=1}}filter(n){const e=this._iterator;return new d(async function*(){let t=0;for(;;){const s=await e.next();if(s.done)return s.value;await n(s.value,t)&&(yield s.value),t+=1}})}map(n){const e=this._iterator;return new d(async function*(){let t=0;for(;;){const s=await e.next();if(s.done)return s.value;yield await n(s.value,t),t+=1}})}async reduce(n,e){let t=0,s=e;if(s===void 0){const r=await this._iterator.next();if(r.done)throw new f("Cannot reduce an empty iterator without an initial value.");s=r.value,t+=1}for(;;){const r=await this._iterator.next();if(r.done)return s;s=await n(s,r.value,t),t+=1}}flatMap(n){const e=this._iterator;return new d(async function*(){let t=0;for(;;){const s=await e.next();if(s.done)return s.value;const r=await n(s.value,t);if(r instanceof Array)for(const o of r)yield o;else yield r;t+=1}})}drop(n){const e=this._iterator;return new d(async function*(){let t=0;for(;t<n;){if((await e.next()).done)return;t+=1}for(;;){const s=await e.next();if(s.done)return s.value;yield s.value}})}take(n){const e=this._iterator;return new d(async function*(){let t=0;for(;t<n;){const s=await e.next();if(s.done)return s.value;yield s.value,t+=1}})}async find(n){let e=0;for(;;){const t=await this._iterator.next();if(t.done)return;if(await n(t.value,e))return t.value;e+=1}}enumerate(){return this.map((n,e)=>[e,n])}unique(){const n=this._iterator;return new d(async function*(){const e=new Set;for(;;){const t=await n.next();if(t.done)return t.value;e.has(t.value)||(e.add(t.value),yield t.value)}})}async count(){let n=0;for(;;){if((await this._iterator.next()).done)return n;n+=1}}async forEach(n){let e=0;for(;;){const t=await this._iterator.next();if(t.done)return;await n(t.value,e),e+=1}}next(...n){return this._iterator.next(...n)}async return(n){const e=await n;return this._iterator.return?await this._iterator.return(e):{done:!0,value:e}}throw(n){if(this._iterator.throw)return this._iterator.throw(n);throw n}groupBy(n){return new T(this.map(async(e,t)=>[await n(e,t),e]))}toArray(){return Array.fromAsync(this)}[(xe=Symbol.toStringTag,Symbol.asyncIterator)](){return this}}var ve;ve=Symbol.toStringTag;const p=class p{constructor(n){a(this,"_elements");a(this,ve,"AggregatedIterator");this._elements=new u(n)}every(n){const e=new Map;for(const[t,s]of this._elements){const[r,o]=e.get(t)??[0,!0];o&&e.set(t,[r+1,n(t,s,r)])}return new h(function*(){for(const[t,[s,r]]of e)yield[t,r]})}some(n){const e=new Map;for(const[t,s]of this._elements){const[r,o]=e.get(t)??[0,!1];o||e.set(t,[r+1,n(t,s,r)])}return new h(function*(){for(const[t,[s,r]]of e)yield[t,r]})}filter(n){const e=this._elements;return new p(function*(){const t=new Map;for(const[s,r]of e){const o=t.get(s)??0;n(s,r,o)&&(yield[s,r]),t.set(s,o+1)}})}map(n){const e=this._elements;return new p(function*(){const t=new Map;for(const[s,r]of e){const o=t.get(s)??0;yield[s,n(s,r,o)],t.set(s,o+1)}})}reduce(n,e){const t=new Map;for(const[s,r]of this._elements){let o,l;if(t.has(s))[o,l]=t.get(s);else if(e!==void 0)o=0,e instanceof Function?l=e(s):l=e;else{t.set(s,[0,r]);continue}t.set(s,[o+1,n(s,l,r,o)])}return new h(function*(){for(const[s,[r,o]]of t)yield[s,o]})}flatMap(n){const e=this._elements;return new p(function*(){const t=new Map;for(const[s,r]of e){const o=t.get(s)??0,l=n(s,r,o);if(l instanceof Array)for(const k of l)yield[s,k];else yield[s,l];t.set(s,o+1)}})}drop(n){const e=this._elements;return new p(function*(){const t=new Map;for(const[s,r]of e){const o=t.get(s)??0;if(o<n){t.set(s,o+1);continue}yield[s,r]}})}take(n){const e=this._elements;return new p(function*(){const t=new Map;for(const[s,r]of e){const o=t.get(s)??0;o>=n||(yield[s,r],t.set(s,o+1))}})}find(n){const e=new Map;for(const[t,s]of this._elements){let[r,o]=e.get(t)??[0,void 0];o===void 0&&(n(t,s,r)&&(o=s),e.set(t,[r+1,o]))}return new h(function*(){for(const[t,[s,r]]of e)yield[t,r]})}enumerate(){return this.map((n,e,t)=>[t,e])}unique(){const n=this._elements;return new p(function*(){const e=new Map;for(const[t,s]of n){const r=e.get(t)??new Set;r.has(s)||(r.add(s),e.set(t,r),yield[t,s])}})}count(){const n=new Map;for(const[e]of this._elements){const t=n.get(e)??0;n.set(e,t+1)}return new h(function*(){for(const[e,t]of n)yield[e,t]})}forEach(n){const e=new Map;for(const[t,s]of this._elements){const r=e.get(t)??0;n(t,s,r),e.set(t,r+1)}}reorganizeBy(n){const e=this._elements;return new p(function*(){const t=new Map;for(const[s,r]of e){const o=t.get(s)??0;yield[n(s,r,o),r],t.set(s,o+1)}})}keys(){const n=this._elements;return new u(function*(){const e=new Set;for(const[t]of n)e.has(t)||(e.add(t),yield t)})}entries(){return this._elements}values(){const n=this._elements;return new u(function*(){for(const[e,t]of n)yield t})}toArray(){const n=this.toMap();return Array.from(n.values())}toMap(){const n=new Map;for(const[e,t]of this._elements){const s=n.get(e)??[];s.push(t),n.set(e,s)}return n}toObject(){const n={};for(const[e,t]of this._elements){const s=n[e]??[];s.push(t),n[e]=s}return n}};let x=p;const ct=Function;var Se,ke;class Xe extends(ke=ct,Se=Symbol.toStringTag,ke){constructor(){super("return this._invoke(...arguments);");a(this,Se,"CallableObject");const e=this.bind(this);return Object.setPrototypeOf(this,e),e}}var Ee;Ee=Symbol.toStringTag;const A=class A{constructor(){a(this,"_subscribers");a(this,Ee,"Publisher");this._subscribers=new Map}clear(){this.publish("__internals__:clear"),this._subscribers.clear()}createScope(){const n=new A;return this.subscribe("__internals__:clear",()=>n.clear()),this.subscribe("*",(e,...t)=>{n.publish(e,...t)}),n}publish(n,...e){let t,s=this._subscribers.get(n);return s?t=s.slice().map(r=>r(...e)):t=[],n.startsWith("__")||(s=this._subscribers.get("*"),s&&s.slice().forEach(r=>r(n,...e))),t}subscribe(n,e){const t=this._subscribers.get(n)??[];return t.push(e),this._subscribers.set(n,t),()=>{const s=t.indexOf(e);if(s<0)throw new E("Unable to unsubscribe the required subscriber. The subscription was already unsubscribed.");t.splice(s,1)}}unsubscribe(n,e){const t=this._subscribers.get(n);if(!t)throw new E("Unable to unsubscribe the required subscriber. The subscription was already unsubscribed or was never subscribed.");const s=t.indexOf(e);if(s<0)throw new E("Unable to unsubscribe the required subscriber. The subscription was already unsubscribed or was never subscribed.");t.splice(s,1),t.length===0&&this._subscribers.delete(n)}};let v=A;const ht=()=>{};var Te,Me;class ft extends(Me=Xe,Te=Symbol.toStringTag,Me){constructor(e,t="default"){super();a(this,"_callback");a(this,"_callbacks");a(this,"_isEnabled");a(this,"_key");a(this,"_invoke");a(this,Te,"SwitchableCallback");this._callbacks=new Map,this._isEnabled=!0,e?this._callbacks.set(t,e):(t="",e=()=>{throw new Ke("The `SwitchableCallback` has no callback defined yet. Did you forget to call the `register` method?")}),this._key=t,this._callback=e,this._invoke=(...s)=>this._callback(...s)}get isEnabled(){return this._isEnabled}get key(){return this._key}enable(e){if(e===void 0){if(!this._key)throw new _("The `SwitchableCallback` has no callback defined yet. Did you forget to call the `register` method?");e=this._key}else if(e){if(!this._callbacks.has(e))throw new _(`The key '${e}' doesn't yet have any associated callback.`)}else throw new _("The key must be a non-empty string.");if(this._isEnabled)throw new m("The `SwitchableCallback` is already enabled.");this._callback=this._callbacks.get(e),this._isEnabled=!0}disable(){if(!this._isEnabled)throw new m("The `SwitchableCallback` is already disabled.");this._callback=ht,this._isEnabled=!1}register(e,t){if(this._callbacks.size===0)this._key=e,this._callback=t;else if(this._callbacks.has(e))throw new _(`The key '${e}' has already been used for another callback.`);this._callbacks.set(e,t)}unregister(e){if(this._key===e)throw new _("Unable to unregister the currently selected callback.");if(!this._callbacks.has(e))throw new _(`The key '${e}' doesn't yet have any associated callback.`);this._callbacks.delete(e)}switch(e){if(!this._callbacks.has(e))throw new _(`The key '${e}' doesn't yet have any associated callback.`);this._key!==e&&(this._key=e,this._isEnabled&&(this._callback=this._callbacks.get(e)))}}var Re,Fe;class dt extends(Fe=Map,Re=Symbol.toStringTag,Fe){constructor(e){super();a(this,"_publisher");a(this,Re,"MapView");if(this._publisher=new v,e)for(const[t,s]of e)this.set(t,s)}set(e,t){return super.set(e,t),this._publisher.publish("entry:add",e,t),this}delete(e){const t=this.get(e);return t===void 0?!1:(super.delete(e),this._publisher.publish("entry:remove",e,t),!0)}clear(){const e=this.size;super.clear(),e>0&&this._publisher.publish("collection:clear")}subscribe(e,t){return this._publisher.subscribe(e,t)}unsubscribe(e,t){this._publisher.unsubscribe(e,t)}}var Pe,Ce;class mt extends(Ce=Set,Pe=Symbol.toStringTag,Ce){constructor(e){super();a(this,"_publisher");a(this,Pe,"SetView");if(this._publisher=new v,e)for(const t of e)this.add(t)}add(e){return super.add(e),this._publisher.publish("entry:add",e),this}delete(e){const t=super.delete(e);return t&&this._publisher.publish("entry:remove",e),t}clear(){const e=this.size;super.clear(),e>0&&this._publisher.publish("collection:clear")}subscribe(e,t){return this._publisher.subscribe(e,t)}unsubscribe(e,t){this._publisher.unsubscribe(e,t)}}var Ie;Ie=Symbol.toStringTag;class wt{constructor(n=!0){a(this,"_preferPersistence");a(this,"_volatile");a(this,"_persistent");a(this,Ie,"JSONStorage");if(!P)throw new Qe("The `JSONStorage` class can only be instantiated within a browser environment.");this._preferPersistence=n,this._volatile=window.sessionStorage,this._persistent=window.localStorage}_get(n,e,t){const s=n.getItem(e);if(s)try{return JSON.parse(s)}catch{console.warn(`The "${s}" value for "${e}" property cannot be parsed. Clearing the storage...`),n.removeItem(e)}return t}_set(n,e,t){const s=JSON.stringify(t);s?n.setItem(e,s):n.removeItem(e)}get(n,e,t=this._preferPersistence){const s=t?this._persistent:this._volatile;return this._get(s,n,e)}recall(n,e){return this._get(this._volatile,n,e)}retrieve(n,e){return this.recall(n)??this.read(n,e)}read(n,e){return this._get(this._persistent,n,e)}has(n,e){return(e?this._persistent:this._volatile).getItem(n)!==null}knows(n){return this._volatile.getItem(n)!==null}find(n){return this.knows(n)??this.exists(n)}exists(n){return this._persistent.getItem(n)!==null}set(n,e,t=this._preferPersistence){const s=t?this._persistent:this._volatile;this._set(s,n,e)}remember(n,e){this._set(this._volatile,n,e)}write(n,e){this._set(this._persistent,n,e)}delete(n,e){(e?this._persistent:this._volatile).removeItem(n)}forget(n){this._volatile.removeItem(n)}erase(n){this._persistent.removeItem(n)}clear(n){this._volatile.removeItem(n),this._persistent.removeItem(n)}}var je;je=Symbol.toStringTag;const N=class N{constructor(n){a(this,"_isPending");a(this,"_isFulfilled");a(this,"_isRejected");a(this,"_promise");a(this,je,"SmartPromise");this._isPending=!0,this._isFulfilled=!1,this._isRejected=!1;const e=s=>(this._isPending=!1,this._isFulfilled=!0,s),t=s=>{throw this._isPending=!1,this._isRejected=!0,s};this._promise=new Promise(n).then(e,t)}static FromPromise(n){return new N((e,t)=>n.then(e,t))}get isPending(){return this._isPending}get isFulfilled(){return this._isFulfilled}get isRejected(){return this._isRejected}then(n,e){return this._promise.then(n,e)}catch(n){return this._promise.catch(n)}finally(n){return this._promise.finally(n)}};let S=N;var Ae,Ne;class I extends(Ne=S,Ae=Symbol.toStringTag,Ne){constructor(e,t){let s,r;super((o,l)=>{s=o,r=l});a(this,"_resolve");a(this,"_reject");a(this,Ae,"DeferredPromise");this._promise=this._promise.then(e,t),this._resolve=s,this._reject=r}get resolve(){return this._resolve}get reject(){return this._reject}watch(e){return e.then(this.resolve,this.reject),this}}var Oe,qe;class Ze extends(qe=S,Oe=Symbol.toStringTag,qe){constructor(e,t){super((s,r)=>{const o=R=>{clearTimeout(q),s(R)},l=R=>{clearTimeout(q),r(R)},q=setTimeout(()=>l(new He("The operation has timed out.")),t);e(o,l)});a(this,Oe,"TimedPromise")}}var ze,$e;class pt extends($e=S,ze=Symbol.toStringTag,$e){constructor(){super(e=>e());a(this,"_count");a(this,ze,"PromiseQueue");this._count=0,this._isPending=!1,this._isFulfilled=!1,this._isRejected=!1}get isPending(){return this._count>0}get isFulfilled(){return this._count===0}get isRejected(){throw new f("`PromiseQueue` doesn't support rejection states.")}enqueue(e,t){if(this._count+=1,e instanceof I){const r=e;e=()=>(r.resolve(),r)}const s=(r,o)=>{this._promise=this._promise.then(e).then(l=>{this._count-=1,r(l)}).catch(l=>{this._count-=1,o(l)})};return t?new Ze(s,t):new Promise(s)}}var M=(i=>(i[i.Millisecond=1]="Millisecond",i[i.Second=1e3]="Second",i[i.Minute=6e4]="Minute",i[i.Hour=36e5]="Hour",i[i.Day=864e5]="Day",i[i.Week=6048e5]="Week",i[i.Month=2592e6]="Month",i[i.Year=31536e6]="Year",i))(M||{}),We=(i=>(i[i.Sunday=0]="Sunday",i[i.Monday=1]="Monday",i[i.Tuesday=2]="Tuesday",i[i.Wednesday=3]="Wednesday",i[i.Thursday=4]="Thursday",i[i.Friday=5]="Friday",i[i.Saturday=6]="Saturday",i))(We||{});function _t(i,n,e=864e5){let t;return i=new Date(i),n=new Date(n),i<n?t=Math.floor:t=Math.ceil,t((n.getTime()-i.getTime())/e)}function yt(i,n,e=864e5){if(i=new Date(i),n=new Date(n),i>=n)throw new b("The end date must be greater than the start date.");return new u(function*(){const t=n.getTime();let s=i.getTime();for(;s<t;)yield new Date(s),s+=e})}function Ue(i,n=864e5){if(n<=1)throw new b("Rounding a timestamp by milliseconds or less makes no sense.Use the timestamp value directly instead.");if(n>864e5)throw new b("Rounding by more than a day leads to unexpected results. Consider using other methods to round dates by weeks, months or years.");return i=new Date(i),new Date(Math.floor(i.getTime()/n)*n)}function bt(i,n=0){i=new Date(i);const e=7-n,t=(i.getUTCDay()+e)%7,s=i.getTime()-864e5*t;return Ue(new Date(s))}var De;De=Symbol.toStringTag;class j{constructor(n,e=40){a(this,"_handle");a(this,"_startTime");a(this,"_isRunning");a(this,"_publisher");a(this,"_start");a(this,"_stop");a(this,De,"GameLoop");this._startTime=0,this._isRunning=!1,P?(this._start=()=>{n(this.elapsedTime),this._handle=window.requestAnimationFrame(this._start)},this._stop=()=>window.cancelAnimationFrame(this._handle)):(console.warn(`Not a browser environment detected. Using setInterval@${e}ms instead of requestAnimationFrame...`),this._start=()=>{this._handle=setInterval(()=>n(this.elapsedTime),e)},this._stop=()=>clearInterval(this._handle)),this._publisher=new v}get startTime(){return this._startTime}get isRunning(){return this._isRunning}get elapsedTime(){return performance.now()-this._startTime}start(n=0){if(this._isRunning)throw new m("The game loop has already been started.");this._startTime=performance.now()-n,this._start(),this._isRunning=!0,this._publisher.publish("start")}stop(){if(!this._isRunning)throw new m("The game loop had already stopped or hadn't yet started.");if(!this._handle)throw new g;this._stop(),this._handle=void 0,this._isRunning=!1,this._publisher.publish("stop")}onStart(n){return this._publisher.subscribe("start",n)}onStop(n){return this._publisher.subscribe("stop",n)}}var Ve,Be;class gt extends(Be=j,Ve=Symbol.toStringTag,Be){constructor(e=M.Second){super(t=>this._publisher.publish("tick",t),e);a(this,Ve,"Clock")}start(e=0){if(this._isRunning)throw new m("The clock has already been started.");this._startTime=performance.now()-e,this._start(),this._isRunning=!0,this._publisher.publish("start")}stop(){if(!this._isRunning)throw new m("The clock had already stopped or hadn't yet started.");if(!this._handle)throw new g;this._stop(),this._handle=void 0,this._isRunning=!1,this._publisher.publish("stop")}onTick(e,t=0){if(t<0)throw new b("The tick step must be a non-negative number.");if(t===0)return this._publisher.subscribe("tick",e);let s=0;return this._publisher.subscribe("tick",r=>{r-s<t||(e(r),s=r)})}}var Je,Le;class xt extends(Le=j,Je=Symbol.toStringTag,Le){constructor(e,t=M.Second){super(()=>{const r=this.remainingTime;r<=0?(this._deferrerStop(),this._publisher.publish("tick",0),this._publisher.publish("expire")):this._publisher.publish("tick",r)},t);a(this,"_duration");a(this,"_deferrer");a(this,Je,"Countdown");this._duration=e}get duration(){return this._duration}get remainingTime(){return this._duration-this.elapsedTime}_deferrerStop(e){if(!this._isRunning)throw new m("The countdown hadn't yet started.");if(!this._deferrer)throw new g;this._stop(),this._handle=void 0,this._isRunning=!1,e!==void 0?this._deferrer.reject(e):this._deferrer.resolve(),this._deferrer=void 0}start(e=this.duration){if(this._isRunning)throw new m("The countdown had already stopped or hadn't yet started.");if(this._deferrer)throw new g;return this._deferrer=new I,super.start(this.duration-e),this._publisher.publish("start"),this._deferrer}stop(e){this._deferrerStop(e),this._publisher.publish("stop",e)}onExpire(e){return this._publisher.subscribe("expire",e)}onTick(e,t=0){if(t<0)throw new b("The tick step must be a non-negative number.");if(t===0)return this._publisher.subscribe("tick",e);let s=this.remainingTime;return this._publisher.subscribe("tick",r=>{s-r<t||(e(r),s=r)})}}var Ye;Ye=Symbol.toStringTag;class vt{constructor(){a(this,Ye,"Curve")}static Linear(n){const e=n-1;return new u(function*(){for(let t=0;t<n;t+=1)yield t/e})}static Exponential(n,e=2){if(e<0)throw new f("The base of the exponential curve cannot be negative.");const t=n-1;return new u(function*(){for(let s=0;s<n;s+=1)yield Math.pow(s/t,e)})}}var Ge;Ge=Symbol.toStringTag;const O=class O{constructor(){a(this,Ge,"Random")}static Boolean(n=.5){return Math.random()<n}static Integer(n,e){return Math.floor(e===void 0?Math.random()*n:Math.random()*(e-n)+n)}static Decimal(n,e){return n===void 0?Math.random():e===void 0?Math.random()*n:Math.random()*(e-n)+n}static Index(n){if(n.length===0)throw new f("You must provide at least one element.");return this.Integer(n.length)}static Choice(n){return n[O.Index(n)]}};let F=O;function St(i){return new Promise(n=>setTimeout(n,i))}function kt(){return new Promise(i=>requestAnimationFrame(()=>i()))}function Et(){return new Promise(i=>setTimeout(i))}function Tt(i,n="text/javascript"){return new Promise((e,t)=>{const s=document.createElement("script");s.async=!0,s.defer=!0,s.src=i,s.type=n,s.onload=r=>e(),s.onerror=r=>t(r),document.body.appendChild(s)})}function Mt(...i){return new u(function*(){for(const n of i)for(const e of n)yield e})}function Rt(i){if(i instanceof Array)return i.length;let n=0;for(const e of i)n+=1;return n}function Ft(i){return new u(function*(){let n=0;for(const e of i)yield[n,e],n+=1})}function Pt(i,n,e=1){if(e<=0)throw new b("Step must be always a positive number, even when generating numbers in reverse order.");return n===void 0&&(n=i,i=0),i>n?new u(function*(){for(let t=i;t>n;t-=e)yield t}):new u(function*(){for(let t=i;t<n;t+=e)yield t})}function Ct(i){const n=Array.from(i);for(let e=n.length-1;e>0;e-=1){const t=Math.floor(Math.random()*(e+1));[n[e],n[t]]=[n[t],n[e]]}return n}function It(i){return new u(function*(){const n=new Set;for(const e of i)n.has(e)||(n.add(e),yield e)})}function et(i,n){const e=i[Symbol.iterator](),t=n[Symbol.iterator]();return new u(function*(){for(;;){const s=e.next(),r=t.next();if(s.done||r.done)break;yield[s.value,r.value]}})}function jt(i,n){if(n===void 0){let r=0,o=0;for(const l of i)r+=l,o+=1;if(o===0)throw new f("You must provide at least one value.");return r/o}let e=0,t=0,s=0;for(const[r,o]of et(i,n)){if(o<=0)throw new f(`The weight for the value #${s} must be greater than zero.`);e+=r*o,t+=o,s+=1}if(s===0)throw new f("You must provide at least one value and weight.");if(t<=0)throw new f("The sum of weights must be greater than zero.");return e/t}function At(i){let n=0;for(let e=0;e<i.length;e+=1){const t=i.charCodeAt(e);n=(n<<5)-n+t,n|=0}return n}function Nt(i){let n=0;for(const e of i)n+=e;return n}function Ot(i){return`${i.charAt(0).toUpperCase()}${i.slice(1)}`}const qt="2.1.7";exports.AggregatedAsyncIterator=T;exports.AggregatedIterator=x;exports.CallableObject=Xe;exports.Clock=gt;exports.Countdown=xt;exports.Curve=vt;exports.DeferredPromise=I;exports.EnvironmentException=Qe;exports.Exception=c;exports.FatalErrorException=g;exports.FileException=C;exports.FileExistsException=it;exports.FileNotFoundException=ot;exports.GameLoop=j;exports.JSONStorage=wt;exports.KeyException=_;exports.MapView=dt;exports.NetworkException=at;exports.NotImplementedException=Ke;exports.PermissionException=lt;exports.PromiseQueue=pt;exports.Publisher=v;exports.Random=F;exports.RangeException=b;exports.ReducedIterator=h;exports.ReferenceException=E;exports.RuntimeException=m;exports.SetView=mt;exports.SmartAsyncIterator=d;exports.SmartIterator=u;exports.SmartPromise=S;exports.SwitchableCallback=ft;exports.TimeUnit=M;exports.TimedPromise=Ze;exports.TimeoutException=He;exports.TypeException=ut;exports.VERSION=qt;exports.ValueException=f;exports.WeekDay=We;exports.average=jt;exports.capitalize=Ot;exports.chain=Mt;exports.count=Rt;exports.dateDifference=_t;exports.dateRange=yt;exports.dateRound=Ue;exports.delay=St;exports.enumerate=Ft;exports.getWeek=bt;exports.hash=At;exports.isBrowser=P;exports.isNode=st;exports.isWorker=rt;exports.loadScript=Tt;exports.nextAnimationFrame=kt;exports.range=Pt;exports.shuffle=Ct;exports.sum=Nt;exports.unique=It;exports.yieldToEventLoop=Et;exports.zip=et;
//# sourceMappingURL=core.cjs.map