-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (52 loc) · 976 Bytes
/
index.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
const defaultData = {
data: null,
expire: 0,
}
/**
*
* @param {string} key
* @returns {any}
*/
function get(key) {
const now = new Date()
const time = now.getTime()
const data = JSON.parse(localStorage.getItem(key) || JSON.stringify(defaultData))
if (data.expire > 0) {
if (time > data.expire) {
localStorage.removeItem(key)
return null
}
}
return data.data
}
/**
*
* @param {string} key
* @param {any} value
* @param {number} secondDuration [second]
* @returns {void}
*/
function set(key, value, secondDuration = 0) {
const now = new Date()
const time = now.getTime()
localStorage.setItem(key, JSON.stringify({
data: value,
expire: secondDuration > 0
? (time + (secondDuration * 1000))
: 0
}))
}
/**
*
* @param {string} key
* @returns {void}
*/
function remove(key) {
localStorage.removeItem(key)
}
const localStorageData = {
get,
set,
remove
}
module.exports = localStorageData