-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
129 lines (105 loc) · 3.79 KB
/
utils.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
'use strict';
let domActionBuffer = [];
let flushTimeout;
window.display = function () {
if (!flushTimeout) flushTimeout = setTimeout(flushDomQueue, 100);
domActionBuffer.push({ action: 'display', arguments: arguments });
};
window.displayRegexArray = function () {
if (!flushTimeout) flushTimeout = setTimeout(flushDomQueue, 100);
domActionBuffer.push({ action: 'displayRegexArray', arguments: arguments });
};
window.clearDisplay = function () {
if (!flushTimeout) flushTimeout = setTimeout(flushDomQueue, 100);
domActionBuffer.push({ action: 'clearDisplay' });
};
function doClearDisplay() {
let outputTag = document.getElementById('output');
while (outputTag.firstChild) {
outputTag.removeChild(outputTag.firstChild);
}
}
function doDisplay() {
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] === 'object') displayObject(arguments[i], false);
else displayValue(arguments[i], true);
}
}
function doDisplayRegexArray() {
displayObject(arguments[0], true)
}
function flushDomQueue() {
flushTimeout = null;
if (domActionBuffer.length === 0) return;
const domActions = [...domActionBuffer];
domActionBuffer = [];
for (let domAction of domActions) {
switch (domAction.action) {
case 'display':
doDisplay(...domAction.arguments);
break;
case 'displayRegexArray':
doDisplayRegexArray(...domAction.arguments);
break;
case 'clearDisplay':
doClearDisplay();
break;
}
}
}
function displayValue(value, addMargin, addPadding) {
let outputTag = document.getElementById('output');
let div = document.createElement('div');
div.innerHTML = value;
if (addMargin) div.style.marginBottom = '30px';
if (addPadding) div.style.paddingLeft = '30px';
outputTag.appendChild(div);
}
function displayObject(object, regexArray) {
if (object == null) return displayValue('null');
if (getTypeName(object) === 'Array' && !regexArray) {
let appendedArrayValues = object.reduce((acc, cur) => acc+=cur+',', '')
if (appendedArrayValues.length > 0)
appendedArrayValues = appendedArrayValues.substr(0, appendedArrayValues.length - 1)
displayValue('[' + appendedArrayValues + ']')
if (Object.keys(object).length > object.length) {
displayValue(' ')
displayValue('Additional array properties:')
}
for (let propertyName in object)
{
if (!Number.isInteger(+propertyName) && object[propertyName] !== undefined)
displayValue(' ' + propertyName + ": " + object[propertyName])
else if (typeof object[propertyName] === 'object', false) {
return displayObject()
}
}
return
}
displayValue(getTypeName(object) + ' {');
for (var propertyName in object) {
if (typeof object[propertyName] === 'object', false)
displayObject(object[propertyName])
else if (propertyName != 'constructor' && (!regexArray || object[propertyName] !== undefined)) {
let prefix = Number.isInteger(+propertyName) && regexArray ? '[' : ''
let suffix = Number.isInteger(+propertyName) && regexArray ? ']' : ''
displayValue(prefix + propertyName + suffix + ': ' + object[propertyName], false, true);
}
}
displayValue('}', true);
}
function getTypeName(object) {
return object.constructor.name;
}
let reloadJS = () => {
let oldScriptTag = document.getElementById('script');
let newScriptTag = document.createElement('script');
newScriptTag.id = 'script';
newScriptTag.src = 'demo.js';
newScriptTag.textContent = '//script';
var body = document.getElementsByTagName('body')[0];
oldScriptTag.remove();
clearDisplay();
body.appendChild(newScriptTag);
};
setInterval(reloadJS, 1000);