diff --git a/.gitignore b/.gitignore index 5191751..6170e8b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .idea/ node_modules/ lib/ +coverage \ No newline at end of file diff --git a/src/common/__test__/string-builder.spec.js b/src/common/__test__/string-builder.spec.js index cbf329f..fdad04c 100644 --- a/src/common/__test__/string-builder.spec.js +++ b/src/common/__test__/string-builder.spec.js @@ -85,3 +85,38 @@ test('makeData should not stringify data if configured not to', () => { const result = sb.makeData(a).build(); expect(result).toEqual(''); }); + +test('combineURLs should combine the base URL with the relative URL', () => { + const sb = new StringBuilder(getGlobalConfig()); + const result = sb.combineURLs('https://github.com', '/users/hg-pyun'); + + expect(result).toBe('https://github.com/users/hg-pyun'); +}); + +test(`combineURLs should not combine the base and relative URL's if the relative one is a complete URL`, () => { + const sb = new StringBuilder(getGlobalConfig()); + const result = sb.combineURLs('https://github.com', 'https://github.com/users/hg-pyun'); + + expect(result).toBe('https://github.com/users/hg-pyun'); +}); + +test('combineURLs should return the base URL when the relative URL is empty', () => { + const sb = new StringBuilder(getGlobalConfig()); + const result = sb.combineURLs('https://github.com', ''); + + expect(result).toBe('https://github.com'); +}); + +test('combineURLs should return the relative URL when the base URL is empty', () => { + const sb = new StringBuilder(getGlobalConfig()); + const result = sb.combineURLs('', 'https://github.com/users/hg-pyun'); + + expect(result).toBe('https://github.com/users/hg-pyun'); +}); + +test('combineURLs should return the relative URL when the base URL is undefined', () => { + const sb = new StringBuilder(getGlobalConfig()); + const result = sb.combineURLs(undefined, 'https://github.com/users/hg-pyun'); + + expect(result).toBe('https://github.com/users/hg-pyun'); +}); diff --git a/src/common/string-builder.ts b/src/common/string-builder.ts index 7a92288..6516b2a 100644 --- a/src/common/string-builder.ts +++ b/src/common/string-builder.ts @@ -80,15 +80,9 @@ class StringBuilder { return this.printQueue.join(' '); } - /** - * Helper imported from Axios library - * @see https://github.com/axios/axios/blob/d99d5faac29899eba68ce671e6b3cbc9832e9ad8/lib/helpers/combineURLs.js - * */ - combineURLs(baseURL: string, relativeURL?: string): string { - return relativeURL - ? baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, '') - : baseURL; - }; + combineURLs(baseURL: string, relativeURL?: string): string { + return relativeURL ? new URL(relativeURL, baseURL || undefined).toString() : baseURL; + } } export default StringBuilder;