Skip to content

Commit

Permalink
rename properties
Browse files Browse the repository at this point in the history
  • Loading branch information
idleberg committed Jun 15, 2024
1 parent d21355d commit 0d504ab
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/porridge-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
storageKeys
} from './util';

const storageType = 'IndexedDB';
const storageArea = 'indexedDB';

/**
* Instantiates the class with provided options
Expand Down Expand Up @@ -90,7 +90,7 @@ export class PorridgeDB {
}

eventDispatcher(this.eventName, {
store: storageType,
storageArea: storageArea,
key: keyName,
oldValue: oldValue,
newValue: keyValue
Expand Down Expand Up @@ -150,7 +150,7 @@ export class PorridgeDB {
}

eventDispatcher(this.eventName, {
store: storageType,
storageArea: storageArea,
key: keyName,
oldValue: oldValue,
newValue: null
Expand Down Expand Up @@ -198,7 +198,7 @@ export class PorridgeDB {
*/
public async clear(): Promise<void> {
eventDispatcher(this.eventName, {
store: storageType,
storageArea: storageArea,
oldValue: null,
newValue: null
});
Expand Down
40 changes: 20 additions & 20 deletions src/porridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ const validStores = [
*/
export class Porridge {
eventName: string;
store: string;
storageArea: string;

constructor(store: 'localStorage' | 'sessionStorage' = 'localStorage', eventName = 'porridge.didChange') {
constructor(storageArea: 'localStorage' | 'sessionStorage' = 'localStorage', eventName = 'porridge.didChange') {
if (typeof eventName !== 'string') {
throw new TypeError('Event name must be of type "string"');
}

this.eventName = eventName;

if (typeof <any>window !== 'undefined' && !(store in <any>window)) {
throw new Error(`Your browser does not support the ${store} API`);
} else if (!validStores.includes(store)) {
if (typeof<any>window !== 'undefined' && !(storageArea in <any>window)) {
throw new Error(`Your browser does not support the ${storageArea} API`);
} else if (!validStores.includes(storageArea)) {
throw new TypeError(`Invalid storage type specified, try ${validStores.join('|')} instead`);
}

this.store = store;
this.storageArea = storageArea;
}

/**
Expand Down Expand Up @@ -87,13 +87,13 @@ export class Porridge {
}

eventDispatcher(this.eventName, {
store: this.store,
storageArea: this.storageArea,
key: keyName,
oldValue: oldValue,
newValue: keyValue
});

return (<any>globalThis)[this.store].setItem(keyName, JSON.stringify(newValue));
return (<any>globalThis)[this.storageArea].setItem(keyName, JSON.stringify(newValue));
}

/**
Expand All @@ -111,7 +111,7 @@ export class Porridge {
* ```
*/
public getItem(keyName: string, options?: WebPorridge.StorageOptions): unknown {
const item = (<any>globalThis)[this.store].getItem(keyName);
const item = (<any>globalThis)[this.storageArea].getItem(keyName);

try {
const decodedItem: WebPorridge.Payload = JSON.parse(item);
Expand Down Expand Up @@ -155,13 +155,13 @@ export class Porridge {
}

eventDispatcher(this.eventName, {
store: this.store,
storageArea: this.storageArea,
key: keyName,
oldValue: oldValue,
newValue: null
});

return (<any>globalThis)[this.store].removeItem(keyName);
return (<any>globalThis)[this.storageArea].removeItem(keyName);
}

/**
Expand All @@ -170,7 +170,7 @@ export class Porridge {
* @returns {unknown}
*/
public key(index: number): unknown {
return (<any>globalThis)[this.store].key(index);
return (<any>globalThis)[this.storageArea].key(index);
}

/**
Expand All @@ -183,7 +183,7 @@ export class Porridge {
* ```
*/
public get length(): number {
return (<any>globalThis)[this.store].length;
return (<any>globalThis)[this.storageArea].length;
}

/**
Expand All @@ -196,12 +196,12 @@ export class Porridge {
*/
public clear(): void {
eventDispatcher(this.eventName, {
store: this.store,
storageArea: this.storageArea,
oldValue: null,
newValue: null
});

return (<any>globalThis)[this.store].clear();
return (<any>globalThis)[this.storageArea].clear();
}

/**
Expand All @@ -215,7 +215,7 @@ export class Porridge {
* ```
*/
public hasItem(keyName: string): boolean {
return Object.keys(globalThis[this.store]).includes(keyName);
return Object.keys(globalThis[this.storageArea]).includes(keyName);
}

/**
Expand All @@ -229,7 +229,7 @@ export class Porridge {
* ```
*/
public keys(): string[] {
return Object.keys(globalThis[this.store]);
return Object.keys(globalThis[this.storageArea]);
}

/**
Expand All @@ -243,7 +243,7 @@ export class Porridge {
* ```
*/
public values(): unknown[] {
return Object.keys(globalThis[this.store])
return Object.keys(globalThis[this.storageArea])
.map(item => this.getItem(item));
}

Expand All @@ -258,7 +258,7 @@ export class Porridge {
* ```
*/
public entries(): [string, unknown][] {
return Object.keys(globalThis[this.store])
return Object.keys(globalThis[this.storageArea])
.map((item: string) => [item, this.getItem(item)]);
}

Expand Down Expand Up @@ -294,7 +294,7 @@ export class Porridge {
* ```
*/
public didExpire(keyName: string): boolean {
const item = (<any>globalThis)[this.store].getItem(keyName);
const item = (<any>globalThis)[this.storageArea].getItem(keyName);
const decodedItem: WebPorridge.Payload = JSON.parse(item);

return didExpire(decodedItem[storageKeys.expires]);
Expand Down

0 comments on commit 0d504ab

Please sign in to comment.