Code coverage report for js\define-map.js

Statements: 98.11% (52 / 53)      Branches: 80% (16 / 20)      Functions: 100% (6 / 6)      Lines: 98.11% (52 / 53)      Ignored: none     

All files » js\ » define-map.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                                            1 5 5     5   1 31         1 4     1   4       4 4 4 4 4 16 16 4 1     3     12 12       4           1 4   3 3 3   4   4 4 4   4   1 4 4   4   4   4 4 12     4 3 3   1       4     5 5   5 4     5 5    
/**
 * Basic attempt at implementing a define function that can take a map of dependency_alias->dependency_mid, and
 * resolve those dependencies into a 'resolved' map.
 *
 * E.g.
 *
 * <pre>
 * defineMap({
 *     lang: "dojo/_base/lang",
 *     relative: "./relative",
 *     module: "module",
 *     _: ["dojo/domReady!"]
 * }, function(deps) {
 *     console.log(deps.lang);
 *     console.log(deps.relative);
 *     console.log(deps.module.id);
 * });
 * </pre>
 *
 * Underscore can be used as a dependency_alias for modules you don't need a reference to.  The dependency_mid for
 * this can be a single mid string, or an array of mid strings.
 */
defineMap = function(mid, dependencies, factory) {
    var METH = "defineMap";
    var DEBUG = 0;
 
    // These are the arguments to be passed to define().
    var argsForDefine = arguments;
 
    function log() {
        Iif (DEBUG) {
            console.log.apply(console, [METH].concat([].slice.apply(arguments)));
        }
    }
 
    function isArray(it) {
        return "[object Array]" === Object.prototype.toString.apply(it);
    }
 
    function processDependencies(dependencies) {
        // Assume dependencies is an array of module ids, and set to mids.
        var mids = dependencies;
 
        // If dependencies is not an array (of module ids) then treat it as an object map of module aliases to module
        // ids. Remember the order of the module mids as well for when we come to resolve everything.
        var moduleNames = null;
        Eif (!isArray(dependencies)) {
            mids = [];
            moduleNames = [];
            for (var depAlias in dependencies) {
                var mid = dependencies[depAlias];
                if ("_" === depAlias) {
                    if ("string" === typeof mid) {
                        mids.push(mid);
                    } else {
                        // assume array
                        mids = mids.concat(mid);
                    }
                } else {
                    mids.push(mid);
                    moduleNames.push(depAlias);
                }
            }
        }
        return {
            mids: mids,
            moduleNames: moduleNames
        };
    }
 
    function handleMoreThanOneArgument() {
        if ("string" !== typeof mid) {
            // explicit module id not provided, shift args
            factory = dependencies;
            dependencies = mid;
            mid = null;
        }
        log("mid", mid, "dependencies", dependencies, "factory", factory);
 
        var processed = processDependencies(dependencies);
        var mids = processed.mids;
        var moduleNames = processed.moduleNames;
 
        log("mids", mids, "moduleName", moduleNames);
 
        function callback() {
            log("callback", "arguments", arguments);
            log("callback", "moduleNames", moduleNames);
 
            var moduleMap = null;
 
            Eif (moduleNames) {
                // moduleNames being defined means we want to use a moduleMap
                moduleMap = {};
                for (var i = 0; i < moduleNames.length; i++) {
                    moduleMap[moduleNames[i]] = arguments[i];
                }
            }
            if ("function" === typeof factory) {
                var args = moduleMap ? [moduleMap] : arguments;
                return factory.apply(null, args);
            } else {
                return factory;
            }
        }
 
        argsForDefine = mid ? [mid, mids, callback] : [mids, callback];
    }
 
    log("define", define);
    log("mid", mid, "dependencies", dependencies, "factory", factory);
 
    if (arguments.length !== 1) {
        handleMoreThanOneArgument();
    }
 
    log("args passed to define", argsForDefine);
    return define.apply(null, argsForDefine);
};