Skip to content

Commit

Permalink
added new syntax to allow options, fixed #! urls
Browse files Browse the repository at this point in the history
this adds a syntax which allows calling url-join with two arguments: an array of strings to join and an (optional) object of options. This could be used in the future to allow keeping trailing slashes etc.
  • Loading branch information
rauberdaniel committed Apr 5, 2016
1 parent e190fe2 commit b8e5d83
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/url-join.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
str = str.replace(/([^:\s])\/+/g, '$1/');

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

// replace ? in parameters with &
str = str.replace(/(\?.+)\?/g, '$1&');
Expand All @@ -22,8 +22,17 @@
}

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

if (typeof arguments[0] === 'object') {
// new syntax with array and options
input = arguments[0];
options = arguments[1] || {};
}

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

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

it('should work for simple case with new syntax', function () {
urljoin(['http://www.google.com/', 'foo/bar', '?test=123'])
.should.eql('http://www.google.com/foo/bar?test=123');
});

it('should work for hashbang urls', function () {
urljoin(['http://www.google.com', '#!', 'foo/bar', '?test=123'])
.should.eql('http://www.google.com/#!/foo/bar?test=123');
});

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

0 comments on commit b8e5d83

Please sign in to comment.