_console.ts ➔ stacktrace   F
last analyzed

Complexity

Conditions 20

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 11
dl 0
loc 16
rs 0
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like _console.ts ➔ stacktrace often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
interface Console {
2
  olog: {
3
    (...data: any[]): void;
4
    (message?: any, ...optionalParams: any[]): void;
5
  };
6
}
7
declare var console_callback: any;
8
9
if (typeof console != "undefined") {
10
  if (typeof console.log != "undefined") {
11
    console.olog = console.log;
12
  } else {
13
    console.olog = function () {};
14
  }
15
}
16
17
if (typeof module == "undefined") {
18
  console.log = function () {
19
    const log = console.olog;
20
    var stack = new Error().stack;
21
22
    /**
23
     * Get Caller Location
24
     */
25
    var file = stack.split("\n")[2].split("/")[4].split("?")[0];
26
27
    /**
28
     * Get Caller Line
29
     */
30
    var line: string | number; //= stack.split("\n")[2].split(":")[5];
31
    var getline = stack.split("\n")[2].split(":");
32
    if (getline.exists(5)) {
33
      line = parseNumber(getline[5]);
34
      //log("number found in index 5", getline[5]);
35
    } else if (getline.exists(4)) {
36
      line = parseNumber(getline[4]);
37
      //log("number found in index 4", getline[4]);
38
    } else if (getline.exists(3)) {
39
      line = parseNumber(getline[3]);
40
      //log("number found in index 3", getline[3]);
41
    }
42
43
    /**
44
     * Get Caller Function Name
45
     */
46
    var caller: any;
47
    var caller_str = stack.split("\n")[2];
48
    const regex = /at\s(.*)\s\(/gm;
49
    caller = regex.exec(caller_str);
50
    if (caller != null && caller.length) {
51
      caller = caller[1];
52
    }
53
54
    /**
55
     * Create Prefix Log
56
     */
57
    var append: string = "";
58
    if (typeof file != "undefined") {
59
      append += `${file}/`;
60
    }
61
    if (caller != null && typeof caller != "undefined") {
62
      append += `${caller}/`;
63
    }
64
    if (typeof line != "undefined") {
65
      append += `${line}:`;
66
    }
67
68
    var input = [];
69
    if (arguments.length == 1) {
70
      input = arguments[0];
71
    } else {
72
      for (let index = 0; index < arguments.length; index++) {
73
        const arg = arguments[index];
74
        input.push(arg);
75
      }
76
    }
77
78
    var args: any[];
79
    if (Array.hasOwnProperty("from")) {
80
      args = Array.from(arguments); // ES5
81
    } else {
82
      args = Array.prototype.slice.call(arguments);
83
    }
84
    args.unshift(append);
85
86
    log.apply(console, args);
87
88
    if (typeof jQuery != "undefined") {
89
      if (!$("#debugConsole").length) {
90
        $("body").append('<div id="debugConsole" style="display:none"></div>');
91
      }
92
      if (typeof console_callback == "function") {
93
        console_callback(input);
94
      } else {
95
        $("#debugConsole").append(
96
          "<p> <kbd>" + typeof input + "</kbd> " + input + "</p>"
97
        );
98
      }
99
    }
100
  };
101
} else {
102
  /**
103
   * Consoler
104
   */
105
  [
106
    ["warn", "\x1b[35m"],
107
    ["error", "\x1b[31m"],
108
    ["log", "\x1b[2m"],
109
  ].forEach(function (pair) {
110
    var method = pair[0],
111
      reset = "\x1b[0m",
112
      color = "\x1b[36m" + pair[1];
113
    console[method] = console[method].bind(
114
      console,
115
      color,
116
      `${method.toUpperCase()} [${new Date().getHours()}:${new Date().getMinutes()}:${new Date().getSeconds()}]`,
117
      reset
118
    );
119
  });
120
121
  console.error = (function () {
122
    var error = console.error;
123
124
    return function (exception: { stack: any }) {
125
      if (typeof exception.stack !== "undefined") {
126
        error.call(console, exception.stack);
127
      } else {
128
        error.apply(console, arguments);
129
      }
130
    };
131
  })();
132
}
133
134
/**
135
 * Get stacktrace
136
 */
137
function stacktrace() {
138
  function st2(f: Function) {
139
    return !f
140
      ? []
141
      : st2(f.caller).concat([
142
          f.toString().split("(")[0].substring(9) +
143
            "(" +
144
            f.arguments.join(",") +
145
            ")",
146
        ]);
147
  }
148
  return st2(arguments.callee.caller);
149
}
150