From 1b04bb93243e304f4430de6f91e831c6153b2ac1 Mon Sep 17 00:00:00 2001 From: Yutao Huang Date: Tue, 5 Feb 2019 10:02:06 -0800 Subject: [PATCH 1/3] In the implementation of Storage.has(key), we should check against null instead of undefined to see if a key exists in the storage. According to the W3C standard (https://www.w3.org/TR/webstorage/#dom-storage-getitem), "... If the given key does not exist in the list associated with the object then this method must return null..." --- src/helpers/storage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/storage.ts b/src/helpers/storage.ts index a677991..dafc7a9 100644 --- a/src/helpers/storage.ts +++ b/src/helpers/storage.ts @@ -150,7 +150,7 @@ export class Storage { */ has(key: string): boolean { this._validateKey(key); - return this.get(key) !== undefined; + return this.get(key) !== null; } /** From cf29e5a5c040b502c11c43748c1bb87913c84b0a Mon Sep 17 00:00:00 2001 From: Yutao Huang Date: Tue, 5 Feb 2019 18:21:53 -0800 Subject: [PATCH 2/3] Missed another check for the return value of Storage.get(key). --- src/helpers/storage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/storage.ts b/src/helpers/storage.ts index dafc7a9..107c32b 100644 --- a/src/helpers/storage.ts +++ b/src/helpers/storage.ts @@ -123,7 +123,7 @@ export class Storage { delete(key: string): T { try { let value = this.get(key); - if (value === undefined) { + if (value === null) { throw new ReferenceError(`Key: ${key} not found.`); } const scopedKey = this._scope(key); From 5ee6a17dac62e684f02cb633bf6b8a5e715a5ef9 Mon Sep 17 00:00:00 2001 From: Yutao Huang Date: Wed, 6 Feb 2019 21:35:04 -0800 Subject: [PATCH 3/3] Addressing PR comment - add a safety check for undefined. --- src/helpers/storage.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/helpers/storage.ts b/src/helpers/storage.ts index 107c32b..023d808 100644 --- a/src/helpers/storage.ts +++ b/src/helpers/storage.ts @@ -123,7 +123,7 @@ export class Storage { delete(key: string): T { try { let value = this.get(key); - if (value === null) { + if (value === null || value === undefined) { throw new ReferenceError(`Key: ${key} not found.`); } const scopedKey = this._scope(key); @@ -150,7 +150,8 @@ export class Storage { */ has(key: string): boolean { this._validateKey(key); - return this.get(key) !== null; + let value = this.get(key); + return value !== null && value !== undefined; } /**