From eb6936f8c574476f3baea11c83500087c216a175 Mon Sep 17 00:00:00 2001 From: Jignesh Kakadiya Date: Tue, 18 Dec 2018 18:26:45 +0530 Subject: [PATCH 1/3] Wrongly removes trailing slash from a hash --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 7e9e294..f4ccc69 100644 --- a/index.js +++ b/index.js @@ -133,7 +133,7 @@ module.exports = (urlString, options) => { urlString = urlObj.toString(); // Remove ending `/` - if (options.removeTrailingSlash || urlObj.pathname === '/') { + if (options.removeTrailingSlash || (urlObj.pathname === '/' && urlObj.hash === '')) { urlString = urlString.replace(/\/$/, ''); } From 98f1812e315a0b278fca15e56d5fb2c7cc89f075 Mon Sep 17 00:00:00 2001 From: Jignesh Kakadiya Date: Tue, 18 Dec 2018 20:01:56 +0530 Subject: [PATCH 2/3] Add tests --- test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test.js b/test.js index 9333d35..c8670fa 100644 --- a/test.js +++ b/test.js @@ -115,6 +115,7 @@ test('removeTrailingSlash option', t => { 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/', options), 'http://sindresorhus.com/redirect/'); + t.is(normalizeUrl('http://sindresorhus.com/#/', options), 'http://sindresorhus.com/#/'); }); test('removeDirectoryIndex option', t => { @@ -151,6 +152,7 @@ test('removeTrailingSlash and removeDirectoryIndex options)', t => { }; 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'); + t.is(normalizeUrl('http://sindresorhus.com/#/path/', options1), 'http://sindresorhus.com/#/path'); const options2 = { removeTrailingSlash: false, @@ -158,6 +160,7 @@ test('removeTrailingSlash and removeDirectoryIndex options)', t => { }; 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/'); + t.is(normalizeUrl('http://sindresorhus.com/#/path/', options2), 'http://sindresorhus.com/#/path/'); }); test('sortQueryParameters option', t => { From 4ed1ba8130f7d4d87855b0f43a0709aa4610aa85 Mon Sep 17 00:00:00 2001 From: Jignesh Kakadiya Date: Tue, 18 Dec 2018 20:18:14 +0530 Subject: [PATCH 3/3] Fix test case --- index.js | 2 +- test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index f4ccc69..4049b8c 100644 --- a/index.js +++ b/index.js @@ -133,7 +133,7 @@ module.exports = (urlString, options) => { urlString = urlObj.toString(); // Remove ending `/` - if (options.removeTrailingSlash || (urlObj.pathname === '/' && urlObj.hash === '')) { + if ((options.removeTrailingSlash || urlObj.pathname === '/') && urlObj.hash === '') { urlString = urlString.replace(/\/$/, ''); } diff --git a/test.js b/test.js index c8670fa..d71a855 100644 --- a/test.js +++ b/test.js @@ -152,7 +152,7 @@ test('removeTrailingSlash and removeDirectoryIndex options)', t => { }; 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'); - t.is(normalizeUrl('http://sindresorhus.com/#/path/', options1), 'http://sindresorhus.com/#/path'); + t.is(normalizeUrl('http://sindresorhus.com/#/path/', options1), 'http://sindresorhus.com/#/path/'); const options2 = { removeTrailingSlash: false,