@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
32 lines (30 loc) • 1 kB
JavaScript
//#region src/utils/getCookie.ts
/**
* Retrieves a cookie by name from a cookie string or document.cookie
* @param name - The name of the cookie to retrieve
* @param cookieString - Optional cookie string to parse (defaults to document.cookie in browser)
* @returns The cookie value or undefined if not found
*/
const getCookie = (name, cookieString) => {
try {
const str = cookieString ?? (typeof document !== "undefined" ? document.cookie : "");
if (!str) return void 0;
const pairs = str.split(";");
for (let i = 0; i < pairs.length; i++) {
const part = pairs[i].trim();
if (!part) continue;
const equalIndex = part.indexOf("=");
if ((equalIndex >= 0 ? part.substring(0, equalIndex) : part) === name) {
const rawValue = equalIndex >= 0 ? part.substring(equalIndex + 1) : "";
try {
return decodeURIComponent(rawValue);
} catch {
return rawValue;
}
}
}
} catch {}
};
//#endregion
exports.getCookie = getCookie;
//# sourceMappingURL=getCookie.cjs.map