Skip to content

Commit

Permalink
Fixing combineURLs to support an empty relativeURL
Browse files Browse the repository at this point in the history
When combining the base and relative URLs, we should forego force
appending a slash to the base when the relative URL is empty.
This leads to a semantic url.
  • Loading branch information
loklaan committed Dec 7, 2016
1 parent cfe33d4 commit 2bd48d4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/helpers/combineURLs.js
Expand Up @@ -8,5 +8,8 @@
* @returns {string} The combined URL
*/
module.exports = function combineURLs(baseURL, relativeURL) {
return baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, '');
var end = relativeURL.replace(/^\/+/, '');
return end
? baseURL.replace(/\/+$/, '') + '/' + end
: baseURL;
};
4 changes: 4 additions & 0 deletions test/specs/helpers/combineURLs.spec.js
Expand Up @@ -12,4 +12,8 @@ describe('helpers::combineURLs', function () {
it('should insert missing slash', function () {
expect(combineURLs('https://api.github.com', 'users')).toBe('https://api.github.com/users');
});

it('should not insert slash when relative url missing/empty', function () {
expect(combineURLs('https://api.github.com/users', '')).toBe('https://api.github.com/users');
});
});

0 comments on commit 2bd48d4

Please sign in to comment.