From 2553ab2c3af04cb6efc87f1e6118cc1601484ffa Mon Sep 17 00:00:00 2001 From: Simon Jang Date: Sat, 19 Jun 2021 12:00:47 +0200 Subject: [PATCH 1/5] Add `removeQueryParameters` boolean option - #133 --- index.d.ts | 2 +- index.js | 4 ++++ index.test-d.ts | 3 +++ readme.md | 20 +++++++++++++++++++- test.js | 22 ++++++++++++++++++++++ 5 files changed, 49 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 848b899..6d205b4 100644 --- a/index.d.ts +++ b/index.d.ts @@ -156,7 +156,7 @@ declare namespace normalizeUrl { //=> 'http://sindresorhus.com/?foo=bar' ``` */ - readonly removeQueryParameters?: ReadonlyArray; + readonly removeQueryParameters?: ReadonlyArray | boolean; /** Removes trailing slash. diff --git a/index.js b/index.js index cac1807..2b9936d 100644 --- a/index.js +++ b/index.js @@ -187,6 +187,10 @@ const normalizeUrl = (urlString, options) => { // Take advantage of many of the Node `url` normalizations urlString = urlObj.toString(); + if (options.removeQueryParameters === true) { + urlString = urlString.split('?')[0]; + } + if (!options.removeSingleSlash && urlObj.pathname === '/' && !oldUrlString.endsWith('/') && urlObj.hash === '') { urlString = urlString.replace(/\/$/, ''); } diff --git a/index.test-d.ts b/index.test-d.ts index a59312e..40f6682 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -15,6 +15,9 @@ 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&ref=test_ref', { + removeQueryParameters: true +}); 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..4d07bf6 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. Other options like `sortQueryParameters` are still applied when toggled. + +```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'); From 1d5951f9fe696978d73a2b8a6cd7a6eb46887d13 Mon Sep 17 00:00:00 2001 From: Simon Jang Date: Sun, 20 Jun 2021 08:45:06 +0200 Subject: [PATCH 2/5] chore: PR fixes --- index.js | 10 ++++++---- index.test-d.ts | 5 ++++- readme.md | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 2b9936d..409bc16 100644 --- a/index.js +++ b/index.js @@ -173,6 +173,12 @@ const normalizeUrl = (urlString, options) => { } } + if (options.removeQueryParameters === true) { + for (const key of [...urlObj.searchParams.keys()]) { + urlObj.searchParams.delete(key); + } + } + // Sort query parameters if (options.sortQueryParameters) { urlObj.searchParams.sort(); @@ -187,10 +193,6 @@ const normalizeUrl = (urlString, options) => { // Take advantage of many of the Node `url` normalizations urlString = urlObj.toString(); - if (options.removeQueryParameters === true) { - urlString = urlString.split('?')[0]; - } - if (!options.removeSingleSlash && urlObj.pathname === '/' && !oldUrlString.endsWith('/') && urlObj.hash === '') { urlString = urlString.replace(/\/$/, ''); } diff --git a/index.test-d.ts b/index.test-d.ts index 40f6682..9326b37 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -15,9 +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&ref=test_ref', { +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 4d07bf6..4b29b29 100644 --- a/readme.md +++ b/readme.md @@ -196,7 +196,7 @@ normalizeUrl('www.sindresorhus.com?foo=bar', { //=> 'http://sindresorhus.com' ``` -`false` will not remove any query parameter. Other options like `sortQueryParameters` are still applied when toggled. +`false` will not remove any query parameter. ```js normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', { From 8035d21df499bcb3dbede942e69d2f83e9262563 Mon Sep 17 00:00:00 2001 From: Simon Jang Date: Sun, 20 Jun 2021 11:36:55 +0200 Subject: [PATCH 3/5] chore: PR feedback fixes --- index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/index.js b/index.js index 409bc16..c9340ab 100644 --- a/index.js +++ b/index.js @@ -174,9 +174,7 @@ const normalizeUrl = (urlString, options) => { } if (options.removeQueryParameters === true) { - for (const key of [...urlObj.searchParams.keys()]) { - urlObj.searchParams.delete(key); - } + urlObj.search = ''; } // Sort query parameters From f10e19a4cc8189d4f57d8278a95e1e95fcec514c Mon Sep 17 00:00:00 2001 From: Simon Jang Date: Mon, 21 Jun 2021 20:14:08 +0200 Subject: [PATCH 4/5] chore: Sync readme and index.d.ts --- index.d.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/index.d.ts b/index.d.ts index 6d205b4..2738007 100644 --- a/index.d.ts +++ b/index.d.ts @@ -155,6 +155,20 @@ 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 | boolean; From f506f361b5eec992c627087f8a338e82240ddbef Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 22 Jun 2021 13:58:45 +0700 Subject: [PATCH 5/5] Update index.d.ts --- index.d.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/index.d.ts b/index.d.ts index 2738007..ca40f8f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -155,14 +155,18 @@ 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