pp_js.go 2.38 KB
Newer Older
zelig's avatar
zelig committed
1
package jsre
obscuren's avatar
obscuren committed
2

zelig's avatar
zelig committed
3
const pp_js = `
4 5
function pp(object, indent) {
    try {
6 7 8 9
        JSON.stringify(object)
    } catch(e) {
        return pp(e, indent);
    }
obscuren's avatar
obscuren committed
10

11
    var str = "";
obscuren's avatar
obscuren committed
12
    if(object instanceof Array) {
13
        str += "[";
obscuren's avatar
obscuren committed
14
        for(var i = 0, l = object.length; i < l; i++) {
15
            str += pp(object[i], indent);
obscuren's avatar
obscuren committed
16 17 18 19 20 21

            if(i < l-1) {
                str += ", ";
            }
        }
        str += " ]";
22
    } else if (object instanceof Error) {
23
        str += "\033[31m" + "Error:\033[0m " + object.message;
24 25
    } else if (isBigNumber(object)) {
        str += "\033[32m'" + object.toString(10) + "'";
obscuren's avatar
obscuren committed
26
    } else if(typeof(object) === "object") {
27
        str += "{\n";
28
        indent += "  ";
29 30
        var last = getFields(object).pop()
        getFields(object).forEach(function (k) {
31 32 33 34 35 36
            str += indent + k + ": ";
            try {
                str += pp(object[k], indent);
            } catch (e) {
                str += pp(e, indent);
            }
obscuren's avatar
obscuren committed
37 38

            if(k !== last) {
39
                str += ",";
obscuren's avatar
obscuren committed
40
            }
41 42 43

            str += "\n";
        });
44
        str += indent.substr(2, indent.length) + "}";
obscuren's avatar
obscuren committed
45 46 47 48 49 50
    } else if(typeof(object) === "string") {
        str += "\033[32m'" + object + "'";
    } else if(typeof(object) === "undefined") {
        str += "\033[1m\033[30m" + object;
    } else if(typeof(object) === "number") {
        str += "\033[31m" + object;
obscuren's avatar
obscuren committed
51
    } else if(typeof(object) === "function") {
52
        str += "\033[35m[Function]";
obscuren's avatar
obscuren committed
53
    } else {
zelig's avatar
zelig committed
54
        str += object;
obscuren's avatar
obscuren committed
55 56 57 58 59 60 61
    }

    str += "\033[0m";

    return str;
}

62 63 64 65 66 67 68 69 70 71
var redundantFields = [
    'valueOf',
    'toString',
    'toLocaleString',
    'hasOwnProperty',
    'isPrototypeOf',
    'propertyIsEnumerable',
    'constructor'
];

72 73 74 75 76
var getFields = function (object) {
    var result = Object.getOwnPropertyNames(object);
    if (object.constructor && object.constructor.prototype) {
        result = result.concat(Object.getOwnPropertyNames(object.constructor.prototype));
    }
77 78 79
    return result.filter(function (field) {
        return redundantFields.indexOf(field) === -1;
    });
80 81
};

82 83 84 85
var isBigNumber = function (object) {
    return typeof BigNumber !== 'undefined' && object instanceof BigNumber;
};

obscuren's avatar
obscuren committed
86 87
function prettyPrint(/* */) {
    var args = arguments;
88
    var ret = "";
obscuren's avatar
obscuren committed
89
    for(var i = 0, l = args.length; i < l; i++) {
90
	    ret += pp(args[i], "") + "\n";
obscuren's avatar
obscuren committed
91
    }
92
    return ret;
obscuren's avatar
obscuren committed
93
}
obscuren's avatar
obscuren committed
94 95

var print = prettyPrint;
obscuren's avatar
obscuren committed
96
`