normalize-url 1.6.1 → 1.9.1
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 +29 -3
- package/package.json +3 -3
- package/readme.md +34 -5
package/index.js
CHANGED
|
@@ -26,7 +26,7 @@ var slashedProtocol = {
|
|
|
26
26
|
'file:': true
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
-
function
|
|
29
|
+
function testParameter(name, filters) {
|
|
30
30
|
return filters.some(function (filter) {
|
|
31
31
|
return filter instanceof RegExp ? filter.test(name) : filter === name;
|
|
32
32
|
});
|
|
@@ -35,10 +35,12 @@ function testQueryParameter(name, filters) {
|
|
|
35
35
|
module.exports = function (str, opts) {
|
|
36
36
|
opts = objectAssign({
|
|
37
37
|
normalizeProtocol: true,
|
|
38
|
+
normalizeHttps: false,
|
|
38
39
|
stripFragment: true,
|
|
39
40
|
stripWWW: true,
|
|
40
41
|
removeQueryParameters: [/^utm_\w+/i],
|
|
41
|
-
removeTrailingSlash: true
|
|
42
|
+
removeTrailingSlash: true,
|
|
43
|
+
removeDirectoryIndex: false
|
|
42
44
|
}, opts);
|
|
43
45
|
|
|
44
46
|
if (typeof str !== 'string') {
|
|
@@ -52,6 +54,10 @@ module.exports = function (str, opts) {
|
|
|
52
54
|
|
|
53
55
|
var urlObj = url.parse(str);
|
|
54
56
|
|
|
57
|
+
if (opts.normalizeHttps && urlObj.protocol === 'https:') {
|
|
58
|
+
urlObj.protocol = 'http:';
|
|
59
|
+
}
|
|
60
|
+
|
|
55
61
|
if (!urlObj.hostname && !urlObj.pathname) {
|
|
56
62
|
throw new Error('Invalid URL');
|
|
57
63
|
}
|
|
@@ -76,6 +82,26 @@ module.exports = function (str, opts) {
|
|
|
76
82
|
urlObj.pathname = urlObj.pathname.replace(/\/{2,}/g, '/');
|
|
77
83
|
}
|
|
78
84
|
|
|
85
|
+
// decode URI octets
|
|
86
|
+
if (urlObj.pathname) {
|
|
87
|
+
urlObj.pathname = decodeURI(urlObj.pathname);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// remove directory index
|
|
91
|
+
if (opts.removeDirectoryIndex === true) {
|
|
92
|
+
opts.removeDirectoryIndex = [/^index\.[a-z]+$/];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (Array.isArray(opts.removeDirectoryIndex) && opts.removeDirectoryIndex.length) {
|
|
96
|
+
var pathComponents = urlObj.pathname.split('/');
|
|
97
|
+
var lastComponent = pathComponents[pathComponents.length - 1];
|
|
98
|
+
|
|
99
|
+
if (testParameter(lastComponent, opts.removeDirectoryIndex)) {
|
|
100
|
+
pathComponents = pathComponents.slice(0, pathComponents.length - 1);
|
|
101
|
+
urlObj.pathname = pathComponents.slice(1).join('/') + '/';
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
79
105
|
// resolve relative paths, but only for slashed protocols
|
|
80
106
|
if (slashedProtocol[urlObj.protocol]) {
|
|
81
107
|
var domain = urlObj.protocol + '//' + urlObj.hostname;
|
|
@@ -106,7 +132,7 @@ module.exports = function (str, opts) {
|
|
|
106
132
|
// remove query unwanted parameters
|
|
107
133
|
if (Array.isArray(opts.removeQueryParameters)) {
|
|
108
134
|
for (var key in queryParameters) {
|
|
109
|
-
if (
|
|
135
|
+
if (testParameter(key, opts.removeQueryParameters)) {
|
|
110
136
|
delete queryParameters[key];
|
|
111
137
|
}
|
|
112
138
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "normalize-url",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.1",
|
|
4
4
|
"description": "Normalize a URL",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/normalize-url",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"url": "sindresorhus.com"
|
|
11
11
|
},
|
|
12
12
|
"engines": {
|
|
13
|
-
"node": ">=
|
|
13
|
+
"node": ">=4"
|
|
14
14
|
},
|
|
15
15
|
"scripts": {
|
|
16
16
|
"test": "xo && ava"
|
|
@@ -45,6 +45,6 @@
|
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"ava": "*",
|
|
48
|
-
"xo": "
|
|
48
|
+
"xo": "^0.16.0"
|
|
49
49
|
}
|
|
50
50
|
}
|
package/readme.md
CHANGED
|
@@ -31,7 +31,7 @@ normalizeUrl('HTTP://xn--xample-hva.com:80/?b=bar&a=foo');
|
|
|
31
31
|
|
|
32
32
|
#### url
|
|
33
33
|
|
|
34
|
-
Type: `
|
|
34
|
+
Type: `string`
|
|
35
35
|
|
|
36
36
|
URL to normalize.
|
|
37
37
|
|
|
@@ -39,7 +39,7 @@ URL to normalize.
|
|
|
39
39
|
|
|
40
40
|
##### normalizeProtocol
|
|
41
41
|
|
|
42
|
-
Type: `
|
|
42
|
+
Type: `boolean`<br>
|
|
43
43
|
Default: `true`
|
|
44
44
|
|
|
45
45
|
Prepend `http:` to the URL if it's protocol-relative.
|
|
@@ -52,9 +52,24 @@ normalizeUrl('//sindresorhus.com:80/', {normalizeProtocol: false});
|
|
|
52
52
|
//=> '//sindresorhus.com'
|
|
53
53
|
```
|
|
54
54
|
|
|
55
|
+
##### normalizeHttps
|
|
56
|
+
|
|
57
|
+
Type: `boolean`<br>
|
|
58
|
+
Default: `false`
|
|
59
|
+
|
|
60
|
+
Normalize `https:` URLs to `http:`.
|
|
61
|
+
|
|
62
|
+
```js
|
|
63
|
+
normalizeUrl('https://sindresorhus.com:80/');
|
|
64
|
+
//=> 'https://sindresorhus.com'
|
|
65
|
+
|
|
66
|
+
normalizeUrl('https://sindresorhus.com:80/', {normalizeHttps: true});
|
|
67
|
+
//=> 'http://sindresorhus.com'
|
|
68
|
+
```
|
|
69
|
+
|
|
55
70
|
##### stripFragment
|
|
56
71
|
|
|
57
|
-
Type: `
|
|
72
|
+
Type: `boolean`<br>
|
|
58
73
|
Default: `true`
|
|
59
74
|
|
|
60
75
|
Remove the fragment at the end of the URL.
|
|
@@ -69,7 +84,7 @@ normalizeUrl('sindresorhus.com/about.html#contact', {stripFragment: false});
|
|
|
69
84
|
|
|
70
85
|
##### stripWWW
|
|
71
86
|
|
|
72
|
-
Type: `
|
|
87
|
+
Type: `boolean`<br>
|
|
73
88
|
Default: `true`
|
|
74
89
|
|
|
75
90
|
Remove `www.` from the URL.
|
|
@@ -84,7 +99,7 @@ normalizeUrl('http://www.sindresorhus.com/about.html#contact', {stripWWW: false}
|
|
|
84
99
|
|
|
85
100
|
##### removeQueryParameters
|
|
86
101
|
|
|
87
|
-
Type: `Array<RegExp|
|
|
102
|
+
Type: `Array<RegExp|string>`<br>
|
|
88
103
|
Default: `[/^utm_\w+/i]`
|
|
89
104
|
|
|
90
105
|
Remove query parameters that matches any of the provided strings or regexes.
|
|
@@ -116,6 +131,20 @@ normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
|
|
|
116
131
|
//=> 'http://sindresorhus.com'
|
|
117
132
|
```
|
|
118
133
|
|
|
134
|
+
##### removeDirectoryIndex
|
|
135
|
+
|
|
136
|
+
Type: `boolean` `Array<RegExp|string>`<br>
|
|
137
|
+
Default: `false`
|
|
138
|
+
|
|
139
|
+
Remove 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.
|
|
140
|
+
|
|
141
|
+
```js
|
|
142
|
+
normalizeUrl('www.sindresorhus.com/foo/default.php', {
|
|
143
|
+
removeDirectoryIndex: [/^default\.[a-z]+$/]
|
|
144
|
+
});
|
|
145
|
+
//=> 'http://sindresorhus.com/foo'
|
|
146
|
+
```
|
|
147
|
+
|
|
119
148
|
|
|
120
149
|
## Related
|
|
121
150
|
|