Skip to content

Commit

Permalink
fix: ignore the base URL when the relative one is a complete URL (#128)
Browse files Browse the repository at this point in the history
Co-authored-by: Haegul Pyun <[email protected]>
  • Loading branch information
brGuirra and hg-pyun authored Jul 23, 2023
1 parent 8067ab1 commit 64ffec5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.idea/
node_modules/
lib/
coverage
35 changes: 35 additions & 0 deletions src/common/__test__/string-builder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
12 changes: 3 additions & 9 deletions src/common/string-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 64ffec5

Please sign in to comment.