Skip to content
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
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const strictUriEncode = require('strict-uri-encode');
const decodeComponent = require('decode-uri-component');
const splitOnFirst = require('split-on-first');

function encoderForArrayFormat(options) {
switch (options.arrayFormat) {
Expand Down Expand Up @@ -150,7 +151,7 @@ function parse(input, options) {
}

for (const param of input.split('&')) {
let [key, value] = param.replace(/\+/g, ' ').split('=');
let [key, value] = splitOnFirst(param.replace(/\+/g, ' '), '=');

// Missing `=` should be `null`:
// http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"ava": "^1.2.1",
"deep-equal": "^1.0.1",
"fast-check": "^1.5.0",
"split-on-first": "^1.0.0",
"tsd-check": "^0.3.0",
"xo": "^0.24.0"
}
Expand Down
5 changes: 5 additions & 0 deletions test/parse-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ test('handles strings not containing query string', t => {
t.deepEqual(m.parseUrl(''), {url: '', query: {}});
});

test('handles strings with query string that contain =', t => {
t.deepEqual(m.parseUrl('https://foo.bar?foo=baz=bar&foo=baz#top'), {url: 'https://foo.bar', query: {foo: ['baz=bar', 'baz']}});
t.deepEqual(m.parseUrl('https://foo.bar?foo=bar=&foo=baz='), {url: 'https://foo.bar', query: {foo: ['bar=', 'baz=']}});
});

test('throws for invalid values', t => {
t.throws(() => {
m.parseUrl(null);
Expand Down
5 changes: 5 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ test('query strings having indexed arrays and format option as `index`', t => {
}), {foo: ['bar', 'baz']});
});

test('query strings having = within parameters (i.e. GraphQL IDs)', t => {
t.deepEqual(m.parse('foo=bar=&foo=ba=z=', {
}), {foo: ['bar=', 'ba=z=']});
});

test('query strings having ordered index arrays and format option as `index`', t => {
t.deepEqual(m.parse('foo[1]=bar&foo[0]=baz&foo[3]=one&foo[2]=two', {
arrayFormat: 'index'
Expand Down