-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathcomparison_table.coffee
More file actions
136 lines (126 loc) · 4.55 KB
/
Copy pathcomparison_table.coffee
File metadata and controls
136 lines (126 loc) · 4.55 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
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
133
134
135
136
# log = -> console?.log?.apply(console, arguments)
isString = (obj)->
Object::toString.call(obj) is "[object String]"
isObject = (obj)->
Object::toString.call(obj) is "[object Object]"
class ForComparison
constructor: (@item)->
###
The goal here is to get a string that evaluates to the desired
value. This string is set to object.asString
For complicated cases which can't be passed in JSON the value
can be passed as a string wrapped in backticks which is evaluated
here.
example: "`parseFloat("nan")`" means NaN.
###
@asString = "#{@item}"
if isString(@item)
if @item.length > 0 and (mtch = @item.match(/^`(.*)`$/))
match = mtch[1]
@item = new Function("return #{match}")()
if isString(@item)
@asString = JSON.stringify(@item)
else if @item is `undefined`
@asString = "undefined"
else if isObject(@item)
@asString = JSON.stringify(@item)
else if isNaN(@item)
@asString = "NaN"
else
@asString = JSON.stringify(@item)
else if @item.length is 0
@asString = '""'
else if @item.toString() is "[object Object]"
@asString = "{}"
else if @item instanceof Array
@asString = JSON.stringify(@item)
testResults: (fc2, comparator="===")->
evalStr = "" + @asString + comparator + fc2.asString
[evalStr, eval('(' + evalStr + ')')]
toString: ->
@asString
###
The values which are strings wrapped in backticks (`) are evaluated
before being compared.
###
values = [true, false, 1, 0, -1,
"`'true'`", "`'false'`", "`'1'`", "`'0'`", "`'-1'`", "",
"`null`", "`undefined`", "`[]`", "`{}`", [[]],
[0], [1], "`parseFloat('nan')`"]
# Ensure that the values going in are converted to string properly (for the table headers)
do ->
testRepr = (what, shouldBe)->
fc = new ForComparison(what)
if fc.toString() isnt shouldBe
throw new Error("Value is not being represented correctly.")
testRepr(true,"true")
testRepr(false,"false")
testRepr(1,"1")
testRepr(0,"0")
testRepr(-1,"-1")
testRepr("`'true'`","\"true\"")
testRepr("`'false'`","\"false\"")
testRepr("`'1'`","\"1\"")
testRepr("`'0'`","\"0\"")
testRepr("`'-1'`","\"-1\"")
testRepr("","\"\"")
testRepr("`null`","null")
testRepr("`undefined`","undefined")
testRepr("`[]`","[]")
testRepr("`{}`","{}")
testRepr([[]],"[[]]")
testRepr([0],"[0]")
testRepr([1],"[1]")
testRepr("`parseFloat('nan')`","NaN")
do ->
testEquality = (tf, item, comparator)->
fc1 = new ForComparison(item)
if fc1.testResults(fc1, comparator)[1] isnt tf
throw new Error("Condition should be #{tf}")
# easy case
testEquality(`true==true`, "`true`", "==")
# these two were giving incorrect values before
testEquality(`[[]]==[[]]`, "`[[]]`", "==")
testEquality(`[]==[]`, "`[]`", "==")
supportsCanvas = do ->
el = document.createElement "canvas"
!!(el.getContext && el.getContext("2d"))
@buildComparisonTable = (values, comparator)->
comps = (new ForComparison(v) for v in values)
$table = $("<table>", class: "comparisons")
$headRow = $("<tr>").append("<td>").appendTo($table)
for comp in comps
$el = if supportsCanvas then rotateText(comp.asString) else $("<span>", class: "rotate", text: comp.asString)
$("<td>", class: "header col").html($el).appendTo($headRow)
for valX, x in comps
$tr = $("<tr>").appendTo($table)
$("<td>", class: "row header").text(valX.asString).appendTo($tr)
for valY, y in comps
td = $("<td>", class: "cell", html: "<div> </div>").appendTo($tr)
[evalStr, tf] = valX.testResults(valY, comparator)
td.attr("title", "#{evalStr} // #{tf}")
if tf
td.addClass("equal")
$table
@buildComparisonTableForIf = (values)->
comps = (new ForComparison(v) for v in values)
$table = $("<table>", class: "comparisons")
for comp in comps
$tr = $("<tr>").html($("<td>", class: "header row", text: comp.asString)).appendTo($table)
$td = $("<td>", class: "cell").html($("<div>", html: " ")).appendTo($tr)
val = (new Function("if(#{comp.asString}){return true}else{return false}"))()
if val
$td.addClass("equal")
expression = " if (#{comp.asString}) { /* #{if val then 'executes' else 'does not execute'} */ } "
$("<td>", class: "expression").html(expression).appendTo($tr)
$table
rotateText = (txt, cHeight=80)->
canv = document.createElement("canvas")
canv.width = "25"
canv.height = cHeight
c = canv.getContext("2d")
c.rotate(Math.PI / 2)
c.font = "15px Monospace"
c.textAlign = "right"
c.fillText(txt,cHeight,-10)
canv