Skip to content

Commit

Permalink
Delegate quote parsing to decode (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey authored Oct 8, 2024
1 parent 88704d6 commit c4a2597
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 16 deletions.
8 changes: 0 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,6 @@ export function parse(
let valStartIdx = startIndex(str, eqIdx + 1, endIdx);
let valEndIdx = endIndex(str, endIdx, valStartIdx);

if (
str.charCodeAt(valStartIdx) === 0x22 /* " */ &&
str.charCodeAt(valEndIdx - 1) === 0x22 /* " */
) {
valStartIdx++;
valEndIdx--;
}

const value = dec(str.slice(valStartIdx, valEndIdx));
obj[key] = value;
}
Expand Down
11 changes: 3 additions & 8 deletions src/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,19 @@ describe("cookie.parse(str)", function () {

it("should URL-decode values", function () {
expect(cookie.parse('foo="bar=123456789&name=Magic+Mouse"')).toEqual({
foo: "bar=123456789&name=Magic+Mouse",
foo: '"bar=123456789&name=Magic+Mouse"',
});

expect(cookie.parse("email=%20%22%2c%3b%2f")).toEqual({ email: ' ",;/' });
});

it("should parse quoted values", function () {
expect(cookie.parse('foo="bar"')).toEqual({ foo: "bar" });
expect(cookie.parse('foo=" a b c "')).toEqual({ foo: " a b c " });
});

it("should trim whitespace around key and value", function () {
expect(cookie.parse(' foo = "bar" ')).toEqual({ foo: "bar" });
expect(cookie.parse(' foo = "bar" ')).toEqual({ foo: '"bar"' });
expect(cookie.parse(" foo = bar ; fizz = buzz ")).toEqual({
foo: "bar",
fizz: "buzz",
});
expect(cookie.parse(' foo = " a b c " ')).toEqual({ foo: " a b c " });
expect(cookie.parse(' foo = " a b c " ')).toEqual({ foo: '" a b c "' });
expect(cookie.parse(" = bar ")).toEqual({ "": "bar" });
expect(cookie.parse(" foo = ")).toEqual({ foo: "" });
expect(cookie.parse(" = ")).toEqual({ "": "" });
Expand Down

0 comments on commit c4a2597

Please sign in to comment.