@bizjournals/js-storage 0.1.0 → 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/__tests__/storage.issupported.spec.js +2 -1
- package/lib/abstract/__tests__/storage.removeitem.spec.js +5 -3
- package/lib/abstract/__tests__/storage.setitem.spec.js +2 -1
- package/lib/abstract/index.js +19 -26
- package/lib/abstract/store/index.js +10 -0
- package/lib/main.js +5 -0
- package/package.json +23 -23
- package/main.js +0 -5
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import StorageAbstract from '../';
|
|
2
2
|
import mockStorage from '../__mocks__/mock-storage';
|
|
3
|
+
import store from '../store';
|
|
3
4
|
|
|
4
5
|
describe('core:class >> storageabstract:issupported', () => {
|
|
5
6
|
let storageAbstract;
|
|
@@ -10,7 +11,7 @@ describe('core:class >> storageabstract:issupported', () => {
|
|
|
10
11
|
mockStorage.removeItem.mockClear();
|
|
11
12
|
|
|
12
13
|
storageAbstract.storage = mockStorage;
|
|
13
|
-
|
|
14
|
+
store.supported = undefined;
|
|
14
15
|
});
|
|
15
16
|
|
|
16
17
|
it('returns false when the storage system is defaulted', () => {
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import StorageAbstract from '../';
|
|
2
2
|
|
|
3
|
+
import store from '../store';
|
|
4
|
+
|
|
3
5
|
describe('core:class >> storageabstract:removeitem', () => {
|
|
4
6
|
let storageClassMock, mockStorage, key = 'test';
|
|
5
7
|
|
|
@@ -20,7 +22,7 @@ describe('core:class >> storageabstract:removeitem', () => {
|
|
|
20
22
|
};
|
|
21
23
|
|
|
22
24
|
storageClassMock.storage = mockStorage;
|
|
23
|
-
|
|
25
|
+
store.supported = undefined;
|
|
24
26
|
});
|
|
25
27
|
|
|
26
28
|
it('halts when it is not supported', () => {
|
|
@@ -33,7 +35,7 @@ describe('core:class >> storageabstract:removeitem', () => {
|
|
|
33
35
|
});
|
|
34
36
|
|
|
35
37
|
it('falls through when encountering an exception', () => {
|
|
36
|
-
|
|
38
|
+
store.supported = true;
|
|
37
39
|
|
|
38
40
|
const badStorage = {
|
|
39
41
|
removeItem: jest.fn().mockImplementation(() => {
|
|
@@ -51,7 +53,7 @@ describe('core:class >> storageabstract:removeitem', () => {
|
|
|
51
53
|
});
|
|
52
54
|
|
|
53
55
|
it('removes both the item at namespace and expiration keys', () => {
|
|
54
|
-
|
|
56
|
+
store.supported = true;
|
|
55
57
|
storageClassMock.removeItem();
|
|
56
58
|
|
|
57
59
|
expect(storageClassMock.namespaceKey).toHaveBeenCalledTimes(1);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import StorageAbstract from '../';
|
|
2
|
+
import store from '../store';
|
|
2
3
|
|
|
3
4
|
describe('core:class >> storageabstract:setitem', () => {
|
|
4
5
|
let storageClassMock, mockStorage, key = 'test';
|
|
@@ -26,7 +27,7 @@ describe('core:class >> storageabstract:setitem', () => {
|
|
|
26
27
|
|
|
27
28
|
storageClassMock.attempts = 0;
|
|
28
29
|
storageClassMock.storage = mockStorage;
|
|
29
|
-
|
|
30
|
+
store.supported = undefined;
|
|
30
31
|
});
|
|
31
32
|
|
|
32
33
|
it('resets the attempts and returns when it is not supported without continuing', () => {
|
package/lib/abstract/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
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
|
+
import store from './store';
|
|
3
4
|
|
|
4
5
|
/*
|
|
5
6
|
|--------------------------------------------------------------------------
|
|
@@ -12,13 +13,6 @@ import { KEY_IS_REQUIRED_TO_FETCH_DATA } from "./exceptions";
|
|
|
12
13
|
|
|
|
13
14
|
*/
|
|
14
15
|
export default class StorageAbstract {
|
|
15
|
-
/**
|
|
16
|
-
* Extensions to this abstraction share an enabled state.
|
|
17
|
-
*
|
|
18
|
-
* @type {boolean}
|
|
19
|
-
*/
|
|
20
|
-
static supported;
|
|
21
|
-
|
|
22
16
|
/**
|
|
23
17
|
* Constructor takes in options for namespace and expiration.
|
|
24
18
|
* Session storage is the default storage type.
|
|
@@ -46,8 +40,8 @@ export default class StorageAbstract {
|
|
|
46
40
|
getItem (key, parse = true) {
|
|
47
41
|
let response;
|
|
48
42
|
|
|
49
|
-
if (!this.isSupported() || this.isItemExpired(key)) {
|
|
50
|
-
return
|
|
43
|
+
if (! this.isSupported() || this.isItemExpired(key)) {
|
|
44
|
+
return undefined;
|
|
51
45
|
}
|
|
52
46
|
|
|
53
47
|
try {
|
|
@@ -71,7 +65,7 @@ export default class StorageAbstract {
|
|
|
71
65
|
*/
|
|
72
66
|
parser (data) {
|
|
73
67
|
try {
|
|
74
|
-
if (typeof data ===
|
|
68
|
+
if (typeof data === 'string') {
|
|
75
69
|
data = JSON.parse(data);
|
|
76
70
|
}
|
|
77
71
|
} catch (e) {
|
|
@@ -99,7 +93,7 @@ export default class StorageAbstract {
|
|
|
99
93
|
* @param {boolean} expires
|
|
100
94
|
*/
|
|
101
95
|
setItem (key, value, expires = true) {
|
|
102
|
-
if (!this.isSupported()) {
|
|
96
|
+
if (! this.isSupported()) {
|
|
103
97
|
return;
|
|
104
98
|
}
|
|
105
99
|
|
|
@@ -110,8 +104,8 @@ export default class StorageAbstract {
|
|
|
110
104
|
|
|
111
105
|
if (expires) {
|
|
112
106
|
this.storage.setItem(
|
|
113
|
-
|
|
114
|
-
|
|
107
|
+
this.expirationKey(key),
|
|
108
|
+
String(this.timestamp(this.expires)),
|
|
115
109
|
);
|
|
116
110
|
}
|
|
117
111
|
} catch (e) {
|
|
@@ -125,7 +119,7 @@ export default class StorageAbstract {
|
|
|
125
119
|
* @param {string} key
|
|
126
120
|
*/
|
|
127
121
|
removeItem (key) {
|
|
128
|
-
if (!this.isSupported()) {
|
|
122
|
+
if (! this.isSupported()) {
|
|
129
123
|
return;
|
|
130
124
|
}
|
|
131
125
|
|
|
@@ -175,7 +169,7 @@ export default class StorageAbstract {
|
|
|
175
169
|
* @returns {string}
|
|
176
170
|
*/
|
|
177
171
|
namespaceKey (key) {
|
|
178
|
-
if (typeof key !==
|
|
172
|
+
if (typeof key !== 'string') {
|
|
179
173
|
throw new Error(KEY_IS_REQUIRED_TO_FETCH_DATA);
|
|
180
174
|
}
|
|
181
175
|
|
|
@@ -202,14 +196,14 @@ export default class StorageAbstract {
|
|
|
202
196
|
* @returns {boolean}
|
|
203
197
|
*/
|
|
204
198
|
isItemExpired (key) {
|
|
205
|
-
if (!this.isSupported()) {
|
|
199
|
+
if (! this.isSupported()) {
|
|
206
200
|
return false;
|
|
207
201
|
}
|
|
208
202
|
|
|
209
203
|
try {
|
|
210
204
|
let value = this.storage.getItem(this.expirationKey(key));
|
|
211
205
|
|
|
212
|
-
if (value
|
|
206
|
+
if (typeof value === 'string' && value.length > 0) {
|
|
213
207
|
let expires = parseInt(value, 10);
|
|
214
208
|
|
|
215
209
|
if (expires - this.timestamp() <= 0) {
|
|
@@ -230,8 +224,8 @@ export default class StorageAbstract {
|
|
|
230
224
|
* @returns {boolean}
|
|
231
225
|
*/
|
|
232
226
|
isSupported () {
|
|
233
|
-
if (typeof
|
|
234
|
-
return
|
|
227
|
+
if (typeof store.supported === 'boolean') {
|
|
228
|
+
return store.supported;
|
|
235
229
|
}
|
|
236
230
|
|
|
237
231
|
const test = 'biz.storage.test';
|
|
@@ -240,10 +234,9 @@ export default class StorageAbstract {
|
|
|
240
234
|
this.storage.setItem(test, '1');
|
|
241
235
|
this.storage.removeItem(test);
|
|
242
236
|
|
|
243
|
-
|
|
237
|
+
store.supported = true;
|
|
244
238
|
} catch (e) {
|
|
245
|
-
|
|
246
|
-
(
|
|
239
|
+
store.supported = e instanceof DOMException && (
|
|
247
240
|
// everything except Firefox
|
|
248
241
|
e.code === 22 ||
|
|
249
242
|
// Firefox
|
|
@@ -253,9 +246,9 @@ export default class StorageAbstract {
|
|
|
253
246
|
e.name === 'QuotaExceededError' ||
|
|
254
247
|
// Firefox
|
|
255
248
|
e.name === 'NS_ERROR_DOM_QUOTA_REACHED'
|
|
256
|
-
|
|
249
|
+
) && (this.storage && this.storage.length !== 0);
|
|
257
250
|
}
|
|
258
251
|
|
|
259
|
-
return
|
|
252
|
+
return store.supported;
|
|
260
253
|
}
|
|
261
254
|
}
|
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
|
}
|