Skip to content

Commit

Permalink
Passes all the tests with these changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tero Keski-Valkama committed Dec 14, 2017
1 parent f1afbd6 commit 8cde667
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
56 changes: 39 additions & 17 deletions lib/url-join.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,49 @@
return str.substr(0, searchString.length) === searchString;
}

function normalize (str, options) {

if (startsWith(str, 'file://')) {

// make sure file protocol has max three slashes
str = str.replace(/(\/{0,3})\/*/g, '$1');
} else {

// make sure protocol is followed by two slashes
str = str.replace(/:\//g, '://');

// remove consecutive slashes
str = str.replace(/([^:\s\%\3\A])\/+/g, '$1/');
function normalize (strArray, options) {
var resultArray = [];

// If the first part is a plain protocol, we combine it with the next part.
if (strArray[0].match(/:\/*$/) && strArray.length > 1) {
var first = strArray.shift();
strArray[0] = first + strArray[0];
// There must be at least two in any, at most three slashes in the file protocol.
if (strArray[0].match(/file:\/\/\//)) {
strArray[0] = strArray[0].replace(/:\/*/, ':///');
} else {
strArray[0] = strArray[0].replace(/:\/*/, '://');
}
}

for (var i = 0; i < strArray.length; i++) {

var component = strArray[i];
if (i > 0) {
// Removing the starting slashes for each component but the first.
component = component.replace(/^[\/]+/, '');
}
if (i < strArray.length - 1) {
// Removing the ending slashes for each component but the last.
component = component.replace(/[\/]+$/, '');
} else {
// For the last component we will combine multiple slashes to a single one.
component = component.replace(/[\/]+$/, '/');
}

resultArray.push(component);

}

var str = resultArray.join('/');
// Each input component is now separated by a single slash except the possible first plain protocol part.

// remove trailing slash before parameters or hash
str = str.replace(/\/(\?|&|#[^!])/g, '$1');

// replace ? in parameters with &
str = str.replace(/(\?.+)\?/g, '$1&');
var parts = str.split('?');
str = parts.shift() + (parts.length > 0 ? '?': '') + parts.join('&');

return str;
}
Expand All @@ -41,9 +64,8 @@
input = arguments[0];
options = arguments[1] || {};
}

var joined = [].slice.call(input, 0).join('/');
return normalize(joined, options);

return normalize([].slice.call(input), options);
};

});
4 changes: 1 addition & 3 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,9 @@ describe('url join', function () {

urljoin('http:', 'www.google.com///', 'foo/bar', '?test=123', '?boom=value', '&key=456')
.should.eql('http://www.google.com/foo/bar?test=123&boom=value&key=456');
});

it('should merge multiple query params with more than two query params properly', function () {
urljoin('http://example.org/x', '?a=1', '?b=2', '?c=3', '?d=4')
.should.eql('http://example.org/x?a=1&b=2&c=3&d=4');
.should.eql('http://example.org/x?a=1&b=2&c=3&d=4');
});

it('should merge slashes in paths correctly', function () {
Expand Down

0 comments on commit 8cde667

Please sign in to comment.