normalize-url 3.2.0 → 3.3.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.js +33 -15
- package/package.json +4 -2
- package/readme.md +20 -15
package/index.js
CHANGED
|
@@ -2,16 +2,17 @@
|
|
|
2
2
|
// TODO: Use the `URL` global when targeting Node.js 10
|
|
3
3
|
const URLParser = typeof URL === 'undefined' ? require('url').URL : URL;
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
const testParameter = (name, filters) => {
|
|
6
6
|
return filters.some(filter => filter instanceof RegExp ? filter.test(name) : filter === name);
|
|
7
|
-
}
|
|
7
|
+
};
|
|
8
8
|
|
|
9
9
|
module.exports = (urlString, opts) => {
|
|
10
10
|
opts = Object.assign({
|
|
11
|
+
defaultProtocol: 'http:',
|
|
11
12
|
normalizeProtocol: true,
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
forceHttp: false,
|
|
14
|
+
forceHttps: false,
|
|
15
|
+
stripHash: true,
|
|
15
16
|
stripWWW: true,
|
|
16
17
|
removeQueryParameters: [/^utm_\w+/i],
|
|
17
18
|
removeTrailingSlash: true,
|
|
@@ -19,6 +20,19 @@ module.exports = (urlString, opts) => {
|
|
|
19
20
|
sortQueryParameters: true
|
|
20
21
|
}, opts);
|
|
21
22
|
|
|
23
|
+
// Backwards compatibility
|
|
24
|
+
if (Reflect.has(opts, 'normalizeHttps')) {
|
|
25
|
+
opts.forceHttp = opts.normalizeHttps;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (Reflect.has(opts, 'normalizeHttp')) {
|
|
29
|
+
opts.forceHttps = opts.normalizeHttp;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (Reflect.has(opts, 'stripFragment')) {
|
|
33
|
+
opts.stripHash = opts.stripFragment;
|
|
34
|
+
}
|
|
35
|
+
|
|
22
36
|
urlString = urlString.trim();
|
|
23
37
|
|
|
24
38
|
const hasRelativeProtocol = urlString.startsWith('//');
|
|
@@ -26,25 +40,25 @@ module.exports = (urlString, opts) => {
|
|
|
26
40
|
|
|
27
41
|
// Prepend protocol
|
|
28
42
|
if (!isRelativeUrl) {
|
|
29
|
-
urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//,
|
|
43
|
+
urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, opts.defaultProtocol);
|
|
30
44
|
}
|
|
31
45
|
|
|
32
46
|
const urlObj = new URLParser(urlString);
|
|
33
47
|
|
|
34
|
-
if (opts.
|
|
35
|
-
throw new Error('The `
|
|
48
|
+
if (opts.forceHttp && opts.forceHttps) {
|
|
49
|
+
throw new Error('The `forceHttp` and `forceHttps` options cannot be used together');
|
|
36
50
|
}
|
|
37
51
|
|
|
38
|
-
if (opts.
|
|
39
|
-
urlObj.protocol = '
|
|
52
|
+
if (opts.forceHttp && urlObj.protocol === 'https:') {
|
|
53
|
+
urlObj.protocol = 'http:';
|
|
40
54
|
}
|
|
41
55
|
|
|
42
|
-
if (opts.
|
|
43
|
-
urlObj.protocol = '
|
|
56
|
+
if (opts.forceHttps && urlObj.protocol === 'http:') {
|
|
57
|
+
urlObj.protocol = 'https:';
|
|
44
58
|
}
|
|
45
59
|
|
|
46
|
-
// Remove
|
|
47
|
-
if (opts.
|
|
60
|
+
// Remove hash
|
|
61
|
+
if (opts.stripHash) {
|
|
48
62
|
urlObj.hash = '';
|
|
49
63
|
}
|
|
50
64
|
|
|
@@ -85,7 +99,11 @@ module.exports = (urlString, opts) => {
|
|
|
85
99
|
urlObj.hostname = urlObj.hostname.replace(/\.$/, '');
|
|
86
100
|
|
|
87
101
|
// Remove `www.`
|
|
88
|
-
|
|
102
|
+
// eslint-disable-next-line no-useless-escape
|
|
103
|
+
if (opts.stripWWW && /^www\.([a-z\-\d]{2,63})\.([a-z\.]{2,5})$/.test(urlObj.hostname)) {
|
|
104
|
+
// Each label should be max 63 at length (min: 2).
|
|
105
|
+
// The extension should be max 5 at length (min: 2).
|
|
106
|
+
// Source: https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
|
|
89
107
|
urlObj.hostname = urlObj.hostname.replace(/^www\./, '');
|
|
90
108
|
}
|
|
91
109
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "normalize-url",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.0",
|
|
4
4
|
"description": "Normalize a URL",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/normalize-url",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"node": ">=6"
|
|
14
14
|
},
|
|
15
15
|
"scripts": {
|
|
16
|
-
"test": "xo && ava"
|
|
16
|
+
"test": "xo && nyc ava"
|
|
17
17
|
},
|
|
18
18
|
"files": [
|
|
19
19
|
"index.js"
|
|
@@ -35,6 +35,8 @@
|
|
|
35
35
|
],
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"ava": "*",
|
|
38
|
+
"coveralls": "^3.0.0",
|
|
39
|
+
"nyc": "^12.0.2",
|
|
38
40
|
"xo": "*"
|
|
39
41
|
}
|
|
40
42
|
}
|
package/readme.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# normalize-url [](https://travis-ci.org/sindresorhus/normalize-url)
|
|
1
|
+
# normalize-url [](https://travis-ci.org/sindresorhus/normalize-url) [](https://coveralls.io/github/sindresorhus/normalize-url?branch=master)
|
|
2
2
|
|
|
3
3
|
> [Normalize](https://en.wikipedia.org/wiki/URL_normalization) a URL
|
|
4
4
|
|
|
@@ -39,12 +39,17 @@ URL to normalize.
|
|
|
39
39
|
|
|
40
40
|
Type: `Object`
|
|
41
41
|
|
|
42
|
+
##### defaultProtocol
|
|
43
|
+
|
|
44
|
+
Type: `string`<br>
|
|
45
|
+
Default: `http:`
|
|
46
|
+
|
|
42
47
|
##### normalizeProtocol
|
|
43
48
|
|
|
44
49
|
Type: `boolean`<br>
|
|
45
50
|
Default: `true`
|
|
46
51
|
|
|
47
|
-
|
|
52
|
+
Prepends `defaultProtocol` to the URL if it's protocol-relative.
|
|
48
53
|
|
|
49
54
|
```js
|
|
50
55
|
normalizeUrl('//sindresorhus.com:80/');
|
|
@@ -54,12 +59,12 @@ normalizeUrl('//sindresorhus.com:80/', {normalizeProtocol: false});
|
|
|
54
59
|
//=> '//sindresorhus.com'
|
|
55
60
|
```
|
|
56
61
|
|
|
57
|
-
#####
|
|
62
|
+
##### forceHttp
|
|
58
63
|
|
|
59
64
|
Type: `boolean`<br>
|
|
60
65
|
Default: `false`
|
|
61
66
|
|
|
62
|
-
|
|
67
|
+
Normalizes `https:` URLs to `http:`.
|
|
63
68
|
|
|
64
69
|
```js
|
|
65
70
|
normalizeUrl('https://sindresorhus.com:80/');
|
|
@@ -69,12 +74,12 @@ normalizeUrl('https://sindresorhus.com:80/', {normalizeHttps: true});
|
|
|
69
74
|
//=> 'http://sindresorhus.com'
|
|
70
75
|
```
|
|
71
76
|
|
|
72
|
-
#####
|
|
77
|
+
##### forceHttps
|
|
73
78
|
|
|
74
79
|
Type: `boolean`<br>
|
|
75
80
|
Default: `false`
|
|
76
81
|
|
|
77
|
-
|
|
82
|
+
Normalizes `http:` URLs to `https:`.
|
|
78
83
|
|
|
79
84
|
```js
|
|
80
85
|
normalizeUrl('https://sindresorhus.com:80/');
|
|
@@ -84,20 +89,20 @@ normalizeUrl('http://sindresorhus.com:80/', {normalizeHttp: true});
|
|
|
84
89
|
//=> 'https://sindresorhus.com'
|
|
85
90
|
```
|
|
86
91
|
|
|
87
|
-
This option
|
|
92
|
+
This option can't be used with the `forceHttp` option at the same time.
|
|
88
93
|
|
|
89
|
-
#####
|
|
94
|
+
##### stripHash
|
|
90
95
|
|
|
91
96
|
Type: `boolean`<br>
|
|
92
97
|
Default: `true`
|
|
93
98
|
|
|
94
|
-
|
|
99
|
+
Removes hash from the URL.
|
|
95
100
|
|
|
96
101
|
```js
|
|
97
102
|
normalizeUrl('sindresorhus.com/about.html#contact');
|
|
98
103
|
//=> 'http://sindresorhus.com/about.html'
|
|
99
104
|
|
|
100
|
-
normalizeUrl('sindresorhus.com/about.html#contact', {
|
|
105
|
+
normalizeUrl('sindresorhus.com/about.html#contact', {stripHash: false});
|
|
101
106
|
//=> 'http://sindresorhus.com/about.html#contact'
|
|
102
107
|
```
|
|
103
108
|
|
|
@@ -106,7 +111,7 @@ normalizeUrl('sindresorhus.com/about.html#contact', {stripFragment: false});
|
|
|
106
111
|
Type: `boolean`<br>
|
|
107
112
|
Default: `true`
|
|
108
113
|
|
|
109
|
-
|
|
114
|
+
Removes `www.` from the URL.
|
|
110
115
|
|
|
111
116
|
```js
|
|
112
117
|
normalizeUrl('http://www.sindresorhus.com/about.html#contact');
|
|
@@ -121,7 +126,7 @@ normalizeUrl('http://www.sindresorhus.com/about.html#contact', {stripWWW: false}
|
|
|
121
126
|
Type: `Array<RegExp|string>`<br>
|
|
122
127
|
Default: `[/^utm_\w+/i]`
|
|
123
128
|
|
|
124
|
-
|
|
129
|
+
Removes query parameters that matches any of the provided strings or regexes.
|
|
125
130
|
|
|
126
131
|
```js
|
|
127
132
|
normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
|
|
@@ -135,7 +140,7 @@ normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
|
|
|
135
140
|
Type: `boolean`<br>
|
|
136
141
|
Default: `true`
|
|
137
142
|
|
|
138
|
-
|
|
143
|
+
Removes trailing slash.
|
|
139
144
|
|
|
140
145
|
**Note:** Trailing slash is always removed if the URL doesn't have a pathname.
|
|
141
146
|
|
|
@@ -155,7 +160,7 @@ normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
|
|
|
155
160
|
Type: `boolean` `Array<RegExp|string>`<br>
|
|
156
161
|
Default: `false`
|
|
157
162
|
|
|
158
|
-
|
|
163
|
+
Removes the default directory index file from path that matches any of the provided strings or regexes. When `true`, the regex `/^index\.[a-z]+$/` is used.
|
|
159
164
|
|
|
160
165
|
```js
|
|
161
166
|
normalizeUrl('www.sindresorhus.com/foo/default.php', {
|
|
@@ -169,7 +174,7 @@ normalizeUrl('www.sindresorhus.com/foo/default.php', {
|
|
|
169
174
|
Type: `boolean`<br>
|
|
170
175
|
Default: `true`
|
|
171
176
|
|
|
172
|
-
|
|
177
|
+
Sorts the query parameters alphabetically by key.
|
|
173
178
|
|
|
174
179
|
```js
|
|
175
180
|
normalizeUrl('www.sindresorhus.com?b=two&a=one&c=three', {
|