Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Oct 2, 2024
1 parent b487a18 commit 96c30a8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
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

0 comments on commit 96c30a8

Please sign in to comment.