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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added comments, fixes #2 #11

Merged
merged 3 commits into from
Mar 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions lib/url-join.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@
else context[name] = definition();
})('urljoin', this, function () {

function normalize (str) {
return str
.replace(/[\/]+/g, '/')
.replace(/\/\?/g, '?')
.replace(/\/\#/g, '#')
.replace(/\:\//g, '://');
function normalize (str, options) {

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

// remove consecutive slashes
str = str.replace(/([^:\s])\/+/g, '$1/');

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

return str;
}

return function () {
var joined = [].slice.call(arguments, 0).join('/');
return normalize(joined);
};

});
});
12 changes: 11 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ describe('url join', function () {
.should.eql('http://www.google.com/foo/bar?test=123');
});

it('should be able to join protocol with slashes', function () {
urljoin('http://', 'www.google.com/', 'foo/bar', '?test=123')
.should.eql('http://www.google.com/foo/bar?test=123');
});

it('should remove extra slashes', function () {
urljoin('http:', 'www.google.com///', 'foo/bar', '?test=123')
.should.eql('http://www.google.com/foo/bar?test=123');
Expand All @@ -20,4 +25,9 @@ describe('url join', function () {
urljoin('http:', 'www.google.com///', 'foo/bar', '?test=123', '#faaaaa')
.should.eql('http://www.google.com/foo/bar?test=123#faaaaa');
});
});

it('should support protocol-relative urls', function () {
urljoin('//www.google.com', 'foo/bar', '?test=123')
.should.eql('//www.google.com/foo/bar?test=123')
});
});