Skip to content

Commit

Permalink
use private class properties
Browse files Browse the repository at this point in the history
  • Loading branch information
idleberg committed Aug 8, 2024
1 parent b286e4e commit db60298
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 41 deletions.
38 changes: 19 additions & 19 deletions src/porridge-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ const storageArea = 'indexedDB';
* ```
*/
export class PorridgeDB {
eventName: string;
customStore: UseStore;
#eventName: string;
#customStore: UseStore;

constructor(options?: WebPorridge.IndexeddbOptions) {
if (typeof (<any>window) !== 'undefined' && !('indexedDB' in <any>window)) {
Expand All @@ -42,8 +42,8 @@ export class PorridgeDB {
...options,
};

this.eventName = eventName;
this.customStore = createStore(db, store);
this.#eventName = eventName;
this.#customStore = createStore(db, store);
}
/**
* Writes single data item to IndexedDB.
Expand Down Expand Up @@ -85,14 +85,14 @@ export class PorridgeDB {
: new Date(options.expires);
}

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

return await setItemIdb(keyName, newValue, this.customStore);
return await setItemIdb(keyName, newValue, this.#customStore);
}

/**
Expand All @@ -110,7 +110,7 @@ export class PorridgeDB {
* ```
*/
public async getItem(keyName: string, options?: WebPorridge.StorageOptions): Promise<unknown> {
const item: WebPorridge.Payload = await getItemIdb(keyName, this.customStore);
const item: WebPorridge.Payload = await getItemIdb(keyName, this.#customStore);

if (!item || didExpire(item[storageKeys.expires])) {
return null;
Expand Down Expand Up @@ -145,14 +145,14 @@ export class PorridgeDB {
return await this.setItem(keyName, item);
}

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

return await removeItemIdb(keyName, this.customStore);
return await removeItemIdb(keyName, this.#customStore);
}

/**
Expand All @@ -166,7 +166,7 @@ export class PorridgeDB {
* ```
*/
public async key(index: number): Promise<unknown> {
return (await keys(this.customStore))[index];
return (await keys(this.#customStore))[index];
}

/**
Expand All @@ -180,7 +180,7 @@ export class PorridgeDB {
*/
public get length(): Promise<number> {
return (async () => {
return (await keys(this.customStore)).length;
return (await keys(this.#customStore)).length;
})();
}

Expand All @@ -193,13 +193,13 @@ export class PorridgeDB {
* ```
*/
public async clear(): Promise<void> {
eventDispatcher(this.eventName, {
eventDispatcher(this.#eventName, {
storageArea: storageArea,
oldValue: null,
newValue: null,
});

return await clear(this.customStore);
return await clear(this.#customStore);
}

/**
Expand All @@ -213,7 +213,7 @@ export class PorridgeDB {
* ```
*/
public async hasItem(keyName: string): Promise<boolean> {
return (await keys(this.customStore)).includes(keyName);
return (await keys(this.#customStore)).includes(keyName);
}

/**
Expand All @@ -227,7 +227,7 @@ export class PorridgeDB {
* ```
*/
public async keys(): Promise<string[]> {
return await keys(this.customStore);
return await keys(this.#customStore);
}

/**
Expand All @@ -241,7 +241,7 @@ export class PorridgeDB {
* ```
*/
public async values(): Promise<unknown[]> {
return Promise.all((await keys(this.customStore)).map(async (item: string) => await this.getItem(item)));
return Promise.all((await keys(this.#customStore)).map(async (item: string) => await this.getItem(item)));
}

/**
Expand All @@ -255,7 +255,7 @@ export class PorridgeDB {
* ```
*/
public async entries(): Promise<[IDBValidKey, unknown][]> {
return Promise.all((await keys(this.customStore)).map(async (item: string) => [item, await this.getItem(item)]));
return Promise.all((await keys(this.#customStore)).map(async (item: string) => [item, await this.getItem(item)]));
}

/**
Expand All @@ -275,7 +275,7 @@ export class PorridgeDB {
throw new TypeError('The callback argument is not a function');
}

addCustomEventListener(this.eventName, keyName, callback);
addCustomEventListener(this.#eventName, keyName, callback);
}

/**
Expand All @@ -289,7 +289,7 @@ export class PorridgeDB {
* ```
*/
public async didExpire(keyName: string): Promise<boolean> {
const item: WebPorridge.Payload = await getItemIdb(keyName, this.customStore);
const item: WebPorridge.Payload = await getItemIdb(keyName, this.#customStore);

return didExpire(item[storageKeys.expires]);
}
Expand Down
44 changes: 22 additions & 22 deletions src/porridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ const validStores = ['localStorage', 'sessionStorage'];
* ```
*/
export class Porridge {
eventName: string;
storageArea: 'localStorage' | 'sessionStorage';
#eventName: string;
#storageArea: 'localStorage' | 'sessionStorage';

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;
this.#eventName = eventName;

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.storageArea = storageArea;
this.#storageArea = storageArea;
}

/**
Expand Down Expand Up @@ -85,14 +85,14 @@ export class Porridge {
: new Date(options.expires);
}

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

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

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

try {
const decodedItem: WebPorridge.Payload = JSON.parse(item);
Expand Down Expand Up @@ -153,14 +153,14 @@ export class Porridge {
return this.setItem(keyName, item);
}

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

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

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

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

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

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

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

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

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

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

/**
Expand All @@ -276,7 +276,7 @@ export class Porridge {
throw new TypeError('The callback argument is not a function');
}

addCustomEventListener(this.eventName, keyName, callback);
addCustomEventListener(this.#eventName, keyName, callback);
}

/**
Expand All @@ -290,7 +290,7 @@ export class Porridge {
* ```
*/
public didExpire(keyName: string): boolean {
const item = (<any>globalThis)[this.storageArea].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 db60298

Please sign in to comment.