From a181f8dd5f2797f653354c02ee417a889f9bb5d3 Mon Sep 17 00:00:00 2001 From: srph Date: Tue, 12 Apr 2016 17:14:14 +0800 Subject: [PATCH] add support for hash urls --- lib/url-join.js | 2 +- test/tests.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/url-join.js b/lib/url-join.js index 35dfa29..397e8c8 100644 --- a/lib/url-join.js +++ b/lib/url-join.js @@ -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(/\/(\?|&|#(\w+)$)/g, '$1'); // replace ? in parameters with & str = str.replace(/(\?.+)\?/g, '$1&'); diff --git a/test/tests.js b/test/tests.js index bda9a6e..7374e79 100644 --- a/test/tests.js +++ b/test/tests.js @@ -16,6 +16,11 @@ describe('url join', function () { .should.eql('http://www.google.com/#!/foo/bar?test=123'); }); + it('should work hash 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');