Skip to content

Commit

Permalink
fix(path): joining an absolute path fragment with another incorrectly…
Browse files Browse the repository at this point in the history
… removes the first slash from the resulting url
  • Loading branch information
bfil committed Jan 30, 2015
1 parent 862d84b commit 4111cfe
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function relativeToFile(name, file){
}

export function join(path1, path2) {
var url1, url2, url3, i, ii;
var url1, url2, url3, i, ii, urlPrefix;

if(!path1){
return path2;
Expand All @@ -54,6 +54,8 @@ export function join(path1, path2) {
if(!path2){
return path1;
}

urlPrefix = path1.indexOf('/') === 0 ? '/' : '';

url1 = path1.split('/');
url2 = path2.split('/');
Expand All @@ -79,5 +81,5 @@ export function join(path1, path2) {
}
}

return url3.join('/').replace(/\:\//g, '://');;
return urlPrefix + url3.join('/').replace(/\:\//g, '://');;
}
30 changes: 29 additions & 1 deletion test/path.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,41 @@ describe('join', () => {
});

it('can combine an absolute path and a simple path', () => {
var path1 = '/one';
var path2 = 'two';

expect(join(path1, path2)).toBe('/one/two');
});

it('can combine an absolute path and a simple path with slash', () => {
var path1 = '/one';
var path2 = '/two';

expect(join(path1, path2)).toBe('/one/two');
});

it('can combine a single slash and a simple path', () => {
var path1 = '/';
var path2 = 'two';

expect(join(path1, path2)).toBe('/two');
});

it('can combine a single slash and a simple path with slash', () => {
var path1 = '/';
var path2 = '/two';

expect(join(path1, path2)).toBe('/two');
});

it('can combine an absolute path with protocol and a simple path', () => {
var path1 = 'http://durandal.io';
var path2 = 'two';

expect(join(path1, path2)).toBe('http://durandal.io/two');
});

it('can combine an absolute path and a simple path with slash', () => {
it('can combine an absolute path with protocol and a simple path with slash', () => {
var path1 = 'http://durandal.io';
var path2 = '/two';

Expand Down

0 comments on commit 4111cfe

Please sign in to comment.