From fd3e7792e0ec0fb72925627869a4d583ed832e54 Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Tue, 22 Jun 2021 05:30:44 +1200 Subject: [PATCH] Don't encode the fragment identifier in `.pick` and `.exclude` (#320) --- index.js | 10 +++++++--- test/exclude.js | 4 ++++ test/pick.js | 4 ++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 7ab5d92..cc57637 100644 --- a/index.js +++ b/index.js @@ -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': @@ -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] || ''; @@ -417,7 +420,7 @@ 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}`; @@ -425,7 +428,8 @@ exports.stringifyUrl = (object, options) => { exports.pick = (input, filter, options) => { options = Object.assign({ - parseFragmentIdentifier: true + parseFragmentIdentifier: true, + [encodeFragmentIdentifier]: false }, options); const {url, query, fragmentIdentifier} = exports.parseUrl(input, options); diff --git a/test/exclude.js b/test/exclude.js index 91e0d4f..646db88 100644 --- a/test/exclude.js +++ b/test/exclude.js @@ -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'); +}); diff --git a/test/pick.js b/test/pick.js index e5e4381..0bfaf72 100644 --- a/test/pick.js +++ b/test/pick.js @@ -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'); +});