All files / src/utils cookies.js

100% Statements 28/28
100% Branches 10/10
100% Functions 3/3
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 317x 21x 21x 20x 18x 8x 7x 7x 7x   17x     7x 248x 248x 248x 365x 365x 365x   13x     7x 13x 13x 13x      
const set = (name, value, days) => {
    let expires = "";
    if (!name) { return new Error("Cookie name not specified"); }
    if (!value) { return new Error("Cookie value not specified"); }
    if (days) {
        if (typeof days !== "number") { return new Error("Cookie expiration must be a number of days"); }
        let date = new Date();
        date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
        expires = "; expires=" + date.toGMTString();
    }
    document.cookie = name + "=" + value + expires + "; path=/";
};
 
const get = (name) => {
    let nameEQ = name + "=",
        ca = document.cookie.split(';');
    for (let i = 0; i < ca.length; i++) {
        let 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;
};
 
const del = (name) => {
    let date = new Date();
    date.setTime(date.getTime() - 1 * 24 * 60 * 60 * 1000);
    document.cookie = name + "=; expires=" + date.toGMTString() + "; path=/";
};
 
export { set, get, del };