This repository has been archived by the owner on Mar 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebCodeConsole.js
133 lines (114 loc) · 4.32 KB
/
webCodeConsole.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
/**
* Created by NicolasGERARD on 11/18/2015.
*/
let WEBCODE = {
appendLine: function (text) {
let webConsoleLine = document.createElement("p");
webConsoleLine.className = "webCodeConsoleLine";
webConsoleLine.innerHTML = text;
WEBCODE.appendChild(webConsoleLine);
},
appendChild: function (element) {
document.querySelector("#webCodeConsole").appendChild(element);
},
print: function (v) {
if (typeof v === 'undefined') {
return "(Undefined)"; // Undefined == null, therefore it must be in first position
} else if (Array.isArray(v)) {
if (v.length === 0) {
return "(Empty Array)";
} else {
return v;
}
} else if (typeof v === 'string') {
if (v.length === 0) {
return "(Empty String)"
} else {
return v;
}
} else if (v === null) {
return "(null)";
} else {
return v;
}
},
htmlEntities: function(str) {
// from https://css-tricks.com/snippets/javascript/htmlentities-for-javascript/
return String(str)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/ /g, ' ');
}
};
window.console.log = function (input) {
let s = "";
if (typeof input === "object") {
s = "{\n";
let keys = Object.keys(input);
for (let i = 0; i < keys.length; i++) {
s += " " + keys[i] + " : " + input[keys[i]] + ";\n";
}
s += "}\n";
} else {
s = String(input);
}
s = WEBCODE.htmlEntities(s);
// the BR replacement must be after the htmlEntities function ...
s = s.replace(/\n/g, '<BR>')
WEBCODE.appendLine(s);
};
// Console table implementation
// https://developer.mozilla.org/en-US/docs/Web/API/Console/table
window.console.table = function (input) {
if (Array.isArray(input) !== true) {
WEBCODE.appendLine("The variable of the function console.table must be an array.");
} else {
if (input.length <= 0) {
WEBCODE.appendLine("The variable of the console.table has no elements.");
} else {
// HTML Headers
let tableElement = document.createElement("table");
let theadElement = document.createElement("thead");
let tbodyElement = document.createElement("tbody");
let trHeadElement = document.createElement("tr");
tableElement.appendChild(theadElement);
tableElement.appendChild(tbodyElement);
theadElement.appendChild(trHeadElement);
for (let i = 0; i < input.length; i++) {
let element = input[i];
// First iteration, we pick the headers
if (i === 0) {
if (typeof element === 'object') {
for (let prop in element) {
let thElement = document.createElement("th");
thElement.innerHTML = WEBCODE.print(prop);
trHeadElement.appendChild(thElement);
}
} else {
// Header
let thElement = document.createElement("th");
thElement.innerHTML = "Values";
trHeadElement.appendChild(thElement);
}
}
let trBodyElement = document.createElement("tr");
tbodyElement.appendChild(trBodyElement);
if (typeof input[0] === 'object') {
for (let prop in element) {
let tdElement = document.createElement("td");
tdElement.innerHTML = WEBCODE.print(element[prop]);
trBodyElement.appendChild(tdElement);
}
} else {
let tdElement = document.createElement("td");
tdElement.innerHTML = WEBCODE.print(element);
let trElement = document.createElement("tr");
trElement.appendChild(tdElement);
}
}
WEBCODE.appendChild(tableElement);
}
}
};