Skip to content

Commit

Permalink
Don't encode the fragment identifier in .pick and .exclude (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richienb committed Jun 21, 2021
1 parent 8887f78 commit fd3e779
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const filterObject = require('filter-obj');

const isNullOrUndefined = value => value === null || value === undefined;

const encodeFragmentIdentifier = Symbol('encodeFragmentIdentifier');

function encoderForArrayFormat(options) {
switch (options.arrayFormat) {
case 'index':
Expand Down Expand Up @@ -402,7 +404,8 @@ exports.parseUrl = (url, options) => {
exports.stringifyUrl = (object, options) => {
options = Object.assign({
encode: true,
strict: true
strict: true,
[encodeFragmentIdentifier]: true
}, options);

const url = removeHash(object.url).split('?')[0] || '';
Expand All @@ -417,15 +420,16 @@ exports.stringifyUrl = (object, options) => {

let hash = getHash(object.url);
if (object.fragmentIdentifier) {
hash = `#${encode(object.fragmentIdentifier, options)}`;
hash = `#${options[encodeFragmentIdentifier] ? encode(object.fragmentIdentifier, options) : object.fragmentIdentifier}`;
}

return `${url}${queryString}${hash}`;
};

exports.pick = (input, filter, options) => {
options = Object.assign({
parseFragmentIdentifier: true
parseFragmentIdentifier: true,
[encodeFragmentIdentifier]: false
}, options);

const {url, query, fragmentIdentifier} = exports.parseUrl(input, options);
Expand Down
4 changes: 4 additions & 0 deletions test/exclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ test('excludes elements in a URL with a filter predicate', t => {
parseNumbers: true
}), 'http://example.com/?b=2&c=3#a');
});

test('excludes elements in a URL without encoding fragment identifiers', t => {
t.is(queryString.exclude('https://example.com?a=b#/home', ['a']), 'https://example.com#/home');
});
4 changes: 4 additions & 0 deletions test/pick.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ test('picks elements in a URL with a filter predicate', t => {
parseNumbers: true
}), 'http://example.com/?a=1#a');
});

test('picks elements in a URL without encoding fragment identifiers', t => {
t.is(queryString.pick('https://example.com?a=b#/home', []), 'https://example.com#/home');
});

0 comments on commit fd3e779

Please sign in to comment.