-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession-storage.js
70 lines (63 loc) · 1.64 KB
/
session-storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* globals DOMException */
export const storageAvailable = (type) => {
let storage = window[type]
try {
let x = '__storage_test__'
storage.setItem(x, x)
storage.removeItem(x)
return true
} catch (e) {
return e instanceof DOMException && (
// everything except Firefox
e.code === 22
// Firefox
|| e.code === 1014
// test name field too, because code might not be present
// everything except Firefox
|| e.name === 'QuotaExceededError'
// Firefox
|| e.name === 'NS_ERROR_DOM_QUOTA_REACHED')
// acknowledge QuotaExceededError only if there's something already stored
&& storage.length !== 0
}
}
export const storeOnSession = (key, item) => {
if (storageAvailable('sessionStorage')) {
window.sessionStorage.setItem(key, item)
return true
}
return false
}
export const getOnSession = (key, default_) => {
if (storageAvailable('sessionStorage')) {
return window.sessionStorage.getItem(key)
}
return default_
}
export const removeFromSession = (key) => {
if (storageAvailable('sessionStorage')) {
window.sessionStorage.removeItem(key)
return true
}
return false
}
export const storeOnLocal = (key, item) => {
if (storageAvailable('localStorage')) {
window.localStorage.setItem(key, item)
return true
}
return false
}
export const getOnLocal = (key, default_) => {
if (storageAvailable('localStorage')) {
return window.localStorage.getItem(key)
}
return default_
}
export const removeFromLocal = (key) => {
if (storageAvailable('localStorage')) {
window.localStorage.removeItem(key)
return true
}
return false
}