From d47c27688dd92bd1e4d46e9bca1ba5dddd4f45a7 Mon Sep 17 00:00:00 2001 From: Orlando Groppo Date: Thu, 5 Sep 2024 19:11:58 +0100 Subject: [PATCH] Add setCookie helper (#20) * Add setCookie helper * bump ver --- CHANGELOG.md | 6 ++++++ package.json | 2 +- src/formatters/formatCookies.test.ts | 15 ++++++++++++++ src/formatters/formatCookies.ts | 15 ++++++++++---- src/helpers/setCookie.ts | 30 ++++++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 src/formatters/formatCookies.test.ts create mode 100644 src/helpers/setCookie.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a0afd3..ffd7e99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # deverything +## 1.4.0 + +### Minor Changes + +- setCookie util + ## 1.3.0 ### Minor Changes diff --git a/package.json b/package.json index 8749e4b..81438aa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deverything", - "version": "1.3.0", + "version": "1.4.0", "description": "Everything you need for Dev", "main": "./dist/index.js", "module": "./dist/index.mjs", diff --git a/src/formatters/formatCookies.test.ts b/src/formatters/formatCookies.test.ts new file mode 100644 index 0000000..8d9e993 --- /dev/null +++ b/src/formatters/formatCookies.test.ts @@ -0,0 +1,15 @@ +import { describe, expect, test } from "@jest/globals"; +import { formatCookies } from "./formatCookies"; + +describe("formatCookies", () => { + test("should return the same number if test is under a thousand", () => { + expect( + formatCookies({ + num: 123, + bool: true, + string: "string", + undef: undefined, + }) + ).toBe(`num=123; bool=true; string=string;`); + }); +}); diff --git a/src/formatters/formatCookies.ts b/src/formatters/formatCookies.ts index 30f065b..76ad810 100644 --- a/src/formatters/formatCookies.ts +++ b/src/formatters/formatCookies.ts @@ -3,10 +3,17 @@ import { PlainObject } from "../types/Object"; /** * * @example formatCookies({}) => "" - * @example formatCookies({ session: "123", _ga: 123 }) => "session=123; _ga=123" + * @example formatCookies({ session: "123", _ga: 123 }) => "session=123; _ga=123;" */ export const formatCookies = (object: PlainObject): string => { - return Object.entries(object) - .map(([key, value]) => `${key}=${value}`) - .join("; "); + return ( + Object.entries(object) + .reduce((acc: string[], [key, value]) => { + // allow nulls to be set? + if (value !== undefined) acc.push(`${key}=${value}`); + + return acc; + }, []) + .join("; ") + ";" + ); }; diff --git a/src/helpers/setCookie.ts b/src/helpers/setCookie.ts new file mode 100644 index 0000000..41eb3cc --- /dev/null +++ b/src/helpers/setCookie.ts @@ -0,0 +1,30 @@ +import { formatCookies } from "../formatters"; + +/** + * Set a client cookie by name, works only in the browser + * @param name - required + * @param value - required, any value tha can be serialized + * @param domain - optional, defailts to current domain + * @param maxAgeSeconds - optional, defaults to "Session" + */ +export const setCookie = ({ + domain, + maxAgeSeconds, + name, + value, +}: { + domain?: string; + maxAgeSeconds?: number; + name: string; + value: any; +}): void => { + if (typeof document === "undefined") { + return; + } + + document.cookie = formatCookies({ + [name]: value, + domain, + "max-age": maxAgeSeconds, + }); +};