Code coverage report for umd/url-builder.js

Statements: 100% (43 / 43)      Branches: 100% (32 / 32)      Functions: 100% (5 / 5)      Lines: 100% (43 / 43)      Ignored: 1 statement, 5 branches     

All files » umd/ » url-builder.js
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 1281     1     1 1       1 1     1             1                 1 11     1                   11 11   11 11   11     11 5     11 25     25 1     24     24 5     19 18         1       25       11 1   1     11                       24   24   24   8 8 3         24 11     24       1  
(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 buffer = [];
        var result = [];
 
        var registeredModules = this._configParser.getModules();
        var config = this._configParser.getConfig();
 
        var basePath = config.basePath;
 
        /* istanbul ignore else */
        if (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);
 
                // 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.
                } else if (!config.combine) {
                    result.push(config.url + 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.
                    buffer.push(path);
                }
            }
 
            module.requested = true;
        }
 
        // Add to the result all modules, which have to be combined.
        if (buffer.length) {
            result.push(config.url + basePath + buffer.join('&' + basePath));
 
            buffer.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;
}));