import { isBrowserEnv } from "./isBrowserEnv"; /** * Returns a cookie with the specified name, * or undefined if nothing is found * * @param name - The name of the cookie whose value you want to get * @param cookiesString - You can specify a custom cookies string (for example, if you are not in a browser) */ export const getCookie = (name: string, cookiesString?: string): string | undefined => { let cookies = cookiesString; if (isBrowserEnv() && !cookies) { cookies = document.cookie; } if (!cookies) return undefined; const matches = cookies.match( new RegExp(`(?:^|; )${name.replace(/([.$?*|{}()[\]\\/+^])/g, `\\$1`)}=([^;]*)`), ); return matches ? decodeURIComponent(matches[1]) : undefined; };