Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept a boolean for the removeQueryParameters option #136

Merged
merged 5 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ declare namespace normalizeUrl {
//=> 'http://sindresorhus.com/?foo=bar'
```
*/
readonly removeQueryParameters?: ReadonlyArray<RegExp | string>;
readonly removeQueryParameters?: ReadonlyArray<RegExp | string> | boolean;

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

if (options.removeQueryParameters === true) {
for (const key of [...urlObj.searchParams.keys()]) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just set urlObj.search = '';.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I didn't knew that. Changed it.

urlObj.searchParams.delete(key);
}
}

// 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'
```
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

index.d.ts and readme should be fully in sync.


##### 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