diff --git a/src/index.ts b/src/index.ts index a746b3c..316dd09 100644 --- a/src/index.ts +++ b/src/index.ts @@ -292,7 +292,7 @@ export function stringifySetCookie( throw new TypeError(`argument name is invalid: ${cookie.name}`); } - const value = cookie.value ? enc(cookie.value) : ""; + const value = cookie.value == null ? "" : enc(cookie.value); if (!cookieValueRegExp.test(value)) { throw new TypeError(`argument val is invalid: ${cookie.value}`); diff --git a/src/stringify-set-cookie.spec.ts b/src/stringify-set-cookie.spec.ts index 69c56c7..f4e9cd4 100644 --- a/src/stringify-set-cookie.spec.ts +++ b/src/stringify-set-cookie.spec.ts @@ -126,6 +126,15 @@ describe("cookie.stringifySetCookie", () => { ).toEqual("foo=YmFy"); }); + it("should apply the encoder to an empty value", function () { + expect( + stringifySetCookie( + { name: "foo", value: "" }, + { encode: (v) => "[" + v + "]" }, + ), + ).toEqual("foo=[]"); + }); + it.each(["foo=bar", 'foo"bar', "foo,bar", "foo\\bar", "foo$bar"])( "should serialize value: %s", (value) => {