misc(v2): refactor utils code to be slightly more concise (#1633)

This commit is contained in:
James George 2019-07-05 23:46:57 +05:30 committed by Yangshun Tay
parent 1c0d5f0135
commit d8b5323836

View file

@ -173,19 +173,17 @@ export function normalizeUrl(rawUrls: string[]): string {
urls[0] = first + urls[0]; urls[0] = first + urls[0];
} }
// There must be two or three slashes in the file protocol, two slashes in anything else. // There must be two or three slashes in the file protocol,
if (urls[0].match(/^file:\/\/\//)) { // two slashes in anything else.
urls[0] = urls[0].replace(/^([^/:]+):\/*/, '$1:///'); const replacement = urls[0].match(/^file:\/\/\//) ? '$1:///' : '$1://';
} else { urls[0] = urls[0].replace(/^([^/:]+):\/*/, replacement);
urls[0] = urls[0].replace(/^([^/:]+):\/*/, '$1://');
}
// eslint-disable-next-line // eslint-disable-next-line
for (let i = 0; i < urls.length; i++) { for (let i = 0; i < urls.length; i++) {
let component = urls[i]; let component = urls[i];
if (typeof component !== 'string') { if (typeof component !== 'string') {
throw new TypeError(`Url must be a string. Received ${component}`); throw new TypeError(`Url must be a string. Received ${typeof component}`);
} }
if (component === '') { if (component === '') {
@ -197,13 +195,10 @@ export function normalizeUrl(rawUrls: string[]): string {
// Removing the starting slashes for each component but the first. // Removing the starting slashes for each component but the first.
component = component.replace(/^[/]+/, ''); component = component.replace(/^[/]+/, '');
} }
if (i < urls.length - 1) {
// Removing the ending slashes for each component but the last. // Removing the ending slashes for each component but the last.
component = component.replace(/[/]+$/, '');
} else {
// For the last component we will combine multiple slashes to a single one. // For the last component we will combine multiple slashes to a single one.
component = component.replace(/[/]+$/, '/'); component = component.replace(/[/]+$/, i < urls.length - 1 ? '' : '/');
}
resultArray.push(component); resultArray.push(component);
} }