Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
8c1fb2d8cb | |||
91dc17667e |
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.25.13",
|
"version": "0.25.15",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -34,7 +34,12 @@ export class Cache<K extends string | number | symbol, T> {
|
|||||||
if(options.storageKey && !options.storage && typeof(Storage) !== 'undefined') options.storage = localStorage;
|
if(options.storageKey && !options.storage && typeof(Storage) !== 'undefined') options.storage = localStorage;
|
||||||
if(options.storage) {
|
if(options.storage) {
|
||||||
if(options.storage instanceof Table) {
|
if(options.storage instanceof Table) {
|
||||||
(async () => (await options.storage?.getAll()).forEach((v: any) => this.add(v)))()
|
(async () => (await options.storage?.getAll()).forEach((v: any) => {
|
||||||
|
if(v) {
|
||||||
|
try { this.add(v) }
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
|
}))()
|
||||||
} else if(options.storageKey) {
|
} else if(options.storageKey) {
|
||||||
const stored = options.storage?.getItem(options.storageKey);
|
const stored = options.storage?.getItem(options.storageKey);
|
||||||
if(stored != null) try { Object.assign(this.store, JSON.parse(stored)); } catch { }
|
if(stored != null) try { Object.assign(this.store, JSON.parse(stored)); } catch { }
|
||||||
@ -55,6 +60,7 @@ export class Cache<K extends string | number | symbol, T> {
|
|||||||
|
|
||||||
private getKey(value: T): K {
|
private getKey(value: T): K {
|
||||||
if(!this.key) throw new Error('No key defined');
|
if(!this.key) throw new Error('No key defined');
|
||||||
|
if(value[this.key] === undefined) throw new Error(`${this.key.toString()} Doesn't exist on ${JSON.stringify(value, null, 2)}`);
|
||||||
return <K>value[this.key];
|
return <K>value[this.key];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,7 +77,7 @@ export function fileText(file: any): Promise<string | null> {
|
|||||||
*/
|
*/
|
||||||
export function timestampFilename(name?: string, date: Date | number | string = new Date()) {
|
export function timestampFilename(name?: string, date: Date | number | string = new Date()) {
|
||||||
if(typeof date == 'number' || typeof date == 'string') date = new Date(date);
|
if(typeof date == 'number' || typeof date == 'string') date = new Date(date);
|
||||||
const timestamp = formatDate('YYYY-MM-DD_HH:mm:ss', date);
|
const timestamp = formatDate('YYYY-MM-DD_HH-mm', date);
|
||||||
return name ? name.replace('{{TIMESTAMP}}', timestamp) : timestamp;
|
return name ? name.replace('{{TIMESTAMP}}', timestamp) : timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user