-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
109 lines (99 loc) · 2.83 KB
/
script.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
// IE6+
var values = [
'true',
'false',
'1',
'0',
'-0',
'-1',
'"true"',
'"false"',
'"anystring"',
'"1"',
'"0"',
'"-0"',
'"-1"',
'""',
'null',
'undefined',
'Infinity',
'-Infinity',
'[]',
'["anyarray"]',
'{}',
'{anyobject:true}',
'[[]]',
'[0]',
'[-0]',
'[1]',
'[-1]',
'[true]',
'[false]',
'NaN'
];
// Simple polyfill of Array.prototype.forEach, doesn't take `this` into account
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(callback) {
for (var i = 0; i < this.length; i++) {
callback(this[i], i, this);
}
}
}
var c = function(tagName, text, className) {
var el = document.createElement(tagName);
if (typeof text === "string") {
var textNode = document.createTextNode(text);
el.appendChild(textNode);
}
if (typeof className === "string") {
el.className = className;
}
return el;
}
// type can be "==", "===", "SameValue" (Object.is()), or "SameValueZero" (Array.prototype.includes())
var createCompTable = function(type) {
var table = c("table");
var tr1 = c("tr", null, "tableheader");
tr1.appendChild(c("th"));
table.appendChild(tr1);
var th, span, result;
values.forEach(function(currentValue) {
eval("var currentValueEvaluated = " + currentValue);
th = c("th");
span = c("span", currentValue);
th.appendChild(span);
tr1.appendChild(th);
var currentTr = c("tr");
currentTr.appendChild(c("th", currentValue));
values.forEach(function(comparingValue) {
eval("var comparingValueEvaluated = " + comparingValue);
switch (type) {
case "==":
result = currentValueEvaluated == comparingValueEvaluated;
break;
case "===":
result = currentValueEvaluated === comparingValueEvaluated;
break;
case "SameValue":
result = Object.is(currentValueEvaluated, comparingValueEvaluated);
break;
case "SameValueZero":
result = [currentValueEvaluated].includes(comparingValueEvaluated);
break;
default:
break;
}
if (result === true) {
currentTr.appendChild(c("td", "T", "true"));
} else {
currentTr.appendChild(c("td", "F", "false"));
}
})
table.appendChild(currentTr);
});
return table;
}
document.body.appendChild(createCompTable("=="));
document.body.appendChild(createCompTable("==="));
document.body.appendChild(createCompTable("SameValue"));
document.body.appendChild(createCompTable("SameValueZero"));