Welcome to mirror list, hosted at ThFree Co, Russian Federation.

getMonkeyPatchSrc.js « lib - github.com/twbs/rewire.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 408b6eea61a5a5087cfe506f8692584271749090 (plain)
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
"use strict"; // run code in ES5 strict mode

var toSrc = require("toSrc");

function getMonkeyPatchSrc(obj) {
    function walkObj(obj, level) {
        var key,
            value,
            src = "";

        for (key in obj) {
            if (obj.hasOwnProperty(key)) {
                value = obj[key];
                if (typeof value === "object" && Array.isArray(value) === false) {
                    src += key + ".";
                    src += walkObj(value, level + 1);
                } else {
                    if (level === 0) {
                        src += "var ";   // in the top level, we need a var statement to override variables
                    }
                    src += key + "=" + toSrc(value, 9999) + ";";
                }
            }
        }


        return src;
    }

    return walkObj(obj, 0);
}

module.exports = getMonkeyPatchSrc;