normalize-url 7.0.3 → 7.2.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 +48 -10
- package/index.js +17 -1
- package/package.json +2 -2
- package/readme.md +46 -11
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.
|
|
@@ -11,10 +13,10 @@ export interface Options {
|
|
|
11
13
|
|
|
12
14
|
@example
|
|
13
15
|
```
|
|
14
|
-
normalizeUrl('//sindresorhus.com
|
|
16
|
+
normalizeUrl('//sindresorhus.com');
|
|
15
17
|
//=> 'http://sindresorhus.com'
|
|
16
18
|
|
|
17
|
-
normalizeUrl('//sindresorhus.com
|
|
19
|
+
normalizeUrl('//sindresorhus.com', {normalizeProtocol: false});
|
|
18
20
|
//=> '//sindresorhus.com'
|
|
19
21
|
```
|
|
20
22
|
*/
|
|
@@ -27,10 +29,10 @@ export interface Options {
|
|
|
27
29
|
|
|
28
30
|
@example
|
|
29
31
|
```
|
|
30
|
-
normalizeUrl('https://sindresorhus.com
|
|
32
|
+
normalizeUrl('https://sindresorhus.com');
|
|
31
33
|
//=> 'https://sindresorhus.com'
|
|
32
34
|
|
|
33
|
-
normalizeUrl('https://sindresorhus.com
|
|
35
|
+
normalizeUrl('https://sindresorhus.com', {forceHttp: true});
|
|
34
36
|
//=> 'http://sindresorhus.com'
|
|
35
37
|
```
|
|
36
38
|
*/
|
|
@@ -45,10 +47,10 @@ export interface Options {
|
|
|
45
47
|
|
|
46
48
|
@example
|
|
47
49
|
```
|
|
48
|
-
normalizeUrl('
|
|
49
|
-
//=> '
|
|
50
|
+
normalizeUrl('http://sindresorhus.com');
|
|
51
|
+
//=> 'http://sindresorhus.com'
|
|
50
52
|
|
|
51
|
-
normalizeUrl('http://sindresorhus.com
|
|
53
|
+
normalizeUrl('http://sindresorhus.com', {forceHttps: true});
|
|
52
54
|
//=> 'https://sindresorhus.com'
|
|
53
55
|
```
|
|
54
56
|
*/
|
|
@@ -87,7 +89,9 @@ export interface Options {
|
|
|
87
89
|
readonly stripHash?: boolean;
|
|
88
90
|
|
|
89
91
|
/**
|
|
90
|
-
|
|
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
|
|
221
|
+
Remove a sole `/` pathname in the output. This option is independent of `removeTrailingSlash`.
|
|
201
222
|
|
|
202
223
|
@default true
|
|
203
224
|
|
|
@@ -228,6 +249,23 @@ export interface Options {
|
|
|
228
249
|
*/
|
|
229
250
|
readonly removeDirectoryIndex?: boolean | ReadonlyArray<RegExp | string>;
|
|
230
251
|
|
|
252
|
+
/**
|
|
253
|
+
Removes an explicit port number from the URL.
|
|
254
|
+
|
|
255
|
+
Port 443 is always removed from HTTPS URLs and 80 is always removed from HTTP URLs regardless of this option.
|
|
256
|
+
|
|
257
|
+
@default false
|
|
258
|
+
|
|
259
|
+
@example
|
|
260
|
+
```
|
|
261
|
+
normalizeUrl('sindresorhus.com:123', {
|
|
262
|
+
removeExplicitPort: true
|
|
263
|
+
});
|
|
264
|
+
//=> 'http://sindresorhus.com'
|
|
265
|
+
```
|
|
266
|
+
*/
|
|
267
|
+
readonly removeExplicitPort?: boolean;
|
|
268
|
+
|
|
231
269
|
/**
|
|
232
270
|
Sorts the query parameters alphabetically by key.
|
|
233
271
|
|
package/index.js
CHANGED
|
@@ -69,6 +69,7 @@ export default function normalizeUrl(urlString, options) {
|
|
|
69
69
|
removeTrailingSlash: true,
|
|
70
70
|
removeSingleSlash: true,
|
|
71
71
|
removeDirectoryIndex: false,
|
|
72
|
+
removeExplicitPort: false,
|
|
72
73
|
sortQueryParameters: true,
|
|
73
74
|
...options,
|
|
74
75
|
};
|
|
@@ -200,10 +201,20 @@ export default function normalizeUrl(urlString, options) {
|
|
|
200
201
|
}
|
|
201
202
|
}
|
|
202
203
|
|
|
203
|
-
if (options.removeQueryParameters === true) {
|
|
204
|
+
if (!Array.isArray(options.keepQueryParameters) && options.removeQueryParameters === true) {
|
|
204
205
|
urlObject.search = '';
|
|
205
206
|
}
|
|
206
207
|
|
|
208
|
+
// Keep wanted query parameters
|
|
209
|
+
if (Array.isArray(options.keepQueryParameters) && options.keepQueryParameters.length > 0) {
|
|
210
|
+
// eslint-disable-next-line unicorn/no-useless-spread -- We are intentionally spreading to get a copy.
|
|
211
|
+
for (const key of [...urlObject.searchParams.keys()]) {
|
|
212
|
+
if (!testParameter(key, options.keepQueryParameters)) {
|
|
213
|
+
urlObject.searchParams.delete(key);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
207
218
|
// Sort query parameters
|
|
208
219
|
if (options.sortQueryParameters) {
|
|
209
220
|
urlObject.searchParams.sort();
|
|
@@ -218,6 +229,11 @@ export default function normalizeUrl(urlString, options) {
|
|
|
218
229
|
urlObject.pathname = urlObject.pathname.replace(/\/$/, '');
|
|
219
230
|
}
|
|
220
231
|
|
|
232
|
+
// Remove an explicit port number, excluding a default port number, if applicable
|
|
233
|
+
if (options.removeExplicitPort && urlObject.port) {
|
|
234
|
+
urlObject.port = '';
|
|
235
|
+
}
|
|
236
|
+
|
|
221
237
|
const oldUrlString = urlString;
|
|
222
238
|
|
|
223
239
|
// Take advantage of many of the Node `url` normalizations
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "normalize-url",
|
|
3
|
-
"version": "7.0
|
|
3
|
+
"version": "7.2.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
|
|
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
|
|
|
@@ -53,10 +54,10 @@ Default: `true`
|
|
|
53
54
|
Prepend `defaultProtocol` to the URL if it's protocol-relative.
|
|
54
55
|
|
|
55
56
|
```js
|
|
56
|
-
normalizeUrl('//sindresorhus.com
|
|
57
|
+
normalizeUrl('//sindresorhus.com');
|
|
57
58
|
//=> 'http://sindresorhus.com'
|
|
58
59
|
|
|
59
|
-
normalizeUrl('//sindresorhus.com
|
|
60
|
+
normalizeUrl('//sindresorhus.com', {normalizeProtocol: false});
|
|
60
61
|
//=> '//sindresorhus.com'
|
|
61
62
|
```
|
|
62
63
|
|
|
@@ -68,10 +69,10 @@ Default: `false`
|
|
|
68
69
|
Normalize `https:` to `http:`.
|
|
69
70
|
|
|
70
71
|
```js
|
|
71
|
-
normalizeUrl('https://sindresorhus.com
|
|
72
|
+
normalizeUrl('https://sindresorhus.com');
|
|
72
73
|
//=> 'https://sindresorhus.com'
|
|
73
74
|
|
|
74
|
-
normalizeUrl('https://sindresorhus.com
|
|
75
|
+
normalizeUrl('https://sindresorhus.com', {forceHttp: true});
|
|
75
76
|
//=> 'http://sindresorhus.com'
|
|
76
77
|
```
|
|
77
78
|
|
|
@@ -83,10 +84,10 @@ Default: `false`
|
|
|
83
84
|
Normalize `http:` to `https:`.
|
|
84
85
|
|
|
85
86
|
```js
|
|
86
|
-
normalizeUrl('
|
|
87
|
-
//=> '
|
|
87
|
+
normalizeUrl('http://sindresorhus.com');
|
|
88
|
+
//=> 'http://sindresorhus.com'
|
|
88
89
|
|
|
89
|
-
normalizeUrl('http://sindresorhus.com
|
|
90
|
+
normalizeUrl('http://sindresorhus.com', {forceHttps: true});
|
|
90
91
|
//=> 'https://sindresorhus.com'
|
|
91
92
|
```
|
|
92
93
|
|
|
@@ -127,7 +128,9 @@ normalizeUrl('sindresorhus.com/about.html#contact', {stripHash: true});
|
|
|
127
128
|
Type: `boolean`\
|
|
128
129
|
Default: `false`
|
|
129
130
|
|
|
130
|
-
Remove
|
|
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
|
|
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/');
|
|
@@ -256,6 +275,22 @@ normalizeUrl('www.sindresorhus.com/foo/default.php', {
|
|
|
256
275
|
//=> 'http://sindresorhus.com/foo'
|
|
257
276
|
```
|
|
258
277
|
|
|
278
|
+
##### removeExplicitPort
|
|
279
|
+
|
|
280
|
+
Type: `boolean`\
|
|
281
|
+
Default: `false`
|
|
282
|
+
|
|
283
|
+
Removes an explicit port number from the URL.
|
|
284
|
+
|
|
285
|
+
Port 443 is always removed from HTTPS URLs and 80 is always removed from HTTP URLs regardless of this option.
|
|
286
|
+
|
|
287
|
+
```js
|
|
288
|
+
normalizeUrl('sindresorhus.com:123', {
|
|
289
|
+
removeExplicitPort: true
|
|
290
|
+
});
|
|
291
|
+
//=> 'http://sindresorhus.com'
|
|
292
|
+
```
|
|
293
|
+
|
|
259
294
|
##### sortQueryParameters
|
|
260
295
|
|
|
261
296
|
Type: `boolean`\
|