Skip to content

Commit e0d3dfd

Browse files
neykovsindresorhus
authored andcommitted
Fix .extract() for query strings containing ? (#109)
1 parent f283341 commit e0d3dfd

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,11 @@ function keysSorter(input) {
114114
}
115115

116116
exports.extract = function (str) {
117-
return str.split('?')[1] || '';
117+
var queryStart = str.indexOf('?');
118+
if (queryStart === -1) {
119+
return '';
120+
}
121+
return str.slice(queryStart + 1);
118122
};
119123

120124
exports.parse = function (str, opts) {

test/extract.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import fn from '../';
44
test('should extract query string from url', t => {
55
t.is(fn.extract('http://foo.bar/?abc=def&hij=klm'), 'abc=def&hij=klm');
66
t.is(fn.extract('http://foo.bar/?'), '');
7+
t.is(fn.extract('http://foo.bar/?regex=ab?c'), 'regex=ab?c');
78
});
89

910
test('should handle strings not containing query string', t => {

0 commit comments

Comments
 (0)