From 3bed080da3f957b8af9730779ff9dd037dc3710c Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Fri, 11 Oct 2024 19:07:12 +0200 Subject: [PATCH] Allow case insensitive options (#194) --- src/index.ts | 12 ++++++++++-- src/serialize.spec.ts | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 5a8e0e8..af222fc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -308,7 +308,11 @@ export function serialize( } if (options.priority) { - switch (options.priority) { + const priority = + typeof options.priority === "string" + ? options.priority.toLowerCase() + : options.sameSite; + switch (priority) { case "low": str += "; Priority=Low"; break; @@ -324,7 +328,11 @@ export function serialize( } if (options.sameSite) { - switch (options.sameSite) { + const sameSite = + typeof options.sameSite === "string" + ? options.sameSite.toLowerCase() + : options.sameSite; + switch (sameSite) { case true: case "strict": str += "; SameSite=Strict"; diff --git a/src/serialize.spec.ts b/src/serialize.spec.ts index e9a6ba8..7091027 100644 --- a/src/serialize.spec.ts +++ b/src/serialize.spec.ts @@ -282,6 +282,13 @@ describe("cookie.serialize(name, value, options)", function () { "foo=bar; Priority=High", ); }); + + it("should set priority case insensitive", function () { + /** @ts-expect-error */ + expect(cookie.serialize("foo", "bar", { priority: "High" })).toEqual( + "foo=bar; Priority=High", + ); + }); }); describe('with "sameSite" option', function () { @@ -320,6 +327,13 @@ describe("cookie.serialize(name, value, options)", function () { "foo=bar", ); }); + + it("should set sameSite case insensitive", function () { + /** @ts-expect-error */ + expect(cookie.serialize("foo", "bar", { sameSite: "Lax" })).toEqual( + "foo=bar; SameSite=Lax", + ); + }); }); describe('with "secure" option', function () {