Skip to content

Commit

Permalink
Support hash in parseUrl() (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
tranvansang authored and sindresorhus committed Oct 2, 2018
1 parent ff8eef3 commit 5b4ce87
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ exports.stringify = (obj, options) => {
};

exports.parseUrl = (input, options) => {
const hashStart = input.indexOf('#');
if (hashStart !== -1) {
input = input.slice(0, hashStart);
}
return {
url: input.split('?')[0] || '',
query: parse(extract(input), options)
Expand Down
4 changes: 3 additions & 1 deletion test/parse-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import test from 'ava';
import m from '..';

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

test('handles strings not containing query string', t => {
t.deepEqual(m.parseUrl('https://foo.bar/'), {url: 'https://foo.bar/', query: {}});
t.deepEqual(m.parseUrl('https://foo.bar/#top'), {url: 'https://foo.bar/', query: {}});
t.deepEqual(m.parseUrl(''), {url: '', query: {}});
});

Expand Down

0 comments on commit 5b4ce87

Please sign in to comment.