Skip to content

Commit

Permalink
Merge pull request #11 from smithki/development
Browse files Browse the repository at this point in the history
Release v0.11.1
  • Loading branch information
smithki authored May 15, 2019
2 parents 52bdd7c + 9756519 commit 5acbfaa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
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.0",
"version": "0.11.1",
"description": "Use web storage (localStorage/sessionStorage) just like plain objects using ES6 Proxies.",
"author": "Ian K Smith <[email protected]>",
"license": "MIT",
Expand Down
4 changes: 3 additions & 1 deletion src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ function createProxy<TStorageDefinitions extends any>(

if (defaults) {
for (const [key, value] of Object.entries(defaults)) {
storageProxy[key] = value;
if (!data[key]) {
storageProxy[key] = value;
}
}
}

Expand Down
12 changes: 11 additions & 1 deletion test/src/storage-proxy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface TestStorage {
defaults: {
example: string;
};
alreadySetDefault: number;
}

function getItem(storageTarget: StorageTarget, path: string) {
Expand Down Expand Up @@ -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<TestStorage>(testNamespace, {
defaults: { example: testStr },
alreadySetDefault: 123,
});
this.sStore = StorageProxy.createSessionStorage<TestStorage>(testNamespace);
}
Expand All @@ -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;
Expand Down

0 comments on commit 5acbfaa

Please sign in to comment.