UNPKG

@byloth/core

Version:

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

1,768 lines (1,767 loc) • 49.2 kB
var j = typeof window < "u" && typeof window.document < "u", W = typeof process < "u" && !!process.versions?.node, U = typeof self == "object" && self.constructor?.name === "DedicatedWorkerGlobalScope", m = class M extends Error { static FromUnknown(e) { if (e instanceof M) return e; if (e instanceof Error) { const t = new M(e.message); return t.stack = e.stack, t.cause = e.cause, t.name = e.name, t; } return new M(`${e}`); } constructor(e, t, n = "Exception") { super(e), this.cause = t, this.name = n; } [Symbol.toStringTag] = "Exception"; }, E = 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"; }, B = class extends E { 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"; }, D = class extends m { constructor(r, e, t = "FileException") { super(r, e, t); } [Symbol.toStringTag] = "FileException"; }, ee = class extends D { constructor(r, e, t = "FileExistsException") { super(r, e, t); } [Symbol.toStringTag] = "FileExistsException"; }, te = class extends D { constructor(r, e, t = "FileNotFoundException") { super(r, e, t); } [Symbol.toStringTag] = "FileNotFoundException"; }, g = class extends m { constructor(r, e, t = "KeyException") { super(r, e, t); } [Symbol.toStringTag] = "KeyException"; }, Y = class extends m { constructor(r, e, t = "NetworkException") { super(r, e, t); } [Symbol.toStringTag] = "NetworkException"; }, ne = class extends Y { 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"; }, re = 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"; }, w = class extends m { constructor(r, e, t = "RuntimeException") { super(r, e, t); } [Symbol.toStringTag] = "RuntimeException"; }, V = class extends w { constructor(r, e, t = "EnvironmentException") { super(r, e, t); } [Symbol.toStringTag] = "EnvironmentException"; }, J = class extends m { constructor(r, e, t = "TimeoutException") { super(r, e, t); } [Symbol.toStringTag] = "TimeoutException"; }, se = class extends m { constructor(r, e, t = "TypeException") { super(r, e, t); } [Symbol.toStringTag] = "TypeException"; }, c = class extends m { constructor(r, e, t = "ValueException") { super(r, e, t); } [Symbol.toStringTag] = "ValueException"; }, k = class extends c { constructor(r, e, t = "RangeException") { super(r, e, t); } [Symbol.toStringTag] = "RangeException"; }, h = class x { _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 x(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 x(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 c("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 x(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 x(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 x(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 x(function* () { const t = /* @__PURE__ */ 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 F(this.map((t, n) => [e(t, n), t])); } toArray() { return Array.from(this); } [Symbol.toStringTag] = "SmartIterator"; [Symbol.iterator]() { return this; } }, d = class S { _elements; constructor(e) { this._elements = new h(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 S(function* () { for (const [n, [s, i]] of t) e(s, i, n) && (yield [s, i]); }); } map(e) { const t = this._elements.enumerate(); return new S(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 c("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 F(function* () { for (const [n, [s, i]] of t) { const o = e(s, i, n); if (o instanceof Array) for (const l of o) yield [s, l]; else yield [s, o]; } }); } drop(e) { const t = this._elements.enumerate(); return new S(function* () { for (const [n, [s, i]] of t) n >= e && (yield [s, i]); }); } take(e) { const t = this._elements.enumerate(); return new S(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 S(function* () { const t = /* @__PURE__ */ 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 F(function* () { for (const [n, [s, i]] of t) yield [e(s, i, n), i]; }); } keys() { const e = this._elements; return new h(function* () { for (const [t] of e) yield t; }); } entries() { return this._elements; } values() { const e = this._elements; return new h(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"; }, L = class p { _elements; constructor(e) { this._elements = new R(e); } async every(e) { const t = /* @__PURE__ */ 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 d(function* () { for (const [n, [s, i]] of t) yield [n, i]; }); } async some(e) { const t = /* @__PURE__ */ 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 d(function* () { for (const [n, [s, i]] of t) yield [n, i]; }); } filter(e) { const t = this._elements; return new p(async function* () { const n = /* @__PURE__ */ 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 p(async function* () { const n = /* @__PURE__ */ 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 = /* @__PURE__ */ new Map(); for await (const [s, i] of this._elements) { let o, l; if (n.has(s)) [o, l] = n.get(s); else if (t !== void 0) o = 0, t instanceof Function ? l = await t(s) : l = await t; else { n.set(s, [0, i]); continue; } n.set(s, [o + 1, await e(s, l, i, o)]); } return new d(function* () { for (const [s, [i, o]] of n) yield [s, o]; }); } flatMap(e) { const t = this._elements; return new p(async function* () { const n = /* @__PURE__ */ new Map(); for await (const [s, i] of t) { const o = n.get(s) ?? 0, l = await e(s, i, o); if (l instanceof Array) for (const a of l) yield [s, a]; else yield [s, l]; n.set(s, o + 1); } }); } drop(e) { const t = this._elements; return new p(async function* () { const n = /* @__PURE__ */ 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 p(async function* () { const n = /* @__PURE__ */ 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 = /* @__PURE__ */ 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 d(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 p(async function* () { const t = /* @__PURE__ */ new Map(); for await (const [n, s] of e) { const i = t.get(n) ?? /* @__PURE__ */ new Set(); i.has(s) || (i.add(s), t.set(n, i), yield [n, s]); } }); } async count() { const e = /* @__PURE__ */ new Map(); for await (const [t] of this._elements) { const n = e.get(t) ?? 0; e.set(t, n + 1); } return new d(function* () { for (const [t, n] of e) yield [t, n]; }); } async forEach(e) { const t = /* @__PURE__ */ 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 p(async function* () { const n = /* @__PURE__ */ 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 = /* @__PURE__ */ 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 = /* @__PURE__ */ 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 v { _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 v(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 v(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 c("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 v(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 v(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 v(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 v(async function* () { const t = /* @__PURE__ */ 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 L(this.map(async (t, n) => [await e(t, n), t])); } toArray() { return Array.fromAsync(this); } [Symbol.toStringTag] = "SmartAsyncIterator"; [Symbol.asyncIterator]() { return this; } }, F = class _ { _elements; constructor(e) { this._elements = new h(e); } every(e) { const t = /* @__PURE__ */ 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 d(function* () { for (const [n, [s, i]] of t) yield [n, i]; }); } some(e) { const t = /* @__PURE__ */ 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 d(function* () { for (const [n, [s, i]] of t) yield [n, i]; }); } filter(e) { const t = this._elements; return new _(function* () { const n = /* @__PURE__ */ 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 _(function* () { const n = /* @__PURE__ */ 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 = /* @__PURE__ */ new Map(); for (const [s, i] of this._elements) { let o, l; if (n.has(s)) [o, l] = n.get(s); else if (t !== void 0) o = 0, t instanceof Function ? l = t(s) : l = t; else { n.set(s, [0, i]); continue; } n.set(s, [o + 1, e(s, l, i, o)]); } return new d(function* () { for (const [s, [i, o]] of n) yield [s, o]; }); } flatMap(e) { const t = this._elements; return new _(function* () { const n = /* @__PURE__ */ new Map(); for (const [s, i] of t) { const o = n.get(s) ?? 0, l = e(s, i, o); if (l instanceof Array) for (const a of l) yield [s, a]; else yield [s, l]; n.set(s, o + 1); } }); } drop(e) { const t = this._elements; return new _(function* () { const n = /* @__PURE__ */ 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 _(function* () { const n = /* @__PURE__ */ 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 = /* @__PURE__ */ 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 d(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 _(function* () { const t = /* @__PURE__ */ new Map(); for (const [n, s] of e) { const i = t.get(n) ?? /* @__PURE__ */ new Set(); i.has(s) || (i.add(s), t.set(n, i), yield [n, s]); } }); } count() { const e = /* @__PURE__ */ new Map(); for (const [t] of this._elements) { const n = e.get(t) ?? 0; e.set(t, n + 1); } return new d(function* () { for (const [t, n] of e) yield [t, n]; }); } forEach(e) { const t = /* @__PURE__ */ 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 _(function* () { const n = /* @__PURE__ */ 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 h(function* () { const t = /* @__PURE__ */ 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 h(function* () { for (const [t, n] of e) yield n; }); } toArray() { const e = this.toMap(); return Array.from(e.values()); } toMap() { const e = /* @__PURE__ */ 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"; }, G = Function, $ = class extends G { constructor() { super("return this._invoke(...arguments);"); const r = this.bind(this); return Object.setPrototypeOf(this, r), r; } [Symbol.toStringTag] = "CallableObject"; }, ie = class extends $ { _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"; }, I = class z { _subscribers; constructor() { this._subscribers = /* @__PURE__ */ new Map(); } createScope() { const e = new z(); 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 l = 0; l < o; l += 1) n[l] = i[l](...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"; }, H = () => { }, P = () => { throw new B("The `SwitchableCallback` has no callback defined yet. Did you forget to call the `register` method?"); }, oe = class extends $ { _callback; _callbacks; _isEnabled; get isEnabled() { return this._isEnabled; } _key; get key() { return this._key; } _invoke; constructor(r, e = "default") { super(), this._callbacks = /* @__PURE__ */ new Map(), this._isEnabled = !0, r ? this._callbacks.set(e, r) : (e = "", r = P), this._key = e, this._callback = r, this._invoke = (...t) => this._callback(...t); } enable(r) { if (r === void 0) { if (!this._key) throw new g("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 g(`The key '${r}' doesn't yet have any associated callback.`); } else throw new g("The key must be a non-empty string."); if (this._isEnabled) throw new w("The `SwitchableCallback` is already enabled."); this._callback = this._callbacks.get(r), this._isEnabled = !0; } disable() { if (!this._isEnabled) throw new w("The `SwitchableCallback` is already disabled."); this._callback = H, 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 g(`The key '${r}' has already been used for another callback.`); this._callbacks.set(r, e); } unregister(r) { if (this._key === r) throw new g("Unable to unregister the currently selected callback."); if (!this._callbacks.has(r)) throw new g(`The key '${r}' doesn't yet have any associated callback.`); this._callbacks.delete(r); } switch(r) { if (!this._callbacks.has(r)) throw new g(`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 = P), this._key = e, this._callback = r; } [Symbol.toStringTag] = "SwitchableCallback"; }, ae = class extends Array { _publisher; constructor(...r) { super(...r), this._publisher = new I(); } 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; }, le = class extends Map { _publisher; constructor(r) { if (super(), this._publisher = new I(), 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"; }, ue = class extends Set { _publisher; constructor(r) { if (super(), this._publisher = new I(), 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"; }, ce = class { _preferPersistence; _volatile; _persistent; constructor(r = !0) { if (!j) throw new V("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"; }, b = class N { static FromPromise(e) { return new N((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"; }, q = class extends b { _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"; }, K = class extends b { constructor(r, e) { super((t, n) => { const s = (a) => { clearTimeout(l), t(a); }, i = (a) => { clearTimeout(l), n(a); }, l = setTimeout(() => i(new J("The operation has timed out.")), e); r(s, i); }); } [Symbol.toStringTag] = "TimedPromise"; }, he = class extends b { _count; get isPending() { return this._count > 0; } get isFulfilled() { return this._count === 0; } get isRejected() { throw new c("`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 q) { 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 K(t, e) : new b(t); } [Symbol.toStringTag] = "PromiseQueue"; }, y = /* @__PURE__ */ (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; })({}), Q = /* @__PURE__ */ (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 = y.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 de(r, e, t = y.Day) { if (r = new Date(r), e = new Date(e), r >= e) throw new k("The end date must be greater than the start date."); return new h(function* () { const n = e.getTime(); let s = r.getTime(); for (; s < n; ) yield new Date(s), s += t; }); } function X(r, e = y.Day) { if (e <= y.Millisecond) throw new k("Rounding a timestamp by milliseconds or less makes no sense.Use the timestamp value directly instead."); if (e > y.Day) throw new k("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 me(r, e = Q.Sunday) { r = new Date(r); const t = 7 - e, n = (r.getUTCDay() + t) % 7, s = r.getTime() - y.Day * n; return X(new Date(s)); } var O = 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, j ? (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 I(); } start(r = 0) { if (this._isRunning) throw new w("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 w("The game loop had already stopped or hadn't yet started."); if (!this._handle) throw new E(); 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"; }, we = class extends O { constructor(r = y.Second) { super((e) => this._publisher.publish("tick", e), r); } start(r = 0) { if (this._isRunning) throw new w("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 w("The clock had already stopped or hadn't yet started."); if (!this._handle) throw new E(); this._stop(), this._handle = void 0, this._isRunning = !1, this._publisher.publish("stop"); } onTick(r, e = 0) { if (e < 0) throw new k("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"; }, pe = class extends O { _duration; get duration() { return this._duration; } get remainingTime() { return this._duration - this.elapsedTime; } _deferrer; constructor(r, e = y.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 w("The countdown hadn't yet started."); if (!this._deferrer) throw new E(); 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 w("The countdown had already stopped or hadn't yet started."); if (this._deferrer) throw new E(); return this._deferrer = new q(), 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 k("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"; }, _e = class { static Linear(r) { const e = r - 1; return new h(function* () { for (let t = 0; t < r; t += 1) yield t / e; }); } static Exponential(r, e = 2) { if (e < 0) throw new c("The base of the exponential curve cannot be negative."); const t = r - 1; return new h(function* () { for (let n = 0; n < r; n += 1) yield Math.pow(n / t, e); }); } constructor() { } [Symbol.toStringTag] = "Curve"; }, ye = class u { 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 c("You must provide at least one element."); return u._Integer(e, t.length); } static _Choice(e, t) { return t[u._Index(e, t)]; } static _Sample(e, t, n, s) { const i = t.length; if (i === 0) throw new c("You must provide at least one element."); if (n < 0) throw new c("Count must be non-negative."); if (n > i) throw new c("Count cannot exceed the number of elements."); if (n === 0) return []; if (s === void 0) { const a = Array.from(t), f = new Array(n); for (let T = 0; T < n; T += 1) { const C = u._Integer(e, T, i); f[T] = a[C], a[C] = a[T]; } return f; } if (s.length !== i) throw new c("Weights array must have the same length as elements array."); const o = new Array(i); for (let a = 0; a < i; a += 1) { if (s[a] <= 0) throw new c(`Weight for element #${a} must be greater than zero.`); o[a] = { index: a, key: Math.pow(e(), 1 / s[a]) }; } o.sort((a, f) => f.key - a.key); const l = new Array(n); for (let a = 0; a < n; a += 1) l[a] = t[o[a].index]; return l; } static #e(e, t, n) { const s = new Array(n - 1); for (let a = 0; a < s.length; a += 1) s[a] = e() * t; s.sort((a, f) => a - f); const i = [ 0, ...s, t ], o = new Array(n); for (let a = 0; a < n; a += 1) o[a] = Math.floor(i[a + 1] - i[a]); let l = t - o.reduce((a, f) => a + f, 0); for (; l > 0; ) o[u._Integer(e, n)] += 1, l -= 1; return o; } static _Split(e, t, n) { if (n < 1) throw new c("The number of splits must be greater than zero."); if (typeof t == "number") { if (t < 0) throw new c("The total must be a non-negative number."); return u.#e(e, t, n); } const s = Array.from(t), i = s.length; if (i === 0) throw new c("You must provide at least one element."); if (n > i) throw new c("The number of splits cannot exceed the number of elements."); const o = u.#e(e, i, n), l = new Array(n); let a = 0; for (let f = 0; f < n; f += 1) l[f] = s.slice(a, a + o[f]), a += o[f]; return l; } static Boolean(e = 0.5) { return u._Boolean(Math.random, e); } static Integer(e, t) { return u._Integer(Math.random, e, t); } static Decimal(e, t) { return u._Decimal(Math.random, e, t); } static Index(e) { return u._Index(Math.random, e); } static Choice(e) { return u._Choice(Math.random, e); } static Sample(e, t, n) { return u._Sample(Math.random, e, t, n); } static Split(e, t) { return u._Split(Math.random, e, t); } static FromSeed(e) { return new u(e); } _next; constructor(e) { this._next = u.#t(e); } boolean(e = 0.5) { return u._Boolean(this._next, e); } integer(e, t) { return u._Integer(this._next, e, t); } decimal(e, t) { return u._Decimal(this._next, e, t); } index(e) { return u._Index(this._next, e); } choice(e) { return u._Choice(this._next, e); } sample(e, t, n) { return u._Sample(this._next, e, t, n); } split(e, t) { return u._Split(this._next, e, t); } [Symbol.toStringTag] = "Random"; }; function be(r) { return new b((e) => setTimeout(e, r)); } function ge() { return new b((r) => requestAnimationFrame(() => r())); } function xe() { return new b((r) => setTimeout(r)); } function ve(r, e = "text/javascript") { return new b((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 Se(...r) { return new h(function* () { for (const e of r) for (const t of e) yield t; }); } function ke(r) { if (r instanceof Array) return r.length; let e = 0; for (const t of r) e += 1; return e; } function Te(r) { return new h(function* () { let e = 0; for (const t of r) yield [e, t], e += 1; }); } function Ee(r, e, t = 1) { if (t <= 0) throw new k("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 h(function* () { for (let n = r; n > e; n -= t) yield n; }) : new h(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 Ie(r) { return new h(function* () { const e = /* @__PURE__ */ new Set(); for (const t of r) e.has(t) || (e.add(t), yield t); }); } function Z(r, e) { const t = r[Symbol.iterator](), n = e[Symbol.iterator](); return new h(function* () { for (; ; ) { const s = t.next(), i = n.next(); if (s.done || i.done) break; yield [s.value, i.value]; } }); } function Ae(r, e) { if (e === void 0) { let i = 0, o = 0; for (const l of r) i += l, o += 1; if (o === 0) throw new c("You must provide at least one value."); return i / o; } let t = 0, n = 0, s = 0; for (const [i, o] of Z(r, e)) { if (o <= 0) throw new c(`The weight for the value #${s} must be greater than zero.`); t += i * o, n += o, s += 1; } if (s === 0) throw new c("You must provide at least one value and weight."); if (n <= 0) throw new c("The sum of weights must be greater than zero."); return t / n; } function Re(r, e, t) { if (e > t) throw new c("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 Ce(r) { let e = 0; for (const t of r) e += t; return e; } function Pe(r) { return `${r.charAt(0).toUpperCase()}${r.slice(1)}`; } var je = "2.2.9"; export { L as AggregatedAsyncIterator, F as AggregatedIterator, ae as ArrayView, $ as CallableObject, ie as CallbackChain, we as Clock, pe as Countdown, _e as Curve, q as DeferredPromise, V as EnvironmentException, m as Exception, E as FatalErrorException, D as FileException, ee as FileExistsException, te as FileNotFoundException, O as GameLoop, ce as JSONStorage, g as KeyException, le as MapView, Y as NetworkException, B as NotImplementedException, re as PermissionException, he as PromiseQueue, I as Publisher, ye as Random, k as RangeException, d as ReducedIterator, A as ReferenceException, ne as ResponseException, w as RuntimeException, ue as SetView, R as SmartAsyncIterator, h as SmartIterator, b as SmartPromise, oe as SwitchableCallback, y as TimeUnit, K as TimedPromise, J as TimeoutException, se as TypeException, je as VERSION, c as ValueException, Q as WeekDay, Ae as average, Pe as capitalize, Se as chain, Re as clamp, ke as count, fe as dateDifference, de as dateRange, X as dateRound, be as delay, Te as enumerate, me as getWeek, Fe as hash, j as isBrowser, W as isNode, U as isWorker, ve as loadScript, ge as nextAnimationFrame, Ee as range, Me as shuffle, Ce as sum, Ie as unique, xe as yieldToEventLoop, Z as zip }; //# sourceMappingURL=core.esm.js.map