@byloth/core
Version:
An unopinionated collection of useful functions and classes that I use widely in all my projects. 🔧
3 lines (2 loc) • 35.9 kB
JavaScript
var Core=(function(a){Object.defineProperty(a,Symbol.toStringTag,{value:"Module"});var P=typeof window<"u"&&typeof window.document<"u",X=typeof process<"u"&&!!process.versions?.node,Z=typeof self=="object"&&self.constructor?.name==="DedicatedWorkerGlobalScope",m=class F extends Error{static FromUnknown(e){if(e instanceof F)return e;if(e instanceof Error){const t=new F(e.message);return t.stack=e.stack,t.cause=e.cause,t.name=e.name,t}return new F(`${e}`)}constructor(e,t,n="Exception"){super(e),this.cause=t,this.name=n}[Symbol.toStringTag]="Exception"},T=class extends m{constructor(r,e,t="FatalErrorException"){r===void 0&&(r="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(r,e,t)}[Symbol.toStringTag]="FatalErrorException"},$=class extends T{constructor(r,e,t="NotImplementedException"){r===void 0&&(r="This feature isn't implemented yet. Please, try again later."),super(r,e,t)}[Symbol.toStringTag]="NotImplementedException"},j=class extends m{constructor(r,e,t="FileException"){super(r,e,t)}[Symbol.toStringTag]="FileException"},U=class extends j{constructor(r,e,t="FileExistsException"){super(r,e,t)}[Symbol.toStringTag]="FileExistsException"},ee=class extends j{constructor(r,e,t="FileNotFoundException"){super(r,e,t)}[Symbol.toStringTag]="FileNotFoundException"},p=class extends m{constructor(r,e,t="KeyException"){super(r,e,t)}[Symbol.toStringTag]="KeyException"},O=class extends m{constructor(r,e,t="NetworkException"){super(r,e,t)}[Symbol.toStringTag]="NetworkException"},te=class extends O{response;constructor(r,e,t="ResponseException"){let n;const s=r.url?` to "${r.url}"`:"",i=r.statusText?`${r.status} (${r.statusText})`:r.status;r.statusText?n=`The request${s} failed with status ${i}.`:n=`The request${s} failed with status ${i}.`,super(n,e,t),this.response=r}[Symbol.toStringTag]="ResponseException"},ne=class extends m{constructor(r,e,t="PermissionException"){super(r,e,t)}[Symbol.toStringTag]="PermissionException"},A=class extends m{constructor(r,e,t="ReferenceException"){super(r,e,t)}[Symbol.toStringTag]="ReferenceException"},_=class extends m{constructor(r,e,t="RuntimeException"){super(r,e,t)}[Symbol.toStringTag]="RuntimeException"},q=class extends _{constructor(r,e,t="EnvironmentException"){super(r,e,t)}[Symbol.toStringTag]="EnvironmentException"},V=class extends m{constructor(r,e,t="TimeoutException"){super(r,e,t)}[Symbol.toStringTag]="TimeoutException"},re=class extends m{constructor(r,e,t="TypeException"){super(r,e,t)}[Symbol.toStringTag]="TypeException"},h=class extends m{constructor(r,e,t="ValueException"){super(r,e,t)}[Symbol.toStringTag]="ValueException"},x=class extends h{constructor(r,e,t="RangeException"){super(r,e,t)}[Symbol.toStringTag]="RangeException"},d=class S{_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 S(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 S(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 h("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 S(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 S(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 S(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 S(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 C(this.map((t,n)=>[e(t,n),t]))}toArray(){return Array.from(this)}[Symbol.toStringTag]="SmartIterator";[Symbol.iterator](){return this}},w=class E{_elements;constructor(e){this._elements=new d(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 E(function*(){for(const[n,[s,i]]of t)e(s,i,n)&&(yield[s,i])})}map(e){const t=this._elements.enumerate();return new E(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 h("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 C(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 E(function*(){for(const[n,[s,i]]of t)n>=e&&(yield[s,i])})}take(e){const t=this._elements.enumerate();return new E(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 E(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 C(function*(){for(const[n,[s,i]]of t)yield[e(s,i,n),i]})}keys(){const e=this._elements;return new d(function*(){for(const[t]of e)yield t})}entries(){return this._elements}values(){const e=this._elements;return new d(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"},B=class g{_elements;constructor(e){this._elements=new R(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 w(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 w(function*(){for(const[n,[s,i]]of t)yield[n,i]})}filter(e){const t=this._elements;return new g(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 g(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 w(function*(){for(const[s,[i,o]]of n)yield[s,o]})}flatMap(e){const t=this._elements;return new g(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 l of u)yield[s,l];else yield[s,u];n.set(s,o+1)}})}drop(e){const t=this._elements;return new g(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 g(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 w(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 g(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 w(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 g(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 R(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 R(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"},R=class k{_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 k(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 k(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 h("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 k(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 k(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 k(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 k(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 B(this.map(async(t,n)=>[await e(t,n),t]))}toArray(){return Array.fromAsync(this)}[Symbol.toStringTag]="SmartAsyncIterator";[Symbol.asyncIterator](){return this}},C=class v{_elements;constructor(e){this._elements=new d(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 w(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 w(function*(){for(const[n,[s,i]]of t)yield[n,i]})}filter(e){const t=this._elements;return new v(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 v(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 w(function*(){for(const[s,[i,o]]of n)yield[s,o]})}flatMap(e){const t=this._elements;return new v(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 l of u)yield[s,l];else yield[s,u];n.set(s,o+1)}})}drop(e){const t=this._elements;return new v(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 v(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 w(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 v(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 w(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 v(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 d(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 d(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"},se=Function,D=class extends se{constructor(){super("return this._invoke(...arguments);");const r=this.bind(this);return Object.setPrototypeOf(this,r),r}[Symbol.toStringTag]="CallableObject"},ie=class extends D{_callbacks;get size(){return this._callbacks.length}constructor(...r){super(),this._callbacks=r}_invoke(...r){const e=this._callbacks.length,t=new Array(e);for(let n=0;n<e;n+=1)t[n]=this._callbacks[n](...r);return t}add(r){return this._callbacks.push(r),this}remove(r){const e=this._callbacks.indexOf(r);return e<0?!1:(this._callbacks.splice(e,1),!0)}clear(){this._callbacks.length=0}[Symbol.toStringTag]="CallbackChain"},M=class H{_subscribers;constructor(){this._subscribers=new Map}createScope(){const e=new H;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);if(s){const i=s.slice(),o=i.length;n=new Array(o);for(let u=0;u<o;u+=1)n[u]=i[u](...t)}else n=[];return 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 A("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 A("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 A("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"},oe=()=>{},Y=()=>{throw new $("The `SwitchableCallback` has no callback defined yet. Did you forget to call the `register` method?")},ae=class extends D{_callback;_callbacks;_isEnabled;get isEnabled(){return this._isEnabled}_key;get key(){return this._key}_invoke;constructor(r,e="default"){super(),this._callbacks=new Map,this._isEnabled=!0,r?this._callbacks.set(e,r):(e="",r=Y),this._key=e,this._callback=r,this._invoke=(...t)=>this._callback(...t)}enable(r){if(r===void 0){if(!this._key)throw new p("The `SwitchableCallback` has no callback defined yet. Did you forget to call the `register` method?");r=this._key}else if(r){if(!this._callbacks.has(r))throw new p(`The key '${r}' 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(r),this._isEnabled=!0}disable(){if(!this._isEnabled)throw new _("The `SwitchableCallback` is already disabled.");this._callback=oe,this._isEnabled=!1}register(r,e){if(this._callbacks.size===0)this._key=r,this._callback=e;else if(this._callbacks.has(r))throw new p(`The key '${r}' has already been used for another callback.`);this._callbacks.set(r,e)}unregister(r){if(this._key===r)throw new p("Unable to unregister the currently selected callback.");if(!this._callbacks.has(r))throw new p(`The key '${r}' doesn't yet have any associated callback.`);this._callbacks.delete(r)}switch(r){if(!this._callbacks.has(r))throw new p(`The key '${r}' doesn't yet have any associated callback.`);this._key!==r&&(this._key=r,this._isEnabled&&(this._callback=this._callbacks.get(r)))}reset(r,e="default"){this._callbacks.clear(),this._isEnabled=!0,r?this._callbacks.set(e,r):(e="",r=Y),this._key=e,this._callback=r}[Symbol.toStringTag]="SwitchableCallback"},le=class extends Array{_publisher;constructor(...r){super(...r),this._publisher=new M}push(...r){const e=this.length,t=super.push(...r);for(let n=0;n<r.length;n+=1)this._publisher.publish("add",r[n],e+n);return t}pop(){const r=this.length-1;if(r<0)return;const e=super.pop();return this._publisher.publish("remove",e,r),e}shift(){if(this.length===0)return;const r=super.shift();return this._publisher.publish("remove",r,0),r}unshift(...r){const e=super.unshift(...r);for(let t=0;t<r.length;t+=1)this._publisher.publish("add",r[t],t);return e}splice(r,e,...t){const n=r<0?Math.max(this.length+r,0):Math.min(r,this.length),s=e===void 0?this.length-n:Math.min(Math.max(e,0),this.length-n),i=super.splice(r,s,...t);for(let o=0;o<i.length;o+=1)this._publisher.publish("remove",i[o],n+o);for(let o=0;o<t.length;o+=1)this._publisher.publish("add",t[o],n+o);return i}clear(){const r=this.length;this.length=0,r>0&&this._publisher.publish("clear")}onAdd(r){return this._publisher.subscribe("add",r)}onRemove(r){return this._publisher.subscribe("remove",r)}onClear(r){return this._publisher.subscribe("clear",r)}[Symbol.toStringTag]="ArrayView";static[Symbol.species]=Array},ue=class extends Map{_publisher;constructor(r){if(super(),this._publisher=new M,r)for(const[e,t]of r)super.set(e,t)}set(r,e){return super.set(r,e),this._publisher.publish("add",r,e),this}delete(r){const e=this.get(r);return e===void 0?!1:(super.delete(r),this._publisher.publish("remove",r,e),!0)}clear(){const r=this.size;super.clear(),r>0&&this._publisher.publish("clear")}onAdd(r){return this._publisher.subscribe("add",r)}onRemove(r){return this._publisher.subscribe("remove",r)}onClear(r){return this._publisher.subscribe("clear",r)}[Symbol.toStringTag]="MapView"},ce=class extends Set{_publisher;constructor(r){if(super(),this._publisher=new M,r)for(const e of r)super.add(e)}add(r){return super.add(r),this._publisher.publish("add",r),this}delete(r){const e=super.delete(r);return e&&this._publisher.publish("remove",r),e}clear(){const r=this.size;super.clear(),r>0&&this._publisher.publish("clear")}onAdd(r){return this._publisher.subscribe("add",r)}onRemove(r){return this._publisher.subscribe("remove",r)}onClear(r){return this._publisher.subscribe("clear",r)}[Symbol.toStringTag]="SetView"},he=class{_preferPersistence;_volatile;_persistent;constructor(r=!0){if(!P)throw new q("The `JSONStorage` class can only be instantiated within a browser environment.");this._preferPersistence=r,this._volatile=window.sessionStorage,this._persistent=window.localStorage}_get(r,e,t){const n=r.getItem(e);if(n)try{return JSON.parse(n)}catch{console.warn(`The "${n}" value for "${e}" property cannot be parsed. Clearing the storage...`),r.removeItem(e)}return t}_set(r,e,t){const n=JSON.stringify(t);n?r.setItem(e,n):r.removeItem(e)}get(r,e,t=this._preferPersistence){const n=t?this._persistent:this._volatile;return this._get(n,r,e)}recall(r,e){return this._get(this._volatile,r,e)}retrieve(r,e){return this.recall(r)??this.read(r,e)}read(r,e){return this._get(this._persistent,r,e)}has(r,e){return(e?this._persistent:this._volatile).getItem(r)!==null}knows(r){return this._volatile.getItem(r)!==null}find(r){return this.knows(r)??this.exists(r)}exists(r){return this._persistent.getItem(r)!==null}set(r,e,t=this._preferPersistence){const n=t?this._persistent:this._volatile;this._set(n,r,e)}remember(r,e){this._set(this._volatile,r,e)}write(r,e){this._set(this._persistent,r,e)}delete(r,e){(e?this._persistent:this._volatile).removeItem(r)}forget(r){this._volatile.removeItem(r)}erase(r){this._persistent.removeItem(r)}clear(r){this._volatile.removeItem(r),this._persistent.removeItem(r)}[Symbol.toStringTag]="JSONStorage"},y=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"},N=class extends y{_resolve;get resolve(){return this._resolve}_reject;get reject(){return this._reject}constructor(r,e){let t,n;super((s,i)=>{t=s,n=i}),this._promise=this._promise.then(r,e),this._resolve=t,this._reject=n}watch(r){return r.then(this.resolve,this.reject),this}[Symbol.toStringTag]="DeferredPromise"},J=class extends y{constructor(r,e){super((t,n)=>{const s=l=>{clearTimeout(u),t(l)},i=l=>{clearTimeout(u),n(l)},u=setTimeout(()=>i(new V("The operation has timed out.")),e);r(s,i)})}[Symbol.toStringTag]="TimedPromise"},de=class extends y{_count;get isPending(){return this._count>0}get isFulfilled(){return this._count===0}get isRejected(){throw new h("`PromiseQueue` doesn't support rejection states.")}constructor(){super(r=>r()),this._count=0,this._isPending=!1,this._isFulfilled=!1,this._isRejected=!1}enqueue(r,e){if(this._count+=1,r instanceof N){const n=r;r=()=>(n.resolve(),n)}const t=(n,s)=>{this._promise=this._promise.then(r).then(i=>{this._count-=1,n(i)}).catch(i=>{this._count-=1,s(i)})};return e?new J(t,e):new y(t)}[Symbol.toStringTag]="PromiseQueue"},b=(function(r){return r[r.Millisecond=1]="Millisecond",r[r.Second=1e3]="Second",r[r.Minute=60*r.Second]="Minute",r[r.Hour=60*r.Minute]="Hour",r[r.Day=24*r.Hour]="Day",r[r.Week=7*r.Day]="Week",r[r.Month=30*r.Day]="Month",r[r.Year=365*r.Day]="Year",r})({}),L=(function(r){return 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})({});function fe(r,e,t=b.Day){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 me(r,e,t=b.Day){if(r=new Date(r),e=new Date(e),r>=e)throw new x("The end date must be greater than the start date.");return new d(function*(){const n=e.getTime();let s=r.getTime();for(;s<n;)yield new Date(s),s+=t})}function G(r,e=b.Day){if(e<=b.Millisecond)throw new x("Rounding a timestamp by milliseconds or less makes no sense.Use the timestamp value directly instead.");if(e>b.Day)throw new x("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 we(r,e=L.Sunday){r=new Date(r);const t=7-e,n=(r.getUTCDay()+t)%7,s=r.getTime()-b.Day*n;return G(new Date(s))}var z=class{_handle;_startTime;get startTime(){return this._startTime}_isRunning;get isRunning(){return this._isRunning}get elapsedTime(){return performance.now()-this._startTime}_publisher;_start;_stop;constructor(r,e=40){this._startTime=0,this._isRunning=!1,P?(this._start=()=>{r(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(()=>r(this.elapsedTime),e)},this._stop=()=>clearInterval(this._handle)),this._publisher=new M}start(r=0){if(this._isRunning)throw new _("The game loop has already been started.");this._startTime=performance.now()-r,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 T;this._stop(),this._handle=void 0,this._isRunning=!1,this._publisher.publish("stop")}onStart(r){return this._publisher.subscribe("start",r)}onStop(r){return this._publisher.subscribe("stop",r)}[Symbol.toStringTag]="GameLoop"},_e=class extends z{constructor(r=b.Second){super(e=>this._publisher.publish("tick",e),r)}start(r=0){if(this._isRunning)throw new _("The clock has already been started.");this._startTime=performance.now()-r,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 T;this._stop(),this._handle=void 0,this._isRunning=!1,this._publisher.publish("stop")}onTick(r,e=0){if(e<0)throw new x("The tick step must be a non-negative number.");if(e===0)return this._publisher.subscribe("tick",r);let t=0;return this._publisher.subscribe("tick",n=>{n-t<e||(r(n),t=n)})}[Symbol.toStringTag]="Clock"},ye=class extends z{_duration;get duration(){return this._duration}get remainingTime(){return this._duration-this.elapsedTime}_deferrer;constructor(r,e=b.Second){const t=()=>{const n=this.remainingTime;n<=0?(this._deferrerStop(),this._publisher.publish("tick",0),this._publisher.publish("expire")):this._publisher.publish("tick",n)};super(t,e),this._duration=r}_deferrerStop(r){if(!this._isRunning)throw new _("The countdown hadn't yet started.");if(!this._deferrer)throw new T;this._stop(),this._handle=void 0,this._isRunning=!1,r!==void 0?this._deferrer.reject(r):this._deferrer.resolve(),this._deferrer=void 0}start(r=this.duration){if(this._isRunning)throw new _("The countdown had already stopped or hadn't yet started.");if(this._deferrer)throw new T;return this._deferrer=new N,super.start(this.duration-r),this._publisher.publish("start"),this._deferrer}stop(r){this._deferrerStop(r),this._publisher.publish("stop",r)}onTick(r,e=0){if(e<0)throw new x("The tick step must be a non-negative number.");if(e===0)return this._publisher.subscribe("tick",r);let t=this.remainingTime;return this._publisher.subscribe("tick",n=>{t-n<e||(r(n),t=n)})}onExpire(r){return this._publisher.subscribe("expire",r)}[Symbol.toStringTag]="Countdown"},be=class{static Linear(r){const e=r-1;return new d(function*(){for(let t=0;t<r;t+=1)yield t/e})}static Exponential(r,e=2){if(e<0)throw new h("The base of the exponential curve cannot be negative.");const t=r-1;return new d(function*(){for(let n=0;n<r;n+=1)yield Math.pow(n/t,e)})}constructor(){}[Symbol.toStringTag]="Curve"},pe=class c{static#t(e){let t=e|0;return()=>{t=t+1831565813|0;let n=t;return n=Math.imul(n^n>>>15,n|1),n^=n+Math.imul(n^n>>>7,n|61),((n^n>>>14)>>>0)/4294967296}}static _Boolean(e,t){return e()<t}static _Integer(e,t,n){return Math.floor(n===void 0?e()*t:e()*(n-t)+t)}static _Decimal(e,t,n){return t===void 0?e():n===void 0?e()*t:e()*(n-t)+t}static _Index(e,t){if(t.length===0)throw new h("You must provide at least one element.");return c._Integer(e,t.length)}static _Choice(e,t){return t[c._Index(e,t)]}static _Sample(e,t,n,s){const i=t.length;if(i===0)throw new h("You must provide at least one element.");if(n<0)throw new h("Count must be non-negative.");if(n>i)throw new h("Count cannot exceed the number of elements.");if(n===0)return[];if(s===void 0){const l=Array.from(t),f=new Array(n);for(let I=0;I<n;I+=1){const Q=c._Integer(e,I,i);f[I]=l[Q],l[Q]=l[I]}return f}if(s.length!==i)throw new h("Weights array must have the same length as elements array.");const o=new Array(i);for(let l=0;l<i;l+=1){if(s[l]<=0)throw new h(`Weight for element #${l} must be greater than zero.`);o[l]={index:l,key:Math.pow(e(),1/s[l])}}o.sort((l,f)=>f.key-l.key);const u=new Array(n);for(let l=0;l<n;l+=1)u[l]=t[o[l].index];return u}static#e(e,t,n){const s=new Array(n-1);for(let l=0;l<s.length;l+=1)s[l]=e()*t;s.sort((l,f)=>l-f);const i=[0,...s,t],o=new Array(n);for(let l=0;l<n;l+=1)o[l]=Math.floor(i[l+1]-i[l]);let u=t-o.reduce((l,f)=>l+f,0);for(;u>0;)o[c._Integer(e,n)]+=1,u-=1;return o}static _Split(e,t,n){if(n<1)throw new h("The number of splits must be greater than zero.");if(typeof t=="number"){if(t<0)throw new h("The total must be a non-negative number.");return c.#e(e,t,n)}const s=Array.from(t),i=s.length;if(i===0)throw new h("You must provide at least one element.");if(n>i)throw new h("The number of splits cannot exceed the number of elements.");const o=c.#e(e,i,n),u=new Array(n);let l=0;for(let f=0;f<n;f+=1)u[f]=s.slice(l,l+o[f]),l+=o[f];return u}static Boolean(e=.5){return c._Boolean(Math.random,e)}static Integer(e,t){return c._Integer(Math.random,e,t)}static Decimal(e,t){return c._Decimal(Math.random,e,t)}static Index(e){return c._Index(Math.random,e)}static Choice(e){return c._Choice(Math.random,e)}static Sample(e,t,n){return c._Sample(Math.random,e,t,n)}static Split(e,t){return c._Split(Math.random,e,t)}static FromSeed(e){return new c(e)}_next;constructor(e){this._next=c.#t(e)}boolean(e=.5){return c._Boolean(this._next,e)}integer(e,t){return c._Integer(this._next,e,t)}decimal(e,t){return c._Decimal(this._next,e,t)}index(e){return c._Index(this._next,e)}choice(e){return c._Choice(this._next,e)}sample(e,t,n){return c._Sample(this._next,e,t,n)}split(e,t){return c._Split(this._next,e,t)}[Symbol.toStringTag]="Random"};function ge(r){return new y(e=>setTimeout(e,r))}function ve(){return new y(r=>requestAnimationFrame(()=>r()))}function xe(){return new y(r=>setTimeout(r))}function Se(r,e="text/javascript"){return new y((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 ke(...r){return new d(function*(){for(const e of r)for(const t of e)yield t})}function Te(r){if(r instanceof Array)return r.length;let e=0;for(const t of r)e+=1;return e}function Ee(r){return new d(function*(){let e=0;for(const t of r)yield[e,t],e+=1})}function Me(r,e,t=1){if(t<=0)throw new x("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 d(function*(){for(let n=r;n>e;n-=t)yield n}):new d(function*(){for(let n=r;n<e;n+=t)yield n})}function Ie(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 Ae(r){return new d(function*(){const e=new Set;for(const t of r)e.has(t)||(e.add(t),yield t)})}function K(r,e){const t=r[Symbol.iterator](),n=e[Symbol.iterator]();return new d(function*(){for(;;){const s=t.next(),i=n.next();if(s.done||i.done)break;yield[s.value,i.value]}})}function Re(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 h("You must provide at least one value.");return i/o}let t=0,n=0,s=0;for(const[i,o]of K(r,e)){if(o<=0)throw new h(`The weight for the value #${s} must be greater than zero.`);t+=i*o,n+=o,s+=1}if(s===0)throw new h("You must provide at least one value and weight.");if(n<=0)throw new h("The sum of weights must be greater than zero.");return t/n}function Ce(r,e,t){if(e>t)throw new h("The minimum bound must be less than or equal to the maximum bound.");return r<e?e:r>t?t:r}function Fe(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 je(r){return`${r.charAt(0).toUpperCase()}${r.slice(1)}`}var De="2.2.9";return a.AggregatedAsyncIterator=B,a.AggregatedIterator=C,a.ArrayView=le,a.CallableObject=D,a.CallbackChain=ie,a.Clock=_e,a.Countdown=ye,a.Curve=be,a.DeferredPromise=N,a.EnvironmentException=q,a.Exception=m,a.FatalErrorException=T,a.FileException=j,a.FileExistsException=U,a.FileNotFoundException=ee,a.GameLoop=z,a.JSONStorage=he,a.KeyException=p,a.MapView=ue,a.NetworkException=O,a.NotImplementedException=$,a.PermissionException=ne,a.PromiseQueue=de,a.Publisher=M,a.Random=pe,a.RangeException=x,a.ReducedIterator=w,a.ReferenceException=A,a.ResponseException=te,a.RuntimeException=_,a.SetView=ce,a.SmartAsyncIterator=R,a.SmartIterator=d,a.SmartPromise=y,a.SwitchableCallback=ae,a.TimeUnit=b,a.TimedPromise=J,a.TimeoutException=V,a.TypeException=re,a.VERSION=De,a.ValueException=h,a.WeekDay=L,a.average=Re,a.capitalize=je,a.chain=ke,a.clamp=Ce,a.count=Te,a.dateDifference=fe,a.dateRange=me,a.dateRound=G,a.delay=ge,a.enumerate=Ee,a.getWeek=we,a.hash=Fe,a.isBrowser=P,a.isNode=X,a.isWorker=Z,a.loadScript=Se,a.nextAnimationFrame=ve,a.range=Me,a.shuffle=Ie,a.sum=Pe,a.unique=Ae,a.yieldToEventLoop=xe,a.zip=K,a})({});
//# sourceMappingURL=core.global.js.map