Skip to content

Commit 63a942e

Browse files
authored
feat: removeItem when storing null or undefined (#93)
1 parent e568328 commit 63a942e

File tree

6 files changed

+32
-35
lines changed

6 files changed

+32
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ It was time to do a full review and refactoring, which results in:
2828
- `indexedDB` database and object store names default values are exported and can be changed
2929
(see the [interoperability guide](./docs/INTEROPERABILITY.md))
3030
- `indexedDB` storage will now works in web workers too
31+
- When trying to store `null` or `undefined`, `removeItem()` instead of just bypassing (meaning the old value was kept)
3132

3233
### Breaking changes
3334

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ this.localStorage.setItem('user', user).subscribe(() => {});
8787

8888
You can store any value, without worrying about stringifying.
8989

90+
Storing `null` or `undefined` can cause issues in some browsers, so the item will be removed instead.
91+
9092
### Deleting data
9193

9294
To delete one item:

projects/ngx-pwa/local-storage/src/lib/databases/indexeddb-database.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,9 @@ export class IndexedDBDatabase implements LocalDatabase {
142142
*/
143143
setItem(key: string, data: any): Observable<boolean> {
144144

145-
/* Storing `null` or `undefined` is known to cause issues in some browsers.
146-
* So it's useless, not storing anything in this case */
145+
/* Storing `undefined` or `null` in `localStorage` can cause issues in some browsers so removing item instead */
147146
if ((data === undefined) || (data === null)) {
148-
149-
/* Trigger success */
150-
return of(true);
151-
147+
return this.removeItem(key);
152148
}
153149

154150
/* Open a transaction in write mode */

projects/ngx-pwa/local-storage/src/lib/databases/localstorage-database.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,25 @@ export class LocalStorageDatabase implements LocalDatabase {
7777
*/
7878
setItem(key: string, data: any): Observable<boolean> {
7979

80-
/* Storing `undefined` or `null` in `localStorage` can cause issues in some browsers and has no sense */
81-
if ((data !== undefined) && (data !== null)) {
82-
83-
let serializedData: string | null = null;
80+
/* Storing `undefined` or `null` in `localStorage` can cause issues in some browsers so removing item instead */
81+
if ((data === undefined) || (data === null)) {
82+
return this.removeItem(key);
83+
}
8484

85-
/* Try to stringify (can fail on circular references) */
86-
try {
87-
serializedData = JSON.stringify(data);
88-
} catch (error) {
89-
return throwError(error as TypeError);
90-
}
85+
let serializedData: string | null = null;
9186

92-
/* Can fail if storage quota is exceeded */
93-
try {
94-
localStorage.setItem(this.prefixKey(key), serializedData);
95-
} catch (error) {
96-
return throwError(error as DOMException);
97-
}
87+
/* Try to stringify (can fail on circular references) */
88+
try {
89+
serializedData = JSON.stringify(data);
90+
} catch (error) {
91+
return throwError(error as TypeError);
92+
}
9893

94+
/* Can fail if storage quota is exceeded */
95+
try {
96+
localStorage.setItem(this.prefixKey(key), serializedData);
97+
} catch (error) {
98+
return throwError(error as DOMException);
9999
}
100100

101101
/* Wrap in a RxJS `Observable` to be consistent with other storages */

projects/ngx-pwa/local-storage/src/lib/databases/memory-database.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ export class MemoryDatabase implements LocalDatabase {
4646
*/
4747
setItem(key: string, data: any): Observable<boolean> {
4848

49-
/* Storing `undefined` or `null` in `localStorage` is useless */
50-
if ((data !== undefined) && (data !== null)) {
51-
52-
this.memoryStorage.set(key, data);
53-
49+
/* Storing `undefined` or `null` in `localStorage` is useless, so removing item instead */
50+
if ((data === undefined) || (data === null)) {
51+
return this.removeItem(key);
5452
}
5553

54+
this.memoryStorage.set(key, data);
55+
5656
/* Wrap in a RxJS `Observable` to be consistent with other storages */
5757
return of(true);
5858

projects/ngx-pwa/local-storage/src/lib/lib.service.spec.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,9 @@ function tests(description: string, localStorageServiceFactory: () => LocalStora
165165

166166
it('null', (done) => {
167167

168-
const value = null;
169-
170-
localStorageService.setItem(key, value).pipe(
171-
mergeMap(() => localStorageService.getItem(key))
168+
localStorageService.setItem(key, 'test').pipe(
169+
mergeMap(() => localStorageService.setItem(key, null)),
170+
mergeMap(() => localStorageService.getItem(key)),
172171
).subscribe((result) => {
173172

174173
expect(result).toBeNull();
@@ -181,10 +180,9 @@ function tests(description: string, localStorageServiceFactory: () => LocalStora
181180

182181
it('undefined', (done) => {
183182

184-
const value = undefined;
185-
186-
localStorageService.setItem(key, value).pipe(
187-
mergeMap(() => localStorageService.getItem(key))
183+
localStorageService.setItem(key, 'test').pipe(
184+
mergeMap(() => localStorageService.setItem(key, undefined)),
185+
mergeMap(() => localStorageService.getItem(key)),
188186
).subscribe((result) => {
189187

190188
expect(result).toBeNull();

0 commit comments

Comments
 (0)