Skip to content

Commit 1e06753

Browse files
authored
Fix removeSingleSlash option adding slashes (#122)
1 parent 035acaf commit 1e06753

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,15 @@ const normalizeUrl = (urlString, options) => {
175175
urlObj.pathname = urlObj.pathname.replace(/\/$/, '');
176176
}
177177

178+
const oldUrlString = urlString;
179+
178180
// Take advantage of many of the Node `url` normalizations
179181
urlString = urlObj.toString();
180182

183+
if (!options.removeSingleSlash && urlObj.pathname === '/' && !oldUrlString.endsWith('/') && urlObj.hash === '') {
184+
urlString = urlString.replace(/\/$/, '');
185+
}
186+
181187
// Remove ending `/` unless removeSingleSlash is false
182188
if ((options.removeTrailingSlash || urlObj.pathname === '/') && urlObj.hash === '' && options.removeSingleSlash) {
183189
urlString = urlString.replace(/\/$/, '');

index.test-d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
1616
removeQueryParameters: ['ref', /test/]
1717
});
1818
normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
19+
normalizeUrl('http://sindresorhus.com/', {removeSingleSlash: false});
1920
normalizeUrl('www.sindresorhus.com/foo/default.php', {
2021
removeDirectoryIndex: [/^default\.[a-z]+$/, 'foo']
2122
});

test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,26 +119,35 @@ test('forceHttps option', t => {
119119

120120
test('removeTrailingSlash option', t => {
121121
const options = {removeTrailingSlash: false};
122+
t.is(normalizeUrl('http://sindresorhus.com'), 'http://sindresorhus.com');
122123
t.is(normalizeUrl('http://sindresorhus.com/'), 'http://sindresorhus.com');
124+
t.is(normalizeUrl('http://sindresorhus.com', options), 'http://sindresorhus.com');
123125
t.is(normalizeUrl('http://sindresorhus.com/', options), 'http://sindresorhus.com');
126+
t.is(normalizeUrl('http://sindresorhus.com/redirect'), 'http://sindresorhus.com/redirect');
124127
t.is(normalizeUrl('http://sindresorhus.com/redirect/'), 'http://sindresorhus.com/redirect');
125128
t.is(normalizeUrl('http://sindresorhus.com/redirect/', options), 'http://sindresorhus.com/redirect/');
129+
t.is(normalizeUrl('http://sindresorhus.com/redirect/', options), 'http://sindresorhus.com/redirect/');
130+
t.is(normalizeUrl('http://sindresorhus.com/#/'), 'http://sindresorhus.com/#/');
126131
t.is(normalizeUrl('http://sindresorhus.com/#/', options), 'http://sindresorhus.com/#/');
127132
t.is(normalizeUrl('http://sindresorhus.com/?unicorns=true'), 'http://sindresorhus.com/?unicorns=true');
128133
t.is(normalizeUrl('http://sindresorhus.com/?unicorns=true', options), 'http://sindresorhus.com/?unicorns=true');
129134
});
130135

131136
test('removeSingleSlash option', t => {
132137
const options = {removeSingleSlash: false};
138+
t.is(normalizeUrl('https://sindresorhus.com', options), 'https://sindresorhus.com');
133139
t.is(normalizeUrl('https://sindresorhus.com/', options), 'https://sindresorhus.com/');
140+
t.is(normalizeUrl('https://sindresorhus.com/redirect', options), 'https://sindresorhus.com/redirect');
134141
t.is(normalizeUrl('https://sindresorhus.com/redirect/', options), 'https://sindresorhus.com/redirect');
135142
t.is(normalizeUrl('https://sindresorhus.com/#/', options), 'https://sindresorhus.com/#/');
136143
t.is(normalizeUrl('https://sindresorhus.com/?unicorns=true', options), 'https://sindresorhus.com/?unicorns=true');
137144
});
138145

139146
test('removeSingleSlash option combined with removeTrailingSlash option', t => {
140147
const options = {removeTrailingSlash: false, removeSingleSlash: false};
148+
t.is(normalizeUrl('https://sindresorhus.com', options), 'https://sindresorhus.com');
141149
t.is(normalizeUrl('https://sindresorhus.com/', options), 'https://sindresorhus.com/');
150+
t.is(normalizeUrl('https://sindresorhus.com/redirect', options), 'https://sindresorhus.com/redirect');
142151
t.is(normalizeUrl('https://sindresorhus.com/redirect/', options), 'https://sindresorhus.com/redirect/');
143152
t.is(normalizeUrl('https://sindresorhus.com/#/', options), 'https://sindresorhus.com/#/');
144153
t.is(normalizeUrl('https://sindresorhus.com/?unicorns=true', options), 'https://sindresorhus.com/?unicorns=true');

0 commit comments

Comments
 (0)