Skip to content

Commit

Permalink
Accept a boolean for the removeQueryParameters option (#136)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
SimonJang and sindresorhus authored Jun 22, 2021
1 parent 305e3f2 commit 6216336
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
20 changes: 19 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RegExp | string>;
readonly removeQueryParameters?: ReadonlyArray<RegExp | string> | boolean;

/**
Removes trailing slash.
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ const normalizeUrl = (urlString, options) => {
}
}

if (options.removeQueryParameters === true) {
urlObj.search = '';
}

// Sort query parameters
if (options.sortQueryParameters) {
urlObj.searchParams.sort();
Expand Down
6 changes: 6 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
20 changes: 19 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ normalizeUrl('http://www.sindresorhus.com', {stripWWW: false});

##### removeQueryParameters

Type: `Array<RegExp | string>`\
Type: `Array<RegExp | string> | boolean`\
Default: `[/^utm_\w+/i]`

Remove query parameters that matches any of the provided strings or regexes.
Expand All @@ -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`\
Expand Down
22 changes: 22 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 6216336

Please sign in to comment.