Skip to content

Commit

Permalink
fix: updated combine url fix method (#1647)
Browse files Browse the repository at this point in the history
* test: added more combineBaseUrl tests

* fix: optimized and updated combineBaseUrl method logic
  • Loading branch information
Giedrius Grabauskas authored and juanpicado committed Jan 8, 2020
1 parent 32aabca commit 4f43347
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,19 @@ export function validateMetadata(object: Package, name: string): Package {
* @return {String} base registry url
*/
export function combineBaseUrl(protocol: string, host: string | void, prefix?: string | void): string {
let result = `${protocol}://${host}`;
const result = `${protocol}://${host}`;

if (prefix) {
prefix = prefix.replace(/\/$/, '');
const prefixOnlySlash = prefix === '/';
if (prefix && !prefixOnlySlash) {
if (prefix.endsWith('/')) {
prefix = prefix.slice(0, -1);
}

if (prefix.startsWith('/')) {
return `${result}${prefix}`;
}

result = prefix.indexOf('/') === 0 ? `${result}${prefix}` : prefix;
return prefix;
}

return result;
Expand Down
3 changes: 3 additions & 0 deletions test/unit/modules/utils/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ describe('Utilities', () => {
});

test('should create a base url for registry', () => {
expect(combineBaseUrl("http", 'domain', '')).toEqual('http://domain');
expect(combineBaseUrl("http", 'domain', '/')).toEqual('http://domain');
expect(combineBaseUrl("http", 'domain', '/prefix/')).toEqual('http://domain/prefix');
expect(combineBaseUrl("http", 'domain', '/prefix/deep')).toEqual('http://domain/prefix/deep');
expect(combineBaseUrl("http", 'domain', 'only-prefix')).toEqual('only-prefix');
});

Expand Down

0 comments on commit 4f43347

Please sign in to comment.