@byloth/core 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core.js +76 -46
- package/dist/core.js.map +1 -1
- package/dist/core.umd.cjs +2 -2
- package/dist/core.umd.cjs.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +6 -1
- package/src/models/deferred-promise.ts +33 -50
- package/src/types.ts +3 -3
- package/src/utils/date.ts +33 -0
- package/src/utils/index.ts +2 -1
- package/src/utils/iterator.ts +12 -4
- package/src/utils/math.ts +49 -0
package/dist/core.js
CHANGED
|
@@ -1,33 +1,29 @@
|
|
|
1
|
-
var
|
|
2
|
-
var h = (s, e, t) => e in s ?
|
|
1
|
+
var u = Object.defineProperty;
|
|
2
|
+
var h = (s, e, t) => e in s ? u(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
|
|
3
3
|
var n = (s, e, t) => (h(s, typeof e != "symbol" ? e + "" : e, t), t);
|
|
4
|
-
class f
|
|
5
|
-
constructor(
|
|
6
|
-
let r, i, c;
|
|
7
|
-
t ? c = (u, l) => {
|
|
8
|
-
r = u, i = l, t(u, l);
|
|
9
|
-
} : c = (u, l) => {
|
|
10
|
-
r = u, i = l;
|
|
11
|
-
};
|
|
12
|
-
super(c);
|
|
4
|
+
class f {
|
|
5
|
+
constructor(e) {
|
|
13
6
|
n(this, "_resolve");
|
|
14
7
|
n(this, "_reject");
|
|
15
|
-
this
|
|
8
|
+
n(this, "_promise");
|
|
9
|
+
this._promise = new Promise((t, r) => {
|
|
10
|
+
this._resolve = t, this._reject = r, e == null || e(t, r);
|
|
11
|
+
});
|
|
16
12
|
}
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
resolve(e) {
|
|
14
|
+
this._resolve(e);
|
|
19
15
|
}
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
reject(e) {
|
|
17
|
+
this._reject(e);
|
|
22
18
|
}
|
|
23
|
-
then(
|
|
24
|
-
return
|
|
19
|
+
then(e, t) {
|
|
20
|
+
return this._promise.then(e, t);
|
|
25
21
|
}
|
|
26
|
-
catch(
|
|
27
|
-
return
|
|
22
|
+
catch(e) {
|
|
23
|
+
return this._promise.catch(e);
|
|
28
24
|
}
|
|
29
|
-
finally(
|
|
30
|
-
return
|
|
25
|
+
finally(e) {
|
|
26
|
+
return this._promise.finally(e);
|
|
31
27
|
}
|
|
32
28
|
}
|
|
33
29
|
class o extends Error {
|
|
@@ -48,7 +44,7 @@ Caused by ${t.stack}` : this.stack += `
|
|
|
48
44
|
Caused by ${t}`);
|
|
49
45
|
}
|
|
50
46
|
}
|
|
51
|
-
class
|
|
47
|
+
class _ {
|
|
52
48
|
constructor(e = !0) {
|
|
53
49
|
n(this, "_preferPersistence");
|
|
54
50
|
n(this, "_volatile");
|
|
@@ -199,41 +195,54 @@ class d {
|
|
|
199
195
|
return this._subscribers.slice().map((t) => t(...e));
|
|
200
196
|
}
|
|
201
197
|
}
|
|
202
|
-
async function
|
|
198
|
+
async function m(s) {
|
|
203
199
|
return new Promise((e, t) => setTimeout(e, s));
|
|
204
200
|
}
|
|
205
|
-
async function
|
|
201
|
+
async function g() {
|
|
206
202
|
return new Promise((s, e) => requestAnimationFrame(() => s()));
|
|
207
203
|
}
|
|
208
|
-
|
|
204
|
+
var l = /* @__PURE__ */ ((s) => (s[s.Second = 1e3] = "Second", s[s.Minute = 6e4] = "Minute", s[s.Hour = 36e5] = "Hour", s[s.Day = 864e5] = "Day", s[s.Week = 6048e5] = "Week", s[s.Month = 2592e6] = "Month", s[s.Year = 31536e6] = "Year", s))(l || {});
|
|
205
|
+
function v(s, e, t = 864e5) {
|
|
206
|
+
return Math.floor((e.getTime() - s.getTime()) / t);
|
|
207
|
+
}
|
|
208
|
+
function* p(s, e, t = 864e5) {
|
|
209
|
+
const r = e.getTime();
|
|
210
|
+
let i = s.getTime();
|
|
211
|
+
for (; i < r; )
|
|
212
|
+
yield new Date(i), i += t;
|
|
213
|
+
}
|
|
214
|
+
function b(s, e = 864e5) {
|
|
215
|
+
return new Date(Math.floor(s.getTime() / e) * e);
|
|
216
|
+
}
|
|
217
|
+
async function w(s, e = "text/javascript") {
|
|
209
218
|
return new Promise((t, r) => {
|
|
210
219
|
const i = document.createElement("script");
|
|
211
220
|
i.async = !0, i.defer = !0, i.src = s, i.type = e, i.onload = () => t(), i.onerror = () => r(), document.body.appendChild(i);
|
|
212
221
|
});
|
|
213
222
|
}
|
|
214
|
-
function
|
|
215
|
-
if (
|
|
223
|
+
function y(s) {
|
|
224
|
+
if (Array.isArray(s))
|
|
216
225
|
return s.length;
|
|
217
226
|
let e = 0;
|
|
218
227
|
for (const t of s)
|
|
219
228
|
e += 1;
|
|
220
229
|
return e;
|
|
221
230
|
}
|
|
222
|
-
function*
|
|
223
|
-
|
|
231
|
+
function* I(s, e, t = 1) {
|
|
232
|
+
e === void 0 && (e = s, s = 0), s > e && (t = t ?? -1);
|
|
224
233
|
for (let r = s; r < e; r += t)
|
|
225
234
|
yield r;
|
|
226
235
|
}
|
|
227
|
-
function
|
|
236
|
+
function M(s) {
|
|
228
237
|
let e = 0;
|
|
229
238
|
for (const t of s)
|
|
230
239
|
e += t;
|
|
231
240
|
return e;
|
|
232
241
|
}
|
|
233
|
-
function
|
|
242
|
+
function S(s) {
|
|
234
243
|
return [...new Set(s)];
|
|
235
244
|
}
|
|
236
|
-
function
|
|
245
|
+
function k(s) {
|
|
237
246
|
let e = 0;
|
|
238
247
|
for (let t = 0; t < s.length; t++) {
|
|
239
248
|
const r = s.charCodeAt(t);
|
|
@@ -241,24 +250,45 @@ function x(s) {
|
|
|
241
250
|
}
|
|
242
251
|
return e;
|
|
243
252
|
}
|
|
244
|
-
function P(s) {
|
|
253
|
+
function P(s = 1, e, t) {
|
|
254
|
+
if (e === void 0 && (e = s, s = 0), s === e)
|
|
255
|
+
return s;
|
|
256
|
+
let r;
|
|
257
|
+
if (t === !0)
|
|
258
|
+
r = (i) => i;
|
|
259
|
+
else if (t === void 0)
|
|
260
|
+
Math.abs(e - s) <= 1 ? r = (i) => i : r = Math.floor;
|
|
261
|
+
else if (t === !1)
|
|
262
|
+
r = Math.floor;
|
|
263
|
+
else {
|
|
264
|
+
const i = 10 ** t;
|
|
265
|
+
r = (c) => Math.floor(c * i) / i;
|
|
266
|
+
}
|
|
267
|
+
return r(Math.random() * (e - s) + s);
|
|
268
|
+
}
|
|
269
|
+
function T(s) {
|
|
245
270
|
return `${s.charAt(0).toUpperCase()}${s.slice(1)}`;
|
|
246
271
|
}
|
|
247
|
-
const
|
|
272
|
+
const j = "1.1.1";
|
|
248
273
|
export {
|
|
274
|
+
l as DateUnit,
|
|
249
275
|
f as DeferredPromise,
|
|
250
276
|
o as Exception,
|
|
251
|
-
|
|
277
|
+
_ as JsonStorage,
|
|
252
278
|
d as Subscribers,
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
b as
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
279
|
+
j as VERSION,
|
|
280
|
+
T as capitalize,
|
|
281
|
+
y as count,
|
|
282
|
+
v as dateDifference,
|
|
283
|
+
p as dateRange,
|
|
284
|
+
b as dateRound,
|
|
285
|
+
m as delay,
|
|
286
|
+
k as hash,
|
|
287
|
+
w as loadScript,
|
|
288
|
+
g as nextAnimationFrame,
|
|
289
|
+
P as random,
|
|
290
|
+
I as range,
|
|
291
|
+
M as sum,
|
|
292
|
+
S as unique
|
|
263
293
|
};
|
|
264
294
|
//# sourceMappingURL=core.js.map
|
package/dist/core.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core.js","sources":["../src/models/deferred-promise.ts","../src/models/exception.ts","../src/models/json-storage.ts","../src/models/subscribers.ts","../src/utils/async.ts","../src/utils/dom.ts","../src/utils/iterator.ts","../src/utils/math.ts","../src/utils/string.ts","../src/index.ts"],"sourcesContent":["import type { PromiseExecutor, PromiseResolver, PromiseRejecter, FulfilledHandler, RejectedHandler } from \"../types.js\";\n\nexport default class DeferredPromise<T, E = unknown> extends Promise<T>\n{\n protected _resolve: PromiseResolver<T>;\n protected _reject: PromiseRejecter<E>;\n\n public get resolve(): PromiseResolver<T>\n {\n return this._resolve;\n }\n public get reject(): PromiseRejecter<E>\n {\n return this._reject;\n }\n\n public constructor(executor?: PromiseExecutor<T>)\n {\n let _resolve: PromiseResolver<T>;\n let _reject: PromiseRejecter<E>;\n\n let _executor: PromiseExecutor<T>;\n\n if (executor)\n {\n _executor = (resolve, reject) =>\n {\n _resolve = resolve;\n _reject = reject;\n\n executor(resolve, reject);\n };\n }\n else\n {\n _executor = (resolve, reject) =>\n {\n _resolve = resolve;\n _reject = reject;\n };\n }\n\n super(_executor);\n\n this._resolve = _resolve!;\n this._reject = _reject!;\n }\n\n public then<F = T, R = E>(onFulfilled?: FulfilledHandler<T, F> | null, onRejected?: RejectedHandler<E, R>)\n : DeferredPromise<F, R>\n {\n super.then(onFulfilled, onRejected);\n\n return (this as unknown) as DeferredPromise<F, R>;\n }\n public catch<R = E>(onRejected?: RejectedHandler<E, R>): DeferredPromise<T, R>\n {\n super.catch(onRejected);\n\n return (this as unknown) as DeferredPromise<T, R>;\n }\n public finally(onFinally?: (() => void) | null): DeferredPromise<T, E>\n {\n super.finally(onFinally);\n\n return this;\n }\n}\n","export default class Exception extends Error\n{\n public static FromUnknown(error: unknown): Exception\n {\n if (error instanceof Exception)\n {\n return error;\n }\n if (error instanceof Error)\n {\n const exc = new Exception(error.message);\n\n exc.stack = error.stack;\n exc.name = error.name;\n\n return exc;\n }\n\n return new Exception(`${error}`);\n }\n\n public constructor(message: string, cause?: unknown, name = \"Exception\")\n {\n super(message);\n\n this.cause = cause;\n this.name = name;\n\n if (cause)\n {\n if (cause instanceof Error)\n {\n this.stack += `\\n\\nCaused by ${cause.stack}`;\n }\n else\n {\n this.stack += `\\n\\nCaused by ${cause}`;\n }\n }\n }\n}\n","/* eslint-disable no-trailing-spaces */\n\n/**\n * A wrapper around the `Storage` API to store and retrieve JSON values.\n *\n * It allows to handle either the `sessionStorage` or the `localStorage`\n * storage at the same time, depending on the required use case.\n */\nexport default class JsonStorage\n{\n protected _preferPersistence: boolean;\n\n protected _volatile: Storage;\n protected _persistent: Storage;\n\n public constructor(preferPersistence = true)\n {\n this._preferPersistence = preferPersistence;\n\n this._volatile = window.sessionStorage;\n this._persistent = window.localStorage;\n }\n\n protected _get<T>(storage: Storage, propertyName: string): T | undefined;\n protected _get<T>(storage: Storage, propertyName: string, defaultValue: T): T;\n protected _get<T>(storage: Storage, propertyName: string, defaultValue?: T): T | undefined;\n protected _get<T>(storage: Storage, propertyName: string, defaultValue?: T): T | undefined\n {\n const propertyValue = storage.getItem(propertyName);\n if (propertyValue)\n {\n try\n {\n return JSON.parse(propertyValue);\n }\n catch (error)\n {\n if (import.meta.env.DEV)\n {\n // eslint-disable-next-line no-console\n console.warn(\n `The \"${propertyValue}\" value for \"${propertyName}\"` +\n \" property cannot be parsed. Clearing the storage...\");\n }\n\n storage.removeItem(propertyName);\n }\n }\n\n return defaultValue;\n }\n protected _set<T>(storage: Storage, propertyName: string, newValue?: T): void\n {\n const encodedValue = JSON.stringify(newValue);\n if (encodedValue)\n {\n storage.setItem(propertyName, encodedValue);\n }\n else\n {\n storage.removeItem(propertyName);\n }\n }\n\n /**\n * Retrieves the value with the specified name from the corresponding storage.\n *\n * @param propertyName The name of the property to retrieve.\n * @param defaultValue The default value to return if the property does not exist.\n * @param persistent Whether to use the persistent `localStorage` or the volatile `sessionStorage`.\n *\n * @returns The value of the property or the default value if the property does not exist.\n */\n public get<T>(propertyName: string, defaultValue: undefined, persistent?: boolean): T | undefined;\n public get<T>(propertyName: string, defaultValue: T, persistent?: boolean): T ;\n public get<T>(propertyName: string, defaultValue?: T, persistent?: boolean): T | undefined;\n public get<T>(propertyName: string, defaultValue?: T, persistent = this._preferPersistence): T | undefined\n {\n const storage = persistent ? this._persistent : this._volatile;\n\n return this._get<T>(storage, propertyName, defaultValue);\n }\n /**\n * Retrieves the value with the specified name from the volatile `sessionStorage`.\n *\n * @param propertyName The name of the property to retrieve.\n * @param defaultValue The default value to return if the property does not exist.\n *\n * @returns The value of the property or the default value if the property does not exist.\n */\n public recall<T>(propertyName: string): T | undefined;\n public recall<T>(propertyName: string, defaultValue: T): T;\n public recall<T>(propertyName: string, defaultValue?: T): T | undefined;\n public recall<T>(propertyName: string, defaultValue?: T): T | undefined\n {\n return this._get<T>(this._volatile, propertyName, defaultValue);\n }\n /**\n * Retrieves the value with the specified name looking first in the\n * volatile `sessionStorage` and then in the persistent `localStorage`.\n *\n * @param propertyName The name of the property to retrieve.\n * @param defaultValue The default value to return if the property does not exist.\n *\n * @returns The value of the property or the default value if the property does not exist.\n */\n public retrieve<T>(propertyName: string): T | undefined;\n public retrieve<T>(propertyName: string, defaultValue: T): T;\n public retrieve<T>(propertyName: string, defaultValue?: T): T | undefined;\n public retrieve<T>(propertyName: string, defaultValue?: T): T | undefined\n {\n return this.recall<T>(propertyName) ?? this.read<T>(propertyName, defaultValue);\n }\n /**\n * Retrieves the value with the specified name from the persistent `localStorage`.\n *\n * @param propertyName The name of the property to retrieve.\n * @param defaultValue The default value to return if the property does not exist.\n *\n * @returns The value of the property or the default value if the property does not exist.\n */\n public read<T>(propertyName: string): T | undefined;\n public read<T>(propertyName: string, defaultValue: T): T;\n public read<T>(propertyName: string, defaultValue?: T): T | undefined;\n public read<T>(propertyName: string, defaultValue?: T): T | undefined\n {\n return this._get<T>(this._persistent, propertyName, defaultValue);\n }\n\n /**\n * Checks whether the property with the specified name exists in the corresponding storage.\n *\n * @param propertyName The name of the property to check.\n * @param persistent Whether to use the persistent `localStorage` or the volatile `sessionStorage`.\n *\n * @returns `true` if the property exists, `false` otherwise.\n */\n public has(propertyName: string, persistent?: boolean): boolean\n {\n const storage = persistent ? this._persistent : this._volatile;\n\n return storage.getItem(propertyName) !== null;\n }\n /**\n * Checks whether the property with the specified name exists in the volatile `sessionStorage`.\n *\n * @param propertyName The name of the property to check.\n *\n * @returns `true` if the property exists, `false` otherwise.\n */\n public knows(propertyName: string): boolean\n {\n return this._volatile.getItem(propertyName) !== null;\n }\n /**\n * Checks whether the property with the specified name exists looking first in the\n * volatile `sessionStorage` and then in the persistent `localStorage`.\n *\n * @param propertyName The name of the property to check.\n *\n * @returns `true` if the property exists, `false` otherwise.\n */\n public find(propertyName: string): boolean\n {\n return this.knows(propertyName) ?? this.exists(propertyName);\n }\n /**\n * Checks whether the property with the specified name exists in the persistent `localStorage`.\n *\n * @param propertyName The name of the property to check.\n *\n * @returns `true` if the property exists, `false` otherwise.\n */\n public exists(propertyName: string): boolean\n {\n return this._persistent.getItem(propertyName) !== null;\n }\n\n /**\n * Sets the value with the specified name in the corresponding storage.\n * If the value is `undefined`, the property is removed from the storage.\n *\n * @param propertyName The name of the property to set.\n * @param newValue The new value to set.\n * @param persistent Whether to use the persistent `localStorage` or the volatile `sessionStorage`.\n */\n public set<T>(propertyName: string, newValue?: T, persistent = this._preferPersistence): void\n {\n const storage = persistent ? this._persistent : this._volatile;\n\n this._set<T>(storage, propertyName, newValue);\n }\n /**\n * Sets the value with the specified name in the volatile `sessionStorage`.\n * If the value is `undefined`, the property is removed from the storage.\n *\n * @param propertyName The name of the property to set.\n * @param newValue The new value to set.\n */\n public remember<T>(propertyName: string, newValue?: T): void\n {\n this._set<T>(this._volatile, propertyName, newValue);\n }\n /**\n * Sets the value with the specified name in the persistent `localStorage`.\n * If the value is `undefined`, the property is removed from the storage.\n *\n * @param propertyName The name of the property to set.\n * @param newValue The new value to set.\n */\n public write<T>(propertyName: string, newValue?: T): void\n {\n this._set<T>(this._persistent, propertyName, newValue);\n }\n\n /**\n * Removes the value with the specified name from the volatile `sessionStorage`.\n *\n * @param propertyName The name of the property to remove.\n */\n public forget(propertyName: string): void\n {\n this._volatile.removeItem(propertyName);\n }\n /**\n * Removes the value with the specified name from the persistent `localStorage`.\n *\n * @param propertyName The name of the property to remove.\n */\n public erase(propertyName: string): void\n {\n this._persistent.removeItem(propertyName);\n }\n /**\n * Removes the value with the specified name from all the storages.\n *\n * @param propertyName The name of the property to remove.\n */\n public clear(propertyName: string): void\n {\n this._volatile.removeItem(propertyName);\n this._persistent.removeItem(propertyName);\n }\n}\n","import Exception from \"./exception.js\";\n\nexport default class Subscribers<P extends unknown[] = [], R = void, T extends (...args: P) => R = (...args: P) => R>\n{\n protected _subscribers: T[];\n\n public constructor()\n {\n this._subscribers = [];\n }\n\n public add(subscriber: T): void\n {\n this._subscribers.push(subscriber);\n }\n public remove(subscriber: T): void\n {\n const index = this._subscribers.indexOf(subscriber);\n if (index < 0)\n {\n throw new Exception(\"Unable to remove the requested subscriber. It was not found.\");\n }\n\n this._subscribers.splice(index, 1);\n }\n\n public call(...args: P): R[]\n {\n return this._subscribers\n .slice()\n .map((subscriber) => subscriber(...args));\n }\n}\n","export async function delay(milliseconds: number): Promise<void>\n{\n return new Promise<void>((resolve, reject) => setTimeout(resolve, milliseconds));\n}\n\nexport async function nextAnimationFrame(): Promise<void>\n{\n return new Promise<void>((resolve, reject) => requestAnimationFrame(() => resolve()));\n}\n","export async function loadScript(scriptUrl: string, scriptType = \"text/javascript\"): Promise<void>\n{\n return new Promise<void>((resolve, reject) =>\n {\n const script = document.createElement(\"script\");\n\n script.async = true;\n script.defer = true;\n script.src = scriptUrl;\n script.type = scriptType;\n\n script.onload = () => resolve();\n script.onerror = () => reject();\n\n document.body.appendChild(script);\n });\n}\n","export function count<T>(elements: Iterable<T>): number\n{\n if (\"length\" in elements) { return elements.length as number; }\n\n let _count = 0;\n for (const _ of elements) { _count += 1; }\n\n return _count;\n}\n\nexport function* range(start: number, end: number, step?: number): Iterable<number>\n{\n if (start > end) { step = step ?? -1; }\n else { step = step ?? 1; }\n\n for (let i = start; i < end; i += step) { yield i; }\n}\n\nexport function sum<T extends number>(elements: Iterable<T>): number\n{\n let _sum = 0;\n for (const value of elements) { _sum += value; }\n\n return _sum;\n}\n\nexport function unique<T>(elements: Iterable<T>): T[]\n{\n return [...new Set(elements)];\n}\n","export function hash(value: string): number\n{\n let hash = 0;\n for (let i = 0; i < value.length; i++)\n {\n const char = value.charCodeAt(i);\n hash = ((hash << 5) - hash) + char;\n hash |= 0;\n }\n\n return hash;\n}\n","export function capitalize(value: string): string\n{\n return `${value.charAt(0).toUpperCase()}${value.slice(1)}`;\n}\n","export const VERSION = \"1.1.0\";\n\nexport { DeferredPromise, Exception, JsonStorage, Subscribers } from \"./models/index.js\";\nexport {\n capitalize,\n count,\n delay,\n hash,\n loadScript,\n nextAnimationFrame,\n range,\n sum,\n unique\n\n} from \"./utils/index.js\";\n\nexport type {\n Constructor,\n FulfilledHandler,\n MaybePromise,\n PromiseExecutor,\n PromiseRejecter,\n PromiseResolver,\n RejectedHandler\n\n} from \"./types.js\";\n"],"names":["DeferredPromise","executor","_resolve","_reject","_executor","resolve","reject","__publicField","onFulfilled","onRejected","onFinally","Exception","error","exc","message","cause","name","JsonStorage","preferPersistence","storage","propertyName","defaultValue","propertyValue","newValue","encodedValue","persistent","Subscribers","subscriber","index","args","delay","milliseconds","nextAnimationFrame","loadScript","scriptUrl","scriptType","script","count","elements","_count","_","range","start","end","step","i","sum","_sum","value","unique","hash","char","capitalize","VERSION"],"mappings":";;;AAEA,MAAqBA,UAAwC,QAC7D;AAAA,EAaW,YAAYC,GACnB;AACQ,QAAAC,GACAC,GAEAC;AAEJ,IAAIH,IAEYG,IAAA,CAACC,GAASC,MACtB;AACe,MAAAJ,IAAAG,GACDF,IAAAG,GAEVL,EAASI,GAASC,CAAM;AAAA,IAAA,IAKhBF,IAAA,CAACC,GAASC,MACtB;AACe,MAAAJ,IAAAG,GACDF,IAAAG;AAAA,IAAA;AAIlB,UAAMF,CAAS;AAtCT,IAAAG,EAAA;AACA,IAAAA,EAAA;AAuCN,SAAK,WAAWL,GAChB,KAAK,UAAUC;AAAA,EACnB;AAAA,EAvCA,IAAW,UACX;AACI,WAAO,KAAK;AAAA,EAChB;AA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AAGA,EAAM,OAAO,CAAC,EAAE,aAAa,GAAGA,EAAM,MAAM,CAAC,CAAC;AAC5D;ACHO,MAAMK,IAAU;"}
|
|
1
|
+
{"version":3,"file":"core.js","sources":["../src/models/deferred-promise.ts","../src/models/exception.ts","../src/models/json-storage.ts","../src/models/subscribers.ts","../src/utils/async.ts","../src/utils/date.ts","../src/utils/dom.ts","../src/utils/iterator.ts","../src/utils/math.ts","../src/utils/string.ts","../src/index.ts"],"sourcesContent":["import type {\n PromiseExecutor,\n PromiseResolver,\n PromiseRejecter,\n FulfilledHandler,\n RejectedHandler,\n MaybePromise\n\n} from \"../types.js\";\n\nexport default class DeferredPromise<T>\n{\n protected _resolve!: PromiseResolver<T>;\n protected _reject!: PromiseRejecter;\n\n protected _promise: Promise<T>;\n\n public constructor(executor?: PromiseExecutor<T>)\n {\n this._promise = new Promise((resolve, reject) =>\n {\n this._resolve = resolve;\n this._reject = reject;\n\n executor?.(resolve, reject);\n });\n }\n\n public resolve(value: MaybePromise<T>)\n {\n this._resolve(value);\n }\n public reject(reason: unknown)\n {\n this._reject(reason);\n }\n\n public then<F = T, R = never>(onFulfilled?: FulfilledHandler<T, F>, onRejected?: RejectedHandler<unknown, R>)\n : Promise<F | R>\n {\n return this._promise.then(onFulfilled, onRejected);\n }\n public catch<R = never>(onRejected?: RejectedHandler<unknown, R>): Promise<T | R>\n {\n return this._promise.catch(onRejected);\n }\n public finally(onFinally?: () => void): Promise<T>\n {\n return this._promise.finally(onFinally);\n }\n}\n","export default class Exception extends Error\n{\n public static FromUnknown(error: unknown): Exception\n {\n if (error instanceof Exception)\n {\n return error;\n }\n if (error instanceof Error)\n {\n const exc = new Exception(error.message);\n\n exc.stack = error.stack;\n exc.name = error.name;\n\n return exc;\n }\n\n return new Exception(`${error}`);\n }\n\n public constructor(message: string, cause?: unknown, name = \"Exception\")\n {\n super(message);\n\n this.cause = cause;\n this.name = name;\n\n if (cause)\n {\n if (cause instanceof Error)\n {\n this.stack += `\\n\\nCaused by ${cause.stack}`;\n }\n else\n {\n this.stack += `\\n\\nCaused by ${cause}`;\n }\n }\n }\n}\n","/* eslint-disable no-trailing-spaces */\n\n/**\n * A wrapper around the `Storage` API to store and retrieve JSON values.\n *\n * It allows to handle either the `sessionStorage` or the `localStorage`\n * storage at the same time, depending on the required use case.\n */\nexport default class JsonStorage\n{\n protected _preferPersistence: boolean;\n\n protected _volatile: Storage;\n protected _persistent: Storage;\n\n public constructor(preferPersistence = true)\n {\n this._preferPersistence = preferPersistence;\n\n this._volatile = window.sessionStorage;\n this._persistent = window.localStorage;\n }\n\n protected _get<T>(storage: Storage, propertyName: string): T | undefined;\n protected _get<T>(storage: Storage, propertyName: string, defaultValue: T): T;\n protected _get<T>(storage: Storage, propertyName: string, defaultValue?: T): T | undefined;\n protected _get<T>(storage: Storage, propertyName: string, defaultValue?: T): T | undefined\n {\n const propertyValue = storage.getItem(propertyName);\n if (propertyValue)\n {\n try\n {\n return JSON.parse(propertyValue);\n }\n catch (error)\n {\n if (import.meta.env.DEV)\n {\n // eslint-disable-next-line no-console\n console.warn(\n `The \"${propertyValue}\" value for \"${propertyName}\"` +\n \" property cannot be parsed. Clearing the storage...\");\n }\n\n storage.removeItem(propertyName);\n }\n }\n\n return defaultValue;\n }\n protected _set<T>(storage: Storage, propertyName: string, newValue?: T): void\n {\n const encodedValue = JSON.stringify(newValue);\n if (encodedValue)\n {\n storage.setItem(propertyName, encodedValue);\n }\n else\n {\n storage.removeItem(propertyName);\n }\n }\n\n /**\n * Retrieves the value with the specified name from the corresponding storage.\n *\n * @param propertyName The name of the property to retrieve.\n * @param defaultValue The default value to return if the property does not exist.\n * @param persistent Whether to use the persistent `localStorage` or the volatile `sessionStorage`.\n *\n * @returns The value of the property or the default value if the property does not exist.\n */\n public get<T>(propertyName: string, defaultValue: undefined, persistent?: boolean): T | undefined;\n public get<T>(propertyName: string, defaultValue: T, persistent?: boolean): T ;\n public get<T>(propertyName: string, defaultValue?: T, persistent?: boolean): T | undefined;\n public get<T>(propertyName: string, defaultValue?: T, persistent = this._preferPersistence): T | undefined\n {\n const storage = persistent ? this._persistent : this._volatile;\n\n return this._get<T>(storage, propertyName, defaultValue);\n }\n /**\n * Retrieves the value with the specified name from the volatile `sessionStorage`.\n *\n * @param propertyName The name of the property to retrieve.\n * @param defaultValue The default value to return if the property does not exist.\n *\n * @returns The value of the property or the default value if the property does not exist.\n */\n public recall<T>(propertyName: string): T | undefined;\n public recall<T>(propertyName: string, defaultValue: T): T;\n public recall<T>(propertyName: string, defaultValue?: T): T | undefined;\n public recall<T>(propertyName: string, defaultValue?: T): T | undefined\n {\n return this._get<T>(this._volatile, propertyName, defaultValue);\n }\n /**\n * Retrieves the value with the specified name looking first in the\n * volatile `sessionStorage` and then in the persistent `localStorage`.\n *\n * @param propertyName The name of the property to retrieve.\n * @param defaultValue The default value to return if the property does not exist.\n *\n * @returns The value of the property or the default value if the property does not exist.\n */\n public retrieve<T>(propertyName: string): T | undefined;\n public retrieve<T>(propertyName: string, defaultValue: T): T;\n public retrieve<T>(propertyName: string, defaultValue?: T): T | undefined;\n public retrieve<T>(propertyName: string, defaultValue?: T): T | undefined\n {\n return this.recall<T>(propertyName) ?? this.read<T>(propertyName, defaultValue);\n }\n /**\n * Retrieves the value with the specified name from the persistent `localStorage`.\n *\n * @param propertyName The name of the property to retrieve.\n * @param defaultValue The default value to return if the property does not exist.\n *\n * @returns The value of the property or the default value if the property does not exist.\n */\n public read<T>(propertyName: string): T | undefined;\n public read<T>(propertyName: string, defaultValue: T): T;\n public read<T>(propertyName: string, defaultValue?: T): T | undefined;\n public read<T>(propertyName: string, defaultValue?: T): T | undefined\n {\n return this._get<T>(this._persistent, propertyName, defaultValue);\n }\n\n /**\n * Checks whether the property with the specified name exists in the corresponding storage.\n *\n * @param propertyName The name of the property to check.\n * @param persistent Whether to use the persistent `localStorage` or the volatile `sessionStorage`.\n *\n * @returns `true` if the property exists, `false` otherwise.\n */\n public has(propertyName: string, persistent?: boolean): boolean\n {\n const storage = persistent ? this._persistent : this._volatile;\n\n return storage.getItem(propertyName) !== null;\n }\n /**\n * Checks whether the property with the specified name exists in the volatile `sessionStorage`.\n *\n * @param propertyName The name of the property to check.\n *\n * @returns `true` if the property exists, `false` otherwise.\n */\n public knows(propertyName: string): boolean\n {\n return this._volatile.getItem(propertyName) !== null;\n }\n /**\n * Checks whether the property with the specified name exists looking first in the\n * volatile `sessionStorage` and then in the persistent `localStorage`.\n *\n * @param propertyName The name of the property to check.\n *\n * @returns `true` if the property exists, `false` otherwise.\n */\n public find(propertyName: string): boolean\n {\n return this.knows(propertyName) ?? this.exists(propertyName);\n }\n /**\n * Checks whether the property with the specified name exists in the persistent `localStorage`.\n *\n * @param propertyName The name of the property to check.\n *\n * @returns `true` if the property exists, `false` otherwise.\n */\n public exists(propertyName: string): boolean\n {\n return this._persistent.getItem(propertyName) !== null;\n }\n\n /**\n * Sets the value with the specified name in the corresponding storage.\n * If the value is `undefined`, the property is removed from the storage.\n *\n * @param propertyName The name of the property to set.\n * @param newValue The new value to set.\n * @param persistent Whether to use the persistent `localStorage` or the volatile `sessionStorage`.\n */\n public set<T>(propertyName: string, newValue?: T, persistent = this._preferPersistence): void\n {\n const storage = persistent ? this._persistent : this._volatile;\n\n this._set<T>(storage, propertyName, newValue);\n }\n /**\n * Sets the value with the specified name in the volatile `sessionStorage`.\n * If the value is `undefined`, the property is removed from the storage.\n *\n * @param propertyName The name of the property to set.\n * @param newValue The new value to set.\n */\n public remember<T>(propertyName: string, newValue?: T): void\n {\n this._set<T>(this._volatile, propertyName, newValue);\n }\n /**\n * Sets the value with the specified name in the persistent `localStorage`.\n * If the value is `undefined`, the property is removed from the storage.\n *\n * @param propertyName The name of the property to set.\n * @param newValue The new value to set.\n */\n public write<T>(propertyName: string, newValue?: T): void\n {\n this._set<T>(this._persistent, propertyName, newValue);\n }\n\n /**\n * Removes the value with the specified name from the volatile `sessionStorage`.\n *\n * @param propertyName The name of the property to remove.\n */\n public forget(propertyName: string): void\n {\n this._volatile.removeItem(propertyName);\n }\n /**\n * Removes the value with the specified name from the persistent `localStorage`.\n *\n * @param propertyName The name of the property to remove.\n */\n public erase(propertyName: string): void\n {\n this._persistent.removeItem(propertyName);\n }\n /**\n * Removes the value with the specified name from all the storages.\n *\n * @param propertyName The name of the property to remove.\n */\n public clear(propertyName: string): void\n {\n this._volatile.removeItem(propertyName);\n this._persistent.removeItem(propertyName);\n }\n}\n","import Exception from \"./exception.js\";\n\nexport default class Subscribers<P extends unknown[] = [], R = void, T extends (...args: P) => R = (...args: P) => R>\n{\n protected _subscribers: T[];\n\n public constructor()\n {\n this._subscribers = [];\n }\n\n public add(subscriber: T): void\n {\n this._subscribers.push(subscriber);\n }\n public remove(subscriber: T): void\n {\n const index = this._subscribers.indexOf(subscriber);\n if (index < 0)\n {\n throw new Exception(\"Unable to remove the requested subscriber. It was not found.\");\n }\n\n this._subscribers.splice(index, 1);\n }\n\n public call(...args: P): R[]\n {\n return this._subscribers\n .slice()\n .map((subscriber) => subscriber(...args));\n }\n}\n","export async function delay(milliseconds: number): Promise<void>\n{\n return new Promise<void>((resolve, reject) => setTimeout(resolve, milliseconds));\n}\n\nexport async function nextAnimationFrame(): Promise<void>\n{\n return new Promise<void>((resolve, reject) => requestAnimationFrame(() => resolve()));\n}\n","export enum DateUnit\n{\n Second = 1000,\n Minute = 60 * Second,\n Hour = 60 * Minute,\n Day = 24 * Hour,\n Week = 7 * Day,\n Month = 30 * Day,\n Year = 365 * Day\n}\n\nexport function dateDifference(start: Date, end: Date, unit = DateUnit.Day)\n{\n return Math.floor((end.getTime() - start.getTime()) / unit);\n}\n\nexport function* dateRange(start: Date, end: Date, offset = DateUnit.Day)\n{\n const endTime = end.getTime();\n\n let unixTime: number = start.getTime();\n while (unixTime < endTime)\n {\n yield new Date(unixTime);\n\n unixTime += offset;\n }\n}\n\nexport function dateRound(date: Date, unit = DateUnit.Day)\n{\n return new Date(Math.floor(date.getTime() / unit) * unit);\n}\n","export async function loadScript(scriptUrl: string, scriptType = \"text/javascript\"): Promise<void>\n{\n return new Promise<void>((resolve, reject) =>\n {\n const script = document.createElement(\"script\");\n\n script.async = true;\n script.defer = true;\n script.src = scriptUrl;\n script.type = scriptType;\n\n script.onload = () => resolve();\n script.onerror = () => reject();\n\n document.body.appendChild(script);\n });\n}\n","export function count<T>(elements: Iterable<T>): number\n{\n if (Array.isArray(elements)) { return elements.length; }\n\n let _count = 0;\n for (const _ of elements) { _count += 1; }\n\n return _count;\n}\n\nexport function range(end: number): Generator<number, void>;\nexport function range(start: number, end: number): Generator<number, void>;\nexport function range(start: number, end: number, step: number): Generator<number, void>;\nexport function* range(start: number, end?: number, step = 1): Generator<number, void>\n{\n if (end === undefined)\n {\n end = start;\n start = 0;\n }\n\n if (start > end) { step = step ?? -1; }\n\n for (let index = start; index < end; index += step) { yield index; }\n}\n\nexport function sum<T extends number>(elements: Iterable<T>): number\n{\n let _sum = 0;\n for (const value of elements) { _sum += value; }\n\n return _sum;\n}\n\nexport function unique<T>(elements: Iterable<T>): T[]\n{\n return [...new Set(elements)];\n}\n","export function hash(value: string): number\n{\n let hash = 0;\n for (let i = 0; i < value.length; i++)\n {\n const char = value.charCodeAt(i);\n hash = ((hash << 5) - hash) + char;\n hash |= 0;\n }\n\n return hash;\n}\n\nexport function random(): number;\nexport function random(max: number): number;\nexport function random(min: number, max: number): number;\nexport function random(min: number, max: number, isDecimal: boolean): number;\nexport function random(min: number, max: number, digits: number): number;\nexport function random(min: number = 1, max?: number, decimals?: boolean | number): number\n{\n if (max === undefined)\n {\n max = min;\n min = 0;\n }\n\n if (min === max)\n {\n return min;\n }\n\n let rounder: (value: number) => number;\n\n if (decimals === true)\n {\n rounder = (value) => value;\n }\n else if (decimals === undefined)\n {\n if (Math.abs(max - min) <= 1)\n {\n rounder = (value) => value;\n }\n else\n {\n rounder = Math.floor;\n }\n }\n else if (decimals === false)\n {\n rounder = Math.floor;\n }\n else\n {\n const digits = 10 ** decimals;\n\n rounder = (value) => Math.floor(value * digits) / digits;\n }\n\n return rounder(Math.random() * (max - min) + min);\n}\n","export function capitalize(value: string): string\n{\n return `${value.charAt(0).toUpperCase()}${value.slice(1)}`;\n}\n","export const VERSION = \"1.1.1\";\n\nexport { DeferredPromise, Exception, JsonStorage, Subscribers } from \"./models/index.js\";\nexport {\n capitalize,\n count,\n delay,\n dateDifference,\n dateRange,\n dateRound,\n DateUnit,\n hash,\n loadScript,\n nextAnimationFrame,\n random,\n range,\n sum,\n unique\n\n} from \"./utils/index.js\";\n\nexport type {\n Constructor,\n FulfilledHandler,\n MaybePromise,\n PromiseExecutor,\n PromiseRejecter,\n PromiseResolver,\n RejectedHandler\n\n} from \"./types.js\";\n"],"names":["DeferredPromise","executor","__publicField","resolve","reject","value","reason","onFulfilled","onRejected","onFinally","Exception","error","exc","message","cause","name","JsonStorage","preferPersistence","storage","propertyName","defaultValue","propertyValue","newValue","encodedValue","persistent","Subscribers","subscriber","index","args","delay","milliseconds","nextAnimationFrame","DateUnit","dateDifference","start","end","unit","dateRange","offset","endTime","unixTime","dateRound","date","loadScript","scriptUrl","scriptType","script","count","elements","_count","_","range","step","sum","_sum","unique","hash","i","char","random","min","max","decimals","rounder","digits","capitalize","VERSION"],"mappings":";;;AAUA,MAAqBA,EACrB;AAAA,EAMW,YAAYC,GACnB;AANU,IAAAC,EAAA;AACA,IAAAA,EAAA;AAEA,IAAAA,EAAA;AAIN,SAAK,WAAW,IAAI,QAAQ,CAACC,GAASC,MACtC;AACI,WAAK,WAAWD,GAChB,KAAK,UAAUC,GAEfH,KAAA,QAAAA,EAAWE,GAASC;AAAA,IAAM,CAC7B;AAAA,EACL;AAAA,EAEO,QAAQC,GACf;AACI,SAAK,SAASA,CAAK;AAAA,EACvB;AAAA,EACO,OAAOC,GACd;AACI,SAAK,QAAQA,CAAM;AAAA,EACvB;AAAA,EAEO,KAAuBC,GAAsCC,GAEpE;AACI,WAAO,KAAK,SAAS,KAAKD,GAAaC,CAAU;AAAA,EACrD;AAAA,EACO,MAA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C,CAAC;AAC5D;ACHO,MAAM6D,IAAU;"}
|
package/dist/core.umd.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(r,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(r=typeof globalThis<"u"?globalThis:r||self,o(r.Core={}))})(this,function(r){"use strict";var k=Object.defineProperty;var j=(r,o,c)=>o in r?k(r,o,{enumerable:!0,configurable:!0,writable:!0,value:c}):r[o]=c;var u=(r,o,c)=>(j(r,typeof o!="symbol"?o+"":o,c),c);class o{constructor(e){u(this,"_resolve");u(this,"_reject");u(this,"_promise");this._promise=new Promise((t,i)=>{this._resolve=t,this._reject=i,e==null||e(t,i)})}resolve(e){this._resolve(e)}reject(e){this._reject(e)}then(e,t){return this._promise.then(e,t)}catch(e){return this._promise.catch(e)}finally(e){return this._promise.finally(e)}}class c extends Error{static FromUnknown(e){if(e instanceof c)return e;if(e instanceof Error){const t=new c(e.message);return t.stack=e.stack,t.name=e.name,t}return new c(`${e}`)}constructor(e,t,i="Exception"){super(e),this.cause=t,this.name=i,t&&(t instanceof Error?this.stack+=`
|
|
2
2
|
|
|
3
3
|
Caused by ${t.stack}`:this.stack+=`
|
|
4
4
|
|
|
5
|
-
Caused by ${t}`)}}class
|
|
5
|
+
Caused by ${t}`)}}class h{constructor(e=!0){u(this,"_preferPersistence");u(this,"_volatile");u(this,"_persistent");this._preferPersistence=e,this._volatile=window.sessionStorage,this._persistent=window.localStorage}_get(e,t,i){const n=e.getItem(t);if(n)try{return JSON.parse(n)}catch{e.removeItem(t)}return i}_set(e,t,i){const n=JSON.stringify(i);n?e.setItem(t,n):e.removeItem(t)}get(e,t,i=this._preferPersistence){const n=i?this._persistent:this._volatile;return this._get(n,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,i=this._preferPersistence){const n=i?this._persistent:this._volatile;this._set(n,e,t)}remember(e,t){this._set(this._volatile,e,t)}write(e,t){this._set(this._persistent,e,t)}forget(e){this._volatile.removeItem(e)}erase(e){this._persistent.removeItem(e)}clear(e){this._volatile.removeItem(e),this._persistent.removeItem(e)}}class f{constructor(){u(this,"_subscribers");this._subscribers=[]}add(e){this._subscribers.push(e)}remove(e){const t=this._subscribers.indexOf(e);if(t<0)throw new c("Unable to remove the requested subscriber. It was not found.");this._subscribers.splice(t,1)}call(...e){return this._subscribers.slice().map(t=>t(...e))}}async function d(s){return new Promise((e,t)=>setTimeout(e,s))}async function _(){return new Promise((s,e)=>requestAnimationFrame(()=>s()))}var a=(s=>(s[s.Second=1e3]="Second",s[s.Minute=6e4]="Minute",s[s.Hour=36e5]="Hour",s[s.Day=864e5]="Day",s[s.Week=6048e5]="Week",s[s.Month=2592e6]="Month",s[s.Year=31536e6]="Year",s))(a||{});function m(s,e,t=864e5){return Math.floor((e.getTime()-s.getTime())/t)}function*g(s,e,t=864e5){const i=e.getTime();let n=s.getTime();for(;n<i;)yield new Date(n),n+=t}function b(s,e=864e5){return new Date(Math.floor(s.getTime()/e)*e)}async function v(s,e="text/javascript"){return new Promise((t,i)=>{const n=document.createElement("script");n.async=!0,n.defer=!0,n.src=s,n.type=e,n.onload=()=>t(),n.onerror=()=>i(),document.body.appendChild(n)})}function w(s){if(Array.isArray(s))return s.length;let e=0;for(const t of s)e+=1;return e}function*y(s,e,t=1){e===void 0&&(e=s,s=0),s>e&&(t=t??-1);for(let i=s;i<e;i+=t)yield i}function S(s){let e=0;for(const t of s)e+=t;return e}function p(s){return[...new Set(s)]}function I(s){let e=0;for(let t=0;t<s.length;t++){const i=s.charCodeAt(t);e=(e<<5)-e+i,e|=0}return e}function M(s=1,e,t){if(e===void 0&&(e=s,s=0),s===e)return s;let i;if(t===!0)i=n=>n;else if(t===void 0)Math.abs(e-s)<=1?i=n=>n:i=Math.floor;else if(t===!1)i=Math.floor;else{const n=10**t;i=l=>Math.floor(l*n)/n}return i(Math.random()*(e-s)+s)}function P(s){return`${s.charAt(0).toUpperCase()}${s.slice(1)}`}const T="1.1.1";r.DateUnit=a,r.DeferredPromise=o,r.Exception=c,r.JsonStorage=h,r.Subscribers=f,r.VERSION=T,r.capitalize=P,r.count=w,r.dateDifference=m,r.dateRange=g,r.dateRound=b,r.delay=d,r.hash=I,r.loadScript=v,r.nextAnimationFrame=_,r.random=M,r.range=y,r.sum=S,r.unique=p,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
|
|
6
6
|
//# sourceMappingURL=core.umd.cjs.map
|
package/dist/core.umd.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core.umd.cjs","sources":["../src/models/deferred-promise.ts","../src/models/exception.ts","../src/models/json-storage.ts","../src/models/subscribers.ts","../src/utils/async.ts","../src/utils/dom.ts","../src/utils/iterator.ts","../src/utils/math.ts","../src/utils/string.ts","../src/index.ts"],"sourcesContent":["import type { PromiseExecutor, PromiseResolver, PromiseRejecter, FulfilledHandler, RejectedHandler } from \"../types.js\";\n\nexport default class DeferredPromise<T, E = unknown> extends Promise<T>\n{\n protected _resolve: PromiseResolver<T>;\n protected _reject: PromiseRejecter<E>;\n\n public get resolve(): PromiseResolver<T>\n {\n return this._resolve;\n }\n public get reject(): PromiseRejecter<E>\n {\n return this._reject;\n }\n\n public constructor(executor?: PromiseExecutor<T>)\n {\n let _resolve: PromiseResolver<T>;\n let _reject: PromiseRejecter<E>;\n\n let _executor: PromiseExecutor<T>;\n\n if (executor)\n {\n _executor = (resolve, reject) =>\n {\n _resolve = resolve;\n _reject = reject;\n\n executor(resolve, reject);\n };\n }\n else\n {\n _executor = (resolve, reject) =>\n {\n _resolve = resolve;\n _reject = reject;\n };\n }\n\n super(_executor);\n\n this._resolve = _resolve!;\n this._reject = _reject!;\n }\n\n public then<F = T, R = E>(onFulfilled?: FulfilledHandler<T, F> | null, onRejected?: RejectedHandler<E, R>)\n : DeferredPromise<F, R>\n {\n super.then(onFulfilled, onRejected);\n\n return (this as unknown) as DeferredPromise<F, R>;\n }\n public catch<R = E>(onRejected?: RejectedHandler<E, R>): DeferredPromise<T, R>\n {\n super.catch(onRejected);\n\n return (this as unknown) as DeferredPromise<T, R>;\n }\n public finally(onFinally?: (() => void) | null): DeferredPromise<T, E>\n {\n super.finally(onFinally);\n\n return this;\n }\n}\n","export default class Exception extends Error\n{\n public static FromUnknown(error: unknown): Exception\n {\n if (error instanceof Exception)\n {\n return error;\n }\n if (error instanceof Error)\n {\n const exc = new Exception(error.message);\n\n exc.stack = error.stack;\n exc.name = error.name;\n\n return exc;\n }\n\n return new Exception(`${error}`);\n }\n\n public constructor(message: string, cause?: unknown, name = \"Exception\")\n {\n super(message);\n\n this.cause = cause;\n this.name = name;\n\n if (cause)\n {\n if (cause instanceof Error)\n {\n this.stack += `\\n\\nCaused by ${cause.stack}`;\n }\n else\n {\n this.stack += `\\n\\nCaused by ${cause}`;\n }\n }\n }\n}\n","/* eslint-disable no-trailing-spaces */\n\n/**\n * A wrapper around the `Storage` API to store and retrieve JSON values.\n *\n * It allows to handle either the `sessionStorage` or the `localStorage`\n * storage at the same time, depending on the required use case.\n */\nexport default class JsonStorage\n{\n protected _preferPersistence: boolean;\n\n protected _volatile: Storage;\n protected _persistent: Storage;\n\n public constructor(preferPersistence = true)\n {\n this._preferPersistence = preferPersistence;\n\n this._volatile = window.sessionStorage;\n this._persistent = window.localStorage;\n }\n\n protected _get<T>(storage: Storage, propertyName: string): T | undefined;\n protected _get<T>(storage: Storage, propertyName: string, defaultValue: T): T;\n protected _get<T>(storage: Storage, propertyName: string, defaultValue?: T): T | undefined;\n protected _get<T>(storage: Storage, propertyName: string, defaultValue?: T): T | undefined\n {\n const propertyValue = storage.getItem(propertyName);\n if (propertyValue)\n {\n try\n {\n return JSON.parse(propertyValue);\n }\n catch (error)\n {\n if (import.meta.env.DEV)\n {\n // eslint-disable-next-line no-console\n console.warn(\n `The \"${propertyValue}\" value for \"${propertyName}\"` +\n \" property cannot be parsed. Clearing the storage...\");\n }\n\n storage.removeItem(propertyName);\n }\n }\n\n return defaultValue;\n }\n protected _set<T>(storage: Storage, propertyName: string, newValue?: T): void\n {\n const encodedValue = JSON.stringify(newValue);\n if (encodedValue)\n {\n storage.setItem(propertyName, encodedValue);\n }\n else\n {\n storage.removeItem(propertyName);\n }\n }\n\n /**\n * Retrieves the value with the specified name from the corresponding storage.\n *\n * @param propertyName The name of the property to retrieve.\n * @param defaultValue The default value to return if the property does not exist.\n * @param persistent Whether to use the persistent `localStorage` or the volatile `sessionStorage`.\n *\n * @returns The value of the property or the default value if the property does not exist.\n */\n public get<T>(propertyName: string, defaultValue: undefined, persistent?: boolean): T | undefined;\n public get<T>(propertyName: string, defaultValue: T, persistent?: boolean): T ;\n public get<T>(propertyName: string, defaultValue?: T, persistent?: boolean): T | undefined;\n public get<T>(propertyName: string, defaultValue?: T, persistent = this._preferPersistence): T | undefined\n {\n const storage = persistent ? this._persistent : this._volatile;\n\n return this._get<T>(storage, propertyName, defaultValue);\n }\n /**\n * Retrieves the value with the specified name from the volatile `sessionStorage`.\n *\n * @param propertyName The name of the property to retrieve.\n * @param defaultValue The default value to return if the property does not exist.\n *\n * @returns The value of the property or the default value if the property does not exist.\n */\n public recall<T>(propertyName: string): T | undefined;\n public recall<T>(propertyName: string, defaultValue: T): T;\n public recall<T>(propertyName: string, defaultValue?: T): T | undefined;\n public recall<T>(propertyName: string, defaultValue?: T): T | undefined\n {\n return this._get<T>(this._volatile, propertyName, defaultValue);\n }\n /**\n * Retrieves the value with the specified name looking first in the\n * volatile `sessionStorage` and then in the persistent `localStorage`.\n *\n * @param propertyName The name of the property to retrieve.\n * @param defaultValue The default value to return if the property does not exist.\n *\n * @returns The value of the property or the default value if the property does not exist.\n */\n public retrieve<T>(propertyName: string): T | undefined;\n public retrieve<T>(propertyName: string, defaultValue: T): T;\n public retrieve<T>(propertyName: string, defaultValue?: T): T | undefined;\n public retrieve<T>(propertyName: string, defaultValue?: T): T | undefined\n {\n return this.recall<T>(propertyName) ?? this.read<T>(propertyName, defaultValue);\n }\n /**\n * Retrieves the value with the specified name from the persistent `localStorage`.\n *\n * @param propertyName The name of the property to retrieve.\n * @param defaultValue The default value to return if the property does not exist.\n *\n * @returns The value of the property or the default value if the property does not exist.\n */\n public read<T>(propertyName: string): T | undefined;\n public read<T>(propertyName: string, defaultValue: T): T;\n public read<T>(propertyName: string, defaultValue?: T): T | undefined;\n public read<T>(propertyName: string, defaultValue?: T): T | undefined\n {\n return this._get<T>(this._persistent, propertyName, defaultValue);\n }\n\n /**\n * Checks whether the property with the specified name exists in the corresponding storage.\n *\n * @param propertyName The name of the property to check.\n * @param persistent Whether to use the persistent `localStorage` or the volatile `sessionStorage`.\n *\n * @returns `true` if the property exists, `false` otherwise.\n */\n public has(propertyName: string, persistent?: boolean): boolean\n {\n const storage = persistent ? this._persistent : this._volatile;\n\n return storage.getItem(propertyName) !== null;\n }\n /**\n * Checks whether the property with the specified name exists in the volatile `sessionStorage`.\n *\n * @param propertyName The name of the property to check.\n *\n * @returns `true` if the property exists, `false` otherwise.\n */\n public knows(propertyName: string): boolean\n {\n return this._volatile.getItem(propertyName) !== null;\n }\n /**\n * Checks whether the property with the specified name exists looking first in the\n * volatile `sessionStorage` and then in the persistent `localStorage`.\n *\n * @param propertyName The name of the property to check.\n *\n * @returns `true` if the property exists, `false` otherwise.\n */\n public find(propertyName: string): boolean\n {\n return this.knows(propertyName) ?? this.exists(propertyName);\n }\n /**\n * Checks whether the property with the specified name exists in the persistent `localStorage`.\n *\n * @param propertyName The name of the property to check.\n *\n * @returns `true` if the property exists, `false` otherwise.\n */\n public exists(propertyName: string): boolean\n {\n return this._persistent.getItem(propertyName) !== null;\n }\n\n /**\n * Sets the value with the specified name in the corresponding storage.\n * If the value is `undefined`, the property is removed from the storage.\n *\n * @param propertyName The name of the property to set.\n * @param newValue The new value to set.\n * @param persistent Whether to use the persistent `localStorage` or the volatile `sessionStorage`.\n */\n public set<T>(propertyName: string, newValue?: T, persistent = this._preferPersistence): void\n {\n const storage = persistent ? this._persistent : this._volatile;\n\n this._set<T>(storage, propertyName, newValue);\n }\n /**\n * Sets the value with the specified name in the volatile `sessionStorage`.\n * If the value is `undefined`, the property is removed from the storage.\n *\n * @param propertyName The name of the property to set.\n * @param newValue The new value to set.\n */\n public remember<T>(propertyName: string, newValue?: T): void\n {\n this._set<T>(this._volatile, propertyName, newValue);\n }\n /**\n * Sets the value with the specified name in the persistent `localStorage`.\n * If the value is `undefined`, the property is removed from the storage.\n *\n * @param propertyName The name of the property to set.\n * @param newValue The new value to set.\n */\n public write<T>(propertyName: string, newValue?: T): void\n {\n this._set<T>(this._persistent, propertyName, newValue);\n }\n\n /**\n * Removes the value with the specified name from the volatile `sessionStorage`.\n *\n * @param propertyName The name of the property to remove.\n */\n public forget(propertyName: string): void\n {\n this._volatile.removeItem(propertyName);\n }\n /**\n * Removes the value with the specified name from the persistent `localStorage`.\n *\n * @param propertyName The name of the property to remove.\n */\n public erase(propertyName: string): void\n {\n this._persistent.removeItem(propertyName);\n }\n /**\n * Removes the value with the specified name from all the storages.\n *\n * @param propertyName The name of the property to remove.\n */\n public clear(propertyName: string): void\n {\n this._volatile.removeItem(propertyName);\n this._persistent.removeItem(propertyName);\n }\n}\n","import Exception from \"./exception.js\";\n\nexport default class Subscribers<P extends unknown[] = [], R = void, T extends (...args: P) => R = (...args: P) => R>\n{\n protected _subscribers: T[];\n\n public constructor()\n {\n this._subscribers = [];\n }\n\n public add(subscriber: T): void\n {\n this._subscribers.push(subscriber);\n }\n public remove(subscriber: T): void\n {\n const index = this._subscribers.indexOf(subscriber);\n if (index < 0)\n {\n throw new Exception(\"Unable to remove the requested subscriber. It was not found.\");\n }\n\n this._subscribers.splice(index, 1);\n }\n\n public call(...args: P): R[]\n {\n return this._subscribers\n .slice()\n .map((subscriber) => subscriber(...args));\n }\n}\n","export async function delay(milliseconds: number): Promise<void>\n{\n return new Promise<void>((resolve, reject) => setTimeout(resolve, milliseconds));\n}\n\nexport async function nextAnimationFrame(): Promise<void>\n{\n return new Promise<void>((resolve, reject) => requestAnimationFrame(() => resolve()));\n}\n","export async function loadScript(scriptUrl: string, scriptType = \"text/javascript\"): Promise<void>\n{\n return new Promise<void>((resolve, reject) =>\n {\n const script = document.createElement(\"script\");\n\n script.async = true;\n script.defer = true;\n script.src = scriptUrl;\n script.type = scriptType;\n\n script.onload = () => resolve();\n script.onerror = () => reject();\n\n document.body.appendChild(script);\n });\n}\n","export function count<T>(elements: Iterable<T>): number\n{\n if (\"length\" in elements) { return elements.length as number; }\n\n let _count = 0;\n for (const _ of elements) { _count += 1; }\n\n return _count;\n}\n\nexport function* range(start: number, end: number, step?: number): Iterable<number>\n{\n if (start > end) { step = step ?? -1; }\n else { step = step ?? 1; }\n\n for (let i = start; i < end; i += step) { yield i; }\n}\n\nexport function sum<T extends number>(elements: Iterable<T>): number\n{\n let _sum = 0;\n for (const value of elements) { _sum += value; }\n\n return _sum;\n}\n\nexport function unique<T>(elements: Iterable<T>): T[]\n{\n return [...new Set(elements)];\n}\n","export function hash(value: string): number\n{\n let hash = 0;\n for (let i = 0; i < value.length; i++)\n {\n const char = value.charCodeAt(i);\n hash = ((hash << 5) - hash) + char;\n hash |= 0;\n }\n\n return hash;\n}\n","export function capitalize(value: string): string\n{\n return `${value.charAt(0).toUpperCase()}${value.slice(1)}`;\n}\n","export const VERSION = \"1.1.0\";\n\nexport { DeferredPromise, Exception, JsonStorage, Subscribers } from \"./models/index.js\";\nexport {\n capitalize,\n count,\n delay,\n hash,\n loadScript,\n nextAnimationFrame,\n range,\n sum,\n unique\n\n} from \"./utils/index.js\";\n\nexport type {\n Constructor,\n FulfilledHandler,\n MaybePromise,\n PromiseExecutor,\n PromiseRejecter,\n PromiseResolver,\n RejectedHandler\n\n} from \"./types.js\";\n"],"names":["DeferredPromise","executor","_resolve","_reject","_executor","resolve","reject","__publicField","onFulfilled","onRejected","onFinally","Exception","error","exc","message","cause","name","JsonStorage","preferPersistence","storage","propertyName","defaultValue","propertyValue","newValue","encodedValue","persistent","Subscribers","subscriber","index","args","delay","milliseconds","nextAnimationFrame","loadScript","scriptUrl","scriptType","script","count","elements","_count","_","range","start","end","step","sum","_sum","value","unique","hash","i","char","capitalize","VERSION"],"mappings":"oYAEA,MAAqBA,UAAwC,OAC7D,CAaW,YAAYC,EACnB,CACQ,IAAAC,EACAC,EAEAC,EAEAH,EAEYG,EAAA,CAACC,EAASC,IACtB,CACeJ,EAAAG,EACDF,EAAAG,EAEVL,EAASI,EAASC,CAAM,CAAA,EAKhBF,EAAA,CAACC,EAASC,IACtB,CACeJ,EAAAG,EACDF,EAAAG,CAAA,EAIlB,MAAMF,CAAS,EAtCTG,EAAA,iBACAA,EAAA,gBAuCN,KAAK,SAAWL,EAChB,KAAK,QAAUC,CACnB,CAvCA,IAAW,SACX,CACI,OAAO,KAAK,QAChB,CACA,IAAW,QACX,CACI,OAAO,KAAK,OAChB,CAkCO,KAAmBK,EAA6CC,EAEvE,CACU,aAAA,KAAKD,EAAaC,CAAU,EAE1B,IACZ,CACO,MAAaA,EACpB,CACI,aAAM,MAAMA,CAAU,EAEd,IACZ,CACO,QAAQC,EACf,CACI,aAAM,QAAQA,CAAS,EAEhB,IACX,CACJ,CCnEA,MAAqBC,UAAkB,KACvC,CACI,OAAc,YAAYC,EAC1B,CACI,GAAIA,aAAiBD,EAEV,OAAAC,EAEX,GAAIA,aAAiB,MACrB,CACI,MAAMC,EAAM,IAAIF,EAAUC,EAAM,OAAO,EAEvC,OAAAC,EAAI,MAAQD,EAAM,MAClBC,EAAI,KAAOD,EAAM,KAEVC,CACX,CAEA,OAAO,IAAIF,EAAU,GAAGC,CAAK,EAAE,CACnC,CAEO,YAAYE,EAAiBC,EAAiBC,EAAO,YAC5D,CACI,MA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ts","../src/models/exception.ts","../src/models/json-storage.ts","../src/models/subscribers.ts","../src/utils/async.ts","../src/utils/date.ts","../src/utils/dom.ts","../src/utils/iterator.ts","../src/utils/math.ts","../src/utils/string.ts","../src/index.ts"],"sourcesContent":["import type {\n PromiseExecutor,\n PromiseResolver,\n PromiseRejecter,\n FulfilledHandler,\n RejectedHandler,\n MaybePromise\n\n} from \"../types.js\";\n\nexport default class DeferredPromise<T>\n{\n protected _resolve!: PromiseResolver<T>;\n protected _reject!: PromiseRejecter;\n\n protected _promise: Promise<T>;\n\n public constructor(executor?: PromiseExecutor<T>)\n {\n this._promise = new Promise((resolve, reject) =>\n {\n this._resolve = resolve;\n this._reject = reject;\n\n executor?.(resolve, reject);\n });\n }\n\n public resolve(value: MaybePromise<T>)\n {\n this._resolve(value);\n }\n public reject(reason: unknown)\n {\n this._reject(reason);\n }\n\n public then<F = T, R = never>(onFulfilled?: FulfilledHandler<T, F>, onRejected?: RejectedHandler<unknown, R>)\n : Promise<F | R>\n {\n return this._promise.then(onFulfilled, onRejected);\n }\n public catch<R = never>(onRejected?: RejectedHandler<unknown, R>): Promise<T | R>\n {\n return this._promise.catch(onRejected);\n }\n public finally(onFinally?: () => void): Promise<T>\n {\n return this._promise.finally(onFinally);\n }\n}\n","export default class Exception extends Error\n{\n public static FromUnknown(error: unknown): Exception\n {\n if (error instanceof Exception)\n {\n return error;\n }\n if (error instanceof Error)\n {\n const exc = new Exception(error.message);\n\n exc.stack = error.stack;\n exc.name = error.name;\n\n return exc;\n }\n\n return new Exception(`${error}`);\n }\n\n public constructor(message: string, cause?: unknown, name = \"Exception\")\n {\n super(message);\n\n this.cause = cause;\n this.name = name;\n\n if (cause)\n {\n if (cause instanceof Error)\n {\n this.stack += `\\n\\nCaused by ${cause.stack}`;\n }\n else\n {\n this.stack += `\\n\\nCaused by ${cause}`;\n }\n }\n }\n}\n","/* eslint-disable no-trailing-spaces */\n\n/**\n * A wrapper around the `Storage` API to store and retrieve JSON values.\n *\n * It allows to handle either the `sessionStorage` or the `localStorage`\n * storage at the same time, depending on the required use case.\n */\nexport default class JsonStorage\n{\n protected _preferPersistence: boolean;\n\n protected _volatile: Storage;\n protected _persistent: Storage;\n\n public constructor(preferPersistence = true)\n {\n this._preferPersistence = preferPersistence;\n\n this._volatile = window.sessionStorage;\n this._persistent = window.localStorage;\n }\n\n protected _get<T>(storage: Storage, propertyName: string): T | undefined;\n protected _get<T>(storage: Storage, propertyName: string, defaultValue: T): T;\n protected _get<T>(storage: Storage, propertyName: string, defaultValue?: T): T | undefined;\n protected _get<T>(storage: Storage, propertyName: string, defaultValue?: T): T | undefined\n {\n const propertyValue = storage.getItem(propertyName);\n if (propertyValue)\n {\n try\n {\n return JSON.parse(propertyValue);\n }\n catch (error)\n {\n if (import.meta.env.DEV)\n {\n // eslint-disable-next-line no-console\n console.warn(\n `The \"${propertyValue}\" value for \"${propertyName}\"` +\n \" property cannot be parsed. Clearing the storage...\");\n }\n\n storage.removeItem(propertyName);\n }\n }\n\n return defaultValue;\n }\n protected _set<T>(storage: Storage, propertyName: string, newValue?: T): void\n {\n const encodedValue = JSON.stringify(newValue);\n if (encodedValue)\n {\n storage.setItem(propertyName, encodedValue);\n }\n else\n {\n storage.removeItem(propertyName);\n }\n }\n\n /**\n * Retrieves the value with the specified name from the corresponding storage.\n *\n * @param propertyName The name of the property to retrieve.\n * @param defaultValue The default value to return if the property does not exist.\n * @param persistent Whether to use the persistent `localStorage` or the volatile `sessionStorage`.\n *\n * @returns The value of the property or the default value if the property does not exist.\n */\n public get<T>(propertyName: string, defaultValue: undefined, persistent?: boolean): T | undefined;\n public get<T>(propertyName: string, defaultValue: T, persistent?: boolean): T ;\n public get<T>(propertyName: string, defaultValue?: T, persistent?: boolean): T | undefined;\n public get<T>(propertyName: string, defaultValue?: T, persistent = this._preferPersistence): T | undefined\n {\n const storage = persistent ? this._persistent : this._volatile;\n\n return this._get<T>(storage, propertyName, defaultValue);\n }\n /**\n * Retrieves the value with the specified name from the volatile `sessionStorage`.\n *\n * @param propertyName The name of the property to retrieve.\n * @param defaultValue The default value to return if the property does not exist.\n *\n * @returns The value of the property or the default value if the property does not exist.\n */\n public recall<T>(propertyName: string): T | undefined;\n public recall<T>(propertyName: string, defaultValue: T): T;\n public recall<T>(propertyName: string, defaultValue?: T): T | undefined;\n public recall<T>(propertyName: string, defaultValue?: T): T | undefined\n {\n return this._get<T>(this._volatile, propertyName, defaultValue);\n }\n /**\n * Retrieves the value with the specified name looking first in the\n * volatile `sessionStorage` and then in the persistent `localStorage`.\n *\n * @param propertyName The name of the property to retrieve.\n * @param defaultValue The default value to return if the property does not exist.\n *\n * @returns The value of the property or the default value if the property does not exist.\n */\n public retrieve<T>(propertyName: string): T | undefined;\n public retrieve<T>(propertyName: string, defaultValue: T): T;\n public retrieve<T>(propertyName: string, defaultValue?: T): T | undefined;\n public retrieve<T>(propertyName: string, defaultValue?: T): T | undefined\n {\n return this.recall<T>(propertyName) ?? this.read<T>(propertyName, defaultValue);\n }\n /**\n * Retrieves the value with the specified name from the persistent `localStorage`.\n *\n * @param propertyName The name of the property to retrieve.\n * @param defaultValue The default value to return if the property does not exist.\n *\n * @returns The value of the property or the default value if the property does not exist.\n */\n public read<T>(propertyName: string): T | undefined;\n public read<T>(propertyName: string, defaultValue: T): T;\n public read<T>(propertyName: string, defaultValue?: T): T | undefined;\n public read<T>(propertyName: string, defaultValue?: T): T | undefined\n {\n return this._get<T>(this._persistent, propertyName, defaultValue);\n }\n\n /**\n * Checks whether the property with the specified name exists in the corresponding storage.\n *\n * @param propertyName The name of the property to check.\n * @param persistent Whether to use the persistent `localStorage` or the volatile `sessionStorage`.\n *\n * @returns `true` if the property exists, `false` otherwise.\n */\n public has(propertyName: string, persistent?: boolean): boolean\n {\n const storage = persistent ? this._persistent : this._volatile;\n\n return storage.getItem(propertyName) !== null;\n }\n /**\n * Checks whether the property with the specified name exists in the volatile `sessionStorage`.\n *\n * @param propertyName The name of the property to check.\n *\n * @returns `true` if the property exists, `false` otherwise.\n */\n public knows(propertyName: string): boolean\n {\n return this._volatile.getItem(propertyName) !== null;\n }\n /**\n * Checks whether the property with the specified name exists looking first in the\n * volatile `sessionStorage` and then in the persistent `localStorage`.\n *\n * @param propertyName The name of the property to check.\n *\n * @returns `true` if the property exists, `false` otherwise.\n */\n public find(propertyName: string): boolean\n {\n return this.knows(propertyName) ?? this.exists(propertyName);\n }\n /**\n * Checks whether the property with the specified name exists in the persistent `localStorage`.\n *\n * @param propertyName The name of the property to check.\n *\n * @returns `true` if the property exists, `false` otherwise.\n */\n public exists(propertyName: string): boolean\n {\n return this._persistent.getItem(propertyName) !== null;\n }\n\n /**\n * Sets the value with the specified name in the corresponding storage.\n * If the value is `undefined`, the property is removed from the storage.\n *\n * @param propertyName The name of the property to set.\n * @param newValue The new value to set.\n * @param persistent Whether to use the persistent `localStorage` or the volatile `sessionStorage`.\n */\n public set<T>(propertyName: string, newValue?: T, persistent = this._preferPersistence): void\n {\n const storage = persistent ? this._persistent : this._volatile;\n\n this._set<T>(storage, propertyName, newValue);\n }\n /**\n * Sets the value with the specified name in the volatile `sessionStorage`.\n * If the value is `undefined`, the property is removed from the storage.\n *\n * @param propertyName The name of the property to set.\n * @param newValue The new value to set.\n */\n public remember<T>(propertyName: string, newValue?: T): void\n {\n this._set<T>(this._volatile, propertyName, newValue);\n }\n /**\n * Sets the value with the specified name in the persistent `localStorage`.\n * If the value is `undefined`, the property is removed from the storage.\n *\n * @param propertyName The name of the property to set.\n * @param newValue The new value to set.\n */\n public write<T>(propertyName: string, newValue?: T): void\n {\n this._set<T>(this._persistent, propertyName, newValue);\n }\n\n /**\n * Removes the value with the specified name from the volatile `sessionStorage`.\n *\n * @param propertyName The name of the property to remove.\n */\n public forget(propertyName: string): void\n {\n this._volatile.removeItem(propertyName);\n }\n /**\n * Removes the value with the specified name from the persistent `localStorage`.\n *\n * @param propertyName The name of the property to remove.\n */\n public erase(propertyName: string): void\n {\n this._persistent.removeItem(propertyName);\n }\n /**\n * Removes the value with the specified name from all the storages.\n *\n * @param propertyName The name of the property to remove.\n */\n public clear(propertyName: string): void\n {\n this._volatile.removeItem(propertyName);\n this._persistent.removeItem(propertyName);\n }\n}\n","import Exception from \"./exception.js\";\n\nexport default class Subscribers<P extends unknown[] = [], R = void, T extends (...args: P) => R = (...args: P) => R>\n{\n protected _subscribers: T[];\n\n public constructor()\n {\n this._subscribers = [];\n }\n\n public add(subscriber: T): void\n {\n this._subscribers.push(subscriber);\n }\n public remove(subscriber: T): void\n {\n const index = this._subscribers.indexOf(subscriber);\n if (index < 0)\n {\n throw new Exception(\"Unable to remove the requested subscriber. It was not found.\");\n }\n\n this._subscribers.splice(index, 1);\n }\n\n public call(...args: P): R[]\n {\n return this._subscribers\n .slice()\n .map((subscriber) => subscriber(...args));\n }\n}\n","export async function delay(milliseconds: number): Promise<void>\n{\n return new Promise<void>((resolve, reject) => setTimeout(resolve, milliseconds));\n}\n\nexport async function nextAnimationFrame(): Promise<void>\n{\n return new Promise<void>((resolve, reject) => requestAnimationFrame(() => resolve()));\n}\n","export enum DateUnit\n{\n Second = 1000,\n Minute = 60 * Second,\n Hour = 60 * Minute,\n Day = 24 * Hour,\n Week = 7 * Day,\n Month = 30 * Day,\n Year = 365 * Day\n}\n\nexport function dateDifference(start: Date, end: Date, unit = DateUnit.Day)\n{\n return Math.floor((end.getTime() - start.getTime()) / unit);\n}\n\nexport function* dateRange(start: Date, end: Date, offset = DateUnit.Day)\n{\n const endTime = end.getTime();\n\n let unixTime: number = start.getTime();\n while (unixTime < endTime)\n {\n yield new Date(unixTime);\n\n unixTime += offset;\n }\n}\n\nexport function dateRound(date: Date, unit = DateUnit.Day)\n{\n return new Date(Math.floor(date.getTime() / unit) * unit);\n}\n","export async function loadScript(scriptUrl: string, scriptType = \"text/javascript\"): Promise<void>\n{\n return new Promise<void>((resolve, reject) =>\n {\n const script = document.createElement(\"script\");\n\n script.async = true;\n script.defer = true;\n script.src = scriptUrl;\n script.type = scriptType;\n\n script.onload = () => resolve();\n script.onerror = () => reject();\n\n document.body.appendChild(script);\n });\n}\n","export function count<T>(elements: Iterable<T>): number\n{\n if (Array.isArray(elements)) { return elements.length; }\n\n let _count = 0;\n for (const _ of elements) { _count += 1; }\n\n return _count;\n}\n\nexport function range(end: number): Generator<number, void>;\nexport function range(start: number, end: number): Generator<number, void>;\nexport function range(start: number, end: number, step: number): Generator<number, void>;\nexport function* range(start: number, end?: number, step = 1): Generator<number, void>\n{\n if (end === undefined)\n {\n end = start;\n start = 0;\n }\n\n if (start > end) { step = step ?? -1; }\n\n for (let index = start; index < end; index += step) { yield index; }\n}\n\nexport function sum<T extends number>(elements: Iterable<T>): number\n{\n let _sum = 0;\n for (const value of elements) { _sum += value; }\n\n return _sum;\n}\n\nexport function unique<T>(elements: Iterable<T>): T[]\n{\n return [...new Set(elements)];\n}\n","export function hash(value: string): number\n{\n let hash = 0;\n for (let i = 0; i < value.length; i++)\n {\n const char = value.charCodeAt(i);\n hash = ((hash << 5) - hash) + char;\n hash |= 0;\n }\n\n return hash;\n}\n\nexport function random(): number;\nexport function random(max: number): number;\nexport function random(min: number, max: number): number;\nexport function random(min: number, max: number, isDecimal: boolean): number;\nexport function random(min: number, max: number, digits: number): number;\nexport function random(min: number = 1, max?: number, decimals?: boolean | number): number\n{\n if (max === undefined)\n {\n max = min;\n min = 0;\n }\n\n if (min === max)\n {\n return min;\n }\n\n let rounder: (value: number) => number;\n\n if (decimals === true)\n {\n rounder = (value) => value;\n }\n else if (decimals === undefined)\n {\n if (Math.abs(max - min) <= 1)\n {\n rounder = (value) => value;\n }\n else\n {\n rounder = Math.floor;\n }\n }\n else if (decimals === false)\n {\n rounder = Math.floor;\n }\n else\n {\n const digits = 10 ** decimals;\n\n rounder = (value) => Math.floor(value * digits) / digits;\n }\n\n return rounder(Math.random() * (max - min) + min);\n}\n","export function capitalize(value: string): string\n{\n return `${value.charAt(0).toUpperCase()}${value.slice(1)}`;\n}\n","export const VERSION = \"1.1.1\";\n\nexport { DeferredPromise, Exception, JsonStorage, Subscribers } from \"./models/index.js\";\nexport {\n capitalize,\n count,\n delay,\n dateDifference,\n dateRange,\n dateRound,\n DateUnit,\n hash,\n loadScript,\n nextAnimationFrame,\n random,\n range,\n sum,\n unique\n\n} from \"./utils/index.js\";\n\nexport type {\n Constructor,\n FulfilledHandler,\n MaybePromise,\n PromiseExecutor,\n PromiseRejecter,\n PromiseResolver,\n RejectedHandler\n\n} from \"./types.js\";\n"],"names":["DeferredPromise","executor","__publicField","resolve","reject","value","reason","onFulfilled","onRejected","onFinally","Exception","error","exc","message","cause","name","JsonStorage","preferPersistence","storage","propertyName","defaultValue","propertyValue","newValue","encodedValue","persistent","Subscribers","subscriber","index","args","delay","milliseconds","nextAnimationFrame","DateUnit","dateDifference","start","end","unit","dateRange","offset","endTime","unixTime","dateRound","date","loadScript","scriptUrl","scriptType","script","count","elements","_count","_","range","step","sum","_sum","unique","hash","i","char","random","min","max","decimals","rounder","digits","capitalize","VERSION"],"mappings":"oYAUA,MAAqBA,CACrB,CAMW,YAAYC,EACnB,CANUC,EAAA,iBACAA,EAAA,gBAEAA,EAAA,iBAIN,KAAK,SAAW,IAAI,QAAQ,CAACC,EAASC,IACtC,CACI,KAAK,SAAWD,EAChB,KAAK,QAAUC,EAEfH,GAAA,MAAAA,EAAWE,EAASC,EAAM,CAC7B,CACL,CAEO,QAAQC,EACf,CACI,KAAK,SAASA,CAAK,CACvB,CACO,OAAOC,EACd,CACI,KAAK,QAAQA,CAAM,CACvB,CAEO,KAAuBC,EAAsCC,EAEpE,CACI,OAAO,KAAK,SAAS,KAAKD,EAAaC,CAAU,CACrD,CACO,MAAiBA,EACxB,CACW,OAAA,KAAK,SAAS,MAAMA,CAAU,CACzC,CACO,QAAQC,EACf,CACW,OAAA,KAAK,SAAS,QAAQA,CAAS,CAC1C,CACJ,CClDA,MAAqBC,UAAkB,KACvC,CACI,OAAc,YAAYC,EAC1B,CACI,GAAIA,aAAiBD,EAEV,OAAAC,EAEX,GAAIA,aAA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AQ,KAAK,OAAA,GAAYF,EAAMD,GAAOA,CAAG,CACpD,CC5DO,SAASK,EAAW5D,EAC3B,CACW,MAAA,GAAGA,EAAM,OAAO,CAAC,EAAE,aAAa,GAAGA,EAAM,MAAM,CAAC,CAAC,EAC5D,CCHO,MAAM6D,EAAU"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@byloth/core",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "An unopinionated collection of useful functions and classes that I use widely in all my projects. 🔧",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Core",
|
|
@@ -61,6 +61,6 @@
|
|
|
61
61
|
"@typescript-eslint/parser": "^6.7.4",
|
|
62
62
|
"eslint": "^8.50.0",
|
|
63
63
|
"typescript": "^5.2.2",
|
|
64
|
-
"vite": "^4.4.
|
|
64
|
+
"vite": "^4.4.10"
|
|
65
65
|
}
|
|
66
66
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
export const VERSION = "1.1.
|
|
1
|
+
export const VERSION = "1.1.1";
|
|
2
2
|
|
|
3
3
|
export { DeferredPromise, Exception, JsonStorage, Subscribers } from "./models/index.js";
|
|
4
4
|
export {
|
|
5
5
|
capitalize,
|
|
6
6
|
count,
|
|
7
7
|
delay,
|
|
8
|
+
dateDifference,
|
|
9
|
+
dateRange,
|
|
10
|
+
dateRound,
|
|
11
|
+
DateUnit,
|
|
8
12
|
hash,
|
|
9
13
|
loadScript,
|
|
10
14
|
nextAnimationFrame,
|
|
15
|
+
random,
|
|
11
16
|
range,
|
|
12
17
|
sum,
|
|
13
18
|
unique
|
|
@@ -1,68 +1,51 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
PromiseExecutor,
|
|
3
|
+
PromiseResolver,
|
|
4
|
+
PromiseRejecter,
|
|
5
|
+
FulfilledHandler,
|
|
6
|
+
RejectedHandler,
|
|
7
|
+
MaybePromise
|
|
2
8
|
|
|
3
|
-
|
|
9
|
+
} from "../types.js";
|
|
10
|
+
|
|
11
|
+
export default class DeferredPromise<T>
|
|
4
12
|
{
|
|
5
|
-
protected _resolve
|
|
6
|
-
protected _reject
|
|
13
|
+
protected _resolve!: PromiseResolver<T>;
|
|
14
|
+
protected _reject!: PromiseRejecter;
|
|
7
15
|
|
|
8
|
-
|
|
9
|
-
{
|
|
10
|
-
return this._resolve;
|
|
11
|
-
}
|
|
12
|
-
public get reject(): PromiseRejecter<E>
|
|
13
|
-
{
|
|
14
|
-
return this._reject;
|
|
15
|
-
}
|
|
16
|
+
protected _promise: Promise<T>;
|
|
16
17
|
|
|
17
18
|
public constructor(executor?: PromiseExecutor<T>)
|
|
18
19
|
{
|
|
19
|
-
|
|
20
|
-
let _reject: PromiseRejecter<E>;
|
|
21
|
-
|
|
22
|
-
let _executor: PromiseExecutor<T>;
|
|
23
|
-
|
|
24
|
-
if (executor)
|
|
25
|
-
{
|
|
26
|
-
_executor = (resolve, reject) =>
|
|
27
|
-
{
|
|
28
|
-
_resolve = resolve;
|
|
29
|
-
_reject = reject;
|
|
30
|
-
|
|
31
|
-
executor(resolve, reject);
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
else
|
|
20
|
+
this._promise = new Promise((resolve, reject) =>
|
|
35
21
|
{
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
_resolve = resolve;
|
|
39
|
-
_reject = reject;
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
super(_executor);
|
|
22
|
+
this._resolve = resolve;
|
|
23
|
+
this._reject = reject;
|
|
44
24
|
|
|
45
|
-
|
|
46
|
-
|
|
25
|
+
executor?.(resolve, reject);
|
|
26
|
+
});
|
|
47
27
|
}
|
|
48
28
|
|
|
49
|
-
public
|
|
50
|
-
: DeferredPromise<F, R>
|
|
29
|
+
public resolve(value: MaybePromise<T>)
|
|
51
30
|
{
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
return (this as unknown) as DeferredPromise<F, R>;
|
|
31
|
+
this._resolve(value);
|
|
55
32
|
}
|
|
56
|
-
public
|
|
33
|
+
public reject(reason: unknown)
|
|
57
34
|
{
|
|
58
|
-
|
|
35
|
+
this._reject(reason);
|
|
36
|
+
}
|
|
59
37
|
|
|
60
|
-
|
|
38
|
+
public then<F = T, R = never>(onFulfilled?: FulfilledHandler<T, F>, onRejected?: RejectedHandler<unknown, R>)
|
|
39
|
+
: Promise<F | R>
|
|
40
|
+
{
|
|
41
|
+
return this._promise.then(onFulfilled, onRejected);
|
|
61
42
|
}
|
|
62
|
-
public
|
|
43
|
+
public catch<R = never>(onRejected?: RejectedHandler<unknown, R>): Promise<T | R>
|
|
63
44
|
{
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
45
|
+
return this._promise.catch(onRejected);
|
|
46
|
+
}
|
|
47
|
+
public finally(onFinally?: () => void): Promise<T>
|
|
48
|
+
{
|
|
49
|
+
return this._promise.finally(onFinally);
|
|
67
50
|
}
|
|
68
51
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export type Constructor<T extends object, P extends Array<unknown> = []> = new (...args: P) => T;
|
|
2
2
|
export type MaybePromise<T> = T | PromiseLike<T>;
|
|
3
3
|
|
|
4
|
-
export type FulfilledHandler<T, R = T> = (value: T) => MaybePromise<R>;
|
|
5
|
-
export type RejectedHandler<E, R =
|
|
4
|
+
export type FulfilledHandler<T = void, R = T> = (value: T) => MaybePromise<R>;
|
|
5
|
+
export type RejectedHandler<E = unknown, R = never> = (reason: E) => MaybePromise<R>;
|
|
6
6
|
|
|
7
7
|
export type PromiseResolver<T = void> = (result: MaybePromise<T>) => void;
|
|
8
|
-
export type PromiseRejecter<E = unknown> = (reason: E) => void;
|
|
8
|
+
export type PromiseRejecter<E = unknown> = (reason: MaybePromise<E>) => void;
|
|
9
9
|
export type PromiseExecutor<T = void, E = unknown> = (resolve: PromiseResolver<T>, reject: PromiseRejecter<E>) => void;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export enum DateUnit
|
|
2
|
+
{
|
|
3
|
+
Second = 1000,
|
|
4
|
+
Minute = 60 * Second,
|
|
5
|
+
Hour = 60 * Minute,
|
|
6
|
+
Day = 24 * Hour,
|
|
7
|
+
Week = 7 * Day,
|
|
8
|
+
Month = 30 * Day,
|
|
9
|
+
Year = 365 * Day
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function dateDifference(start: Date, end: Date, unit = DateUnit.Day)
|
|
13
|
+
{
|
|
14
|
+
return Math.floor((end.getTime() - start.getTime()) / unit);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function* dateRange(start: Date, end: Date, offset = DateUnit.Day)
|
|
18
|
+
{
|
|
19
|
+
const endTime = end.getTime();
|
|
20
|
+
|
|
21
|
+
let unixTime: number = start.getTime();
|
|
22
|
+
while (unixTime < endTime)
|
|
23
|
+
{
|
|
24
|
+
yield new Date(unixTime);
|
|
25
|
+
|
|
26
|
+
unixTime += offset;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function dateRound(date: Date, unit = DateUnit.Day)
|
|
31
|
+
{
|
|
32
|
+
return new Date(Math.floor(date.getTime() / unit) * unit);
|
|
33
|
+
}
|
package/src/utils/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { delay, nextAnimationFrame } from "./async.js";
|
|
2
|
+
export { dateDifference, dateRange, dateRound, DateUnit } from "./date.js";
|
|
2
3
|
export { loadScript } from "./dom.js";
|
|
3
4
|
export { count, range, sum, unique } from "./iterator.js";
|
|
4
|
-
export { hash } from "./math.js";
|
|
5
|
+
export { hash, random } from "./math.js";
|
|
5
6
|
export { capitalize } from "./string.js";
|
package/src/utils/iterator.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export function count<T>(elements: Iterable<T>): number
|
|
2
2
|
{
|
|
3
|
-
if (
|
|
3
|
+
if (Array.isArray(elements)) { return elements.length; }
|
|
4
4
|
|
|
5
5
|
let _count = 0;
|
|
6
6
|
for (const _ of elements) { _count += 1; }
|
|
@@ -8,12 +8,20 @@ export function count<T>(elements: Iterable<T>): number
|
|
|
8
8
|
return _count;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
export function
|
|
11
|
+
export function range(end: number): Generator<number, void>;
|
|
12
|
+
export function range(start: number, end: number): Generator<number, void>;
|
|
13
|
+
export function range(start: number, end: number, step: number): Generator<number, void>;
|
|
14
|
+
export function* range(start: number, end?: number, step = 1): Generator<number, void>
|
|
12
15
|
{
|
|
16
|
+
if (end === undefined)
|
|
17
|
+
{
|
|
18
|
+
end = start;
|
|
19
|
+
start = 0;
|
|
20
|
+
}
|
|
21
|
+
|
|
13
22
|
if (start > end) { step = step ?? -1; }
|
|
14
|
-
else { step = step ?? 1; }
|
|
15
23
|
|
|
16
|
-
for (let
|
|
24
|
+
for (let index = start; index < end; index += step) { yield index; }
|
|
17
25
|
}
|
|
18
26
|
|
|
19
27
|
export function sum<T extends number>(elements: Iterable<T>): number
|
package/src/utils/math.ts
CHANGED
|
@@ -10,3 +10,52 @@ export function hash(value: string): number
|
|
|
10
10
|
|
|
11
11
|
return hash;
|
|
12
12
|
}
|
|
13
|
+
|
|
14
|
+
export function random(): number;
|
|
15
|
+
export function random(max: number): number;
|
|
16
|
+
export function random(min: number, max: number): number;
|
|
17
|
+
export function random(min: number, max: number, isDecimal: boolean): number;
|
|
18
|
+
export function random(min: number, max: number, digits: number): number;
|
|
19
|
+
export function random(min: number = 1, max?: number, decimals?: boolean | number): number
|
|
20
|
+
{
|
|
21
|
+
if (max === undefined)
|
|
22
|
+
{
|
|
23
|
+
max = min;
|
|
24
|
+
min = 0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (min === max)
|
|
28
|
+
{
|
|
29
|
+
return min;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let rounder: (value: number) => number;
|
|
33
|
+
|
|
34
|
+
if (decimals === true)
|
|
35
|
+
{
|
|
36
|
+
rounder = (value) => value;
|
|
37
|
+
}
|
|
38
|
+
else if (decimals === undefined)
|
|
39
|
+
{
|
|
40
|
+
if (Math.abs(max - min) <= 1)
|
|
41
|
+
{
|
|
42
|
+
rounder = (value) => value;
|
|
43
|
+
}
|
|
44
|
+
else
|
|
45
|
+
{
|
|
46
|
+
rounder = Math.floor;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else if (decimals === false)
|
|
50
|
+
{
|
|
51
|
+
rounder = Math.floor;
|
|
52
|
+
}
|
|
53
|
+
else
|
|
54
|
+
{
|
|
55
|
+
const digits = 10 ** decimals;
|
|
56
|
+
|
|
57
|
+
rounder = (value) => Math.floor(value * digits) / digits;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return rounder(Math.random() * (max - min) + min);
|
|
61
|
+
}
|