From 1d24ce0d86f37adaed8d435943bb4e19ea692e2a Mon Sep 17 00:00:00 2001 From: hanbin9775 Date: Sun, 4 Dec 2022 20:22:41 +0900 Subject: [PATCH] fix: Encode fragmentIdentifier with encodeURI method not encodeURIComponent at stringifyUrl --- index.js | 10 +++++++++- test/stringify-url.js | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d45e67d9..33b514ab 100644 --- a/index.js +++ b/index.js @@ -222,7 +222,15 @@ function validateArrayFormatSeparator(value) { function encode(value, options) { if (options.encode) { - return options.strict ? strictUriEncode(value) : encodeURIComponent(value); + if (options[encodeFragmentIdentifier]) { + return encodeURI(value); + } + + if (options.strict) { + return strictUriEncode(value); + } + + return encodeURIComponent(value); } return value; diff --git a/test/stringify-url.js b/test/stringify-url.js index 2b3c3728..9b6ff43e 100644 --- a/test/stringify-url.js +++ b/test/stringify-url.js @@ -26,6 +26,7 @@ test('stringify URL with fragment identifier', t => { t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar/#abc', query: {}, fragmentIdentifier: 'top'}), 'https://foo.bar/#top'); t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar', query: {}}), 'https://foo.bar'); t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar', query: {}, fragmentIdentifier: 'foo bar'}), 'https://foo.bar#foo%20bar'); + t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar/', query: {}, fragmentIdentifier: '/foo/bar'}), 'https://foo.bar/#/foo/bar'); }); test('skipEmptyString:: stringify URL with a query string', t => {