all files / umd/ url-builder.js

100% Statements 50/50
100% Branches 38/38
100% Functions 5/5
100% Lines 50/50
1 statement, 6 branches Ignored     
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141                                               26×                       28× 28× 28×   28×   28× 28×     28× 28×     28× 55×     55×     54× 54×     54×       49× 42×                     55×       28×       28×     28×                       54×   54×   54×   18× 18×         54× 25×     54×        
(function (global, factory) {
    'use strict';
 
    var built = factory(global);
 
    /* istanbul ignore else */
    Eif (typeof module === 'object' && module) {
        module.exports = built;
    }
 
    /* istanbul ignore next */
    Iif (typeof define === 'function' && define.amd) {
        define(factory);
    }
 
    global.URLBuilder = built;
}(typeof global !== 'undefined' ? global : /* istanbul ignore next */ this, function (global) {
 
    'use strict';
 
// External protocols regex, supports:
// "http", "https", "//" and "www."
var REGEX_EXTERNAL_PROTOCOLS = /^https?:\/\/|\/\/|www\./;
 
/**
 * Creates an instance of URLBuilder class.
 *
 * @constructor
 * @param {object} - instance of {@link ConfigParser} object.
 */
 
function URLBuilder(configParser) {
    this._configParser = configParser;
}
 
URLBuilder.prototype = {
    constructor: URLBuilder,
 
    /**
     * Returns a list of URLs from provided list of modules.
     *
     * @param {array} modules List of modules for which URLs should be created.
     * @return {array} List of URLs.
     */
    build: function (modules) {
        var bufferAbsoluteURL = [];
        var bufferRelativeURL = [];
        var result = [];
 
        var config = this._configParser.getConfig();
 
        var basePath = config.basePath;
        var registeredModules = this._configParser.getModules();
 
        /* istanbul ignore else */
        Eif (basePath.charAt(basePath.length - 1) !== '/') {
            basePath += '/';
        }
 
        for (var i = 0; i < modules.length; i++) {
            var module = registeredModules[modules[i]];
 
            // If module has fullPath, individual URL have to be created.
            if (module.fullPath) {
                result.push(module.fullPath);
 
            } else {
                var path = this._getModulePath(module);
                var absolutePath = path.indexOf('/') === 0;
 
                // If the URL starts with external protocol, individual URL shall be created.
                if (REGEX_EXTERNAL_PROTOCOLS.test(path)) {
                    result.push(path);
 
                // If combine is disabled, create individual URL based on config URL and module path.
                // If the module path starts with "/", do not include basePath in the URL.
                } else if (!config.combine) {
                    result.push(config.url + (absolutePath ? '' : basePath) + path);
 
                } else {
                    // If combine is true and module does not have full path, it will be collected
                    // in a buffer to be loaded among with other modules from combo loader.
                    // We will put the path in different buffer depending on the fact if it is absolute URL or not.
                    if (absolutePath) {
                        bufferAbsoluteURL.push(path);
                    } else {
                        bufferRelativeURL.push(path);
                    }
                }
            }
 
            module.requested = true;
        }
 
        // Add to the result all modules, which have to be combined.
        if (bufferRelativeURL.length) {
            result.push(config.url + basePath + bufferRelativeURL.join('&' + basePath));
            bufferRelativeURL.length = 0;
 
        }
 
        if (bufferAbsoluteURL.length) {
            result.push(config.url + bufferAbsoluteURL.join('&'));
            bufferAbsoluteURL.length = 0;
        }
 
        return result;
    },
 
    /**
     * Returns the path for a module. If module has property path, it will be returned directly. Otherwise,
     * the name of module will be used and extension .js will be added to module name if omitted.
     *
     * @protected
     * @param {object} module The module which path should be returned.
     * @return {string} Module path.
     */
    _getModulePath: function (module) {
        var path = module.path || module.name;
 
        var paths = this._configParser.getConfig().paths;
 
        for (var key in paths) {
            /* istanbul ignore else */
            Eif (Object.prototype.hasOwnProperty.call(paths, key)) {
                if (path === key || path.indexOf(key + '/') === 0) {
                    path = paths[key] + path.substring(key.length);
                }
            }
        }
 
        if (!REGEX_EXTERNAL_PROTOCOLS.test(path) && path.indexOf('.js') !== path.length - 3) {
            path += '.js';
        }
 
        return path;
    }
};
 
    return URLBuilder;
}));