normalize-url 5.1.0 → 5.3.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.d.ts CHANGED
@@ -137,7 +137,7 @@ declare namespace normalizeUrl {
137
137
  /**
138
138
  Removes trailing slash.
139
139
 
140
- __Note__: Trailing slash is always removed if the URL doesn't have a pathname.
140
+ __Note__: Trailing slash is always removed if the URL doesn't have a pathname unless the `removeSingleSlash` option is set to `false`.
141
141
 
142
142
  @default true
143
143
 
@@ -155,6 +155,22 @@ declare namespace normalizeUrl {
155
155
  */
156
156
  readonly removeTrailingSlash?: boolean;
157
157
 
158
+ /**
159
+ Remove a sole `/` pathname in the output. This option is independant of `removeTrailingSlash`.
160
+
161
+ @default true
162
+
163
+ @example
164
+ ```
165
+ normalizeUrl('https://sindresorhus.com/');
166
+ //=> 'https://sindresorhus.com'
167
+
168
+ normalizeUrl('https://sindresorhus.com/', {removeSingleSlash: false});
169
+ //=> 'https://sindresorhus.com/'
170
+ ```
171
+ */
172
+ readonly removeSingleSlash?: boolean;
173
+
158
174
  /**
159
175
  Removes the default directory index file from path that matches any of the provided strings or regexes.
160
176
  When `true`, the regex `/^index\.[a-z]+$/` is used.
package/index.js CHANGED
@@ -9,7 +9,7 @@ const testParameter = (name, filters) => {
9
9
  };
10
10
 
11
11
  const normalizeDataURL = (urlString, {stripHash}) => {
12
- const match = /^data:(?<type>.*?),(?<data>.*?)(?:#(?<hash>.*))?$/.exec(urlString);
12
+ const match = /^data:(?<type>[^,]*?),(?<data>[^#]*?)(?:#(?<hash>.*))?$/.exec(urlString);
13
13
 
14
14
  if (!match) {
15
15
  throw new Error(`Invalid URL: ${urlString}`);
@@ -70,6 +70,7 @@ const normalizeUrl = (urlString, options) => {
70
70
  stripWWW: true,
71
71
  removeQueryParameters: [/^utm_\w+/i],
72
72
  removeTrailingSlash: true,
73
+ removeSingleSlash: true,
73
74
  removeDirectoryIndex: false,
74
75
  sortQueryParameters: true,
75
76
  ...options
@@ -82,6 +83,10 @@ const normalizeUrl = (urlString, options) => {
82
83
  return normalizeDataURL(urlString, options);
83
84
  }
84
85
 
86
+ if (/^view-source:/i.test(urlString)) {
87
+ throw new Error('`view-source:` is not supported as it is a non-standard protocol');
88
+ }
89
+
85
90
  const hasRelativeProtocol = urlString.startsWith('//');
86
91
  const isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString);
87
92
 
@@ -174,11 +179,17 @@ const normalizeUrl = (urlString, options) => {
174
179
  urlObj.pathname = urlObj.pathname.replace(/\/$/, '');
175
180
  }
176
181
 
182
+ const oldUrlString = urlString;
183
+
177
184
  // Take advantage of many of the Node `url` normalizations
178
185
  urlString = urlObj.toString();
179
186
 
180
- // Remove ending `/`
181
- if ((options.removeTrailingSlash || urlObj.pathname === '/') && urlObj.hash === '') {
187
+ if (!options.removeSingleSlash && urlObj.pathname === '/' && !oldUrlString.endsWith('/') && urlObj.hash === '') {
188
+ urlString = urlString.replace(/\/$/, '');
189
+ }
190
+
191
+ // Remove ending `/` unless removeSingleSlash is false
192
+ if ((options.removeTrailingSlash || urlObj.pathname === '/') && urlObj.hash === '' && options.removeSingleSlash) {
182
193
  urlString = urlString.replace(/\/$/, '');
183
194
  }
184
195
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "normalize-url",
3
- "version": "5.1.0",
3
+ "version": "5.3.1",
4
4
  "description": "Normalize a URL",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/normalize-url",
package/readme.md CHANGED
@@ -171,7 +171,7 @@ Default: `true`
171
171
 
172
172
  Remove trailing slash.
173
173
 
174
- **Note:** Trailing slash is always removed if the URL doesn't have a pathname.
174
+ **Note:** Trailing slash is always removed if the URL doesn't have a pathname unless the `removeSingleSlash` option is set to `false`.
175
175
 
176
176
  ```js
177
177
  normalizeUrl('http://sindresorhus.com/redirect/');
@@ -184,6 +184,22 @@ normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
184
184
  //=> 'http://sindresorhus.com'
185
185
  ```
186
186
 
187
+ ##### removeSingleSlash
188
+
189
+ Type: `boolean`\
190
+ Default: `true`
191
+
192
+ Remove a sole `/` pathname in the output. This option is independant of `removeTrailingSlash`.
193
+
194
+ ```js
195
+ normalizeUrl('https://sindresorhus.com/');
196
+ //=> 'https://sindresorhus.com'
197
+
198
+ normalizeUrl('https://sindresorhus.com/', {removeSingleSlash: false});
199
+ //=> 'https://sindresorhus.com/'
200
+ ```
201
+
202
+
187
203
  ##### removeDirectoryIndex
188
204
 
189
205
  Type: `boolean | Array<RegExp | string>`\