normalize-url 7.0.3 → 7.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  export interface Options {
2
2
  /**
3
3
  @default 'http:'
4
+
5
+ Values: `'https:' | 'http:'`
4
6
  */
5
- readonly defaultProtocol?: string;
7
+ readonly defaultProtocol?: string; // TODO: Make this `'https:' | 'http:'` in the next major version.
6
8
 
7
9
  /**
8
10
  Prepends `defaultProtocol` to the URL if it's protocol-relative.
@@ -87,7 +89,9 @@ export interface Options {
87
89
  readonly stripHash?: boolean;
88
90
 
89
91
  /**
90
- Removes HTTP(S) protocol from an URL `http://sindresorhus.com` → `sindresorhus.com`.
92
+ Remove the protocol from the URL: `http://sindresorhus.com` → `sindresorhus.com`.
93
+
94
+ It will only remove `https://` and `http://` protocols.
91
95
 
92
96
  @default false
93
97
 
@@ -175,6 +179,23 @@ export interface Options {
175
179
  */
176
180
  readonly removeQueryParameters?: ReadonlyArray<RegExp | string> | boolean;
177
181
 
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
+
178
199
  /**
179
200
  Removes trailing slash.
180
201
 
@@ -197,7 +218,7 @@ export interface Options {
197
218
  readonly removeTrailingSlash?: boolean;
198
219
 
199
220
  /**
200
- Remove a sole `/` pathname in the output. This option is independant of `removeTrailingSlash`.
221
+ Remove a sole `/` pathname in the output. This option is independent of `removeTrailingSlash`.
201
222
 
202
223
  @default true
203
224
 
package/index.js CHANGED
@@ -200,10 +200,20 @@ export default function normalizeUrl(urlString, options) {
200
200
  }
201
201
  }
202
202
 
203
- if (options.removeQueryParameters === true) {
203
+ if (!Array.isArray(options.keepQueryParameters) && options.removeQueryParameters === true) {
204
204
  urlObject.search = '';
205
205
  }
206
206
 
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
+
207
217
  // Sort query parameters
208
218
  if (options.sortQueryParameters) {
209
219
  urlObject.searchParams.sort();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "normalize-url",
3
- "version": "7.0.3",
3
+ "version": "7.1.0",
4
4
  "description": "Normalize a URL",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/normalize-url",
@@ -16,7 +16,7 @@
16
16
  "node": ">=12.20"
17
17
  },
18
18
  "scripts": {
19
- "test": "ava && tsd"
19
+ "test": "xo && c8 ava && tsd"
20
20
  },
21
21
  "files": [
22
22
  "index.js",
package/readme.md CHANGED
@@ -12,7 +12,7 @@ Useful when you need to display, store, deduplicate, sort, compare, etc, URLs.
12
12
  npm install normalize-url
13
13
  ```
14
14
 
15
- *If you need to use this in the browser, use version 4: `npm i normalize-url@4`*
15
+ *If you need Safari support, use version 4: `npm i normalize-url@4`*
16
16
 
17
17
  ## Usage
18
18
 
@@ -43,7 +43,8 @@ Type: `object`
43
43
  ##### defaultProtocol
44
44
 
45
45
  Type: `string`\
46
- Default: `http:`
46
+ Default: `http:`\
47
+ Values: `'https:' | 'http:'`
47
48
 
48
49
  ##### normalizeProtocol
49
50
 
@@ -127,7 +128,9 @@ normalizeUrl('sindresorhus.com/about.html#contact', {stripHash: true});
127
128
  Type: `boolean`\
128
129
  Default: `false`
129
130
 
130
- Remove HTTP(S) protocol from the URL: `http://sindresorhus.com` → `sindresorhus.com`.
131
+ Remove the protocol from the URL: `http://sindresorhus.com` → `sindresorhus.com`.
132
+
133
+ It will only remove `https://` and `http://` protocols.
131
134
 
132
135
  ```js
133
136
  normalizeUrl('https://sindresorhus.com');
@@ -207,6 +210,22 @@ normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', {
207
210
  //=> 'http://www.sindresorhus.com/?foo=bar&ref=test_ref&utm_medium=test'
208
211
  ```
209
212
 
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
+
210
229
  ##### removeTrailingSlash
211
230
 
212
231
  Type: `boolean`\
@@ -232,7 +251,7 @@ normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
232
251
  Type: `boolean`\
233
252
  Default: `true`
234
253
 
235
- Remove a sole `/` pathname in the output. This option is independant of `removeTrailingSlash`.
254
+ Remove a sole `/` pathname in the output. This option is independent of `removeTrailingSlash`.
236
255
 
237
256
  ```js
238
257
  normalizeUrl('https://sindresorhus.com/');