Skip to content

Commit 8f057e7

Browse files
committed
Fixed #3: Error trying to get an item from storage
1 parent dff0988 commit 8f057e7

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 2.0.1
2+
3+
### Fixes
4+
5+
1. Fixed [#3](https://github.com/jherax/proxy-storage/issues/3): Error trying to get an item from storage.
6+
17
# 2.0.0
28

39
### Breaking changes

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ $ yarn add proxy-storage
4949
`proxy-storage` can be included directly from a CDN in your page:
5050

5151
```html
52-
<!-- last version: 2.0.0 -->
53-
<script src="https://cdn.rawgit.com/jherax/proxy-storage/2.0.0/dist/proxy-storage.min.js"></script>
52+
<!-- last version: 2.0.1 -->
53+
<script src="https://cdn.rawgit.com/jherax/proxy-storage/2.0.1/dist/proxy-storage.min.js"></script>
5454
```
5555

5656
In the above case, the [library](#api) is included into the namespace `proxyStorage` as a global object.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "proxy-storage",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Storage mechanism that implements the Web Storage interface",
55
"author": "David Rivera <jherax@gmail.com>",
66
"main": "dist/proxy-storage.js",

src/web-storage.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ function executeInterceptors(command, ...args) {
4444
}, value);
4545
}
4646

47+
/**
48+
* @private
49+
*
50+
* Try to parse a value
51+
*
52+
* @param {string} value: the value to parse
53+
* @return {any}
54+
*/
55+
function tryParse(value) {
56+
let parsed;
57+
try {
58+
parsed = JSON.parse(value);
59+
} catch (e) {
60+
parsed = value;
61+
}
62+
return parsed;
63+
}
64+
4765
/**
4866
* @private
4967
*
@@ -55,12 +73,7 @@ function executeInterceptors(command, ...args) {
5573
*/
5674
function copyKeys(instance, storage) {
5775
Object.keys(storage).forEach((key) => {
58-
let value = storage[key];
59-
try {
60-
instance[key] = JSON.parse(value);
61-
} catch (e) {
62-
instance[key] = value;
63-
}
76+
instance[key] = tryParse(storage[key]);
6477
});
6578
}
6679

@@ -161,7 +174,10 @@ class WebStorage {
161174
checkEmpty(key);
162175
let value = proxy[this.__storage__].getItem(key);
163176
if (value === undefined) value = null;
164-
else value = JSON.parse(value);
177+
else {
178+
value = tryParse(value);
179+
this[key] = value;
180+
}
165181
let v = executeInterceptors('getItem', key, value);
166182
if (v !== undefined) value = v;
167183
return value;

0 commit comments

Comments
 (0)