forked from dorey/JavaScript-Equality-Table
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomparison-table.js
More file actions
83 lines (75 loc) · 2.89 KB
/
comparison-table.js
File metadata and controls
83 lines (75 loc) · 2.89 KB
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
var EqualityTable = (function(cmpStr){
var comparisons = [true, false, 1, 0, -1, "true", "false", "1", "0", "-1", "", null,
undefined, [], {}, [[]], [0], [1], parseFloat("nan")];
function map(f, x) {
var result = [];
for (var i = 0; i < x.length; i += 1)
result.push(f(x[i]));
return result;
}
function repr(x) {
if (typeof(x) === "string") return '"' + x.replace('"', '\\"') + '"';
if (x && x.constructor === Array) return "[" + map(repr, x).join(", ") + "]";
if (x && typeof(x) === "object") return "{}";
return String(x);
}
function buildTable(cmpStr) {
var _tableHtml = $("<table />", {'class':'comparisons'}),
_curRow = $("<tr />").html("<td />"),
representations = [],
_tVal, i, j, result;
$.each(comparisons, function(i){
_tVal = repr(comparisons[i]);
_curRow.append($("<td />", {'class':'col header'}).html($("<span />").html(_tVal)))
representations.push(_tVal);
})
_tableHtml.append(_curRow);
if(cmpStr=="if-statement") {
_curRow = $("<tr />");
_curRow.append($("<td />", {'class':'row header'}).html("If (<i>value</i>)"))
var elem;
$.each(comparisons, function(i){
elem = $("<td />", {'class':'cell green'}).html("<div />")
if(comparisons[i]) {
elem.addClass('green');
elem.attr('title', "if("+comparisons[i]+"){/*--executes--*/}")
} else {
elem.addClass('red');
elem.attr('title', "if("+comparisons[i]+"){/*--does not execute--*/}")
}
_curRow.append(elem)
})
_tableHtml.append(_curRow);
} else {
var elem;
for (i = 0; i < comparisons.length; i += 1) {
_curRow = $("<tr />");
_curRow.append($("<td />", {'class':'row header'}).html(representations[i]))
for (j = 0; j < comparisons.length; j += 1) {
elem = $("<td />", {'class': 'cell'}).html("<div />");
if(cmpStr==="===") {
if(comparisons[i]===comparisons[j]) {
elem.addClass('green');
elem.attr('title', ""+representations[i]+"==="+representations[j]+" » true ")
} else {
elem.addClass('red');
elem.attr('title', ""+representations[i]+"==="+representations[j]+" » false ")
}
} else if(cmpStr==="=="){
if(comparisons[i]==comparisons[j]) {
elem.addClass('green');
elem.attr('title', ""+representations[i]+"=="+representations[j]+" » true ")
} else {
elem.addClass('red');
elem.attr('title', ""+representations[i]+"=="+representations[j]+" » false ")
}
}
_curRow.append(elem);
}
_tableHtml.append(_curRow);
}
}
return _tableHtml;
}
return buildTable;
})()