@ztimson/utils 0.26.18 → 0.26.20
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/cache.d.ts +1 -0
- package/dist/index.cjs +11 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +11 -10
- package/dist/index.mjs.map +1 -1
- package/dist/string.d.ts +2 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -423,8 +423,9 @@ class Cache {
|
|
|
423
423
|
__publicField(this, "store", {});
|
|
424
424
|
/** Whether cache is complete */
|
|
425
425
|
__publicField(this, "complete", false);
|
|
426
|
+
__publicField(this, "_loading");
|
|
426
427
|
/** Await initial loading */
|
|
427
|
-
__publicField(this, "loading");
|
|
428
|
+
__publicField(this, "loading", new Promise((r) => this._loading = r));
|
|
428
429
|
/**
|
|
429
430
|
* Get all cached items
|
|
430
431
|
* @return {T[]} Array of items
|
|
@@ -433,8 +434,6 @@ class Cache {
|
|
|
433
434
|
var _a, _b, _c, _d;
|
|
434
435
|
this.key = key;
|
|
435
436
|
this.options = options;
|
|
436
|
-
let done;
|
|
437
|
-
this.loading = new Promise((r) => done = r);
|
|
438
437
|
if (this.options.persistentStorage != null) {
|
|
439
438
|
if (typeof this.options.persistentStorage == "string")
|
|
440
439
|
this.options.persistentStorage = { storage: localStorage, key: this.options.persistentStorage };
|
|
@@ -444,7 +443,7 @@ class Cache {
|
|
|
444
443
|
const table = await persists.storage.createTable({ name: persists.key, key: this.key });
|
|
445
444
|
const rows = await table.getAll();
|
|
446
445
|
Object.assign(this.store, rows.reduce((acc, row) => ({ ...acc, [this.getKey(row)]: row }), {}));
|
|
447
|
-
|
|
446
|
+
this._loading();
|
|
448
447
|
})();
|
|
449
448
|
} else if (((_d = (_c = this.options.persistentStorage) == null ? void 0 : _c.storage) == null ? void 0 : _d.getItem) != void 0) {
|
|
450
449
|
const stored = this.options.persistentStorage.storage.getItem(this.options.persistentStorage.key);
|
|
@@ -452,8 +451,10 @@ class Cache {
|
|
|
452
451
|
Object.assign(this.store, JSON.parse(stored));
|
|
453
452
|
} catch {
|
|
454
453
|
}
|
|
455
|
-
|
|
454
|
+
this._loading();
|
|
456
455
|
}
|
|
456
|
+
} else {
|
|
457
|
+
this._loading();
|
|
457
458
|
}
|
|
458
459
|
return new Proxy(this, {
|
|
459
460
|
get: (target, prop) => {
|
|
@@ -638,16 +639,16 @@ function formatBytes(bytes, decimals = 2) {
|
|
|
638
639
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
639
640
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + " " + sizes[i];
|
|
640
641
|
}
|
|
641
|
-
function formatMs(ms) {
|
|
642
|
+
function formatMs(ms, short = false) {
|
|
642
643
|
if (isNaN(ms) || ms < 0) return "Invalid input";
|
|
643
644
|
const seconds = ms / 1e3;
|
|
644
645
|
const minutes = seconds / 60;
|
|
645
646
|
const hours = minutes / 60;
|
|
646
647
|
const days = hours / 24;
|
|
647
|
-
if (days >= 1) return `${days.toFixed(1)} days`;
|
|
648
|
-
else if (hours >= 1) return `${hours.toFixed(1)} hours`;
|
|
649
|
-
else if (minutes >= 1) return `${minutes.toFixed(1)} minutes`;
|
|
650
|
-
else return `${seconds.toFixed(1)} seconds`;
|
|
648
|
+
if (days >= 1) return `${days.toFixed(1)} ${short ? "d" : "days"}`;
|
|
649
|
+
else if (hours >= 1) return `${hours.toFixed(1)} ${short ? "h" : "hours"}`;
|
|
650
|
+
else if (minutes >= 1) return `${minutes.toFixed(1)} ${short ? "m" : "minutes"}`;
|
|
651
|
+
else return `${seconds.toFixed(1)} ${short ? "s" : "seconds"}`;
|
|
651
652
|
}
|
|
652
653
|
function formatPhoneNumber(number) {
|
|
653
654
|
const parts = /(\+?1)?.*?(\d{3}).*?(\d{3}).*?(\d{4})/g.exec(number);
|