Skip to content

Commit

Permalink
Make defaultProtocol option accept protocol without colon
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 4, 2022
1 parent 52f2290 commit c7fd781
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
12 changes: 5 additions & 7 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
export type Options = {
/**
@default 'http:'
Values: `'https:' | 'http:'`
@default 'http'
*/
readonly defaultProtocol?: string; // TODO: Make this `'https:' | 'http:'` in the next major version.
readonly defaultProtocol?: 'https' | 'http';

/**
Prepends `defaultProtocol` to the URL if it's protocol-relative.
Expand All @@ -23,7 +21,7 @@ export type Options = {
readonly normalizeProtocol?: boolean;

/**
Normalizes `https:` URLs to `http:`.
Normalizes HTTPS URLs to HTTP.
@default false
Expand All @@ -39,9 +37,9 @@ export type Options = {
readonly forceHttp?: boolean;

/**
Normalizes `http:` URLs to `https:`.
Normalizes HTTP URLs to HTTPS.
This option can't be used with the `forceHttp` option at the same time.
This option cannot be used with the `forceHttp` option at the same time.
@default false
Expand Down
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const normalizeDataURL = (urlString, {stripHash}) => {
}

// Lowercase MIME type
const mimeType = (mediaType.shift() || '').toLowerCase();
const mimeType = mediaType.shift()?.toLowerCase() ?? '';
const attributes = mediaType
.map(attribute => {
let [key, value = ''] = attribute.split('=').map(string => string.trim());
Expand Down Expand Up @@ -72,7 +72,7 @@ const normalizeDataURL = (urlString, {stripHash}) => {

export default function normalizeUrl(urlString, options) {
options = {
defaultProtocol: 'http:',
defaultProtocol: 'http',
normalizeProtocol: true,
forceHttp: false,
forceHttps: false,
Expand All @@ -89,6 +89,11 @@ export default function normalizeUrl(urlString, options) {
...options,
};

// Legacy: Append `:` to the protocol if missing.
if (typeof options.defaultProtocol === 'string' && !options.defaultProtocol.endsWith(':')) {
options.defaultProtocol = `${options.defaultProtocol}:`;
}

urlString = urlString.trim();

// Data URL
Expand Down
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import normalizeUrl from './index.js';
expectType<string>(normalizeUrl('sindresorhus.com'));
expectType<string>(normalizeUrl('HTTP://xn--xample-hva.com:80/?b=bar&a=foo'));

normalizeUrl('//sindresorhus.com:80/', {defaultProtocol: 'https:'});
normalizeUrl('//sindresorhus.com:80/', {defaultProtocol: 'https'});
normalizeUrl('//sindresorhus.com:80/', {normalizeProtocol: false});
normalizeUrl('https://sindresorhus.com:80/', {forceHttp: true});
normalizeUrl('http://sindresorhus.com:80/', {forceHttps: true});
Expand Down
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ Type: `object`
##### defaultProtocol

Type: `string`\
Default: `http:`\
Values: `'https:' | 'http:'`
Default: `'http'`\
Values: `'https' | 'http'`

##### normalizeProtocol

Expand All @@ -68,7 +68,7 @@ normalizeUrl('//sindresorhus.com', {normalizeProtocol: false});
Type: `boolean`\
Default: `false`

Normalize `https:` to `http:`.
Normalize HTTPS to HTTP.

```js
normalizeUrl('https://sindresorhus.com');
Expand All @@ -83,7 +83,7 @@ normalizeUrl('https://sindresorhus.com', {forceHttp: true});
Type: `boolean`\
Default: `false`

Normalize `http:` to `https:`.
Normalize HTTP to HTTPS.

```js
normalizeUrl('http://sindresorhus.com');
Expand All @@ -93,7 +93,7 @@ normalizeUrl('http://sindresorhus.com', {forceHttps: true});
//=> 'https://sindresorhus.com'
```

This option can't be used with the `forceHttp` option at the same time.
This option cannot be used with the `forceHttp` option at the same time.

##### stripAuthentication

Expand Down
12 changes: 10 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ test('main', t => {
t.is(normalizeUrl('sindresorhus.com '), 'http://sindresorhus.com');
t.is(normalizeUrl('sindresorhus.com.'), 'http://sindresorhus.com');
t.is(normalizeUrl('SindreSorhus.com'), 'http://sindresorhus.com');
t.is(normalizeUrl('sindresorhus.com', {defaultProtocol: 'https:'}), 'https://sindresorhus.com');
t.is(normalizeUrl('HTTP://sindresorhus.com'), 'http://sindresorhus.com');
t.is(normalizeUrl('//sindresorhus.com'), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com'), 'http://sindresorhus.com');
Expand Down Expand Up @@ -42,6 +41,15 @@ test('main', t => {
t.is(normalizeUrl('https://i.vimeocdn.com/filter/overlay?src0=https://i.vimeocdn.com/video/598160082_1280x720.jpg&src1=https://f.vimeocdn.com/images_v6/share/play_icon_overlay.png'), 'https://i.vimeocdn.com/filter/overlay?src0=https://i.vimeocdn.com/video/598160082_1280x720.jpg&src1=https://f.vimeocdn.com/images_v6/share/play_icon_overlay.png');
});

test('defaultProtocol option', t => {
t.is(normalizeUrl('sindresorhus.com', {defaultProtocol: 'https'}), 'https://sindresorhus.com');
t.is(normalizeUrl('sindresorhus.com', {defaultProtocol: 'http'}), 'http://sindresorhus.com');

// Legacy
t.is(normalizeUrl('sindresorhus.com', {defaultProtocol: 'https:'}), 'https://sindresorhus.com');
t.is(normalizeUrl('sindresorhus.com', {defaultProtocol: 'http:'}), 'http://sindresorhus.com');
});

test('stripAuthentication option', t => {
t.is(normalizeUrl('http://user:password@www.sindresorhus.com'), 'http://sindresorhus.com');
t.is(normalizeUrl('https://user:password@www.sindresorhus.com'), 'https://sindresorhus.com');
Expand Down Expand Up @@ -365,7 +373,7 @@ test('data URL', t => {

// Options.
const options = {
defaultProtocol: 'http:',
defaultProtocol: 'http',
normalizeProtocol: true,
forceHttp: true,
stripHash: true,
Expand Down

0 comments on commit c7fd781

Please sign in to comment.