Code coverage report for umd/config-parser.js

Statements: 100% (37 / 37)      Branches: 100% (20 / 20)      Functions: 100% (10 / 10)      Lines: 100% (37 / 37)      Ignored: 1 statement, 6 branches     

All files » umd/ » config-parser.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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 1511     1     1 1       1 1     1         1                 1 13 13 13   13     1                                   75   75                 33                 57                 137                     13 26 26 7   19         13                     7 55 55   55   55       7                     75 13   13 10     13         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.ConfigParser = built;
}(typeof global !== 'undefined' ? global : /* istanbul ignore next */ this, function (global) {
 
    'use strict';
 
var hasOwnProperty = Object.prototype.hasOwnProperty;
 
/**
 * Creates an instance of ConfigurationParser class.
 *
 * @constructor
 * @param {object=} - The configuration object to be parsed.
 */
 
function ConfigParser(config) {
    this._config = {};
    this._modules = {};
    this._conditionalModules = {};
 
    this._parseConfig(config);
}
 
ConfigParser.prototype = {
    constructor: ConfigParser,
 
    /**
     * Adds a module to the configuration.
     *
     * @param {object} module The module which should be added to the configuration. Should have the following
     *     properties:
     *     <ul>
     *         <strong>Obligatory properties</strong>:
     *         <li>name (String) The name of the module</li>
     *         <li>dependencies (Array) The modules from which it depends</li>
     *     </ul>
     *
     *     <strong>Optional parameters:</strong>
     *     The same as those which config parameter of {@link Loader#define} method accepts.
     */
    addModule: function (module) {
        this._modules[module.name] = module;
 
        this._registerConditionalModule(module);
    },
 
    /**
     * Returns the current configuration.
     *
     * @return {object} The current configuration.
     */
    getConfig: function () {
        return this._config;
    },
 
    /**
     * Returns map with all currently registered conditional modules and their triggers.
     *
     * @return {object} Map with all currently registered conditional modules.
     */
    getConditionalModules: function () {
        return this._conditionalModules;
    },
 
    /**
     * Returns map with all currently registered modules.
     *
     * @return {object} Map with all currently registered modules.
     */
    getModules: function () {
        return this._modules;
    },
 
    /**
     * Parses configuration object.
     *
     * @protected
     * @param {object} config Configuration object to be parsed.
     * @return {object} The created configuration
     */
    _parseConfig: function (config) {
        for (var key in config) { /* istanbul ignore else */
            Eif (hasOwnProperty.call(config, key)) {
                if (key === 'modules') {
                    this._parseModules(config[key]);
                } else {
                    this._config[key] = config[key];
                }
            }
        }
 
        return this._config;
    },
 
    /**
     * Parses a provided modules configuration.
     *
     * @protected
     * @param {object} modules Map of modules to be parsed.
     * @return {object} Map of parsed modules.
     */
    _parseModules: function (modules) {
        for (var key in modules) { /* istanbul ignore else */
            Eif (hasOwnProperty.call(modules, key)) {
                var module = modules[key];
 
                module.name = key;
 
                this.addModule(module);
            }
        }
 
        return this._modules;
    },
 
    /**
     * Registers conditional module to the configuration.
     *
     * @protected
     * @param {object} module Module object
     */
    _registerConditionalModule: function (module) {
        // Create HashMap of all modules, which have conditional modules, as an Array.
        if (module.condition) {
            var existingModules = this._conditionalModules[module.condition.trigger];
 
            if (!existingModules) {
                this._conditionalModules[module.condition.trigger] = existingModules = [];
            }
 
            existingModules.push(module.name);
        }
    }
};
 
    return ConfigParser;
}));