UNPKG

@sebgroup/frontend-tools

Version:
110 lines (106 loc) 3.94 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); /** * CookieStorage is a handler for reading and writing cookies in Javascript * Usage is similar to `localStorage` and `sessionStorage` */ class CookieStorage { get length() { return this.keys().length; } /** * Retrieve an item from the stored cookie * @param key The key of the cookie to be retrieved * @returns The value of the cookie, `null` if it doesn't exist */ getItem(key) { if (!key) { return null; } return (decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(key).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null); } /** * Sets an item to the stored cookie * @param key The key of the cookie to be set * @param value The value of the cookie * @param options Available options are `expires`, `maxAge` and `secure` @see SetItemOptions interface for reference * @returns `true` if successful */ setItem(key, value, options = {}) { if (!key || /^(?:expires|max\-age|path|domain|secure)$/i.test(key)) { return false; } let expires = ""; let maxAge = ""; if (options) { if (options.expires && options.expires instanceof Date) { expires = "; Expires=" + options.expires.toUTCString(); } if (options.maxAge && typeof options.maxAge === "number" && options.maxAge !== Infinity) { maxAge = `; Max-age=${options.maxAge}`; } } document.cookie = `${encodeURIComponent(key)}=${encodeURIComponent(value)}${expires}${maxAge}`; return this.hasItem(key); } /** * Remove an item from the stored cookie * @param {string} key The key of the cookie to be removed * @returns {boolean} `true` if successful */ removeItem(key) { if (this.hasItem(key)) { document.cookie = `${encodeURIComponent(key)}=; max-age=-1`; } return !this.hasItem(key); } /** * Verifies if an item exists in the cookie * @param {string} key The key of the item to be verified * @returns {boolean} `true` if it exists */ hasItem(key) { if (!key || /^(?:expires|max\-age|path|domain|secure)$/i.test(key)) { return false; } return new RegExp("(?:^|;\\s*)" + encodeURIComponent(key).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=").test(document.cookie); } clear() { const cookies = document.cookie.split(";"); for (let i = 0; i < cookies.length; i++) { const cookie = cookies[i]; const eqPos = cookie.indexOf("="); const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie; const value = eqPos > -1 ? cookie.substr(eqPos + 1, cookie.length) : ""; document.cookie = `${name}${value ? "=" : ""}; max-age=-1`; } } /** * Retrives the list of keys in the stored cookie * @returns The list of keys in the stored cookie */ keys() { const aKeys = document.cookie .replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "") .split(/\s*(?:\=[^;]*)?;\s*/); for (let nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); } return aKeys; } /** * @param index The index of the array of keys * @returns The key at the given index, if any */ key(index) { return this.keys()[index]; } } exports.CookieStorage = CookieStorage; //# sourceMappingURL=CookieStorage.js.map