Skip to content

Commit 6e24307

Browse files
ozgurgsindresorhus
andauthored
Add keepQueryParameters option (#173)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 4ce3a64 commit 6e24307

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

index.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,23 @@ export interface Options {
179179
*/
180180
readonly removeQueryParameters?: ReadonlyArray<RegExp | string> | boolean;
181181

182+
/**
183+
Keeps only query parameters that matches any of the provided strings or regexes.
184+
185+
__Note__: It overrides the `removeQueryParameters` option.
186+
187+
@default undefined
188+
189+
@example
190+
```
191+
normalizeUrl('https://sindresorhus.com?foo=bar&ref=unicorn', {
192+
keepQueryParameters: ['ref']
193+
});
194+
//=> 'https://sindresorhus.com/?ref=unicorn'
195+
```
196+
*/
197+
readonly keepQueryParameters?: ReadonlyArray<RegExp | string>;
198+
182199
/**
183200
Removes trailing slash.
184201

index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,20 @@ export default function normalizeUrl(urlString, options) {
200200
}
201201
}
202202

203-
if (options.removeQueryParameters === true) {
203+
if (!Array.isArray(options.keepQueryParameters) && options.removeQueryParameters === true) {
204204
urlObject.search = '';
205205
}
206206

207+
// Keep wanted query parameters
208+
if (Array.isArray(options.keepQueryParameters) && options.keepQueryParameters.length > 0) {
209+
// eslint-disable-next-line unicorn/no-useless-spread -- We are intentionally spreading to get a copy.
210+
for (const key of [...urlObject.searchParams.keys()]) {
211+
if (!testParameter(key, options.keepQueryParameters)) {
212+
urlObject.searchParams.delete(key);
213+
}
214+
}
215+
}
216+
207217
// Sort query parameters
208218
if (options.sortQueryParameters) {
209219
urlObject.searchParams.sort();

index.test-d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ normalizeUrl('www.sindresorhus.com?foo=bar', {
2121
normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', {
2222
removeQueryParameters: false,
2323
});
24+
normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
25+
keepQueryParameters: ['ref', /test/],
26+
});
2427
normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
2528
normalizeUrl('http://sindresorhus.com/', {removeSingleSlash: false});
2629
normalizeUrl('www.sindresorhus.com/foo/default.php', {

readme.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,22 @@ normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', {
210210
//=> 'http://www.sindresorhus.com/?foo=bar&ref=test_ref&utm_medium=test'
211211
```
212212

213+
##### keepQueryParameters
214+
215+
Type: `Array<RegExp | string>`\
216+
Default: `undefined`
217+
218+
Keeps only query parameters that matches any of the provided strings or regexes.
219+
220+
**Note:** It overrides the `removeQueryParameters` option.
221+
222+
```js
223+
normalizeUrl('https://sindresorhus.com?foo=bar&ref=unicorn', {
224+
keepQueryParameters: ['ref']
225+
});
226+
//=> 'https://sindresorhus.com/?ref=unicorn'
227+
```
228+
213229
##### removeTrailingSlash
214230

215231
Type: `boolean`\

test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,17 @@ test('removeQueryParameters boolean `false` option', t => {
141141
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');
142142
});
143143

144+
test('keepQueryParameters option', t => {
145+
const options = {
146+
stripWWW: false,
147+
removeQueryParameters: false,
148+
keepQueryParameters: [/^utm_\w+/i, 'ref'],
149+
};
150+
t.is(normalizeUrl('https://sindresorhus.com', options), 'https://sindresorhus.com');
151+
t.is(normalizeUrl('www.sindresorhus.com?foo=bar', options), 'http://www.sindresorhus.com');
152+
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');
153+
});
154+
144155
test('forceHttp option', t => {
145156
const options = {forceHttp: true};
146157
t.is(normalizeUrl('https://sindresorhus.com'), 'https://sindresorhus.com');

0 commit comments

Comments
 (0)