Skip to content

Ignore ending ampersand when parsing #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 9, 2021
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
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ function parse(query, options) {
}

for (const param of query.split('&')) {
if (param === '') {
continue;
}

let [key, value] = splitOnFirst(options.decode ? param.replace(/\+/g, ' ') : param, '=');

// Missing `=` should be `null`:
Expand Down
5 changes: 5 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ test('query strings starting with a `&`', t => {
t.deepEqual(queryString.parse('&foo=bar&foo=baz'), {foo: ['bar', 'baz']});
});

test('query strings ending with a `&`', t => {
t.deepEqual(queryString.parse('foo=bar&'), {foo: 'bar'});
Copy link
Owner

@sindresorhus sindresorhus Feb 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also do an assertion for foo=bar&&&?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, good idea. It's added

t.deepEqual(queryString.parse('foo=bar&&&'), {foo: 'bar'});
});

test('parse a query string', t => {
t.deepEqual(queryString.parse('foo=bar'), {foo: 'bar'});
});
Expand Down