Permalink
Browse files

Improved _sanitizeCondition logic such that it survives JS minificati…

…on in tact.
  • Loading branch information...
1 parent 624136e commit 8e652e2f8b19af81f74b5e07904ee16d10c9ca9b @hiddentao committed Mar 28, 2013
Showing with 15 additions and 24 deletions.
  1. +5 −6 docs/squel.html
  2. +4 −7 squel.js
  3. +1 −1 squel.min.js
  4. +5 −6 src/squel.coffee
  5. +0 −4 test/baseclasses.test.coffee
View
@@ -37,13 +37,12 @@
<span class="nv">arr = </span><span class="nx">obj</span><span class="p">.</span><span class="nx">constructor</span><span class="p">.</span><span class="nx">toString</span><span class="p">().</span><span class="nx">match</span> <span class="sr">/function\s*(\w+)/</span><span class="p">;</span>
<span class="k">if</span> <span class="nx">arr</span> <span class="o">&amp;&amp;</span> <span class="nx">arr</span><span class="p">.</span><span class="nx">length</span> <span class="o">is</span> <span class="mi">2</span>
<span class="k">return</span> <span class="nx">arr</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
- <span class="k">return</span> <span class="kc">undefined</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">&#182;</a> </div> <p>Sanitize the given condition.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_sanitizeCondition: </span><span class="nf">(condition) -&gt;</span>
- <span class="nv">t = </span><span class="k">typeof</span> <span class="nx">condition</span>
- <span class="nv">c = </span><span class="nx">@_getObjectClassName</span><span class="p">(</span><span class="nx">condition</span><span class="p">)</span>
-
- <span class="k">if</span> <span class="s">&#39;Expression&#39;</span> <span class="o">isnt</span> <span class="nx">c</span> <span class="o">and</span> <span class="s">&#39;Expression&#39;</span> <span class="o">isnt</span> <span class="nx">t</span> <span class="o">and</span> <span class="s">&quot;string&quot;</span> <span class="o">isnt</span> <span class="nx">t</span>
- <span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s">&quot;condition must be a string or Expression instance&quot;</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">&#182;</a> </div> <p>If it's an expression builder instance then convert it to string form.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="s">&#39;Expression&#39;</span> <span class="o">is</span> <span class="nx">t</span> <span class="o">or</span> <span class="s">&#39;Expression&#39;</span> <span class="o">is</span> <span class="nx">c</span>
+ <span class="k">return</span> <span class="kc">undefined</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">&#182;</a> </div> <p>Sanitize the given condition.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_sanitizeCondition: </span><span class="nf">(condition) -&gt;</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">&#182;</a> </div> <p>If it's an expression builder instance then convert it to string form.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="nx">condition</span> <span class="k">instanceof</span> <span class="nx">cls</span><span class="p">.</span><span class="nx">Expression</span>
<span class="nv">condition = </span><span class="nx">condition</span><span class="p">.</span><span class="nx">toString</span><span class="p">()</span>
+
+ <span class="k">if</span> <span class="s">&quot;string&quot;</span> <span class="o">isnt</span> <span class="k">typeof</span> <span class="nx">condition</span>
+ <span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s">&quot;condition must be a string or Expression instance&quot;</span>
+
<span class="nx">condition</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">&#182;</a> </div> <p>Sanitize the given name.
The 'type' parameter is used to construct a meaningful error message in case validation fails.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_sanitizeName: </span><span class="nf">(value, type) -&gt;</span>
<span class="k">if</span> <span class="s">&quot;string&quot;</span> <span class="o">isnt</span> <span class="k">typeof</span> <span class="nx">value</span>
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -85,14 +85,13 @@ class cls.BaseBuilder extends cls.Cloneable
# Sanitize the given condition.
_sanitizeCondition: (condition) ->
- t = typeof condition
- c = @_getObjectClassName(condition)
-
- if 'Expression' isnt c and 'Expression' isnt t and "string" isnt t
- throw new Error "condition must be a string or Expression instance"
# If it's an expression builder instance then convert it to string form.
- if 'Expression' is t or 'Expression' is c
+ if condition instanceof cls.Expression
condition = condition.toString()
+
+ if "string" isnt typeof condition
+ throw new Error "condition must be a string or Expression instance"
+
condition
@@ -122,7 +122,6 @@ test['Builder base class'] =
'empty expression': ->
e = squel.expr()
assert.same "", @inst._sanitizeCondition(e)
- assert.ok @inst._getObjectClassName.calledWithExactly(e)
'non-empty expression': ->
e = squel.expr()
.and("s.name <> 'Fred'")
@@ -131,17 +130,14 @@ test['Builder base class'] =
.or("s.id = 6")
.end()
assert.same "s.name <> 'Fred' OR (s.id = 5 OR s.id = 6)", @inst._sanitizeCondition(e)
- assert.ok @inst._getObjectClassName.calledWithExactly(e)
'if string': ->
s = 'BLA BLA'
assert.same 'BLA BLA', @inst._sanitizeCondition(s)
- assert.ok @inst._getObjectClassName.calledWithExactly(s)
'if neither Expression nor String': ->
testFn = => @inst._sanitizeCondition(1)
assert.throws testFn, 'condition must be a string or Expression instance'
- assert.ok @inst._getObjectClassName.calledWithExactly(1)
'_sanitizeName':

0 comments on commit 8e652e2

Please sign in to comment.