Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
idleberg committed Jun 15, 2024
1 parent c11259e commit 8af6be8
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 69 deletions.
17 changes: 17 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"arrowParens": "always",
"bracketSameLine": true,
"bracketSpacing": true,
"experimentalTernaries": false,
"insertPragma": false,
"printWidth": 120,
"proseWrap": "preserve",
"quoteProps": "as-needed",
"requirePragma": false,
"semi": true,
"singleAttributePerLine": false,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "all",
"useTabs": true
}
5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@
"typescript": "^5.4.5",
"uvu": "^0.5.6"
},
"prettier": {
"semi": true,
"singleQuote": true,
"tabWidth": 4
},
"lint-staged": {
"*.(json|ts)": "eslint --cache --fix",
"*.md": "prettier --write"
Expand Down
46 changes: 17 additions & 29 deletions src/porridge-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,10 @@ import {
get as getItemIdb,
keys,
set as setItemIdb,
UseStore
UseStore,
} from 'idb-keyval';

import {
addCustomEventListener,
didExpire,
eventDispatcher,
getType,
storageKeys
} from './util';
import { addCustomEventListener, didExpire, eventDispatcher, getType, storageKeys } from './util';

const storageArea = 'indexedDB';

Expand All @@ -37,16 +31,16 @@ export class PorridgeDB {
customStore: UseStore;

constructor(options?: WebPorridge.IndexeddbOptions) {
if (typeof <any>window !== 'undefined' && !('indexedDB' in <any>window)) {
if (typeof (<any>window) !== 'undefined' && !('indexedDB' in <any>window)) {
throw Error(`Your browser does not support the IndexedDB API`);
}

const { db, eventName, store } = {
db: 'web-porridge:db',
eventName: 'porridgeDB.didChange',
store: '(default store)',
...options
}
...options,
};

this.eventName = eventName;
this.customStore = createStore(db, store);
Expand All @@ -71,29 +65,29 @@ export class PorridgeDB {
const oldValue = await this.getItem(keyName, options);

if (options?.prop?.length) {
const item = await this.getItem(keyName) || {};
const item = (await this.getItem(keyName)) || {};
setProperty(item, options.prop, keyValue);

return await this.setItem(keyName, item, {
...options,
prop: undefined
prop: undefined,
});
}

const newValue = {
[storageKeys.value]: keyValue,
[storageKeys.type]: getType(keyValue)
[storageKeys.type]: getType(keyValue),
};

if (options?.expires && String(options.expires).length) {
newValue[storageKeys.expires] = new Date(options.expires);
}

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

return await setItemIdb(keyName, newValue, this.customStore);
Expand Down Expand Up @@ -143,17 +137,17 @@ export class PorridgeDB {
const oldValue = await this.getItem(keyName, options);

if (options?.prop?.length) {
const item = await this.getItem(keyName) || {};
const item = (await this.getItem(keyName)) || {};
deleteProperty(item, options.prop);

return await this.setItem(keyName, item);
}

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

return await removeItemIdb(keyName, this.customStore);
Expand Down Expand Up @@ -189,7 +183,7 @@ export class PorridgeDB {
}

/**
* Clears IndexedDB
* Clears IndexedDB
*
* @example
* ```js
Expand All @@ -200,7 +194,7 @@ export class PorridgeDB {
eventDispatcher(this.eventName, {
storageArea: storageArea,
oldValue: null,
newValue: null
newValue: null,
});

return await clear(this.customStore);
Expand Down Expand Up @@ -245,10 +239,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 @@ -262,10 +253,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 Down
25 changes: 10 additions & 15 deletions src/porridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ import {
eventDispatcher,
getType,
serialize,
storageKeys
storageKeys,
} from './util';

const validStores = [
'localStorage',
'sessionStorage'
];
const validStores = ['localStorage', 'sessionStorage'];

/**
* Instantiates the class with provided options
Expand All @@ -39,7 +36,7 @@ export class Porridge {

this.eventName = eventName;

if (typeof<any>window !== 'undefined' && !(storageArea in <any>window)) {
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`);
Expand Down Expand Up @@ -73,7 +70,7 @@ export class Porridge {

return this.setItem(keyName, item, {
...options,
prop: undefined
prop: undefined,
});
}

Expand All @@ -90,7 +87,7 @@ export class Porridge {
storageArea: this.storageArea,
key: keyName,
oldValue: oldValue,
newValue: keyValue
newValue: keyValue,
});

return (<any>globalThis)[this.storageArea].setItem(keyName, JSON.stringify(newValue));
Expand All @@ -116,7 +113,7 @@ export class Porridge {
try {
const decodedItem: WebPorridge.Payload = JSON.parse(item);

if (!decodedItem || (didExpire(decodedItem[storageKeys.expires]))) {
if (!decodedItem || didExpire(decodedItem[storageKeys.expires])) {
return null;
}

Expand Down Expand Up @@ -158,7 +155,7 @@ export class Porridge {
storageArea: this.storageArea,
key: keyName,
oldValue: oldValue,
newValue: null
newValue: null,
});

return (<any>globalThis)[this.storageArea].removeItem(keyName);
Expand Down Expand Up @@ -198,7 +195,7 @@ export class Porridge {
eventDispatcher(this.eventName, {
storageArea: this.storageArea,
oldValue: null,
newValue: null
newValue: null,
});

return (<any>globalThis)[this.storageArea].clear();
Expand Down Expand Up @@ -243,8 +240,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 @@ -258,8 +254,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 Down
10 changes: 5 additions & 5 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { WebPorridge } from '../types';
export const storageKeys: WebPorridge.StorageKeys = {
value: '@value',
type: '@type',
expires: '@expires'
expires: '@expires',
};

/**
Expand Down Expand Up @@ -32,7 +32,7 @@ export function serialize(item: unknown): unknown {
export function deserialize(item): unknown {
const decodedString = item[storageKeys.value];

switch(item[storageKeys.type]) {
switch (item[storageKeys.type]) {
case 'boolean':
case 'null':
case 'number':
Expand Down Expand Up @@ -106,8 +106,8 @@ export function eventDispatcher(eventName: string, payload: WebPorridge.StorageE
try {
globalThis.dispatchEvent(
new CustomEvent(eventName, {
detail: payload
})
detail: payload,
}),
);
} catch (_error) {
// TODO: fix CustomEvent failing on NodeJS
Expand All @@ -123,7 +123,7 @@ export function addCustomEventListener(eventName: string, keyName: string, callb
if (typeof callback === 'function') {
callback({
key: keyName,
value: e.detail.value
value: e.detail.value,
});
}
});
Expand Down
12 changes: 6 additions & 6 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { defineConfig } from 'tsup';

export default defineConfig({
target: 'esnext',
clean: true,
dts: true,
entry: ['src/index.ts'],
format: 'esm',
minify: true,
clean: true,
dts: true,
entry: ['src/index.ts'],
format: 'esm',
minify: true,
outDir: 'lib',
treeshake: 'recommended'
treeshake: 'recommended',
});
16 changes: 7 additions & 9 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
declare namespace WebPorridge {
type StorageOptions ={
type StorageOptions = {
expires?: string;
prop?: string;
}
};

type IndexeddbOptions = {
db: string;
name: string;
}
};

type Payload = {
'@expires'?: string;
'@type': 'boolean' | 'date' | 'null' | 'number' | 'object' | 'string' | 'undefined';
'@value': string;
}
};

type StorageKeys = {
expires: '@expires';
value: '@value';
type: '@type';
}
};

type StorageEvent = {
key?: string;
newValue: unknown;
oldValue: unknown;
storageArea: 'localStorage' | 'sessionStorage' | 'indexedDB';
}
};
}

export {
WebPorridge
};
export { WebPorridge };

0 comments on commit 8af6be8

Please sign in to comment.