Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
55b871f4c1 | |||
fb077775b6 |
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.26.18",
|
"version": "0.26.20",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
13
src/cache.ts
13
src/cache.ts
@ -22,8 +22,10 @@ export class Cache<K extends string | number | symbol, T> {
|
|||||||
[key: string | number | symbol]: CachedValue<T> | any;
|
[key: string | number | symbol]: CachedValue<T> | any;
|
||||||
/** Whether cache is complete */
|
/** Whether cache is complete */
|
||||||
complete = false;
|
complete = false;
|
||||||
|
|
||||||
|
private _loading!: Function;
|
||||||
/** Await initial loading */
|
/** Await initial loading */
|
||||||
loading!: Promise<void>;
|
loading = new Promise<void>(r => this._loading = r);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create new cache
|
* Create new cache
|
||||||
@ -31,9 +33,6 @@ export class Cache<K extends string | number | symbol, T> {
|
|||||||
* @param options
|
* @param options
|
||||||
*/
|
*/
|
||||||
constructor(public readonly key?: keyof T, public readonly options: CacheOptions = {}) {
|
constructor(public readonly key?: keyof T, public readonly options: CacheOptions = {}) {
|
||||||
let done!: Function;
|
|
||||||
this.loading = new Promise(r => done = r);
|
|
||||||
|
|
||||||
// Persistent storage
|
// Persistent storage
|
||||||
if(this.options.persistentStorage != null) {
|
if(this.options.persistentStorage != null) {
|
||||||
if(typeof this.options.persistentStorage == 'string')
|
if(typeof this.options.persistentStorage == 'string')
|
||||||
@ -45,13 +44,15 @@ export class Cache<K extends string | number | symbol, T> {
|
|||||||
const table: Table<any, any> = await persists.storage.createTable({name: persists.key, key: this.key});
|
const table: Table<any, any> = await persists.storage.createTable({name: persists.key, key: this.key});
|
||||||
const rows = await table.getAll();
|
const rows = await table.getAll();
|
||||||
Object.assign(this.store, rows.reduce((acc, row) => ({...acc, [this.getKey(row)]: row}), {}));
|
Object.assign(this.store, rows.reduce((acc, row) => ({...acc, [this.getKey(row)]: row}), {}));
|
||||||
done();
|
this._loading();
|
||||||
})();
|
})();
|
||||||
} else if((<any>this.options.persistentStorage?.storage)?.getItem != undefined) {
|
} else if((<any>this.options.persistentStorage?.storage)?.getItem != undefined) {
|
||||||
const stored = (<Storage>this.options.persistentStorage.storage).getItem(this.options.persistentStorage.key);
|
const stored = (<Storage>this.options.persistentStorage.storage).getItem(this.options.persistentStorage.key);
|
||||||
if(stored != null) try { Object.assign(this.store, JSON.parse(stored)); } catch { }
|
if(stored != null) try { Object.assign(this.store, JSON.parse(stored)); } catch { }
|
||||||
done();
|
this._loading();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
this._loading();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle index lookups
|
// Handle index lookups
|
||||||
|
@ -46,18 +46,19 @@ export function formatBytes(bytes: number, decimals = 2) {
|
|||||||
/**
|
/**
|
||||||
* Convert milliseconds to human-readable duration
|
* Convert milliseconds to human-readable duration
|
||||||
* @param {string} ms milliseconds
|
* @param {string} ms milliseconds
|
||||||
|
* @param {boolean} short Use unit initial instead of word
|
||||||
* @return {string} formated duration
|
* @return {string} formated duration
|
||||||
*/
|
*/
|
||||||
export function formatMs(ms: number): string {
|
export function formatMs(ms: number, short = false): string {
|
||||||
if (isNaN(ms) || ms < 0) return "Invalid input";
|
if (isNaN(ms) || ms < 0) return "Invalid input";
|
||||||
const seconds = ms / 1000;
|
const seconds = ms / 1000;
|
||||||
const minutes = seconds / 60;
|
const minutes = seconds / 60;
|
||||||
const hours = minutes / 60;
|
const hours = minutes / 60;
|
||||||
const days = hours / 24;
|
const days = hours / 24;
|
||||||
if (days >= 1) return `${days.toFixed(1)} days`;
|
if (days >= 1) return `${days.toFixed(1)} ${short ? 'd' : 'days'}`;
|
||||||
else if (hours >= 1) return `${hours.toFixed(1)} hours`;
|
else if (hours >= 1) return `${hours.toFixed(1)} ${short ? 'h' : 'hours'}`;
|
||||||
else if (minutes >= 1) return `${minutes.toFixed(1)} minutes`;
|
else if (minutes >= 1) return `${minutes.toFixed(1)} ${short ? 'm' : 'minutes'}`;
|
||||||
else return `${seconds.toFixed(1)} seconds`;
|
else return `${seconds.toFixed(1)} ${short ? 's' : 'seconds'}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user