Skip to content

Commit c961164

Browse files
committed
restructuring how the comparisons are made. fixes dorey#2
1 parent 69de183 commit c961164

5 files changed

Lines changed: 361 additions & 11 deletions

File tree

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
COFFEE = $(shell which coffee)
2+
3+
watch:
4+
${COFFEE} -w -o ./js -c ./coffee
5+
6+
build:
7+
${COFFEE} -o ./js -c ./coffee

coffee/comparison_table.coffee

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# log = -> console?.log?.apply(console, arguments)
2+
3+
isString = (obj)->
4+
Object::toString.call(obj) is "[object String]"
5+
isObject = (obj)->
6+
Object::toString.call(obj) is "[object Object]"
7+
8+
class ForComparison
9+
constructor: (@item)->
10+
###
11+
The goal here is to get a string that evaluates to the desired
12+
value. This string is set to object.asString
13+
14+
For complicated cases which can't be passed in JSON the value
15+
can be passed as a string wrapped in backticks which is evaluated
16+
here.
17+
example: "`parseFloat("nan")`" means NaN.
18+
###
19+
@asString = "#{@item}"
20+
if isString(@item)
21+
if @item.length > 0 and (mtch = @item.match(/^`(.*)`$/))
22+
match = mtch[1]
23+
@item = new Function("return #{match}")()
24+
if isString(@item)
25+
@asString = JSON.stringify(@item)
26+
else if @item is `undefined`
27+
@asString = "undefined"
28+
else if isObject(@item)
29+
@asString = JSON.stringify(@item)
30+
else if isNaN(@item)
31+
@asString = "NaN"
32+
else
33+
@asString = JSON.stringify(@item)
34+
else if @item.length is 0
35+
@asString = '""'
36+
else if @item.toString() is "[object Object]"
37+
@asString = "{}"
38+
else if @item instanceof Array
39+
@asString = JSON.stringify(@item)
40+
testResults: (fc2, comparator="===")->
41+
if @asString is "{}"
42+
evalStr = "[#{@asString}#{comparator}#{fc2.asString}][0]"
43+
else
44+
evalStr = "#{@asString}#{comparator}#{fc2.asString}"
45+
[evalStr, eval(evalStr)]
46+
toString: ->
47+
@asString
48+
49+
###
50+
The values which are strings wrapped in backticks (`) are evaluated
51+
before being compared.
52+
###
53+
values = [true, false, 1, 0, -1,
54+
"`'true'`", "`'false'`", "`'1'`", "`'0'`", "`'-1'`", "",
55+
"`null`", "`undefined`", "`[]`", "`{}`", [[]],
56+
[0], [1], "`parseFloat('nan')`"]
57+
58+
# Ensure that the values going in are converted to string properly (for the table headers)
59+
do ->
60+
testRepr = (what, shouldBe)->
61+
fc = new ForComparison(what)
62+
if fc.toString() isnt shouldBe
63+
throw new Error("Value is not being represented correctly.")
64+
testRepr(true,"true")
65+
testRepr(false,"false")
66+
testRepr(1,"1")
67+
testRepr(0,"0")
68+
testRepr(-1,"-1")
69+
testRepr("`'true'`","\"true\"")
70+
testRepr("`'false'`","\"false\"")
71+
testRepr("`'1'`","\"1\"")
72+
testRepr("`'0'`","\"0\"")
73+
testRepr("`'-1'`","\"-1\"")
74+
testRepr("","\"\"")
75+
testRepr("`null`","null")
76+
testRepr("`undefined`","undefined")
77+
testRepr("`[]`","[]")
78+
testRepr("`{}`","{}")
79+
testRepr([[]],"[[]]")
80+
testRepr([0],"[0]")
81+
testRepr([1],"[1]")
82+
testRepr("`parseFloat('nan')`","NaN")
83+
84+
do ->
85+
testEquality = (tf, item, comparator)->
86+
fc1 = new ForComparison(item)
87+
if fc1.testResults(fc1, comparator)[1] isnt tf
88+
throw new Error("Condition should be #{tf}")
89+
# easy case
90+
testEquality(`true==true`, "`true`", "==")
91+
# these two were giving incorrect values before
92+
testEquality(`[[]]==[[]]`, "`[[]]`", "==")
93+
testEquality(`[]==[]`, "`[]`", "==")
94+
95+
@buildComparisonTable = (values, comparator)->
96+
comps = (new ForComparison(v) for v in values)
97+
$table = $("<table>", class: "comparisons")
98+
$headRow = $("<tr>").append("<td>").appendTo($table)
99+
for comp in comps
100+
$("<td>", class: "header col").html($("<span>", text: comp.asString)).appendTo($headRow)
101+
for valX, x in comps
102+
$tr = $("<tr>").appendTo($table)
103+
$("<td>", class: "row header").text(valX.asString).appendTo($tr)
104+
for valY, y in comps
105+
td = $("<td>", class: "cell", html: "<div>&nbsp;</div>").appendTo($tr)
106+
[evalStr, tf] = valX.testResults(valY, comparator)
107+
td.attr("title", "#{evalStr} // #{tf}")
108+
if tf
109+
td.addClass("green")
110+
$table
111+
112+
@buildComparisonTableForIf = (values)->
113+
comps = (new ForComparison(v) for v in values)
114+
$table = $("<table>", class: "comparisons")
115+
for comp in comps
116+
$tr = $("<tr>").html($("<td>", text: comp.asString)).appendTo($table)
117+
$td = $("<td>", class: "cell").html($("<div>", html: "&nbsp;")).appendTo($tr)
118+
val = (new Function("if(#{comp.asString}){return true}else{return false}"))()
119+
if val
120+
$td.addClass("green")
121+
expression = " if (#{comp.asString}) { /* #{if val then 'executes' else 'does not execute'} */ } "
122+
$("<td>", class: "expression").html(expression).appendTo($tr)
123+
124+
$table

index.html

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
<link rel="stylesheet" href="simple.css" type="text/css" media="all" title="simple" charset="utf-8">
99
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript" charset="utf-8"></script>
1010
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js" type="text/javascript" charset="utf-8"></script>
11-
<script src="comparison-table.js" type="text/javascript" charset="utf-8"></script>
11+
<script type="text/javascript" src="js/comparison_table.js"></script>
1212

1313
<!-- Originally from here:
1414
http://blog.codekills.net/archives/89-Equality-in-JavaScript.html
15-
15+
16+
2013-Nov-11. Correcting issue where "[]===[]" was incorrectly marked as true.
17+
1618
Converted from this gist:
1719
https://gist.github.com/761080
1820
-->
@@ -59,9 +61,16 @@ <h4>Moral of the story:</h4>
5961
</div>
6062
</div>
6163
<script type="text/javascript" charset="utf-8">
62-
$("#two-equals").append(EqualityTable("=="))
63-
$("#three-equals").append(EqualityTable("==="))
64-
$("#if-statement").append(EqualityTable("if-statement"))
64+
/*
65+
Strings in backticks (`) are evaluated.
66+
*/
67+
var values = [true, false, 1, 0, -1, "`'true'`", "`'false'`", "`'1'`", "`'0'`", "`'-1'`",
68+
"", "`null`", "`undefined`", "`[]`", "`{}`", [[]], [0], [1], "`parseFloat('nan')`"];
69+
70+
buildComparisonTable(values, "==").appendTo("#two-equals")
71+
buildComparisonTable(values, "===").appendTo("#three-equals")
72+
buildComparisonTableForIf(values).appendTo("#if-statement")
73+
6574
$('#tabs').tabs();
6675
</script>
6776
</body>

js/comparison_table.js

Lines changed: 207 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)