Skip to content

Commit 192223b

Browse files
authored
Add removeExplicitPort option (#174)
1 parent 13a2b88 commit 192223b

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

index.d.ts

+17
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,23 @@ export interface Options {
249249
*/
250250
readonly removeDirectoryIndex?: boolean | ReadonlyArray<RegExp | string>;
251251

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+
252269
/**
253270
Sorts the query parameters alphabetically by key.
254271

index.js

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export default function normalizeUrl(urlString, options) {
6969
removeTrailingSlash: true,
7070
removeSingleSlash: true,
7171
removeDirectoryIndex: false,
72+
removeExplicitPort: false,
7273
sortQueryParameters: true,
7374
...options,
7475
};
@@ -228,6 +229,11 @@ export default function normalizeUrl(urlString, options) {
228229
urlObject.pathname = urlObject.pathname.replace(/\/$/, '');
229230
}
230231

232+
// Remove an explicit port number, excluding a default port number, if applicable
233+
if (options.removeExplicitPort && urlObject.port) {
234+
urlObject.port = '';
235+
}
236+
231237
const oldUrlString = urlString;
232238

233239
// Take advantage of many of the Node `url` normalizations

index.test-d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ normalizeUrl('http://sindresorhus.com/', {removeSingleSlash: false});
2929
normalizeUrl('www.sindresorhus.com/foo/default.php', {
3030
removeDirectoryIndex: [/^default\.[a-z]+$/, 'foo'],
3131
});
32+
normalizeUrl('www.sindresorhus.com/', {removeExplicitPort: false});
3233
normalizeUrl('www.sindresorhus.com?b=two&a=one&c=three', {
3334
sortQueryParameters: false,
3435
});

readme.md

+16
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,22 @@ normalizeUrl('www.sindresorhus.com/foo/default.php', {
275275
//=> 'http://sindresorhus.com/foo'
276276
```
277277

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+
278294
##### sortQueryParameters
279295

280296
Type: `boolean`\

test.js

+8
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ test('removeTrailingSlash option', t => {
192192
t.is(normalizeUrl('http://sindresorhus.com/?unicorns=true', options), 'http://sindresorhus.com/?unicorns=true');
193193
});
194194

195+
test('removeExplicitPort option', t => {
196+
const options = {removeExplicitPort: true};
197+
t.is(normalizeUrl('http://sindresorhus.com:123', options), 'http://sindresorhus.com');
198+
t.is(normalizeUrl('https://sindresorhus.com:123', options), 'https://sindresorhus.com');
199+
t.is(normalizeUrl('http://sindresorhus.com:443', options), 'http://sindresorhus.com');
200+
t.is(normalizeUrl('https://sindresorhus.com:80', options), 'https://sindresorhus.com');
201+
});
202+
195203
test('removeSingleSlash option', t => {
196204
const options = {removeSingleSlash: false};
197205
t.is(normalizeUrl('https://sindresorhus.com', options), 'https://sindresorhus.com');

0 commit comments

Comments
 (0)