1/** 2 * Created by NicolasGERARD on 11/18/2015. 3 */ 4let WEBCODE = { 5 appendLine: function (text) { 6 let webConsoleLine = document.createElement("p"); 7 webConsoleLine.className = "webCodeConsoleLine"; 8 webConsoleLine.innerHTML = text; 9 WEBCODE.appendChild(webConsoleLine); 10 }, 11 appendChild: function (element) { 12 document.querySelector("#webCodeConsole").appendChild(element); 13 }, 14 print: function (v) { 15 if (typeof v === 'undefined') { 16 return "(Undefined)"; // Undefined == null, therefore it must be in first position 17 } else if (Array.isArray(v)) { 18 if (v.length === 0) { 19 return "(Empty Array)"; 20 } else { 21 return v; 22 } 23 } else if (typeof v === 'string') { 24 if (v.length === 0) { 25 return "(Empty String)" 26 } else { 27 return v; 28 } 29 } else if (v === null) { 30 return "(null)"; 31 } else { 32 return v; 33 } 34 }, 35 htmlEntities: function(str) { 36 // from https://css-tricks.com/snippets/javascript/htmlentities-for-javascript/ 37 return String(str) 38 .replace(/&/g, '&') 39 .replace(/</g, '<') 40 .replace(/>/g, '>') 41 .replace(/"/g, '"') 42 .replace(/ /g, ' '); 43 } 44}; 45 46 47window.console.log = function (input) { 48 let s = ""; 49 if (typeof input === "object") { 50 s = "{\n"; 51 let keys = Object.keys(input); 52 for (let i = 0; i < keys.length; i++) { 53 s += " " + keys[i] + " : " + input[keys[i]] + ";\n"; 54 } 55 s += "}\n"; 56 } else { 57 s = String(input); 58 } 59 s = WEBCODE.htmlEntities(s); 60 // the BR replacement must be after the htmlEntities function ... 61 s = s.replace(/\n/g, '<BR>') 62 WEBCODE.appendLine(s); 63}; 64 65// Console table implementation 66// https://developer.mozilla.org/en-US/docs/Web/API/Console/table 67window.console.table = function (input) { 68 if (Array.isArray(input) !== true) { 69 70 WEBCODE.appendLine("The variable of the function console.table must be an array."); 71 72 } else { 73 if (input.length <= 0) { 74 75 WEBCODE.appendLine("The variable of the console.table has no elements."); 76 77 } else { 78 // HTML Headers 79 let tableElement = document.createElement("table"); 80 let theadElement = document.createElement("thead"); 81 let tbodyElement = document.createElement("tbody"); 82 let trHeadElement = document.createElement("tr"); 83 84 tableElement.appendChild(theadElement); 85 tableElement.appendChild(tbodyElement); 86 theadElement.appendChild(trHeadElement); 87 88 89 for (let i = 0; i < input.length; i++) { 90 91 let element = input[i]; 92 93 // First iteration, we pick the headers 94 if (i === 0) { 95 96 if (typeof element === 'object') { 97 for (let prop in element) { 98 let thElement = document.createElement("th"); 99 thElement.innerHTML = WEBCODE.print(prop); 100 trHeadElement.appendChild(thElement); 101 } 102 } else { 103 // Header 104 let thElement = document.createElement("th"); 105 thElement.innerHTML = "Values"; 106 trHeadElement.appendChild(thElement); 107 } 108 109 } 110 111 let trBodyElement = document.createElement("tr"); 112 tbodyElement.appendChild(trBodyElement); 113 114 if (typeof input[0] === 'object') { 115 for (let prop in element) { 116 let tdElement = document.createElement("td"); 117 tdElement.innerHTML = WEBCODE.print(element[prop]); 118 trBodyElement.appendChild(tdElement); 119 } 120 } else { 121 let tdElement = document.createElement("td"); 122 tdElement.innerHTML = WEBCODE.print(element); 123 let trElement = document.createElement("tr"); 124 trElement.appendChild(tdElement); 125 } 126 127 } 128 WEBCODE.appendChild(tableElement); 129 130 } 131 } 132}; 133 134