UNPKG

@polgubau/utils

Version:

A collection of utility functions for TypeScript

1 lines 2.09 kB
{"version":3,"sources":["../../../../src/storage/cookies/cookies.ts"],"sourcesContent":["// cookieUtils.ts\n/**\n * Sets a cookie with the specified name, value, and optional expiration in days.\n * @param {string} name - The name of the cookie.\n * @param {string} value - The value to be stored in the cookie.\n * @param {number} [days] - Optional number of days until the cookie expires.\n */\nexport const setCookie = (name: string, value: string, days?: number): void => {\n const expirationDate = new Date();\n expirationDate.setDate(expirationDate.getDate() + (days ?? 0));\n\n const cookieValue = encodeURIComponent(value) + (days ? `; expires=${expirationDate.toUTCString()}` : \"\");\n document.cookie = `${name}=${cookieValue}; path=/`;\n};\n\n/**\n * Retrieves the value of a cookie by its name.\n * @param {string} name - The name of the cookie to retrieve.\n * @returns {string|null} The value of the cookie if found, or null if not found.\n */\nexport const getCookie = (name: string): string | undefined => {\n const cookies = document.cookie.split(\"; \");\n for (const cookie of cookies) {\n const [cookieName, cookieValue] = cookie.split(\"=\");\n if (cookieName === name) {\n return decodeURIComponent(cookieValue ?? \"\");\n }\n }\n return undefined;\n};\n/**\n * Deletes a cookie by setting its expiration date to the past.\n * @param {string} name - The name of the cookie to delete.\n */\nexport const deleteCookie = (name: string): void => {\n document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;\n};\n"],"mappings":";AAOO,IAAM,YAAY,CAAC,MAAc,OAAe,SAAwB;AAC7E,QAAM,iBAAiB,oBAAI,KAAK;AAChC,iBAAe,QAAQ,eAAe,QAAQ,KAAK,QAAQ,EAAE;AAE7D,QAAM,cAAc,mBAAmB,KAAK,KAAK,OAAO,aAAa,eAAe,YAAY,CAAC,KAAK;AACtG,WAAS,SAAS,GAAG,IAAI,IAAI,WAAW;AAC1C;AAOO,IAAM,YAAY,CAAC,SAAqC;AAC7D,QAAM,UAAU,SAAS,OAAO,MAAM,IAAI;AAC1C,aAAW,UAAU,SAAS;AAC5B,UAAM,CAAC,YAAY,WAAW,IAAI,OAAO,MAAM,GAAG;AAClD,QAAI,eAAe,MAAM;AACvB,aAAO,mBAAmB,eAAe,EAAE;AAAA,IAC7C;AAAA,EACF;AACA,SAAO;AACT;AAKO,IAAM,eAAe,CAAC,SAAuB;AAClD,WAAS,SAAS,GAAG,IAAI;AAC3B;","names":[]}