-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (36 loc) · 1015 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
const Cookies = require("js-cookie");
const create = (flash = {}) => {
if (Object.values(flash).filter(obj => obj).length > 0) {
const name = uniqueName();
storeName(name);
Cookies.set(name, flash);
}
};
const uniqueName = (name = getName()) => {
return Cookies.get(name) === undefined ? name : uniqueName(getName());
};
const getName = () => {
return `jsFlash-${Date.now()}`;
};
const storeName = name => {
const storedNames = Cookies.getJSON("jsFlashNames");
const names = storedNames === undefined ? [] : storedNames;
names.push(name);
Cookies.set("jsFlashNames", names);
};
const get = () => {
const storedFlash = Cookies.getJSON("jsFlashNames");
if (storedFlash === undefined) {
return [];
} else {
const flashes = storedFlash.map(flashName => {
const flash = Cookies.getJSON(flashName);
Cookies.remove(flashName);
return flash;
});
Cookies.remove("jsFlashNames");
return flashes;
}
};
exports.create = create;
exports.get = get;