Skip to content

Commit c7fd781

Browse files
committed
Make defaultProtocol option accept protocol without colon
1 parent 52f2290 commit c7fd781

File tree

5 files changed

+28
-17
lines changed

5 files changed

+28
-17
lines changed

index.d.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
export type Options = {
22
/**
3-
@default 'http:'
4-
5-
Values: `'https:' | 'http:'`
3+
@default 'http'
64
*/
7-
readonly defaultProtocol?: string; // TODO: Make this `'https:' | 'http:'` in the next major version.
5+
readonly defaultProtocol?: 'https' | 'http';
86

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

2523
/**
26-
Normalizes `https:` URLs to `http:`.
24+
Normalizes HTTPS URLs to HTTP.
2725
2826
@default false
2927
@@ -39,9 +37,9 @@ export type Options = {
3937
readonly forceHttp?: boolean;
4038

4139
/**
42-
Normalizes `http:` URLs to `https:`.
40+
Normalizes HTTP URLs to HTTPS.
4341
44-
This option can't be used with the `forceHttp` option at the same time.
42+
This option cannot be used with the `forceHttp` option at the same time.
4543
4644
@default false
4745

index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const normalizeDataURL = (urlString, {stripHash}) => {
3737
}
3838

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

7373
export default function normalizeUrl(urlString, options) {
7474
options = {
75-
defaultProtocol: 'http:',
75+
defaultProtocol: 'http',
7676
normalizeProtocol: true,
7777
forceHttp: false,
7878
forceHttps: false,
@@ -89,6 +89,11 @@ export default function normalizeUrl(urlString, options) {
8989
...options,
9090
};
9191

92+
// Legacy: Append `:` to the protocol if missing.
93+
if (typeof options.defaultProtocol === 'string' && !options.defaultProtocol.endsWith(':')) {
94+
options.defaultProtocol = `${options.defaultProtocol}:`;
95+
}
96+
9297
urlString = urlString.trim();
9398

9499
// Data URL

index.test-d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import normalizeUrl from './index.js';
44
expectType<string>(normalizeUrl('sindresorhus.com'));
55
expectType<string>(normalizeUrl('HTTP://xn--xample-hva.com:80/?b=bar&a=foo'));
66

7-
normalizeUrl('//sindresorhus.com:80/', {defaultProtocol: 'https:'});
7+
normalizeUrl('//sindresorhus.com:80/', {defaultProtocol: 'https'});
88
normalizeUrl('//sindresorhus.com:80/', {normalizeProtocol: false});
99
normalizeUrl('https://sindresorhus.com:80/', {forceHttp: true});
1010
normalizeUrl('http://sindresorhus.com:80/', {forceHttps: true});

readme.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ Type: `object`
4545
##### defaultProtocol
4646

4747
Type: `string`\
48-
Default: `http:`\
49-
Values: `'https:' | 'http:'`
48+
Default: `'http'`\
49+
Values: `'https' | 'http'`
5050

5151
##### normalizeProtocol
5252

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

71-
Normalize `https:` to `http:`.
71+
Normalize HTTPS to HTTP.
7272

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

86-
Normalize `http:` to `https:`.
86+
Normalize HTTP to HTTPS.
8787

8888
```js
8989
normalizeUrl('http://sindresorhus.com');
@@ -93,7 +93,7 @@ normalizeUrl('http://sindresorhus.com', {forceHttps: true});
9393
//=> 'https://sindresorhus.com'
9494
```
9595

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

9898
##### stripAuthentication
9999

test.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ test('main', t => {
66
t.is(normalizeUrl('sindresorhus.com '), 'http://sindresorhus.com');
77
t.is(normalizeUrl('sindresorhus.com.'), 'http://sindresorhus.com');
88
t.is(normalizeUrl('SindreSorhus.com'), 'http://sindresorhus.com');
9-
t.is(normalizeUrl('sindresorhus.com', {defaultProtocol: 'https:'}), 'https://sindresorhus.com');
109
t.is(normalizeUrl('HTTP://sindresorhus.com'), 'http://sindresorhus.com');
1110
t.is(normalizeUrl('//sindresorhus.com'), 'http://sindresorhus.com');
1211
t.is(normalizeUrl('http://sindresorhus.com'), 'http://sindresorhus.com');
@@ -42,6 +41,15 @@ test('main', t => {
4241
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');
4342
});
4443

44+
test('defaultProtocol option', t => {
45+
t.is(normalizeUrl('sindresorhus.com', {defaultProtocol: 'https'}), 'https://sindresorhus.com');
46+
t.is(normalizeUrl('sindresorhus.com', {defaultProtocol: 'http'}), 'http://sindresorhus.com');
47+
48+
// Legacy
49+
t.is(normalizeUrl('sindresorhus.com', {defaultProtocol: 'https:'}), 'https://sindresorhus.com');
50+
t.is(normalizeUrl('sindresorhus.com', {defaultProtocol: 'http:'}), 'http://sindresorhus.com');
51+
});
52+
4553
test('stripAuthentication option', t => {
4654
t.is(normalizeUrl('http://user:[email protected]'), 'http://sindresorhus.com');
4755
t.is(normalizeUrl('https://user:[email protected]'), 'https://sindresorhus.com');
@@ -365,7 +373,7 @@ test('data URL', t => {
365373

366374
// Options.
367375
const options = {
368-
defaultProtocol: 'http:',
376+
defaultProtocol: 'http',
369377
normalizeProtocol: true,
370378
forceHttp: true,
371379
stripHash: true,

0 commit comments

Comments
 (0)