@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/cache.d.ts
CHANGED
|
@@ -24,6 +24,7 @@ export declare class Cache<K extends string | number | symbol, T> {
|
|
|
24
24
|
[key: string | number | symbol]: CachedValue<T> | any;
|
|
25
25
|
/** Whether cache is complete */
|
|
26
26
|
complete: boolean;
|
|
27
|
+
private _loading;
|
|
27
28
|
/** Await initial loading */
|
|
28
29
|
loading: Promise<void>;
|
|
29
30
|
/**
|
package/dist/index.cjs
CHANGED
|
@@ -427,8 +427,9 @@ ${opts.message || this.desc}`;
|
|
|
427
427
|
__publicField(this, "store", {});
|
|
428
428
|
/** Whether cache is complete */
|
|
429
429
|
__publicField(this, "complete", false);
|
|
430
|
+
__publicField(this, "_loading");
|
|
430
431
|
/** Await initial loading */
|
|
431
|
-
__publicField(this, "loading");
|
|
432
|
+
__publicField(this, "loading", new Promise((r) => this._loading = r));
|
|
432
433
|
/**
|
|
433
434
|
* Get all cached items
|
|
434
435
|
* @return {T[]} Array of items
|
|
@@ -437,8 +438,6 @@ ${opts.message || this.desc}`;
|
|
|
437
438
|
var _a, _b, _c, _d;
|
|
438
439
|
this.key = key;
|
|
439
440
|
this.options = options;
|
|
440
|
-
let done;
|
|
441
|
-
this.loading = new Promise((r) => done = r);
|
|
442
441
|
if (this.options.persistentStorage != null) {
|
|
443
442
|
if (typeof this.options.persistentStorage == "string")
|
|
444
443
|
this.options.persistentStorage = { storage: localStorage, key: this.options.persistentStorage };
|
|
@@ -448,7 +447,7 @@ ${opts.message || this.desc}`;
|
|
|
448
447
|
const table = await persists.storage.createTable({ name: persists.key, key: this.key });
|
|
449
448
|
const rows = await table.getAll();
|
|
450
449
|
Object.assign(this.store, rows.reduce((acc, row) => ({ ...acc, [this.getKey(row)]: row }), {}));
|
|
451
|
-
|
|
450
|
+
this._loading();
|
|
452
451
|
})();
|
|
453
452
|
} else if (((_d = (_c = this.options.persistentStorage) == null ? void 0 : _c.storage) == null ? void 0 : _d.getItem) != void 0) {
|
|
454
453
|
const stored = this.options.persistentStorage.storage.getItem(this.options.persistentStorage.key);
|
|
@@ -456,8 +455,10 @@ ${opts.message || this.desc}`;
|
|
|
456
455
|
Object.assign(this.store, JSON.parse(stored));
|
|
457
456
|
} catch {
|
|
458
457
|
}
|
|
459
|
-
|
|
458
|
+
this._loading();
|
|
460
459
|
}
|
|
460
|
+
} else {
|
|
461
|
+
this._loading();
|
|
461
462
|
}
|
|
462
463
|
return new Proxy(this, {
|
|
463
464
|
get: (target, prop) => {
|
|
@@ -642,16 +643,16 @@ ${opts.message || this.desc}`;
|
|
|
642
643
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
643
644
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + " " + sizes[i];
|
|
644
645
|
}
|
|
645
|
-
function formatMs(ms) {
|
|
646
|
+
function formatMs(ms, short = false) {
|
|
646
647
|
if (isNaN(ms) || ms < 0) return "Invalid input";
|
|
647
648
|
const seconds = ms / 1e3;
|
|
648
649
|
const minutes = seconds / 60;
|
|
649
650
|
const hours = minutes / 60;
|
|
650
651
|
const days = hours / 24;
|
|
651
|
-
if (days >= 1) return `${days.toFixed(1)} days`;
|
|
652
|
-
else if (hours >= 1) return `${hours.toFixed(1)} hours`;
|
|
653
|
-
else if (minutes >= 1) return `${minutes.toFixed(1)} minutes`;
|
|
654
|
-
else return `${seconds.toFixed(1)} seconds`;
|
|
652
|
+
if (days >= 1) return `${days.toFixed(1)} ${short ? "d" : "days"}`;
|
|
653
|
+
else if (hours >= 1) return `${hours.toFixed(1)} ${short ? "h" : "hours"}`;
|
|
654
|
+
else if (minutes >= 1) return `${minutes.toFixed(1)} ${short ? "m" : "minutes"}`;
|
|
655
|
+
else return `${seconds.toFixed(1)} ${short ? "s" : "seconds"}`;
|
|
655
656
|
}
|
|
656
657
|
function formatPhoneNumber(number) {
|
|
657
658
|
const parts = /(\+?1)?.*?(\d{3}).*?(\d{3}).*?(\d{4})/g.exec(number);
|