From be2ff4156e41d51a90e854c51f19a3a38d0b5490 Mon Sep 17 00:00:00 2001 From: Ian K Smith Date: Tue, 14 May 2019 17:05:55 -0600 Subject: [PATCH] Add defaults feature --- src/lib.ts | 25 ++++++++++++++++++++----- test/src/storage-proxy.spec.ts | 33 ++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/src/lib.ts b/src/lib.ts index e81d086..29a3643 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -61,6 +61,7 @@ function getDataFromStorage(namespace: string, storageTarget: StorageTarget) { function createProxy( storageTarget: StorageTarget, namespace: string, + defaults?: Partial, ): StorageProxy { if (!namespace) throw new Error('[storage-proxy] Namespace cannot be an empty `string`, `undefined`, or `null`.'); @@ -77,12 +78,20 @@ function createProxy( window[storageTarget].setItem(namespace, JSON.stringify(proxyData)); }); - return new Proxy(proxyData, { + const storageProxy = new Proxy(proxyData, { 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)) { + storageProxy[key] = value; + } + } + + return storageProxy; } // --- Proxy factory -------------------------------------------------------- // @@ -99,8 +108,11 @@ export const StorageProxy = { * * @return a `StorageProxy` object targeting `localStorage`. */ - createLocalStorage(namespace: string): StorageProxy { - return createProxy(StorageTarget.Local, namespace); + createLocalStorage( + namespace: string, + defaults?: Partial, + ): StorageProxy { + return createProxy(StorageTarget.Local, namespace, defaults); }, /** @@ -110,8 +122,11 @@ export const StorageProxy = { * * @return a `StorageProxy` object targeting `sessionStorage`. */ - createSessionStorage(namespace: string): StorageProxy { - return createProxy(StorageTarget.Session, namespace); + createSessionStorage( + namespace: string, + defaults?: Partial, + ): StorageProxy { + return createProxy(StorageTarget.Session, namespace, defaults); }, /** diff --git a/test/src/storage-proxy.spec.ts b/test/src/storage-proxy.spec.ts index 754448a..d61ecb5 100644 --- a/test/src/storage-proxy.spec.ts +++ b/test/src/storage-proxy.spec.ts @@ -15,6 +15,9 @@ interface TestStorage { bar: string; baz: typeof testObj; fizz: number; + defaults: { + example: string; + }; } function getItem(storageTarget: StorageTarget, path: string) { @@ -45,32 +48,44 @@ export class StorageProxyTestFixture { localStorage.setItem(testNamespace, JSON.stringify({ bar: testStr, test: 999, baz: testObj })); sessionStorage.setItem(testNamespace, JSON.stringify({ bar: testStr, test: 999, baz: testObj })); - this.lStore = StorageProxy.createLocalStorage(testNamespace); + this.lStore = StorageProxy.createLocalStorage(testNamespace, { + defaults: { example: testStr }, + }); this.sStore = StorageProxy.createSessionStorage(testNamespace); } - @Test('Set a namespaced `localStorage` key') - public setNamespacedLocalStorageKeyTest() { + @Test('Sets default items in storage') + public defaultsMergeTest() { + Expect(getItem(StorageTarget.Local, 'defaults.example')).toEqual(testStr); + } + + @Test('Defaults are available on the `StorageProxy` object') + public defaultsAreGettableTest() { + Expect(this.lStore.defaults!.example).toEqual(testStr); + } + + @Test('Set a `localStorage` key') + public setLocalStorageKeyTest() { this.lStore.fizz = 123; Expect(getItem(StorageTarget.Local, 'fizz')).toEqual(123); } - @Test('Set a namespaced `sessionStorage` key') - public setNamespacedSessionStorageKeyTest() { + @Test('Set a `sessionStorage` key') + public setSessionStorageKeyTest() { this.sStore.fizz = 123; Expect(getItem(StorageTarget.Session, 'fizz')).toEqual(123); } - @Test('Get namespaced `localStorage` key') - public getNamespacedLocalStorageKeyTest() { + @Test('Get `localStorage` key') + public getLocalStorageKeyTest() { const data = this.lStore.bar; Expect(data).toEqual(testStr); } - @Test('Get namespaced `sessionStorage` key') - public getNamespacedSessionStorageKeyTest() { + @Test('Get `sessionStorage` key') + public getSessionStorageKeyTest() { const data = this.sStore.bar; Expect(data).toEqual(testStr); }