Skip to content

Commit

Permalink
Merge pull request #14 from smithki/development
Browse files Browse the repository at this point in the history
Release v0.11.2
  • Loading branch information
smithki authored Aug 15, 2019
2 parents 5acbfaa + 5dd6ab0 commit 7fbac68
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,30 @@ myLocalStorage.bar.spam = 'This works too!';
const copied = { ...myLocalStorage };
```

In TypeScript, you can pass an interface as a [generic type parameter](https://www.typescriptlang.org/docs/handbook/generics.html) to the factory function:
Additionally, you can pass default values. This is handy if your stored data contains deep objects that need to be accessible even when the contained data is `undefined`:

```ts
const myLocalStorage = StorageProxy.createLocalStorage('my-namespace', {
one: {
two: 'three',
four: {},
},
});

console.log(myLocalStorage.one.two.three) // => "three"
myLocalStorage.one.two.three.four.five = 'six'; // Works!
```

In TypeScript, you can define the shape of your stored data by passing a [generic type parameter](https://www.typescriptlang.org/docs/handbook/generics.html) to the factory function:

```ts
const myStorage = StorageProxy.createLocalStorage<{
hello: string;
foo: number[];
bar: { baz: string, spam?: string };
}>('my-namespace');

myStorage.foo // Works!
myStorage.bar.baz // Works!
myStorage.yolo // Compiler error!
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "storage-proxy",
"version": "0.11.1",
"version": "0.11.2",
"description": "Use web storage (localStorage/sessionStorage) just like plain objects using ES6 Proxies.",
"author": "Ian K Smith <[email protected]>",
"license": "MIT",
Expand Down
23 changes: 18 additions & 5 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ export type StorageProxy<TStorageDefinitions> = Partial<TStorageDefinitions> & {

// --- Utilities ------------------------------------------------------------ //

/**
* Checks if the passed value is undefined.
*
* @param value - The value to check.
*
* @return Returns true if value is undefined, else false.
*/
export function isUndefined(value: any): value is undefined {
return value === undefined;
}

/**
* Initializes the web storage interface. If no storage exists, we save an empty
* object.
Expand Down Expand Up @@ -73,21 +84,21 @@ function createProxy<TStorageDefinitions extends any>(
[isStorageProxy]: true,
[storageTargetSymbol]: storageTarget,
};
const proxyData = onChange(data, (path, value, prevValue) => {
const proxyData = onChange(data, (_path, value, prevValue) => {
if (value === prevValue) return;
window[storageTarget].setItem(namespace, JSON.stringify(proxyData));
});

const storageProxy = new Proxy(proxyData, {
get: (target, prop, receiver) => {
get: (_target, prop, _receiver) => {
if (typeof proxyData[prop as any] === 'undefined') return null;
return proxyData[prop as any];
},
});

if (defaults) {
for (const [key, value] of Object.entries(defaults)) {
if (!data[key]) {
if (isUndefined(data[key])) {
storageProxy[key] = value;
}
}
Expand All @@ -106,7 +117,8 @@ export const StorageProxy = {
/**
* Creates a `localStorage` proxy object that can be used like a plain JS object.
*
* @param namespace - An optional namespace to prefix `localStorage` keys with.
* @param namespace - A namespace to prefix `localStorage` keys with.
* @param defaults - Optional default values for this `StorageProxy` object.
*
* @return a `StorageProxy` object targeting `localStorage`.
*/
Expand All @@ -120,7 +132,8 @@ export const StorageProxy = {
/**
* Creates a `sessionStorage` proxy object that can be used like a plain JS object.
*
* @param namespace - An optional namespace to prefix `sessionStorage` keys with.
* @param namespace - A namespace to prefix `sessionStorage` keys with.
* @param defaults - Optional default values for this `StorageProxy` object.
*
* @return a `StorageProxy` object targeting `sessionStorage`.
*/
Expand Down

0 comments on commit 7fbac68

Please sign in to comment.