From 96c30a8a133357f17129cef314719a5a329bfe00 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Tue, 1 Oct 2024 22:34:21 -0700 Subject: [PATCH] Simplify code --- index.js | 7 ++++--- test/parse.js | 8 ++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 558a1cf..77e9fbe 100644 --- a/index.js +++ b/index.js @@ -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; } @@ -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; } diff --git a/test/parse.js b/test/parse.js index cc69bc9..b4d64d2 100644 --- a/test/parse.js +++ b/test/parse.js @@ -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 () { @@ -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 () {