Skip to content

Commit 2f43074

Browse files
authored
Merge pull request velopert#453 from velopert/fix/storage-string
fix: do not stringify if it is already string
2 parents ea5509f + 37ceae3 commit 2f43074

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/lib/storage.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class FallbackStorage {
1616
valid: boolean = checkLocalStorage();
1717

1818
setItem(key: string, value: any) {
19-
const string = JSON.stringify(value);
19+
const string = typeof value === 'string' ? value : JSON.stringify(value);
2020
if (this.valid) {
2121
localStorage.setItem(key, string);
2222
return;
@@ -28,11 +28,12 @@ class FallbackStorage {
2828
let value = this.valid
2929
? localStorage.getItem(key)
3030
: this.fallbackStorage[key];
31+
if (!value) return null;
3132
try {
3233
const parsed = JSON.parse(value || '');
3334
return parsed;
3435
} catch (e) {
35-
return null;
36+
return value || null;
3637
}
3738
}
3839

0 commit comments

Comments
 (0)