diff --git a/CHANGELOG.md b/CHANGELOG.md index 3876bc9..b7f1be5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # deverything +## 0.47.0 + +### Minor Changes + +- get cookie + ## 0.46.1 ### Patch Changes diff --git a/package.json b/package.json index 8368f49..00bcba8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deverything", - "version": "0.46.1", + "version": "0.47.0", "description": "Everything you need for Dev", "main": "./dist/index.js", "module": "./dist/index.mjs", diff --git a/src/helpers/getCookieByName.ts b/src/helpers/getCookieByName.ts new file mode 100644 index 0000000..73289c2 --- /dev/null +++ b/src/helpers/getCookieByName.ts @@ -0,0 +1,21 @@ +/** + * Get a client cookie by name, works only in the browser + * @param name + * @returns the cookie value, if exists + */ +export const getCookieByName = (name: string): string | undefined => { + if (typeof document === "undefined") { + return undefined; + } + + const cookies = document.cookie.split(";"); + + for (const cookie of cookies) { + const [cookieName, cookieValue] = cookie.trim().split("="); + if (cookieName === name) { + return decodeURIComponent(cookieValue); + } + } + + return undefined; +}; diff --git a/src/helpers/index.ts b/src/helpers/index.ts index ef59f8c..534c96f 100644 --- a/src/helpers/index.ts +++ b/src/helpers/index.ts @@ -14,6 +14,7 @@ export * from "./enumValues"; export * from "./first"; export * from "./firstKey"; export * from "./firstValue"; +export * from "./getCookieByName"; export * from "./getKeys"; export * from "./getUrlSearchParam"; export * from "./getUrlSearchParams";