Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jul 9, 2021
1 parent 9a44d07 commit d112def
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
8 changes: 3 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
const DATA_URL_DEFAULT_MIME_TYPE = 'text/plain';
const DATA_URL_DEFAULT_CHARSET = 'us-ascii';

const testParameter = (name, filters) => {
return filters.some(filter => filter instanceof RegExp ? filter.test(name) : filter === name);
};
const testParameter = (name, filters) => filters.some(filter => filter instanceof RegExp ? filter.test(name) : filter === name);

const normalizeDataURL = (urlString, {stripHash}) => {
const match = /^data:(?<type>[^,]*?),(?<data>[^#]*?)(?:#(?<hash>.*))?$/.exec(urlString);
Expand Down Expand Up @@ -43,7 +41,7 @@ const normalizeDataURL = (urlString, {stripHash}) => {
.filter(Boolean);

const normalizedMediaType = [
...attributes
...attributes,
];

if (isBase64) {
Expand Down Expand Up @@ -72,7 +70,7 @@ export default function normalizeUrl(urlString, options) {
removeSingleSlash: true,
removeDirectoryIndex: false,
sortQueryParameters: true,
...options
...options,
};

urlString = urlString.trim();
Expand Down
12 changes: 6 additions & 6 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ normalizeUrl('sindresorhus.com/about.html#contact', {stripHash: true});
normalizeUrl('https://sindresorhus.com', {stripProtocol: true});
normalizeUrl('http://www.sindresorhus.com', {stripWWW: false});
normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
removeQueryParameters: ['ref', /test/]
removeQueryParameters: ['ref', /test/],
});
normalizeUrl('www.sindresorhus.com?foo=bar', {
removeQueryParameters: true
removeQueryParameters: true,
});
normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', {
removeQueryParameters: false
removeQueryParameters: false,
});
normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
normalizeUrl('http://sindresorhus.com/', {removeSingleSlash: false});
normalizeUrl('www.sindresorhus.com/foo/default.php', {
removeDirectoryIndex: [/^default\.[a-z]+$/, 'foo']
removeDirectoryIndex: [/^default\.[a-z]+$/, 'foo'],
});
normalizeUrl('www.sindresorhus.com?b=two&a=one&c=three', {
sortQueryParameters: false
sortQueryParameters: false,
});
normalizeUrl('www.sindresorhus.com/about#:~:text=hello', {
stripTextFragment: false
stripTextFragment: false,
});
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"ava": "^3.15.0",
"nyc": "^15.1.0",
"tsd": "^0.17.0",
"xo": "^0.40.3"
"xo": "^0.41.0"
},
"nyc": {
"reporter": [
Expand Down
32 changes: 17 additions & 15 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ test('stripWWW option', t => {
test('removeQueryParameters option', t => {
const options = {
stripWWW: false,
removeQueryParameters: [/^utm_\w+/i, 'ref']
removeQueryParameters: [/^utm_\w+/i, 'ref'],
};
t.is(normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test'), 'http://sindresorhus.com/?foo=bar');
t.is(normalizeUrl('http://www.sindresorhus.com', options), 'http://www.sindresorhus.com');
Expand All @@ -122,7 +122,7 @@ test('removeQueryParameters option', t => {
test('removeQueryParameters boolean `true` option', t => {
const options = {
stripWWW: false,
removeQueryParameters: true
removeQueryParameters: true,
};

t.is(normalizeUrl('http://www.sindresorhus.com', options), 'http://www.sindresorhus.com');
Expand All @@ -133,7 +133,7 @@ test('removeQueryParameters boolean `true` option', t => {
test('removeQueryParameters boolean `false` option', t => {
const options = {
stripWWW: false,
removeQueryParameters: false
removeQueryParameters: false,
};

t.is(normalizeUrl('http://www.sindresorhus.com', options), 'http://www.sindresorhus.com');
Expand All @@ -153,7 +153,7 @@ test('forceHttp option with forceHttps', t => {
t.throws(() => {
normalizeUrl('https://www.sindresorhus.com', {forceHttp: true, forceHttps: true});
}, {
message: 'The `forceHttp` and `forceHttps` options cannot be used together'
message: 'The `forceHttp` and `forceHttps` options cannot be used together',
});
});

Expand Down Expand Up @@ -231,7 +231,7 @@ test('removeDirectoryIndex option', t => {
test('removeTrailingSlash and removeDirectoryIndex options)', t => {
const options1 = {
removeTrailingSlash: true,
removeDirectoryIndex: true
removeDirectoryIndex: true,
};
t.is(normalizeUrl('http://sindresorhus.com/path/', options1), 'http://sindresorhus.com/path');
t.is(normalizeUrl('http://sindresorhus.com/path/index.html', options1), 'http://sindresorhus.com/path');
Expand All @@ -240,7 +240,7 @@ test('removeTrailingSlash and removeDirectoryIndex options)', t => {

const options2 = {
removeTrailingSlash: false,
removeDirectoryIndex: true
removeDirectoryIndex: true,
};
t.is(normalizeUrl('http://sindresorhus.com/path/', options2), 'http://sindresorhus.com/path/');
t.is(normalizeUrl('http://sindresorhus.com/path/index.html', options2), 'http://sindresorhus.com/path/');
Expand All @@ -249,15 +249,15 @@ test('removeTrailingSlash and removeDirectoryIndex options)', t => {

test('sortQueryParameters option', t => {
const options1 = {
sortQueryParameters: true
sortQueryParameters: true,
};
t.is(normalizeUrl('http://sindresorhus.com/?a=Z&b=Y&c=X&d=W', options1), 'http://sindresorhus.com/?a=Z&b=Y&c=X&d=W');
t.is(normalizeUrl('http://sindresorhus.com/?b=Y&c=X&a=Z&d=W', options1), 'http://sindresorhus.com/?a=Z&b=Y&c=X&d=W');
t.is(normalizeUrl('http://sindresorhus.com/?a=Z&d=W&b=Y&c=X', options1), 'http://sindresorhus.com/?a=Z&b=Y&c=X&d=W');
t.is(normalizeUrl('http://sindresorhus.com/', options1), 'http://sindresorhus.com');

const options2 = {
sortQueryParameters: false
sortQueryParameters: false,
};
t.is(normalizeUrl('http://sindresorhus.com/?a=Z&b=Y&c=X&d=W', options2), 'http://sindresorhus.com/?a=Z&b=Y&c=X&d=W');
t.is(normalizeUrl('http://sindresorhus.com/?b=Y&c=X&a=Z&d=W', options2), 'http://sindresorhus.com/?b=Y&c=X&a=Z&d=W');
Expand All @@ -269,19 +269,19 @@ test('invalid urls', t => {
t.throws(() => {
normalizeUrl('http://');
}, {
message: 'Invalid URL'
message: 'Invalid URL',
});

t.throws(() => {
normalizeUrl('/');
}, {
message: 'Invalid URL'
message: 'Invalid URL',
});

t.throws(() => {
normalizeUrl('/relative/path/');
}, {
message: 'Invalid URL'
message: 'Invalid URL',
});
});

Expand Down Expand Up @@ -309,8 +309,10 @@ test('remove duplicate pathname slashes', t => {

test('data URL', t => {
// Invalid URL.
t.throws(() => normalizeUrl('data:'), {
message: 'Invalid URL: data:'
t.throws(() => {
normalizeUrl('data:');
}, {
message: 'Invalid URL: data:',
});

// Strip default MIME type
Expand Down Expand Up @@ -357,7 +359,7 @@ test('data URL', t => {
removeQueryParameters: [/^utm_\w+/i, 'ref'],
sortQueryParameters: true,
removeTrailingSlash: true,
removeDirectoryIndex: true
removeDirectoryIndex: true,
};
t.is(normalizeUrl('data:,sindresorhus.com/', options), 'data:,sindresorhus.com/');
t.is(normalizeUrl('data:,sindresorhus.com/index.html', options), 'data:,sindresorhus.com/index.html');
Expand All @@ -375,7 +377,7 @@ test('view-source URL', t => {
t.throws(() => {
normalizeUrl('view-source:https://www.sindresorhus.com');
}, {
message: '`view-source:` is not supported as it is a non-standard protocol'
message: '`view-source:` is not supported as it is a non-standard protocol',
});
});

Expand Down

0 comments on commit d112def

Please sign in to comment.