Skip to content

Commit

Permalink
Add removeExplicitPort option (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianizaguirre authored Sep 27, 2022
1 parent 13a2b88 commit 192223b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
17 changes: 17 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,23 @@ export interface Options {
*/
readonly removeDirectoryIndex?: boolean | ReadonlyArray<RegExp | string>;

/**
Removes an explicit port number from the URL.
Port 443 is always removed from HTTPS URLs and 80 is always removed from HTTP URLs regardless of this option.
@default false
@example
```
normalizeUrl('sindresorhus.com:123', {
removeExplicitPort: true
});
//=> 'http://sindresorhus.com'
```
*/
readonly removeExplicitPort?: boolean;

/**
Sorts the query parameters alphabetically by key.
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export default function normalizeUrl(urlString, options) {
removeTrailingSlash: true,
removeSingleSlash: true,
removeDirectoryIndex: false,
removeExplicitPort: false,
sortQueryParameters: true,
...options,
};
Expand Down Expand Up @@ -228,6 +229,11 @@ export default function normalizeUrl(urlString, options) {
urlObject.pathname = urlObject.pathname.replace(/\/$/, '');
}

// Remove an explicit port number, excluding a default port number, if applicable
if (options.removeExplicitPort && urlObject.port) {
urlObject.port = '';
}

const oldUrlString = urlString;

// Take advantage of many of the Node `url` normalizations
Expand Down
1 change: 1 addition & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ normalizeUrl('http://sindresorhus.com/', {removeSingleSlash: false});
normalizeUrl('www.sindresorhus.com/foo/default.php', {
removeDirectoryIndex: [/^default\.[a-z]+$/, 'foo'],
});
normalizeUrl('www.sindresorhus.com/', {removeExplicitPort: false});
normalizeUrl('www.sindresorhus.com?b=two&a=one&c=three', {
sortQueryParameters: false,
});
Expand Down
16 changes: 16 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,22 @@ normalizeUrl('www.sindresorhus.com/foo/default.php', {
//=> 'http://sindresorhus.com/foo'
```

##### removeExplicitPort

Type: `boolean`\
Default: `false`

Removes an explicit port number from the URL.

Port 443 is always removed from HTTPS URLs and 80 is always removed from HTTP URLs regardless of this option.

```js
normalizeUrl('sindresorhus.com:123', {
removeExplicitPort: true
});
//=> 'http://sindresorhus.com'
```

##### sortQueryParameters

Type: `boolean`\
Expand Down
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ test('removeTrailingSlash option', t => {
t.is(normalizeUrl('http://sindresorhus.com/?unicorns=true', options), 'http://sindresorhus.com/?unicorns=true');
});

test('removeExplicitPort option', t => {
const options = {removeExplicitPort: true};
t.is(normalizeUrl('http://sindresorhus.com:123', options), 'http://sindresorhus.com');
t.is(normalizeUrl('https://sindresorhus.com:123', options), 'https://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com:443', options), 'http://sindresorhus.com');
t.is(normalizeUrl('https://sindresorhus.com:80', options), 'https://sindresorhus.com');
});

test('removeSingleSlash option', t => {
const options = {removeSingleSlash: false};
t.is(normalizeUrl('https://sindresorhus.com', options), 'https://sindresorhus.com');
Expand Down

0 comments on commit 192223b

Please sign in to comment.