Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Simplify code
  • Loading branch information
blakeembrey committed Oct 2, 2024
commit 96c30a8a133357f17129cef314719a5a329bfe00
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ function parse(str, options) {
if (endIdx === -1) {
endIdx = str.length;
} else if (eqIdx > endIdx) {
// backtrack on prior semicolon
index = str.lastIndexOf(';', eqIdx - 1) + 1;
continue;
}
Expand Down Expand Up @@ -150,9 +151,9 @@ function startIndex(str, index, max) {
}

function endIndex(str, index, min) {
do {
if (str.charCodeAt(index - 1) !== 0x20 /* */) break;
} while (--index >= min);
while (index > min) {
if (str.charCodeAt(--index) !== 0x20 /* */) return index + 1;
}
return index;
}

Expand Down
8 changes: 6 additions & 2 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('cookie.parse(str)', function () {
})

it('should parse cookie with empty value', function () {
assert.deepEqual(cookie.parse('foo= ; bar='), { foo: '', bar: '' })
assert.deepEqual(cookie.parse('foo=; bar='), { foo: '', bar: '' })
})

it('should URL-decode values', function () {
Expand All @@ -36,12 +36,16 @@ describe('cookie.parse(str)', function () {

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

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

it('should return original value on escape error', function () {
Expand Down