UNPKG

@byloth/core

Version:

An unopinionated collection of useful functions and classes that I use widely in all my projects. 🔧

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