From 4111cfef9d00e4b6fbd2d04241d0e7c48526387c Mon Sep 17 00:00:00 2001 From: Bruno Filippone Date: Fri, 30 Jan 2015 14:54:44 +0000 Subject: [PATCH] fix(path): joining an absolute path fragment with another incorrectly removes the first slash from the resulting url --- src/index.js | 6 ++++-- test/path.spec.js | 30 +++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index c80a8b8..dcaca96 100644 --- a/src/index.js +++ b/src/index.js @@ -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; @@ -54,6 +54,8 @@ export function join(path1, path2) { if(!path2){ return path1; } + + urlPrefix = path1.indexOf('/') === 0 ? '/' : ''; url1 = path1.split('/'); url2 = path2.split('/'); @@ -79,5 +81,5 @@ export function join(path1, path2) { } } - return url3.join('/').replace(/\:\//g, '://');; + return urlPrefix + url3.join('/').replace(/\:\//g, '://');; } \ No newline at end of file diff --git a/test/path.spec.js b/test/path.spec.js index bd081a8..74b91a2 100644 --- a/test/path.spec.js +++ b/test/path.spec.js @@ -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';