-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustomCookie.js
68 lines (58 loc) · 1.67 KB
/
customCookie.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
function useCustomCookie() {
const store = new Map();
Object.defineProperty(document, "myCookie", {
get() {
const cookies = [];
const time = Date.now();
// get all the entries from the store
for (const [name, { value, expires }] of store) {
// if the entry is expired
// remove it from the store
if (expires <= time) {
store.delete(name);
}
// else push the key-value pair in the cookies array
else {
cookies.push(`${name}=${value}`);
}
}
// return all the key-value pairs as a string
return cookies.join("; ");
},
set(val) {
// get the key value of the data
// and option from the string
const { key, value, options } = parseCookieString(val);
// if option has max-age
// set the expiry date
let expires = Infinity;
if (options["max-age"]) {
expires = Date.now() + Number(options["max-age"]) * 1000;
}
// add the entry in the store
store.set(key, { value, expires });
},
});
function parseCookieString(str) {
// split the string on ;
// separate the data and the options
const [nameValue, ...rest] = str.split(";");
// get the key value separated from the data
const [key, value] = separateKeyValue(nameValue);
// parse all the options and store it
// like max-age
const options = {};
for (const option of rest) {
const [key, value] = separateKeyValue(option);
options[key] = value;
}
return {
key,
value,
options,
};
}
function separateKeyValue(str) {
return str.split("=").map((s) => s.trim());
}
}