diff --git a/package.json b/package.json index 1c7d092..6539e48 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "storage-proxy", - "version": "0.11.0", + "version": "0.11.1", "description": "Use web storage (localStorage/sessionStorage) just like plain objects using ES6 Proxies.", "author": "Ian K Smith ", "license": "MIT", diff --git a/src/lib.ts b/src/lib.ts index 29a3643..2626008 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -87,7 +87,9 @@ function createProxy( if (defaults) { for (const [key, value] of Object.entries(defaults)) { - storageProxy[key] = value; + if (!data[key]) { + storageProxy[key] = value; + } } } diff --git a/test/src/storage-proxy.spec.ts b/test/src/storage-proxy.spec.ts index d61ecb5..30ea514 100644 --- a/test/src/storage-proxy.spec.ts +++ b/test/src/storage-proxy.spec.ts @@ -18,6 +18,7 @@ interface TestStorage { defaults: { example: string; }; + alreadySetDefault: number; } function getItem(storageTarget: StorageTarget, path: string) { @@ -45,11 +46,15 @@ export class StorageProxyTestFixture { @SetupFixture public setupFixture() { - localStorage.setItem(testNamespace, JSON.stringify({ bar: testStr, test: 999, baz: testObj })); + localStorage.setItem( + testNamespace, + JSON.stringify({ bar: testStr, test: 999, baz: testObj, alreadySetDefault: 999 }), + ); sessionStorage.setItem(testNamespace, JSON.stringify({ bar: testStr, test: 999, baz: testObj })); this.lStore = StorageProxy.createLocalStorage(testNamespace, { defaults: { example: testStr }, + alreadySetDefault: 123, }); this.sStore = StorageProxy.createSessionStorage(testNamespace); } @@ -64,6 +69,11 @@ export class StorageProxyTestFixture { Expect(this.lStore.defaults!.example).toEqual(testStr); } + @Test('Default key that is already set in storage is not overwritten') + public defaultsNotAllPowerfulTest() { + Expect(this.lStore.alreadySetDefault).toEqual(999); + } + @Test('Set a `localStorage` key') public setLocalStorageKeyTest() { this.lStore.fizz = 123;