@funnelenvy-npm/fe-dev-utils
Version:
Helper function to build client side A/B tests
52 lines (51 loc) • 1.98 kB
JavaScript
;
/**
* @desc set() sets a cookie with optional days
* @param {Object} options - Options object to configure the function.
* @param {String} options.name - the name of the cookie
* @param {String} options.value - the value of the cookie
* @param {Number} options.optDays - days the cookie will exist for
* NOTE: Not passing optDays will create a "Session Cookie"
* @param {String} options.domain - the domain value of the cookie
* Example: ".domain.com" would span all sub domains of domain.com
* @return {Undefined}
*
* @desc get() gets value of cookie
* @param {Object} options - Options object to configure the function.
* @param {String} options.name - name of cookie to get
* @return {String|Null} - string value of cookie NOT A BOOL!
*
* @desc del() removes cookie
* @param {Object} options - Options object to configure the function.
* @param {String} options.name - name of cookie to delete
* @return {Undefined}
*/
Object.defineProperty(exports, "__esModule", { value: true });
const cookie = {
set({ name, value = '', optDays, domain }) {
let cookieStr = `${name}=${value}`;
if (optDays) {
const date = new Date();
date.setTime(date.getTime() + optDays * 24 * 60 * 60 * 1000);
cookieStr += `; expires=${date.toUTCString()}`;
}
if (domain) {
cookieStr += `; domain=${domain}`;
}
document.cookie = `${cookieStr}; path=/`;
},
get({ name }) {
const nameEQ = `${name}=`;
const cookiesArray = document.cookie.split(';');
for (let i = 0; i < cookiesArray.length; i++) {
const c = cookiesArray[i].trim();
if (c.indexOf(nameEQ) === 0)
return c.substring(nameEQ.length, c.length);
}
return null;
},
del({ name }) {
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/`;
},
};
exports.default = cookie;