Skip to content

Commit

Permalink
Fixing combineURLs to support an empty relativeURL (#581)
Browse files Browse the repository at this point in the history
* Fixing combineURLs to support an empty relativeURL

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.

* Fixing combineURLs, allowing single slash relatives
  • Loading branch information
loklaan authored and nickuraltsev committed Dec 8, 2016
1 parent cfe33d4 commit fe7d09b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/helpers/combineURLs.js
Expand Up @@ -8,5 +8,7 @@
* @returns {string} The combined URL
*/
module.exports = function combineURLs(baseURL, relativeURL) {
return baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, '');
return relativeURL
? baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, '')
: baseURL;
};
8 changes: 8 additions & 0 deletions test/specs/helpers/combineURLs.spec.js
Expand Up @@ -12,4 +12,12 @@ 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');
});

it('should allow a single slash for relative url', function () {
expect(combineURLs('https://api.github.com/users', '/')).toBe('https://api.github.com/users/');
});
});

0 comments on commit fe7d09b

Please sign in to comment.