Trim URL before adding missing proto (Closes #1205)

This commit is contained in:
Gregory Schier
2018-10-11 12:15:36 -04:00
parent 2174a8f9f9
commit 8ffaa833fa
2 changed files with 10 additions and 4 deletions

View File

@@ -11,6 +11,11 @@ describe('setDefaultProtocol()', () => {
expect(url).toBe('http://google.com');
});
it('correctly sets protocol for padded domain', () => {
const url = setDefaultProtocol(' google.com ');
expect(url).toBe('http://google.com');
});
it('does not set for valid url', () => {
const url = setDefaultProtocol('https://google.com');
expect(url).toBe('https://google.com');

View File

@@ -5,17 +5,18 @@
* @returns {string}
*/
module.exports.setDefaultProtocol = function(url, defaultProto) {
const trimmedUrl = url.trim();
defaultProto = defaultProto || 'http:';
// If no url, don't bother returning anything
if (!url) {
if (!trimmedUrl) {
return '';
}
// Default the proto if it doesn't exist
if (url.indexOf('://') === -1) {
url = `${defaultProto}//${url}`;
if (trimmedUrl.indexOf('://') === -1) {
return `${defaultProto}//${trimmedUrl}`;
}
return url;
return trimmedUrl;
};