From 6216336d35c45cfc7af4efec40425a8ecaad65d2 Mon Sep 17 00:00:00 2001 From: Simon Jang Date: Tue, 22 Jun 2021 10:53:47 +0200 Subject: [PATCH] Accept a boolean for the `removeQueryParameters` option (#136) Co-authored-by: Sindre Sorhus --- index.d.ts | 20 +++++++++++++++++++- index.js | 4 ++++ index.test-d.ts | 6 ++++++ readme.md | 20 +++++++++++++++++++- test.js | 22 ++++++++++++++++++++++ 5 files changed, 70 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 848b899..ca40f8f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -155,8 +155,26 @@ declare namespace normalizeUrl { }); //=> 'http://sindresorhus.com/?foo=bar' ``` + + If a boolean is provided, `true` will remove all the query parameters. + + ``` + normalizeUrl('www.sindresorhus.com?foo=bar', { + removeQueryParameters: true + }); + //=> 'http://sindresorhus.com' + ``` + + `false` will not remove any query parameter. + + ``` + normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', { + removeQueryParameters: false + }); + //=> 'http://www.sindresorhus.com/?foo=bar&ref=test_ref&utm_medium=test' + ``` */ - readonly removeQueryParameters?: ReadonlyArray; + readonly removeQueryParameters?: ReadonlyArray | boolean; /** Removes trailing slash. diff --git a/index.js b/index.js index cac1807..c9340ab 100644 --- a/index.js +++ b/index.js @@ -173,6 +173,10 @@ const normalizeUrl = (urlString, options) => { } } + if (options.removeQueryParameters === true) { + urlObj.search = ''; + } + // Sort query parameters if (options.sortQueryParameters) { urlObj.searchParams.sort(); diff --git a/index.test-d.ts b/index.test-d.ts index a59312e..9326b37 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -15,6 +15,12 @@ normalizeUrl('http://www.sindresorhus.com', {stripWWW: false}); normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', { removeQueryParameters: ['ref', /test/] }); +normalizeUrl('www.sindresorhus.com?foo=bar', { + removeQueryParameters: true +}); +normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', { + removeQueryParameters: false +}); normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false}); normalizeUrl('http://sindresorhus.com/', {removeSingleSlash: false}); normalizeUrl('www.sindresorhus.com/foo/default.php', { diff --git a/readme.md b/readme.md index b3406aa..4b29b29 100644 --- a/readme.md +++ b/readme.md @@ -175,7 +175,7 @@ normalizeUrl('http://www.sindresorhus.com', {stripWWW: false}); ##### removeQueryParameters -Type: `Array`\ +Type: `Array | boolean`\ Default: `[/^utm_\w+/i]` Remove query parameters that matches any of the provided strings or regexes. @@ -187,6 +187,24 @@ normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', { //=> 'http://sindresorhus.com/?foo=bar' ``` +If a boolean is provided, `true` will remove all the query parameters. + +```js +normalizeUrl('www.sindresorhus.com?foo=bar', { + removeQueryParameters: true +}); +//=> 'http://sindresorhus.com' +``` + +`false` will not remove any query parameter. + +```js +normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', { + removeQueryParameters: false +}); +//=> 'http://www.sindresorhus.com/?foo=bar&ref=test_ref&utm_medium=test' +``` + ##### removeTrailingSlash Type: `boolean`\ diff --git a/test.js b/test.js index d3be87a..4aa6822 100644 --- a/test.js +++ b/test.js @@ -119,6 +119,28 @@ test('removeQueryParameters option', t => { t.is(normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', options), 'http://www.sindresorhus.com/?foo=bar'); }); +test('removeQueryParameters boolean `true` option', t => { + const options = { + stripWWW: false, + removeQueryParameters: true + }; + + t.is(normalizeUrl('http://www.sindresorhus.com', options), 'http://www.sindresorhus.com'); + t.is(normalizeUrl('www.sindresorhus.com?foo=bar', options), 'http://www.sindresorhus.com'); + t.is(normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', options), 'http://www.sindresorhus.com'); +}); + +test('removeQueryParameters boolean `false` option', t => { + const options = { + stripWWW: false, + removeQueryParameters: false + }; + + t.is(normalizeUrl('http://www.sindresorhus.com', options), 'http://www.sindresorhus.com'); + t.is(normalizeUrl('www.sindresorhus.com?foo=bar', options), 'http://www.sindresorhus.com/?foo=bar'); + t.is(normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', options), 'http://www.sindresorhus.com/?foo=bar&ref=test_ref&utm_medium=test'); +}); + test('forceHttp option', t => { const options = {forceHttp: true}; t.is(normalizeUrl('https://sindresorhus.com'), 'https://sindresorhus.com');