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 : " " )).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
0 commit comments