@bizjournals/js-storage 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/abstract/index.js +15 -16
- package/lib/main.js +5 -0
- package/package.json +23 -23
- package/main.js +0 -5
package/lib/abstract/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { DEFAULT_EXPIRATION, DEFAULT_NAMESPACE, EXPIRES_KEY_SUFFIX } from
|
|
2
|
-
import { KEY_IS_REQUIRED_TO_FETCH_DATA } from
|
|
1
|
+
import { DEFAULT_EXPIRATION, DEFAULT_NAMESPACE, EXPIRES_KEY_SUFFIX } from './constants';
|
|
2
|
+
import { KEY_IS_REQUIRED_TO_FETCH_DATA } from './exceptions';
|
|
3
3
|
import store from './store';
|
|
4
4
|
|
|
5
5
|
/*
|
|
@@ -40,8 +40,8 @@ export default class StorageAbstract {
|
|
|
40
40
|
getItem (key, parse = true) {
|
|
41
41
|
let response;
|
|
42
42
|
|
|
43
|
-
if (!this.isSupported() || this.isItemExpired(key)) {
|
|
44
|
-
return
|
|
43
|
+
if (! this.isSupported() || this.isItemExpired(key)) {
|
|
44
|
+
return undefined;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
try {
|
|
@@ -65,7 +65,7 @@ export default class StorageAbstract {
|
|
|
65
65
|
*/
|
|
66
66
|
parser (data) {
|
|
67
67
|
try {
|
|
68
|
-
if (typeof data ===
|
|
68
|
+
if (typeof data === 'string') {
|
|
69
69
|
data = JSON.parse(data);
|
|
70
70
|
}
|
|
71
71
|
} catch (e) {
|
|
@@ -93,7 +93,7 @@ export default class StorageAbstract {
|
|
|
93
93
|
* @param {boolean} expires
|
|
94
94
|
*/
|
|
95
95
|
setItem (key, value, expires = true) {
|
|
96
|
-
if (!this.isSupported()) {
|
|
96
|
+
if (! this.isSupported()) {
|
|
97
97
|
return;
|
|
98
98
|
}
|
|
99
99
|
|
|
@@ -104,8 +104,8 @@ export default class StorageAbstract {
|
|
|
104
104
|
|
|
105
105
|
if (expires) {
|
|
106
106
|
this.storage.setItem(
|
|
107
|
-
|
|
108
|
-
|
|
107
|
+
this.expirationKey(key),
|
|
108
|
+
String(this.timestamp(this.expires)),
|
|
109
109
|
);
|
|
110
110
|
}
|
|
111
111
|
} catch (e) {
|
|
@@ -119,7 +119,7 @@ export default class StorageAbstract {
|
|
|
119
119
|
* @param {string} key
|
|
120
120
|
*/
|
|
121
121
|
removeItem (key) {
|
|
122
|
-
if (!this.isSupported()) {
|
|
122
|
+
if (! this.isSupported()) {
|
|
123
123
|
return;
|
|
124
124
|
}
|
|
125
125
|
|
|
@@ -169,7 +169,7 @@ export default class StorageAbstract {
|
|
|
169
169
|
* @returns {string}
|
|
170
170
|
*/
|
|
171
171
|
namespaceKey (key) {
|
|
172
|
-
if (typeof key !==
|
|
172
|
+
if (typeof key !== 'string') {
|
|
173
173
|
throw new Error(KEY_IS_REQUIRED_TO_FETCH_DATA);
|
|
174
174
|
}
|
|
175
175
|
|
|
@@ -196,14 +196,14 @@ export default class StorageAbstract {
|
|
|
196
196
|
* @returns {boolean}
|
|
197
197
|
*/
|
|
198
198
|
isItemExpired (key) {
|
|
199
|
-
if (!this.isSupported()) {
|
|
199
|
+
if (! this.isSupported()) {
|
|
200
200
|
return false;
|
|
201
201
|
}
|
|
202
202
|
|
|
203
203
|
try {
|
|
204
204
|
let value = this.storage.getItem(this.expirationKey(key));
|
|
205
205
|
|
|
206
|
-
if (value
|
|
206
|
+
if (typeof value === 'string' && value.length > 0) {
|
|
207
207
|
let expires = parseInt(value, 10);
|
|
208
208
|
|
|
209
209
|
if (expires - this.timestamp() <= 0) {
|
|
@@ -224,7 +224,7 @@ export default class StorageAbstract {
|
|
|
224
224
|
* @returns {boolean}
|
|
225
225
|
*/
|
|
226
226
|
isSupported () {
|
|
227
|
-
if (typeof store.supported ===
|
|
227
|
+
if (typeof store.supported === 'boolean') {
|
|
228
228
|
return store.supported;
|
|
229
229
|
}
|
|
230
230
|
|
|
@@ -236,8 +236,7 @@ export default class StorageAbstract {
|
|
|
236
236
|
|
|
237
237
|
store.supported = true;
|
|
238
238
|
} catch (e) {
|
|
239
|
-
store.supported = e instanceof DOMException &&
|
|
240
|
-
(
|
|
239
|
+
store.supported = e instanceof DOMException && (
|
|
241
240
|
// everything except Firefox
|
|
242
241
|
e.code === 22 ||
|
|
243
242
|
// Firefox
|
|
@@ -247,7 +246,7 @@ export default class StorageAbstract {
|
|
|
247
246
|
e.name === 'QuotaExceededError' ||
|
|
248
247
|
// Firefox
|
|
249
248
|
e.name === 'NS_ERROR_DOM_QUOTA_REACHED'
|
|
250
|
-
|
|
249
|
+
) && (this.storage && this.storage.length !== 0);
|
|
251
250
|
}
|
|
252
251
|
|
|
253
252
|
return store.supported;
|
package/lib/main.js
ADDED
package/package.json
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
2
|
+
"name": "@bizjournals/js-storage",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "ACBJ javascript storage classes",
|
|
5
|
+
"repository": {
|
|
6
|
+
"git": "https://gitlab.bizjournals.com/bizjournals/js-storage"
|
|
7
|
+
},
|
|
8
|
+
"main": "lib/main.js",
|
|
9
|
+
"type": "module",
|
|
10
|
+
"sideEffects": false,
|
|
11
|
+
"scripts": {
|
|
12
|
+
"test": "jest",
|
|
13
|
+
"preversion": "npm test",
|
|
14
|
+
"postversion": "git push && git push --tags && npm publish"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [],
|
|
17
|
+
"author": "DPD",
|
|
18
|
+
"license": "UNLICENSED",
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@babel/preset-env": "^7.10.3",
|
|
21
|
+
"babel-jest": "^25.4.0",
|
|
22
|
+
"jest": "^25.4.0"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {}
|
|
25
25
|
}
|