Skip to content

Commit

Permalink
feat: add file protocol support
Browse files Browse the repository at this point in the history
  • Loading branch information
xtuc committed Apr 10, 2017
1 parent 3144ba1 commit 48ebe0d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/url-join.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
// remove consecutive slashes
str = str.replace(/([^:\s])\/+/g, '$1/');

// set file:// for file protocol
str = str.replace(/(file:)\/+/g, '$1///');

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

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

it('should support file protocol urls', function () {
urljoin('file:', 'android_asset', 'foo/bar')
.should.eql('file:///android_asset/foo/bar')

urljoin('file:', '///android_asset', 'foo/bar')
.should.eql('file:///android_asset/foo/bar')

urljoin('file:', '/android_asset', 'foo/bar')
.should.eql('file:///android_asset/foo/bar')

urljoin('file:///', 'android_asset', 'foo/bar')
.should.eql('file:///android_asset/foo/bar')
});

it('should merge multiple query params properly', function () {
urljoin('http:', 'www.google.com///', 'foo/bar', '?test=123', '?key=456')
.should.eql('http://www.google.com/foo/bar?test=123&key=456');
Expand Down

0 comments on commit 48ebe0d

Please # to comment.