Skip to content

Commit

Permalink
Fix removeSingleSlash option adding slashes (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed Sep 30, 2020
1 parent 035acaf commit 1e06753
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.js
Expand Up @@ -175,9 +175,15 @@ const normalizeUrl = (urlString, options) => {
urlObj.pathname = urlObj.pathname.replace(/\/$/, '');
}

const oldUrlString = urlString;

// Take advantage of many of the Node `url` normalizations
urlString = urlObj.toString();

if (!options.removeSingleSlash && urlObj.pathname === '/' && !oldUrlString.endsWith('/') && urlObj.hash === '') {
urlString = urlString.replace(/\/$/, '');
}

// Remove ending `/` unless removeSingleSlash is false
if ((options.removeTrailingSlash || urlObj.pathname === '/') && urlObj.hash === '' && options.removeSingleSlash) {
urlString = urlString.replace(/\/$/, '');
Expand Down
1 change: 1 addition & 0 deletions index.test-d.ts
Expand Up @@ -16,6 +16,7 @@ normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
removeQueryParameters: ['ref', /test/]
});
normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
normalizeUrl('http://sindresorhus.com/', {removeSingleSlash: false});
normalizeUrl('www.sindresorhus.com/foo/default.php', {
removeDirectoryIndex: [/^default\.[a-z]+$/, 'foo']
});
Expand Down
9 changes: 9 additions & 0 deletions test.js
Expand Up @@ -119,26 +119,35 @@ test('forceHttps option', t => {

test('removeTrailingSlash option', t => {
const options = {removeTrailingSlash: false};
t.is(normalizeUrl('http://sindresorhus.com'), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/'), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com', options), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/', options), 'http://sindresorhus.com');
t.is(normalizeUrl('http://sindresorhus.com/redirect'), 'http://sindresorhus.com/redirect');
t.is(normalizeUrl('http://sindresorhus.com/redirect/'), 'http://sindresorhus.com/redirect');
t.is(normalizeUrl('http://sindresorhus.com/redirect/', options), 'http://sindresorhus.com/redirect/');
t.is(normalizeUrl('http://sindresorhus.com/redirect/', options), 'http://sindresorhus.com/redirect/');
t.is(normalizeUrl('http://sindresorhus.com/#/'), 'http://sindresorhus.com/#/');
t.is(normalizeUrl('http://sindresorhus.com/#/', options), 'http://sindresorhus.com/#/');
t.is(normalizeUrl('http://sindresorhus.com/?unicorns=true'), 'http://sindresorhus.com/?unicorns=true');
t.is(normalizeUrl('http://sindresorhus.com/?unicorns=true', options), 'http://sindresorhus.com/?unicorns=true');
});

test('removeSingleSlash option', t => {
const options = {removeSingleSlash: false};
t.is(normalizeUrl('https://sindresorhus.com', options), 'https://sindresorhus.com');
t.is(normalizeUrl('https://sindresorhus.com/', options), 'https://sindresorhus.com/');
t.is(normalizeUrl('https://sindresorhus.com/redirect', options), 'https://sindresorhus.com/redirect');
t.is(normalizeUrl('https://sindresorhus.com/redirect/', options), 'https://sindresorhus.com/redirect');
t.is(normalizeUrl('https://sindresorhus.com/#/', options), 'https://sindresorhus.com/#/');
t.is(normalizeUrl('https://sindresorhus.com/?unicorns=true', options), 'https://sindresorhus.com/?unicorns=true');
});

test('removeSingleSlash option combined with removeTrailingSlash option', t => {
const options = {removeTrailingSlash: false, removeSingleSlash: false};
t.is(normalizeUrl('https://sindresorhus.com', options), 'https://sindresorhus.com');
t.is(normalizeUrl('https://sindresorhus.com/', options), 'https://sindresorhus.com/');
t.is(normalizeUrl('https://sindresorhus.com/redirect', options), 'https://sindresorhus.com/redirect');
t.is(normalizeUrl('https://sindresorhus.com/redirect/', options), 'https://sindresorhus.com/redirect/');
t.is(normalizeUrl('https://sindresorhus.com/#/', options), 'https://sindresorhus.com/#/');
t.is(normalizeUrl('https://sindresorhus.com/?unicorns=true', options), 'https://sindresorhus.com/?unicorns=true');
Expand Down

0 comments on commit 1e06753

Please sign in to comment.