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

Add keepQueryParameters option #173

Merged
merged 11 commits into from
Sep 1, 2022
15 changes: 15 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,21 @@ export interface Options {
*/
readonly removeQueryParameters?: ReadonlyArray<RegExp | string> | boolean;

/**
Keeps query parameters that matches any of the provided strings or regexes.
ozgurg marked this conversation as resolved.
Show resolved Hide resolved

ozgurg marked this conversation as resolved.
Show resolved Hide resolved
@default []

@example
```
normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
keepQueryParameters: ['ref']
});
//=> 'http://sindresorhus.com/?ref=test_ref'
ozgurg marked this conversation as resolved.
Show resolved Hide resolved
```
*/
ozgurg marked this conversation as resolved.
Show resolved Hide resolved
readonly keepQueryParameters?: ReadonlyArray<RegExp | string>;

/**
Removes trailing slash.

Expand Down
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export default function normalizeUrl(urlString, options) {
stripTextFragment: true,
stripWWW: true,
removeQueryParameters: [/^utm_\w+/i],
keepQueryParameters: [],
removeTrailingSlash: true,
removeSingleSlash: true,
removeDirectoryIndex: false,
Expand Down Expand Up @@ -204,6 +205,16 @@ export default function normalizeUrl(urlString, options) {
urlObject.search = '';
}
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved

// Keep wanted query parameters
if (Array.isArray(options.keepQueryParameters) && options.keepQueryParameters.length > 0) {
// eslint-disable-next-line unicorn/no-useless-spread -- We are intentionally spreading to get a copy.
for (const key of [...urlObject.searchParams.keys()]) {
if (!testParameter(key, options.keepQueryParameters)) {
urlObject.searchParams.delete(key);
}
}
}

// Sort query parameters
if (options.sortQueryParameters) {
urlObject.searchParams.sort();
Expand Down
3 changes: 3 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ normalizeUrl('www.sindresorhus.com?foo=bar', {
normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', {
removeQueryParameters: false,
});
normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
keepQueryParameters: ['ref', /test/],
});
normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
normalizeUrl('http://sindresorhus.com/', {removeSingleSlash: false});
normalizeUrl('www.sindresorhus.com/foo/default.php', {
Expand Down
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,20 @@ normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', {
//=> 'http://www.sindresorhus.com/?foo=bar&ref=test_ref&utm_medium=test'
```

##### keepQueryParameters

Type: `Array<RegExp | string>`\
Default: `[]`
ozgurg marked this conversation as resolved.
Show resolved Hide resolved

Keep query parameters that matches any of the provided strings or regexes.

```js
normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
keepQueryParameters: ['ref']
});
//=> 'http://sindresorhus.com/?ref=test_ref'
```

##### removeTrailingSlash

Type: `boolean`\
Expand Down
11 changes: 11 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ test('removeQueryParameters boolean `false` option', t => {
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('keepQueryParameters option', t => {
const options = {
stripWWW: false,
removeQueryParameters: false,
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
keepQueryParameters: [/^utm_\w+/i, 'ref'],
};
t.is(normalizeUrl('http://www.sindresorhus.com', options), 'http://www.sindresorhus.com');
ozgurg marked this conversation as resolved.
Show resolved Hide resolved
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/?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