/
Chocolata.js
42 lines (36 loc) · 1.06 KB
/
Chocolata.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
import config from './config';
class Chocolata {
static setItem(name, value, daysToExpire = config.daysToExpire) {
if (typeof value === 'object') {
value = JSON.stringify(value);
}
const date = new Date();
date.setTime(date.getTime() + daysToExpire * 24 * 60 * 60 * 1000);
const expires = '; expires=' + date.toUTCString();
document.cookie = name + '=' + value + expires + '; path=/';
}
static getStringItem(name) {
var nameEQ = name + '=';
var ca = document.cookie.split(';').reverse();
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
static getObjectItem(name) {
return JSON.parse(this.getStringItem(name));
}
static getItem(name) {
try {
return this.getObjectItem(name);
} catch (e) {
return this.getStringItem(name);
}
}
static removeItem(name) {
this.setItem(name, '', -1);
}
}
export default Chocolata;