cookies.js 1.09 KB
Newer Older
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
const Cookies = {
    set: (key, value, expiration) => {
        key = [encodeURIComponent(key) + "=" + encodeURIComponent(value)]
        expiration && ("expiry" in expiration && ("number" === typeof expiration.expiry && (expiration.expiry = new Date(1E3 * expiration.expiry + +new Date)),
                key.push("expires=" + expiration.expiry.toGMTString())),
                "domain" in expiration && key.push("domain=" + expiration.domain),
                "path" in expiration && key.push("path=" + expiration.path),
                "secure" in expiration && expiration.secure && key.push("secure"))
        document.cookie = key.join("; ")
    },
    get: (key, bool) => {
        const arr = []
        for (let cookies = document.cookie.split(/; */), i = 0; i < cookies.length; i++) {
            var cookie = cookies[i].split("=")
            cookie[0] === encodeURIComponent(key) && arr.push(decodeURIComponent(cookie[1].replace(/\+/g, "%20")))
        }

        return bool ? arr : arr[0]
    },
    clear: (b, c) => {
        c || (c = {});
        c.expiry = -86400;
        Cookies.set(b, "", c)
    }
};