Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing combineURLs to support an empty relativeURL #581

Merged
merged 2 commits into from Dec 8, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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(/^\/+/, '');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If relativeURL is '/', it will not be added, right? For example:

combineURLs('http://test.com/foo', '/'); // => 'http://test.com/foo'

I would suggest to append relativeURL unless it's falsy ('', undefined, null). Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this, and covered the case by using the full baseURL (it could include an ending slash).

combineURLs('http://test.com/foo/', ''); // => 'http://test.com/foo/'

But yes I think it may be confusing without the support for your suggestion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, could you please update the PR?

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');
});
});