Skip to content

Commit

Permalink
handle empty string and non string arguments. Closes #36, #18
Browse files Browse the repository at this point in the history
  • Loading branch information
jfromaniello committed Feb 2, 2018
1 parent b4cb727 commit da05242
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
30 changes: 13 additions & 17 deletions lib/url-join.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@
else context[name] = definition();
})('urljoin', this, function () {

function startsWith(str, searchString) {
return str.substr(0, searchString.length) === searchString;
}

function normalize (strArray, options) {
function normalize (strArray) {
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();
Expand All @@ -23,15 +19,16 @@
} else {
strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1://');
}

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

var component = strArray[i];

if (typeof component !== 'string') {
component = component && component.toString() || '';
throw new TypeError('Url must be a string. Received ' + component);
}

if (component === '') { continue; }

if (i > 0) {
// Removing the starting slashes for each component but the first.
component = component.replace(/^[\/]+/, '');
Expand All @@ -43,9 +40,9 @@
// For the last component we will combine multiple slashes to a single one.
component = component.replace(/[\/]+$/, '/');
}

resultArray.push(component);

}

var str = resultArray.join('/');
Expand All @@ -62,16 +59,15 @@
}

return function () {
var input = arguments;
var options = {};
var input;

if (typeof arguments[0] === 'object') {
// new syntax with array and options
input = arguments[0];
options = arguments[1] || {};
} else {
input = [].slice.call(arguments);
}
return normalize([].slice.call(input), options);

return normalize(input);
};

});
20 changes: 17 additions & 3 deletions test/tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var urljoin = require('../lib/url-join');
var assert = require('assert');

describe('url join', function () {
it('should work for simple case', function () {
Expand Down Expand Up @@ -98,9 +99,15 @@ describe('url join', function () {
.should.eql('/test');
});

it('should merge a simple path with a number correctly', function() {
urljoin('http://blabla.com/', 1)
.should.eql('http://blabla.com/1');
it('should fail with segments that are not string', function() {
assert.throws(() => urljoin('http://blabla.com/', 1),
/Url must be a string. Received 1/);
assert.throws(() => urljoin('http://blabla.com/', undefined, 'test'),
/Url must be a string. Received undefined/);
assert.throws(() => urljoin('http://blabla.com/', null, 'test'),
/Url must be a string. Received null/);
assert.throws(() => urljoin('http://blabla.com/', { foo: 123 }, 'test'),
/Url must be a string. Received \[object Object\]/);
});

it('should merge a path with colon properly', function(){
Expand Down Expand Up @@ -128,4 +135,11 @@ describe('url join', function () {
urljoin('file:', '//example.org', 'a')
.should.eql('file://example.org/a');
});

it('should skip empty strings', function() {
urljoin('http://foobar.com', '', 'test')
.should.eql('http://foobar.com/test');
urljoin('', 'http://foobar.com', '', 'test')
.should.eql('http://foobar.com/test');
});
});

0 comments on commit da05242

Please sign in to comment.