@byloth/core
Version:
An unopinionated collection of useful functions and classes that I use widely in all my projects. 🔧
3 lines (2 loc) • 31.7 kB
JavaScript
(function(a,b){typeof exports=="object"&&typeof module<"u"?b(exports):typeof define=="function"&&define.amd?define(["exports"],b):(a=typeof globalThis<"u"?globalThis:a||self,b(a.Core={}))})(this,(function(a){"use strict";const b=typeof window<"u"&&typeof window.document<"u",D=typeof process<"u"&&!!process.versions?.node,$=typeof self=="object"&&self.constructor?.name==="DedicatedWorkerGlobalScope";class h extends Error{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.cause=e.cause,t.name=e.name,t}return new h(`${e}`)}constructor(e,t,n="Exception"){super(e),this.cause=t,this.name=n}[Symbol.toStringTag]="Exception"}class v extends h{constructor(e,t,n="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,n)}[Symbol.toStringTag]="FatalErrorException"}class j extends v{constructor(e,t,n="NotImplementedException"){e===void 0&&(e="This feature isn't implemented yet. Please, try again later."),super(e,t,n)}[Symbol.toStringTag]="NotImplementedException"}class E extends h{constructor(e,t,n="FileException"){super(e,t,n)}[Symbol.toStringTag]="FileException"}class V extends E{constructor(e,t,n="FileExistsException"){super(e,t,n)}[Symbol.toStringTag]="FileExistsException"}class B extends E{constructor(e,t,n="FileNotFoundException"){super(e,t,n)}[Symbol.toStringTag]="FileNotFoundException"}class p extends h{constructor(e,t,n="KeyException"){super(e,t,n)}[Symbol.toStringTag]="KeyException"}class J extends h{constructor(e,t,n="NetworkException"){super(e,t,n)}[Symbol.toStringTag]="NetworkException"}class L extends h{constructor(e,t,n="PermissionException"){super(e,t,n)}[Symbol.toStringTag]="PermissionException"}class k extends h{constructor(e,t,n="ReferenceException"){super(e,t,n)}[Symbol.toStringTag]="ReferenceException"}class _ extends h{constructor(e,t,n="RuntimeException"){super(e,t,n)}[Symbol.toStringTag]="RuntimeException"}class N extends _{constructor(e,t,n="EnvironmentException"){super(e,t,n)}[Symbol.toStringTag]="EnvironmentException"}class O extends h{constructor(e,t,n="TimeoutException"){super(e,t,n)}[Symbol.toStringTag]="TimeoutException"}class Y extends h{constructor(e,t,n="TypeException"){super(e,t,n)}[Symbol.toStringTag]="TypeException"}class f extends h{constructor(e,t,n="ValueException"){super(e,t,n)}[Symbol.toStringTag]="ValueException"}class g extends f{constructor(e,t,n="RangeException"){super(e,t,n)}[Symbol.toStringTag]="RangeException"}class l{_iterator;constructor(e){e instanceof Function?this._iterator=e():Symbol.iterator in e?this._iterator=e[Symbol.iterator]():this._iterator=e}every(e){let t=0;for(;;){const n=this._iterator.next();if(n.done)return!0;if(!e(n.value,t))return!1;t+=1}}some(e){let t=0;for(;;){const n=this._iterator.next();if(n.done)return!1;if(e(n.value,t))return!0;t+=1}}filter(e){const t=this._iterator;return new l(function*(){let n=0;for(;;){const s=t.next();if(s.done)return s.value;e(s.value,n)&&(yield s.value),n+=1}})}map(e){const t=this._iterator;return new l(function*(){let n=0;for(;;){const s=t.next();if(s.done)return s.value;yield e(s.value,n),n+=1}})}reduce(e,t){let n=0,s=t;if(s===void 0){const i=this._iterator.next();if(i.done)throw new f("Cannot reduce an empty iterator without an initial value.");s=i.value,n+=1}for(;;){const i=this._iterator.next();if(i.done)return s;s=e(s,i.value,n),n+=1}}flatMap(e){const t=this._iterator;return new l(function*(){let n=0;for(;;){const s=t.next();if(s.done)return s.value;const i=e(s.value,n);if(i instanceof Array)for(const o of i)yield o;else yield i;n+=1}})}drop(e){const t=this._iterator;return new l(function*(){let n=0;for(;n<e;){if(t.next().done)return;n+=1}for(;;){const s=t.next();if(s.done)return s.value;yield s.value}})}take(e){const t=this._iterator;return new l(function*(){let n=0;for(;n<e;){const s=t.next();if(s.done)return s.value;yield s.value,n+=1}})}find(e){let t=0;for(;;){const n=this._iterator.next();if(n.done)return;if(e(n.value,t))return n.value;t+=1}}enumerate(){return this.map((e,t)=>[t,e])}unique(){const e=this._iterator;return new l(function*(){const t=new Set;for(;;){const n=e.next();if(n.done)return n.value;t.has(n.value)||(t.add(n.value),yield n.value)}})}count(){let e=0;for(;;){if(this._iterator.next().done)return e;e+=1}}forEach(e){let t=0;for(;;){const n=this._iterator.next();if(n.done)return;e(n.value,t),t+=1}}next(...e){return this._iterator.next(...e)}return(e){return this._iterator.return?this._iterator.return(e):{done:!0,value:e}}throw(e){if(this._iterator.throw)return this._iterator.throw(e);throw e}groupBy(e){return new d(this.map((t,n)=>[e(t,n),t]))}toArray(){return Array.from(this)}[Symbol.toStringTag]="SmartIterator";[Symbol.iterator](){return this}}class c{_elements;constructor(e){this._elements=new l(e)}every(e){for(const[t,[n,s]]of this._elements.enumerate())if(!e(n,s,t))return!1;return!0}some(e){for(const[t,[n,s]]of this._elements.enumerate())if(e(n,s,t))return!0;return!1}filter(e){const t=this._elements.enumerate();return new c(function*(){for(const[n,[s,i]]of t)e(s,i,n)&&(yield[s,i])})}map(e){const t=this._elements.enumerate();return new c(function*(){for(const[n,[s,i]]of t)yield[s,e(s,i,n)]})}reduce(e,t){let n=0,s=t;if(s===void 0){const i=this._elements.next();if(i.done)throw new f("Cannot reduce an empty iterator without an initial value.");s=i.value[1],n+=1}for(const[i,o]of this._elements)s=e(i,s,o,n),n+=1;return s}flatMap(e){const t=this._elements.enumerate();return new d(function*(){for(const[n,[s,i]]of t){const o=e(s,i,n);if(o instanceof Array)for(const u of o)yield[s,u];else yield[s,o]}})}drop(e){const t=this._elements.enumerate();return new c(function*(){for(const[n,[s,i]]of t)n>=e&&(yield[s,i])})}take(e){const t=this._elements.enumerate();return new c(function*(){for(const[n,[s,i]]of t){if(n>=e)break;yield[s,i]}})}find(e){for(const[t,[n,s]]of this._elements.enumerate())if(e(n,s,t))return s}enumerate(){return this.map((e,t,n)=>[n,t])}unique(){const e=this._elements;return new c(function*(){const t=new Set;for(const[n,s]of e)t.has(s)||(t.add(s),yield[n,s])})}count(){let e=0;for(const t of this._elements)e+=1;return e}forEach(e){for(const[t,[n,s]]of this._elements.enumerate())e(n,s,t)}reorganizeBy(e){const t=this._elements.enumerate();return new d(function*(){for(const[n,[s,i]]of t)yield[e(s,i,n),i]})}keys(){const e=this._elements;return new l(function*(){for(const[t]of e)yield t})}entries(){return this._elements}values(){const e=this._elements;return new l(function*(){for(const[t,n]of e)yield n})}toArray(){return Array.from(this.values())}toMap(){return new Map(this.entries())}toObject(){return Object.fromEntries(this.entries())}[Symbol.toStringTag]="ReducedIterator"}class y{_elements;constructor(e){this._elements=new m(e)}async every(e){const t=new Map;for await(const[n,s]of this._elements){const[i,o]=t.get(n)??[0,!0];o&&t.set(n,[i+1,await e(n,s,i)])}return new c(function*(){for(const[n,[s,i]]of t)yield[n,i]})}async some(e){const t=new Map;for await(const[n,s]of this._elements){const[i,o]=t.get(n)??[0,!1];o||t.set(n,[i+1,await e(n,s,i)])}return new c(function*(){for(const[n,[s,i]]of t)yield[n,i]})}filter(e){const t=this._elements;return new y(async function*(){const n=new Map;for await(const[s,i]of t){const o=n.get(s)??0;await e(s,i,o)&&(yield[s,i]),n.set(s,o+1)}})}map(e){const t=this._elements;return new y(async function*(){const n=new Map;for await(const[s,i]of t){const o=n.get(s)??0;yield[s,await e(s,i,o)],n.set(s,o+1)}})}async reduce(e,t){const n=new Map;for await(const[s,i]of this._elements){let o,u;if(n.has(s))[o,u]=n.get(s);else if(t!==void 0)o=0,t instanceof Function?u=await t(s):u=await t;else{n.set(s,[0,i]);continue}n.set(s,[o+1,await e(s,u,i,o)])}return new c(function*(){for(const[s,[i,o]]of n)yield[s,o]})}flatMap(e){const t=this._elements;return new y(async function*(){const n=new Map;for await(const[s,i]of t){const o=n.get(s)??0,u=await e(s,i,o);if(u instanceof Array)for(const S of u)yield[s,S];else yield[s,u];n.set(s,o+1)}})}drop(e){const t=this._elements;return new y(async function*(){const n=new Map;for await(const[s,i]of t){const o=n.get(s)??0;if(o<e){n.set(s,o+1);continue}yield[s,i]}})}take(e){const t=this._elements;return new y(async function*(){const n=new Map;for await(const[s,i]of t){const o=n.get(s)??0;o>=e||(yield[s,i],n.set(s,o+1))}})}async find(e){const t=new Map;for await(const[n,s]of this._elements){let[i,o]=t.get(n)??[0,void 0];o===void 0&&(await e(n,s,i)&&(o=s),t.set(n,[i+1,o]))}return new c(function*(){for(const[n,[s,i]]of t)yield[n,i]})}enumerate(){return this.map((e,t,n)=>[n,t])}unique(){const e=this._elements;return new y(async function*(){const t=new Map;for await(const[n,s]of e){const i=t.get(n)??new Set;i.has(s)||(i.add(s),t.set(n,i),yield[n,s])}})}async count(){const e=new Map;for await(const[t]of this._elements){const n=e.get(t)??0;e.set(t,n+1)}return new c(function*(){for(const[t,n]of e)yield[t,n]})}async forEach(e){const t=new Map;for await(const[n,s]of this._elements){const i=t.get(n)??0;await e(n,s,i),t.set(n,i+1)}}reorganizeBy(e){const t=this._elements;return new y(async function*(){const n=new Map;for await(const[s,i]of t){const o=n.get(s)??0;yield[await e(s,i,o),i],n.set(s,o+1)}})}keys(){const e=this._elements;return new m(async function*(){const t=new Set;for await(const[n]of e)t.has(n)||(t.add(n),yield n)})}entries(){return this._elements}values(){const e=this._elements;return new m(async function*(){for await(const[t,n]of e)yield n})}async toArray(){const e=await this.toMap();return Array.from(e.values())}async toMap(){const e=new Map;for await(const[t,n]of this._elements){const s=e.get(t)??[];s.push(n),e.set(t,s)}return e}async toObject(){const e={};for await(const[t,n]of this._elements){const s=e[t]??[];s.push(n),e[t]=s}return e}[Symbol.toStringTag]="AggregatedAsyncIterator"}class m{_iterator;constructor(e){if(e instanceof Function){const t=e();Symbol.asyncIterator in t?this._iterator=t:this._iterator=(async function*(){let n=[];for(;;){const s=t.next(...n);if(s.done)return s.value;n=[yield s.value]}})()}else if(Symbol.asyncIterator in e)this._iterator=e[Symbol.asyncIterator]();else if(Symbol.iterator in e){const t=e[Symbol.iterator]();this._iterator=(async function*(){for(;;){const n=t.next();if(n.done)return n.value;yield n.value}})()}else this._iterator=(async function*(){let t=[];for(;;){const n=await e.next(...t);if(n.done)return n.value;t=[yield n.value]}})()}async every(e){let t=0;for(;;){const n=await this._iterator.next();if(n.done)return!0;if(!await e(n.value,t))return!1;t+=1}}async some(e){let t=0;for(;;){const n=await this._iterator.next();if(n.done)return!1;if(await e(n.value,t))return!0;t+=1}}filter(e){const t=this._iterator;return new m(async function*(){let n=0;for(;;){const s=await t.next();if(s.done)return s.value;await e(s.value,n)&&(yield s.value),n+=1}})}map(e){const t=this._iterator;return new m(async function*(){let n=0;for(;;){const s=await t.next();if(s.done)return s.value;yield await e(s.value,n),n+=1}})}async reduce(e,t){let n=0,s=t;if(s===void 0){const i=await this._iterator.next();if(i.done)throw new f("Cannot reduce an empty iterator without an initial value.");s=i.value,n+=1}for(;;){const i=await this._iterator.next();if(i.done)return s;s=await e(s,i.value,n),n+=1}}flatMap(e){const t=this._iterator;return new m(async function*(){let n=0;for(;;){const s=await t.next();if(s.done)return s.value;const i=await e(s.value,n);if(i instanceof Array)for(const o of i)yield o;else yield i;n+=1}})}drop(e){const t=this._iterator;return new m(async function*(){let n=0;for(;n<e;){if((await t.next()).done)return;n+=1}for(;;){const s=await t.next();if(s.done)return s.value;yield s.value}})}take(e){const t=this._iterator;return new m(async function*(){let n=0;for(;n<e;){const s=await t.next();if(s.done)return s.value;yield s.value,n+=1}})}async find(e){let t=0;for(;;){const n=await this._iterator.next();if(n.done)return;if(await e(n.value,t))return n.value;t+=1}}enumerate(){return this.map((e,t)=>[t,e])}unique(){const e=this._iterator;return new m(async function*(){const t=new Set;for(;;){const n=await e.next();if(n.done)return n.value;t.has(n.value)||(t.add(n.value),yield n.value)}})}async count(){let e=0;for(;;){if((await this._iterator.next()).done)return e;e+=1}}async forEach(e){let t=0;for(;;){const n=await this._iterator.next();if(n.done)return;await e(n.value,t),t+=1}}next(...e){return this._iterator.next(...e)}async return(e){const t=await e;return this._iterator.return?await this._iterator.return(t):{done:!0,value:t}}throw(e){if(this._iterator.throw)return this._iterator.throw(e);throw e}groupBy(e){return new y(this.map(async(t,n)=>[await e(t,n),t]))}toArray(){return Array.fromAsync(this)}[Symbol.toStringTag]="SmartAsyncIterator";[Symbol.asyncIterator](){return this}}class d{_elements;constructor(e){this._elements=new l(e)}every(e){const t=new Map;for(const[n,s]of this._elements){const[i,o]=t.get(n)??[0,!0];o&&t.set(n,[i+1,e(n,s,i)])}return new c(function*(){for(const[n,[s,i]]of t)yield[n,i]})}some(e){const t=new Map;for(const[n,s]of this._elements){const[i,o]=t.get(n)??[0,!1];o||t.set(n,[i+1,e(n,s,i)])}return new c(function*(){for(const[n,[s,i]]of t)yield[n,i]})}filter(e){const t=this._elements;return new d(function*(){const n=new Map;for(const[s,i]of t){const o=n.get(s)??0;e(s,i,o)&&(yield[s,i]),n.set(s,o+1)}})}map(e){const t=this._elements;return new d(function*(){const n=new Map;for(const[s,i]of t){const o=n.get(s)??0;yield[s,e(s,i,o)],n.set(s,o+1)}})}reduce(e,t){const n=new Map;for(const[s,i]of this._elements){let o,u;if(n.has(s))[o,u]=n.get(s);else if(t!==void 0)o=0,t instanceof Function?u=t(s):u=t;else{n.set(s,[0,i]);continue}n.set(s,[o+1,e(s,u,i,o)])}return new c(function*(){for(const[s,[i,o]]of n)yield[s,o]})}flatMap(e){const t=this._elements;return new d(function*(){const n=new Map;for(const[s,i]of t){const o=n.get(s)??0,u=e(s,i,o);if(u instanceof Array)for(const S of u)yield[s,S];else yield[s,u];n.set(s,o+1)}})}drop(e){const t=this._elements;return new d(function*(){const n=new Map;for(const[s,i]of t){const o=n.get(s)??0;if(o<e){n.set(s,o+1);continue}yield[s,i]}})}take(e){const t=this._elements;return new d(function*(){const n=new Map;for(const[s,i]of t){const o=n.get(s)??0;o>=e||(yield[s,i],n.set(s,o+1))}})}find(e){const t=new Map;for(const[n,s]of this._elements){let[i,o]=t.get(n)??[0,void 0];o===void 0&&(e(n,s,i)&&(o=s),t.set(n,[i+1,o]))}return new c(function*(){for(const[n,[s,i]]of t)yield[n,i]})}enumerate(){return this.map((e,t,n)=>[n,t])}unique(){const e=this._elements;return new d(function*(){const t=new Map;for(const[n,s]of e){const i=t.get(n)??new Set;i.has(s)||(i.add(s),t.set(n,i),yield[n,s])}})}count(){const e=new Map;for(const[t]of this._elements){const n=e.get(t)??0;e.set(t,n+1)}return new c(function*(){for(const[t,n]of e)yield[t,n]})}forEach(e){const t=new Map;for(const[n,s]of this._elements){const i=t.get(n)??0;e(n,s,i),t.set(n,i+1)}}reorganizeBy(e){const t=this._elements;return new d(function*(){const n=new Map;for(const[s,i]of t){const o=n.get(s)??0;yield[e(s,i,o),i],n.set(s,o+1)}})}keys(){const e=this._elements;return new l(function*(){const t=new Set;for(const[n]of e)t.has(n)||(t.add(n),yield n)})}entries(){return this._elements}values(){const e=this._elements;return new l(function*(){for(const[t,n]of e)yield n})}toArray(){const e=this.toMap();return Array.from(e.values())}toMap(){const e=new Map;for(const[t,n]of this._elements){const s=e.get(t)??[];s.push(n),e.set(t,s)}return e}toObject(){const e={};for(const[t,n]of this._elements){const s=e[t]??[];s.push(n),e[t]=s}return e}[Symbol.toStringTag]="AggregatedIterator"}const G=Function;class M extends G{constructor(){super("return this._invoke(...arguments);");const e=this.bind(this);return Object.setPrototypeOf(this,e),e}[Symbol.toStringTag]="CallableObject"}class K extends M{_callbacks;get size(){return this._callbacks.length}constructor(...e){super(),this._callbacks=e}_invoke(...e){return this._callbacks.map(t=>t(...e))}add(e){return this._callbacks.push(e),this}remove(e){const t=this._callbacks.indexOf(e);return t<0?!1:(this._callbacks.splice(t,1),!0)}clear(){this._callbacks.length=0}[Symbol.toStringTag]="CallbackChain"}class x{_subscribers;constructor(){this._subscribers=new Map}createScope(){const e=new x;return this.subscribe("__internals__:clear",()=>e.clear()),this.subscribe("*",(t,...n)=>{e.publish(t,...n)}),e}publish(e,...t){let n,s=this._subscribers.get(e);return s?n=s.slice().map(i=>i(...t)):n=[],e.startsWith("__")||(s=this._subscribers.get("*"),s&&s.slice().forEach(i=>i(e,...t))),n}subscribe(e,t){const n=this._subscribers.get(e)??[];return n.push(t),this._subscribers.set(e,n),()=>{const s=n.indexOf(t);if(s<0)throw new k("Unable to unsubscribe the required subscriber. The subscription was already unsubscribed.");n.splice(s,1)}}unsubscribe(e,t){const n=this._subscribers.get(e);if(!n)throw new k("Unable to unsubscribe the required subscriber. The subscription was already unsubscribed or was never subscribed.");const s=n.indexOf(t);if(s<0)throw new k("Unable to unsubscribe the required subscriber. The subscription was already unsubscribed or was never subscribed.");n.splice(s,1),n.length===0&&this._subscribers.delete(e)}unsubscribeAll(e){this._subscribers.delete(e)}clear(){this.publish("__internals__:clear"),this._subscribers.clear()}[Symbol.toStringTag]="Publisher"}const Q=()=>{};class H extends M{_callback;_callbacks;_isEnabled;get isEnabled(){return this._isEnabled}_key;get key(){return this._key}_invoke;constructor(e,t="default"){super(),this._callbacks=new Map,this._isEnabled=!0,e?this._callbacks.set(t,e):(t="",e=(()=>{throw new j("The `SwitchableCallback` has no callback defined yet. Did you forget to call the `register` method?")})),this._key=t,this._callback=e,this._invoke=(...n)=>this._callback(...n)}enable(e){if(e===void 0){if(!this._key)throw new p("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 p(`The key '${e}' doesn't yet have any associated callback.`)}else throw new p("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=Q,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 p(`The key '${e}' has already been used for another callback.`);this._callbacks.set(e,t)}unregister(e){if(this._key===e)throw new p("Unable to unregister the currently selected callback.");if(!this._callbacks.has(e))throw new p(`The key '${e}' doesn't yet have any associated callback.`);this._callbacks.delete(e)}switch(e){if(!this._callbacks.has(e))throw new p(`The key '${e}' doesn't yet have any associated callback.`);this._key!==e&&(this._key=e,this._isEnabled&&(this._callback=this._callbacks.get(e)))}[Symbol.toStringTag]="SwitchableCallback"}class X extends Map{_publisher;constructor(e){if(super(),this._publisher=new x,e)for(const[t,n]of e)this.set(t,n)}set(e,t){return super.set(e,t),this._publisher.publish("add",e,t),this}delete(e){const t=this.get(e);return t===void 0?!1:(super.delete(e),this._publisher.publish("remove",e,t),!0)}clear(){const e=this.size;super.clear(),e>0&&this._publisher.publish("clear")}onAdd(e){return this._publisher.subscribe("add",e)}onRemove(e){return this._publisher.subscribe("remove",e)}onClear(e){return this._publisher.subscribe("clear",e)}[Symbol.toStringTag]="MapView"}class Z extends Set{_publisher;constructor(e){if(super(),this._publisher=new x,e)for(const t of e)this.add(t)}add(e){return super.add(e),this._publisher.publish("add",e),this}delete(e){const t=super.delete(e);return t&&this._publisher.publish("remove",e),t}clear(){const e=this.size;super.clear(),e>0&&this._publisher.publish("clear")}onAdd(e){return this._publisher.subscribe("add",e)}onRemove(e){return this._publisher.subscribe("remove",e)}onClear(e){return this._publisher.subscribe("clear",e)}[Symbol.toStringTag]="SetView"}class W{_preferPersistence;_volatile;_persistent;constructor(e=!0){if(!b)throw new N("The `JSONStorage` class can only be instantiated within a browser environment.");this._preferPersistence=e,this._volatile=window.sessionStorage,this._persistent=window.localStorage}_get(e,t,n){const s=e.getItem(t);if(s)try{return JSON.parse(s)}catch{console.warn(`The "${s}" value for "${t}" property cannot be parsed. Clearing the storage...`),e.removeItem(t)}return n}_set(e,t,n){const s=JSON.stringify(n);s?e.setItem(t,s):e.removeItem(t)}get(e,t,n=this._preferPersistence){const s=n?this._persistent:this._volatile;return this._get(s,e,t)}recall(e,t){return this._get(this._volatile,e,t)}retrieve(e,t){return this.recall(e)??this.read(e,t)}read(e,t){return this._get(this._persistent,e,t)}has(e,t){return(t?this._persistent:this._volatile).getItem(e)!==null}knows(e){return this._volatile.getItem(e)!==null}find(e){return this.knows(e)??this.exists(e)}exists(e){return this._persistent.getItem(e)!==null}set(e,t,n=this._preferPersistence){const s=n?this._persistent:this._volatile;this._set(s,e,t)}remember(e,t){this._set(this._volatile,e,t)}write(e,t){this._set(this._persistent,e,t)}delete(e,t){(t?this._persistent:this._volatile).removeItem(e)}forget(e){this._volatile.removeItem(e)}erase(e){this._persistent.removeItem(e)}clear(e){this._volatile.removeItem(e),this._persistent.removeItem(e)}[Symbol.toStringTag]="JSONStorage"}class w{static FromPromise(e){return new w((t,n)=>e.then(t,n))}_isPending;get isPending(){return this._isPending}_isFulfilled;get isFulfilled(){return this._isFulfilled}_isRejected;get isRejected(){return this._isRejected}_promise;constructor(e){this._isPending=!0,this._isFulfilled=!1,this._isRejected=!1;const t=s=>(this._isPending=!1,this._isFulfilled=!0,s),n=s=>{throw this._isPending=!1,this._isRejected=!0,s};this._promise=new Promise(e).then(t,n)}then(e,t){return this._promise.then(e,t)}catch(e){return this._promise.catch(e)}finally(e){return this._promise.finally(e)}[Symbol.toStringTag]="SmartPromise"}class R extends w{_resolve;get resolve(){return this._resolve}_reject;get reject(){return this._reject}constructor(e,t){let n,s;super((i,o)=>{n=i,s=o}),this._promise=this._promise.then(e,t),this._resolve=n,this._reject=s}watch(e){return e.then(this.resolve,this.reject),this}[Symbol.toStringTag]="DeferredPromise"}class I extends w{constructor(e,t){super((n,s)=>{const i=P=>{clearTimeout(S),n(P)},o=P=>{clearTimeout(S),s(P)},S=setTimeout(()=>o(new O("The operation has timed out.")),t);e(i,o)})}[Symbol.toStringTag]="TimedPromise"}class U extends w{_count;get isPending(){return this._count>0}get isFulfilled(){return this._count===0}get isRejected(){throw new f("`PromiseQueue` doesn't support rejection states.")}constructor(){super(e=>e()),this._count=0,this._isPending=!1,this._isFulfilled=!1,this._isRejected=!1}enqueue(e,t){if(this._count+=1,e instanceof R){const s=e;e=()=>(s.resolve(),s)}const n=(s,i)=>{this._promise=this._promise.then(e).then(o=>{this._count-=1,s(o)}).catch(o=>{this._count-=1,i(o)})};return t?new I(n,t):new w(n)}[Symbol.toStringTag]="PromiseQueue"}var T=(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))(T||{}),z=(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))(z||{});function ee(r,e,t=864e5){let n;return r=new Date(r),e=new Date(e),r<e?n=Math.floor:n=Math.ceil,n((e.getTime()-r.getTime())/t)}function te(r,e,t=864e5){if(r=new Date(r),e=new Date(e),r>=e)throw new g("The end date must be greater than the start date.");return new l(function*(){const n=e.getTime();let s=r.getTime();for(;s<n;)yield new Date(s),s+=t})}function q(r,e=864e5){if(e<=1)throw new g("Rounding a timestamp by milliseconds or less makes no sense.Use the timestamp value directly instead.");if(e>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()/e)*e)}function ne(r,e=0){r=new Date(r);const t=7-e,n=(r.getUTCDay()+t)%7,s=r.getTime()-864e5*n;return q(new Date(s))}class F{_handle;_startTime;get startTime(){return this._startTime}_isRunning;get isRunning(){return this._isRunning}get elapsedTime(){return performance.now()-this._startTime}_publisher;_start;_stop;constructor(e,t=40){this._startTime=0,this._isRunning=!1,b?(this._start=()=>{e(this.elapsedTime),this._handle=window.requestAnimationFrame(this._start)},this._stop=()=>window.cancelAnimationFrame(this._handle)):(console.warn(`Not a browser environment detected. Using setInterval@${t}ms instead of requestAnimationFrame...`),this._start=()=>{this._handle=setInterval(()=>e(this.elapsedTime),t)},this._stop=()=>clearInterval(this._handle)),this._publisher=new x}start(e=0){if(this._isRunning)throw new _("The game loop 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 game loop had already stopped or hadn't yet started.");if(!this._handle)throw new v;this._stop(),this._handle=void 0,this._isRunning=!1,this._publisher.publish("stop")}onStart(e){return this._publisher.subscribe("start",e)}onStop(e){return this._publisher.subscribe("stop",e)}[Symbol.toStringTag]="GameLoop"}class se extends F{constructor(e=T.Second){super(t=>this._publisher.publish("tick",t),e)}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 v;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 n=0;return this._publisher.subscribe("tick",s=>{s-n<t||(e(s),n=s)})}[Symbol.toStringTag]="Clock"}class ie extends F{_duration;get duration(){return this._duration}get remainingTime(){return this._duration-this.elapsedTime}_deferrer;constructor(e,t=T.Second){const n=()=>{const s=this.remainingTime;s<=0?(this._deferrerStop(),this._publisher.publish("tick",0),this._publisher.publish("expire")):this._publisher.publish("tick",s)};super(n,t),this._duration=e}_deferrerStop(e){if(!this._isRunning)throw new _("The countdown hadn't yet started.");if(!this._deferrer)throw new v;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 v;return this._deferrer=new R,super.start(this.duration-e),this._publisher.publish("start"),this._deferrer}stop(e){this._deferrerStop(e),this._publisher.publish("stop",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 n=this.remainingTime;return this._publisher.subscribe("tick",s=>{n-s<t||(e(s),n=s)})}onExpire(e){return this._publisher.subscribe("expire",e)}[Symbol.toStringTag]="Countdown"}class re{static Linear(e){const t=e-1;return new l(function*(){for(let n=0;n<e;n+=1)yield n/t})}static Exponential(e,t=2){if(t<0)throw new f("The base of the exponential curve cannot be negative.");const n=e-1;return new l(function*(){for(let s=0;s<e;s+=1)yield Math.pow(s/n,t)})}constructor(){}[Symbol.toStringTag]="Curve"}class C{static Boolean(e=.5){return Math.random()<e}static Integer(e,t){return Math.floor(t===void 0?Math.random()*e:Math.random()*(t-e)+e)}static Decimal(e,t){return e===void 0?Math.random():t===void 0?Math.random()*e:Math.random()*(t-e)+e}static Index(e){if(e.length===0)throw new f("You must provide at least one element.");return this.Integer(e.length)}static Choice(e){return e[C.Index(e)]}constructor(){}[Symbol.toStringTag]="Random"}function oe(r){return new w(e=>setTimeout(e,r))}function ae(){return new w(r=>requestAnimationFrame(()=>r()))}function le(){return new w(r=>setTimeout(r))}function ue(r,e="text/javascript"){return new w((t,n)=>{const s=document.createElement("script");s.async=!0,s.defer=!0,s.src=r,s.type=e,s.onload=i=>t(),s.onerror=i=>n(i),document.body.appendChild(s)})}function ce(...r){return new l(function*(){for(const e of r)for(const t of e)yield t})}function he(r){if(r instanceof Array)return r.length;let e=0;for(const t of r)e+=1;return e}function fe(r){return new l(function*(){let e=0;for(const t of r)yield[e,t],e+=1})}function de(r,e,t=1){if(t<=0)throw new g("Step must be always a positive number, even when generating numbers in reverse order.");return e===void 0&&(e=r,r=0),r>e?new l(function*(){for(let n=r;n>e;n-=t)yield n}):new l(function*(){for(let n=r;n<e;n+=t)yield n})}function me(r){const e=Array.from(r);for(let t=e.length-1;t>0;t-=1){const n=Math.floor(Math.random()*(t+1));[e[t],e[n]]=[e[n],e[t]]}return e}function we(r){return new l(function*(){const e=new Set;for(const t of r)e.has(t)||(e.add(t),yield t)})}function A(r,e){const t=r[Symbol.iterator](),n=e[Symbol.iterator]();return new l(function*(){for(;;){const s=t.next(),i=n.next();if(s.done||i.done)break;yield[s.value,i.value]}})}function _e(r,e){if(e===void 0){let i=0,o=0;for(const u of r)i+=u,o+=1;if(o===0)throw new f("You must provide at least one value.");return i/o}let t=0,n=0,s=0;for(const[i,o]of A(r,e)){if(o<=0)throw new f(`The weight for the value #${s} must be greater than zero.`);t+=i*o,n+=o,s+=1}if(s===0)throw new f("You must provide at least one value and weight.");if(n<=0)throw new f("The sum of weights must be greater than zero.");return t/n}function ye(r){let e=0;for(let t=0;t<r.length;t+=1){const n=r.charCodeAt(t);e=(e<<5)-e+n,e|=0}return e}function pe(r){let e=0;for(const t of r)e+=t;return e}function be(r){return`${r.charAt(0).toUpperCase()}${r.slice(1)}`}const ge="2.2.2";a.AggregatedAsyncIterator=y,a.AggregatedIterator=d,a.CallableObject=M,a.CallbackChain=K,a.Clock=se,a.Countdown=ie,a.Curve=re,a.DeferredPromise=R,a.EnvironmentException=N,a.Exception=h,a.FatalErrorException=v,a.FileException=E,a.FileExistsException=V,a.FileNotFoundException=B,a.GameLoop=F,a.JSONStorage=W,a.KeyException=p,a.MapView=X,a.NetworkException=J,a.NotImplementedException=j,a.PermissionException=L,a.PromiseQueue=U,a.Publisher=x,a.Random=C,a.RangeException=g,a.ReducedIterator=c,a.ReferenceException=k,a.RuntimeException=_,a.SetView=Z,a.SmartAsyncIterator=m,a.SmartIterator=l,a.SmartPromise=w,a.SwitchableCallback=H,a.TimeUnit=T,a.TimedPromise=I,a.TimeoutException=O,a.TypeException=Y,a.VERSION=ge,a.ValueException=f,a.WeekDay=z,a.average=_e,a.capitalize=be,a.chain=ce,a.count=he,a.dateDifference=ee,a.dateRange=te,a.dateRound=q,a.delay=oe,a.enumerate=fe,a.getWeek=ne,a.hash=ye,a.isBrowser=b,a.isNode=D,a.isWorker=$,a.loadScript=ue,a.nextAnimationFrame=ae,a.range=de,a.shuffle=me,a.sum=pe,a.unique=we,a.yieldToEventLoop=le,a.zip=A,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})}));
//# sourceMappingURL=core.umd.cjs.map