UNPKG

@analytics/cookie-utils

Version:
66 lines (51 loc) 1.68 kB
/* // simple set cookie('test', 'a') // complex set - cookie(name, value, ttl, path, domain, secure) cookie('test', 'a', 60*60*24, '/api', '*.example.com', true) // get cookie('test') // destroy cookie('test', '', -1) */ function cookie(name, value, ttl, path, domain, secure) { if (arguments.length > 1) { /* eslint-disable no-return-assign */ return document.cookie = "".concat(name, "=").concat(encodeURIComponent(value)).concat(!ttl ? '' : "; expires=".concat(new Date(+new Date() + ttl * 1000).toUTCString())).concat(!path ? '' : "; path=".concat(path)).concat(!domain ? '' : "; domain=".concat(domain)).concat(!secure ? '' : '; secure'); /* eslint-enable */ } return decodeURIComponent(("; ".concat(document.cookie).split("; ".concat(name, "="))[1] || '').split(';')[0]); } /* This module will shake out unused code and work in browser and node 🎉 */ var cookie$1 = cookie; function hasCookieSupport() { try { var key = '___c'; // Try to set cookie cookie$1(key, '1'); // Cleanup cookie cookie$1(key, '', -1); return document.cookie.indexOf(key) !== -1; } catch (e) { return false; } } /** * Get a cookie value * @param {string} name - key of cookie * @return {string} value of cookie */ var getCookie = cookie$1; /** * Set a cookie value * @param {string} name - key of cookie * @param {string} value - value of cookie * @param {string} days - days to keep cookie */ var setCookie = cookie$1; /** * Remove a cookie value * @param {string} name - key of cookie */ function removeCookie(name) { return cookie$1(name, '', -1); } export { hasCookieSupport, setCookie, getCookie, removeCookie };