Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add remove explicit port option #174

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,22 @@ export interface Options {
*/
readonly removeDirectoryIndex?: boolean | ReadonlyArray<RegExp | string>;

/**
Removes a port number corresponding to the protocol part of a url, excluding a default port
number e.g., 80 for http, and 443 for https.

@default false

@example
```
normalizeUrl('sindresorhus.com:123', {
removeExplicitPort: true
});
//=> '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
28 changes: 28 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,34 @@ normalizeUrl('www.sindresorhus.com/foo/default.php', {
//=> 'http://sindresorhus.com/foo'
```

##### removeExplicitPort

Type: `boolean`\
Default: `false`

Removes a port number corresponding to the protocol part of a url, excluding a default port number e.g., 80 for http, and 443 for https.
ianizaguirre marked this conversation as resolved.
Show resolved Hide resolved

```js
normalizeUrl('sindresorhus.com:123', {
removeExplicitPort: true
});
//=> 'sindresorhus.com'
ianizaguirre marked this conversation as resolved.
Show resolved Hide resolved
```

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

```js
normalizeUrl('https://sindresorhus.com:80', {
removeExplicitPort: true
});
//=> 'https://sindresorhus.com'
```
ianizaguirre marked this conversation as resolved.
Show resolved Hide resolved

##### 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