All files url.ts

94.12% Statements 32/34
90% Branches 18/20
100% Functions 2/2
93.33% Lines 28/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70          8x 1x               103x 32x   32x 32x   31x       31x 16x 16x       31x 5x   26x     31x 87x   87x       87x   84x   54x   84x   53x     31x     84x       31x 31x   31x 31x   31x    
/**
 * Converts url to relative url
 * @param url - Valid url
 * @category Utility
 */
export function urlToRelative(url: string): string {
    return url.replace(/^[^/]+\/\/[^/]+/, '')
}
 
/**
 * Joins two url paths
 * @param url - Valid url
 * @category Utility
 */
export function urlJoin(...concat: string[]) {
    const resultArray: string[] = [];
 
    const strArray = [...concat]
    if (strArray.length === 0) { return ''; }
 
    Iif (typeof strArray[0] !== 'string') {
        throw new TypeError('Url must be a string. Received ' + strArray[0]);
    }
 
    if (strArray[0].match(/^[^/:]+:\/*$/) && strArray.length > 1) {
        const first = strArray.shift();
        strArray[0] = first + strArray[0];
    }
 
    // There must be two or three slashes in the file protocol, two slashes in anything else.
    if (strArray[0].match(/^file:\/\/\//)) {
        strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1:///');
    } else {
        strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1://');
    }
 
    for (var i = 0; i < strArray.length; i++) {
        var component = strArray[i];
 
        Iif (typeof component !== 'string') {
            throw new TypeError('Url must be a string. Received ' + component);
        }
 
        if (component === '') { continue; }
 
        if (i > 0) {
            // Removing the starting slashes for each component but the first.
            component = component.replace(/^[\/]+/, '');
        }
        if (i < strArray.length - 1) {
            // 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.
            component = component.replace(/[\/]+$/, '/');
        }
 
        resultArray.push(component);
 
    }
 
    var str = resultArray.join('/');
    str = str.replace(/\/(\?|&|#[^!])/g, '$1');
     // replace ? in parameters with &
     var parts = str.split('?');
     str = parts.shift() + (parts.length > 0 ? '?': '') + parts.join('&');
 
     return str;
}