Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore the base URL when the relative one is a complete URL #128

Merged
merged 2 commits into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;