From 192223b25569092ab89ab98afae98123d82fdf37 Mon Sep 17 00:00:00 2001 From: Ian Izaguirre Date: Tue, 27 Sep 2022 04:26:10 -0400 Subject: [PATCH] Add `removeExplicitPort` option (#174) --- index.d.ts | 17 +++++++++++++++++ index.js | 6 ++++++ index.test-d.ts | 1 + readme.md | 16 ++++++++++++++++ test.js | 8 ++++++++ 5 files changed, 48 insertions(+) diff --git a/index.d.ts b/index.d.ts index ee5d112..bf356b3 100644 --- a/index.d.ts +++ b/index.d.ts @@ -249,6 +249,23 @@ export interface Options { */ readonly removeDirectoryIndex?: boolean | ReadonlyArray; + /** + 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. diff --git a/index.js b/index.js index ceca8cb..33203d8 100644 --- a/index.js +++ b/index.js @@ -69,6 +69,7 @@ export default function normalizeUrl(urlString, options) { removeTrailingSlash: true, removeSingleSlash: true, removeDirectoryIndex: false, + removeExplicitPort: false, sortQueryParameters: true, ...options, }; @@ -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 diff --git a/index.test-d.ts b/index.test-d.ts index 8898dce..53c17b2 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -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, }); diff --git a/readme.md b/readme.md index 318c0f5..c330903 100644 --- a/readme.md +++ b/readme.md @@ -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`\ diff --git a/test.js b/test.js index df18e49..902ecf1 100644 --- a/test.js +++ b/test.js @@ -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');