Compare commits
8 Commits
0.25.26
...
2352dd25db
Author | SHA1 | Date | |
---|---|---|---|
2352dd25db | |||
80307b363b | |||
59bce9d28d | |||
387e6b2512 | |||
d748d740ea | |||
c8f44584a2 | |||
8b84d170a6 | |||
37df7e6090 |
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.25.26",
|
"version": "0.26.1",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
14
src/cache.ts
14
src/cache.ts
@ -39,7 +39,7 @@ export class Cache<K extends string | number | symbol, T> {
|
|||||||
if(typeof this.options.persistentStorage == 'string')
|
if(typeof this.options.persistentStorage == 'string')
|
||||||
this.options.persistentStorage = {storage: localStorage, key: this.options.persistentStorage};
|
this.options.persistentStorage = {storage: localStorage, key: this.options.persistentStorage};
|
||||||
|
|
||||||
if(this.options.persistentStorage?.storage instanceof Database) {
|
if(this.options.persistentStorage?.storage?.constructor.name == 'Database') {
|
||||||
(async () => {
|
(async () => {
|
||||||
const persists: any = this.options.persistentStorage;
|
const persists: any = this.options.persistentStorage;
|
||||||
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});
|
||||||
@ -47,8 +47,8 @@ export class Cache<K extends string | number | symbol, T> {
|
|||||||
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();
|
done();
|
||||||
})();
|
})();
|
||||||
} else {
|
} else if(this.options.persistentStorage?.storage?.constructor.name == 'Storage') {
|
||||||
const stored = 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();
|
done();
|
||||||
}
|
}
|
||||||
@ -75,10 +75,10 @@ export class Cache<K extends string | number | symbol, T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private save(key?: K) {
|
private save(key?: K) {
|
||||||
const persists: any = this.options.persistentStorage;
|
const persists: {storage: any, key: string} = <any>this.options.persistentStorage;
|
||||||
if(!!persists?.storage) {
|
if(!!persists?.storage) {
|
||||||
if(persists.storage instanceof Database) {
|
if(persists.storage?.constructor.name == 'Database') {
|
||||||
(<Database>persists.storage).createTable({name: persists.storage.key, key: <string>this.key}).then(table => {
|
(<Database>persists.storage).createTable({name: persists.key, key: <string>this.key}).then(table => {
|
||||||
if(key) {
|
if(key) {
|
||||||
table.set(key, this.get(key));
|
table.set(key, this.get(key));
|
||||||
} else {
|
} else {
|
||||||
@ -86,7 +86,7 @@ export class Cache<K extends string | number | symbol, T> {
|
|||||||
this.all().forEach(row => table.add(row));
|
this.all().forEach(row => table.add(row));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else if(persists.storage?.constructor.name == 'Storage') {
|
||||||
persists.storage.setItem(persists.storage.key, JSONSanitize(this.all(true)));
|
persists.storage.setItem(persists.storage.key, JSONSanitize(this.all(true)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,7 +64,6 @@ export function fromCsv<T = any>(csv: string, hasHeaders = true): T[] {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert an array of objects to a CSV string
|
* Convert an array of objects to a CSV string
|
||||||
*
|
*
|
||||||
|
@ -10,6 +10,7 @@ describe('Cache', () => {
|
|||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
storageMock = {
|
storageMock = {
|
||||||
|
constructor: {name: 'Storage' as any},
|
||||||
getItem: jest.fn(),
|
getItem: jest.fn(),
|
||||||
setItem: jest.fn(),
|
setItem: jest.fn(),
|
||||||
removeItem: jest.fn(),
|
removeItem: jest.fn(),
|
||||||
|
Reference in New Issue
Block a user