diff --git a/README.md b/README.md index 09cb444..9d735a1 100644 --- a/README.md +++ b/README.md @@ -182,7 +182,8 @@ The default `encode` function is the global `encodeURIComponent`. The default `decode` function is the global `decodeURIComponent`, wrapped in a `try..catch`. If an error is thrown it will return the cookie's original value. If you provide your own encode/decode -scheme you must ensure errors are appropriately handled. +scheme you must ensure errors are appropriately handled. Custom decode functions can return `undefined`, +which will skip the cookie during `parse` and try again with a future cookie of the same name. ## Example diff --git a/src/index.ts b/src/index.ts index a95a2e9..2e0022e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -87,7 +87,8 @@ export interface ParseOptions { * * The default function is the global `decodeURIComponent`, wrapped in a `try..catch`. If an error * is thrown it will return the cookie's original value. If you provide your own encode/decode - * scheme you must ensure errors are appropriately handled. + * scheme you must ensure errors are appropriately handled. Custom decode functions can return `undefined`, + * which will skip the cookie during `parse` and try again with a future cookie of the same name. * * @default decode */ diff --git a/src/parse-cookie.spec.ts b/src/parse-cookie.spec.ts index 2444909..dc17fad 100644 --- a/src/parse-cookie.spec.ts +++ b/src/parse-cookie.spec.ts @@ -110,5 +110,20 @@ describe("cookie.parseCookie", function () { }), ).toEqual({ foo: "bar" }); }); + + it("should skip undefined decoded duplicates", function () { + let calls = 0; + + expect( + cookie.parseCookie("foo=bar; foo=baz", { + decode: function (value) { + calls++; + return value === "bar" ? undefined : value; + }, + }), + ).toEqual({ foo: "baz" }); + + expect(calls).toBe(2); + }); }); });