normalize-url 7.0.1 → 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
 
@@ -226,7 +247,7 @@ export interface Options {
226
247
  //=> 'http://sindresorhus.com/foo'
227
248
  ```
228
249
  */
229
- readonly removeDirectoryIndex?: ReadonlyArray<RegExp | string>;
250
+ readonly removeDirectoryIndex?: boolean | ReadonlyArray<RegExp | string>;
230
251
 
231
252
  /**
232
253
  Sorts the query parameters alphabetically by key.
package/index.js CHANGED
@@ -192,6 +192,7 @@ export default function normalizeUrl(urlString, options) {
192
192
 
193
193
  // Remove query unwanted parameters
194
194
  if (Array.isArray(options.removeQueryParameters)) {
195
+ // eslint-disable-next-line unicorn/no-useless-spread -- We are intentionally spreading to get a copy.
195
196
  for (const key of [...urlObject.searchParams.keys()]) {
196
197
  if (testParameter(key, options.removeQueryParameters)) {
197
198
  urlObject.searchParams.delete(key);
@@ -199,13 +200,28 @@ export default function normalizeUrl(urlString, options) {
199
200
  }
200
201
  }
201
202
 
202
- if (options.removeQueryParameters === true) {
203
+ if (!Array.isArray(options.keepQueryParameters) && options.removeQueryParameters === true) {
203
204
  urlObject.search = '';
204
205
  }
205
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
+
206
217
  // Sort query parameters
207
218
  if (options.sortQueryParameters) {
208
219
  urlObject.searchParams.sort();
220
+
221
+ // Calling `.sort()` encodes the search parameters, so we need to decode them again.
222
+ try {
223
+ urlObject.search = decodeURIComponent(urlObject.search);
224
+ } catch {}
209
225
  }
210
226
 
211
227
  if (options.removeTrailingSlash) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "normalize-url",
3
- "version": "7.0.1",
3
+ "version": "7.1.0",
4
4
  "description": "Normalize a URL",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/normalize-url",
@@ -38,11 +38,10 @@
38
38
  "canonical"
39
39
  ],
40
40
  "devDependencies": {
41
- "ava": "^3.15.0",
42
- "c8": "^7.7.3",
43
- "tsd": "^0.17.0",
44
- "typescript": "^4.3.5",
45
- "xo": "^0.41.0"
41
+ "ava": "^4.0.1",
42
+ "c8": "^7.11.0",
43
+ "tsd": "^0.19.1",
44
+ "xo": "^0.47.0"
46
45
  },
47
46
  "c8": {
48
47
  "reporter": [
package/readme.md CHANGED
@@ -4,13 +4,15 @@
4
4
 
5
5
  Useful when you need to display, store, deduplicate, sort, compare, etc, URLs.
6
6
 
7
+ **Note:** This package does **not** do URL sanitization. [Garbage in, garbage out.](https://en.wikipedia.org/wiki/Garbage_in,_garbage_out) If you use this in a server context and accept URLs as user input, it's up to you to protect against invalid URLs, [path traversal attacks](https://owasp.org/www-community/attacks/Path_Traversal), etc.
8
+
7
9
  ## Install
8
10
 
9
- ```
10
- $ npm install normalize-url
11
+ ```sh
12
+ npm install normalize-url
11
13
  ```
12
14
 
13
- *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`*
14
16
 
15
17
  ## Usage
16
18
 
@@ -41,7 +43,8 @@ Type: `object`
41
43
  ##### defaultProtocol
42
44
 
43
45
  Type: `string`\
44
- Default: `http:`
46
+ Default: `http:`\
47
+ Values: `'https:' | 'http:'`
45
48
 
46
49
  ##### normalizeProtocol
47
50
 
@@ -125,7 +128,9 @@ normalizeUrl('sindresorhus.com/about.html#contact', {stripHash: true});
125
128
  Type: `boolean`\
126
129
  Default: `false`
127
130
 
128
- 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.
129
134
 
130
135
  ```js
131
136
  normalizeUrl('https://sindresorhus.com');
@@ -205,6 +210,22 @@ normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', {
205
210
  //=> 'http://www.sindresorhus.com/?foo=bar&ref=test_ref&utm_medium=test'
206
211
  ```
207
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
+
208
229
  ##### removeTrailingSlash
209
230
 
210
231
  Type: `boolean`\
@@ -230,7 +251,7 @@ normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
230
251
  Type: `boolean`\
231
252
  Default: `true`
232
253
 
233
- 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`.
234
255
 
235
256
  ```js
236
257
  normalizeUrl('https://sindresorhus.com/');
@@ -240,7 +261,6 @@ normalizeUrl('https://sindresorhus.com/', {removeSingleSlash: false});
240
261
  //=> 'https://sindresorhus.com/'
241
262
  ```
242
263
 
243
-
244
264
  ##### removeDirectoryIndex
245
265
 
246
266
  Type: `boolean | Array<RegExp | string>`\