Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
15 changes: 15 additions & 0 deletions src/parse-cookie.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
Loading