Skip to content
Browse files

fixed where array bug, added docs for conditions

  • Loading branch information...
1 parent 3ab715b commit 38958d850cb276be7cdda5cb08a0d004efd238ad @kieran committed Jun 25, 2011
Showing with 164 additions and 71 deletions.
  1. +37 −6 Readme.md
  2. +1 −1 docs/helpers.html
  3. +12 −5 docs/key.html
  4. +21 −26 docs/route.html
  5. +23 −30 docs/router.html
  6. +11 −2 lib/key.js
  7. +59 −1 tests/barista.test.js
View
43 Readme.md
@@ -1,4 +1,4 @@
-Barista is a simple URL router for nodejs.
+Barista is a simple URL router for NodeJS.
Getting Barista
===============
@@ -18,23 +18,54 @@ var Router = require('barista').Router;
var router = new Router;
```
+Adding routes
+-------------
-### Adding routes
+### A simple example
```javascript
-// a basic example
router.match( '/products', 'GET' )
.to( 'products.index' )
+```
+
+### Rails-esque variable names
-// Rails-style variables
+```javascript
router.match( '/products/:id', 'GET' )
.to( 'products.show' )
-// optional parts
+router.match( '/profiles/:username', 'GET' )
+ .to( 'users.show' )
+
router.match( '/products/:id(.:format)', 'GET' )
.to( 'products.show' )
+```
-// convenience methods
+### Match conditions
+
+```javascript
+router.match( '/:beverage/near/:zipcode', 'GET' )
+ .to( 'beverage.byZipCode' )
+ .where({
+ // an array of options
+ beverage: [ 'coffee', 'tea', 'beer', 'warm_sake' ],
+ // a regex pattern
+ zipcode: /^\d{5}(-\d{4})?$/
+ })
+
+router.match( '/:beverage/near/:location', 'GET' )
+ .to( 'beverage.byLocation' )
+ .where({
+ // could be a postal code
+ // OR a zip code
+ // OR the word 'me' (geolocation FTW)
+ location: [ /^\d{5}(-\d{4})?$/, /^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/, 'me' ]
+ })
+```
+
+### Convenience methods
+
+```javascript
router.get( '/products/:id(.:format)' )
.to( 'products.show' )
View
2 docs/helpers.html
@@ -15,7 +15,7 @@
<span class="kd">var</span> <span class="nx">args</span> <span class="o">=</span> <span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">)</span>
<span class="k">for</span> <span class="p">(</span> <span class="nx">i</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">args</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span> <span class="p">)</span> <span class="p">{</span>
-
+
<span class="k">for</span> <span class="p">(</span> <span class="kd">var</span> <span class="nx">prop</span> <span class="k">in</span> <span class="nx">args</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">kindof</span><span class="p">(</span><span class="nx">args</span><span class="p">[</span><span class="nx">i</span><span class="p">][</span><span class="nx">prop</span><span class="p">])</span> <span class="o">==</span> <span class="s1">&#39;object&#39;</span> <span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>deep copy</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="nx">prop</span><span class="p">]</span> <span class="o">=</span> <span class="nx">mixin</span><span class="p">(</span> <span class="p">{},</span> <span class="nx">args</span><span class="p">[</span><span class="nx">i</span><span class="p">][</span><span class="nx">prop</span><span class="p">]</span> <span class="p">)</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>shallow copy</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="nx">prop</span><span class="p">]</span> <span class="o">=</span> <span class="nx">args</span><span class="p">[</span><span class="nx">i</span><span class="p">][</span><span class="nx">prop</span><span class="p">]</span>
View
17 docs/key.html
@@ -34,15 +34,14 @@
if it matches the key conditions</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">url</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">string</span> <span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">self</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">string</span><span class="p">))</span> <span class="p">{</span>
<span class="cm">/*</span>
-<span class="cm"> -- no longer needed </span>
+<span class="cm"> -- no longer needed</span>
<span class="cm"> snake_caseify the controller, if there is one</span>
<span class="cm"> if (self.name == &#39;controller&#39;) return snakeize(string)</span>
<span class="cm"> */</span>
<span class="k">return</span> <span class="nx">string</span>
<span class="p">}</span>
<span class="k">return</span> <span class="kc">false</span> <span class="c1">// doesn&#39;t match, let it go</span>
- <span class="p">};</span>
- </pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <h2>key.where( conditions )</h2>
+ <span class="p">};</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <h2>key.where( conditions )</h2>
<p>adds conditions that the key must match</p>
@@ -51,8 +50,16 @@
<span class="kd">var</span> <span class="nx">condition</span> <span class="o">=</span> <span class="nx">conditions</span><span class="p">[</span><span class="nx">self</span><span class="p">.</span><span class="nx">name</span><span class="p">]</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">condition</span> <span class="k">instanceof</span> <span class="nb">RegExp</span><span class="p">)</span> <span class="nx">self</span><span class="p">.</span><span class="nx">regex</span> <span class="o">=</span> <span class="nx">condition</span> <span class="c1">// e.g. /\d+/</span>
-
- <span class="k">if</span> <span class="p">(</span><span class="nx">condition</span> <span class="k">instanceof</span> <span class="nb">String</span><span class="p">)</span> <span class="nx">self</span><span class="p">.</span><span class="nx">regex</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="nx">condition</span><span class="p">)</span> <span class="c1">// e.g. &quot;/\d+/&quot;</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>an array of allowed values, e.g. ['stop','play','pause']</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">condition</span> <span class="k">instanceof</span> <span class="nb">Array</span><span class="p">)</span> <span class="nx">self</span><span class="p">.</span><span class="nx">regex</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="o">+</span><span class="nx">condition</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39;|&#39;</span><span class="p">)</span><span class="o">+</span><span class="s1">&#39;/&#39;</span><span class="p">)</span>
+
+ <span class="k">if</span> <span class="p">(</span><span class="nx">condition</span> <span class="k">instanceof</span> <span class="nb">String</span><span class="p">)</span> <span class="nx">self</span><span class="p">.</span><span class="nx">regex</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="nx">condition</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/^\//</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">).</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\/[gis]?$/</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">))</span> <span class="c1">// e.g. &quot;\d+&quot;</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>an array of allowed values, e.g. ['stop','play','pause']</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">condition</span> <span class="k">instanceof</span> <span class="nb">Array</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">condition</span> <span class="o">=</span> <span class="nx">condition</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">cond</span><span class="p">){</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">cond</span> <span class="k">instanceof</span> <span class="nb">RegExp</span><span class="p">)</span> <span class="k">return</span> <span class="nx">cond</span><span class="p">.</span><span class="nx">toString</span><span class="p">()</span>
+ <span class="k">return</span> <span class="nx">cond</span>
+ <span class="p">}).</span><span class="nx">map</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">cond</span><span class="p">){</span>
+ <span class="k">return</span> <span class="nx">cond</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/^\//</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">).</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\/[gis]?$/</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
+ <span class="p">})</span>
+ <span class="nx">self</span><span class="p">.</span><span class="nx">regex</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="nx">condition</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39;|&#39;</span><span class="p">))</span>
+ <span class="p">}</span>
<span class="k">return</span> <span class="nx">self</span>
<span class="p">}</span>
View
47 docs/route.html
@@ -36,7 +36,7 @@
<p>builds &amp; tests on a full regex of the entire path</p>
-<pre><code>route.test( '/products/19/edit' )
+<pre><code>route.test( '/products/19/edit' )
=&gt; true
</code></pre>
@@ -63,9 +63,9 @@
<span class="nx">endpoint</span> <span class="o">=</span> <span class="kc">undefined</span>
<span class="p">}</span>
- <span class="cm">/* </span>
-<span class="cm"> TODO: make endpoint optional, since you can have the </span>
-<span class="cm"> controller &amp; action in the URL utself, </span>
+ <span class="cm">/*</span>
+<span class="cm"> TODO: make endpoint optional, since you can have the</span>
+<span class="cm"> controller &amp; action in the URL utself,</span>
<span class="cm"> even though that&#39;s a terrible idea...</span>
<span class="cm"> */</span>
@@ -110,45 +110,42 @@
<span class="p">}</span>
<span class="k">return</span> <span class="nx">self</span> <span class="c1">// chainable</span>
- <span class="p">};</span>
- </pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">&#182;</a> </div> <h2>route.stringify( params )</h2>
+ <span class="p">};</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">&#182;</a> </div> <h2>route.stringify( params )</h2>
<p>builds a string url for this Route from a params object</p>
<p>returns: [ "url", [leftover params] ]</p>
<p><strong>this is meant to be called &amp; modified by router.url()</strong></p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">stringify</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">params</span> <span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">url</span> <span class="o">=</span> <span class="p">[]</span> <span class="c1">// urls start life as an array to enble a second pass</span>
-
+
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">self</span><span class="p">.</span><span class="nx">parts</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">part</span> <span class="o">=</span> <span class="nx">self</span><span class="p">.</span><span class="nx">parts</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">part</span> <span class="k">instanceof</span> <span class="nx">Key</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span><span class="p">(</span><span class="nx">params</span><span class="p">[</span><span class="nx">part</span><span class="p">.</span><span class="nx">name</span><span class="p">])</span> <span class="o">!=</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">&amp;&amp;</span>
- <span class="nx">part</span><span class="p">.</span><span class="nx">regex</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">params</span><span class="p">[</span><span class="nx">part</span><span class="p">.</span><span class="nx">name</span><span class="p">]))</span> <span class="p">{</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>there's a param named this &amp;&amp; the param matches the key's regex</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">url</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">part</span><span class="p">.</span><span class="nx">url</span><span class="p">(</span><span class="nx">params</span><span class="p">[</span><span class="nx">part</span><span class="p">.</span><span class="nx">name</span><span class="p">]));</span> <span class="c1">// push it onto the stack </span>
- <span class="k">delete</span> <span class="nx">params</span><span class="p">[</span><span class="nx">part</span><span class="p">.</span><span class="nx">name</span><span class="p">]</span> <span class="c1">// and remove from list of params </span>
+ <span class="nx">part</span><span class="p">.</span><span class="nx">regex</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">params</span><span class="p">[</span><span class="nx">part</span><span class="p">.</span><span class="nx">name</span><span class="p">]))</span> <span class="p">{</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>there's a param named this &amp;&amp; the param matches the key's regex</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">url</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">part</span><span class="p">.</span><span class="nx">url</span><span class="p">(</span><span class="nx">params</span><span class="p">[</span><span class="nx">part</span><span class="p">.</span><span class="nx">name</span><span class="p">]));</span> <span class="c1">// push it onto the stack</span>
+ <span class="k">delete</span> <span class="nx">params</span><span class="p">[</span><span class="nx">part</span><span class="p">.</span><span class="nx">name</span><span class="p">]</span> <span class="c1">// and remove from list of params</span>
<span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">self</span><span class="p">.</span><span class="nx">optional</span><span class="p">)</span> <span class="p">{</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>(sub)route doesn't match, move on</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="kc">false</span>
<span class="p">}</span>
<span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">part</span> <span class="k">instanceof</span> <span class="nx">Route</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-19">&#182;</a> </div> <p>sub-routes must be handled in the next pass
to avoid leftover param duplication</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">url</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">part</span><span class="p">)</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="c1">// string</span>
<span class="nx">url</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">part</span><span class="p">)</span>
<span class="p">}</span>
- <span class="p">}</span>
- </pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">&#182;</a> </div> <p>second pass, resolve optional parts</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">url</span><span class="p">)</span> <span class="p">{</span>
- <span class="k">if</span> <span class="p">(</span><span class="nx">url</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="k">instanceof</span> <span class="nx">Route</span><span class="p">)</span> <span class="p">{</span>
- <span class="nx">url</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nx">url</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">params</span><span class="p">)</span> <span class="c1">// recursion is your friend</span></pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">&#182;</a> </div> <p>it resolved to a url fragment!</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">url</span><span class="p">[</span><span class="nx">i</span><span class="p">])</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">&#182;</a> </div> <p>replace leftover params hash with the new, smaller leftover params hash</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">params</span> <span class="o">=</span> <span class="nx">url</span><span class="p">[</span><span class="nx">i</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">&#182;</a> </div> <p>leave only the string for joining </p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">url</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nx">url</span><span class="p">[</span><span class="nx">i</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
+ <span class="p">}</span></pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">&#182;</a> </div> <p>second pass, resolve optional parts</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">url</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">url</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="k">instanceof</span> <span class="nx">Route</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">url</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nx">url</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">params</span><span class="p">)</span> <span class="c1">// recursion is your friend</span></pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">&#182;</a> </div> <p>it resolved to a url fragment!</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">url</span><span class="p">[</span><span class="nx">i</span><span class="p">])</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">&#182;</a> </div> <p>replace leftover params hash with the new, smaller leftover params hash</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">params</span> <span class="o">=</span> <span class="nx">url</span><span class="p">[</span><span class="nx">i</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">&#182;</a> </div> <p>leave only the string for joining</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">url</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nx">url</span><span class="p">[</span><span class="nx">i</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="k">delete</span> <span class="nx">url</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="c1">// get rid of these shits</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
-
+
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">self</span><span class="p">.</span><span class="nx">params</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-24">&#182;</a> </div> <p>remove from leftovers, they're implied in the to() portion of the route</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">delete</span> <span class="nx">params</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span>
<span class="p">}</span>
<span class="k">return</span> <span class="p">[</span> <span class="nx">url</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">),</span> <span class="nx">params</span> <span class="p">]</span>
- <span class="p">};</span>
- </pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-25">&#182;</a> </div> <h2>route.keysAndRoutes()</h2>
+ <span class="p">};</span></pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-25">&#182;</a> </div> <h2>route.keysAndRoutes()</h2>
<p>just the parts that aren't strings. basically</p>
@@ -182,13 +179,12 @@
<p>returns: a params hash || false (if the route doesn't match)</p>
-<p><strong>this is meant to be called by Router.first() &amp;&amp; Router.all()</strong></p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">parse</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">urlParam</span><span class="p">,</span> <span class="nx">method</span> <span class="p">)</span> <span class="p">{</span>
- </pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-28">&#182;</a> </div> <p>parse the URL with the regex &amp; step along with the parts,
-assigning the vals from the url to the names of the keys as we go (potentially stoopid)</p> </td> <td class="code"> <div class="highlight"><pre> </pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">&#182;</a> </div> <p>let's chop off the QS to make life easier</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">url</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;url&#39;</span><span class="p">).</span><span class="nx">parse</span><span class="p">(</span><span class="nx">urlParam</span><span class="p">)</span>
+<p><strong>this is meant to be called by Router.first() &amp;&amp; Router.all()</strong></p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">parse</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">urlParam</span><span class="p">,</span> <span class="nx">method</span> <span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-28">&#182;</a> </div> <p>parse the URL with the regex &amp; step along with the parts,
+assigning the vals from the url to the names of the keys as we go (potentially stoopid)</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">&#182;</a> </div> <p>let's chop off the QS to make life easier</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">url</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;url&#39;</span><span class="p">).</span><span class="nx">parse</span><span class="p">(</span><span class="nx">urlParam</span><span class="p">)</span>
<span class="kd">var</span> <span class="nx">path</span> <span class="o">=</span> <span class="nx">url</span><span class="p">.</span><span class="nx">pathname</span>
<span class="kd">var</span> <span class="nx">params</span> <span class="o">=</span> <span class="p">{</span><span class="nx">method</span><span class="o">:</span><span class="nx">method</span><span class="p">}</span>
-
- <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">key</span> <span class="k">in</span> <span class="nx">self</span><span class="p">.</span><span class="nx">params</span><span class="p">)</span> <span class="p">{</span> <span class="nx">params</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">self</span><span class="p">.</span><span class="nx">params</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="p">}</span> </pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">&#182;</a> </div> <p>if the method doesn't match, gtfo immediately</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">self</span><span class="p">.</span><span class="nx">method</span> <span class="o">!=</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">&amp;&amp;</span> <span class="nx">self</span><span class="p">.</span><span class="nx">method</span> <span class="o">!=</span> <span class="nx">params</span><span class="p">.</span><span class="nx">method</span><span class="p">)</span> <span class="k">return</span> <span class="kc">false</span>
+
+ <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">key</span> <span class="k">in</span> <span class="nx">self</span><span class="p">.</span><span class="nx">params</span><span class="p">)</span> <span class="p">{</span> <span class="nx">params</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">self</span><span class="p">.</span><span class="nx">params</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="p">}</span></pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">&#182;</a> </div> <p>if the method doesn't match, gtfo immediately</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">self</span><span class="p">.</span><span class="nx">method</span> <span class="o">!=</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">&amp;&amp;</span> <span class="nx">self</span><span class="p">.</span><span class="nx">method</span> <span class="o">!=</span> <span class="nx">params</span><span class="p">.</span><span class="nx">method</span><span class="p">)</span> <span class="k">return</span> <span class="kc">false</span>
<span class="cm">/* TODO: implement substring checks for possible performance boost */</span></pre></div> </td> </tr> <tr id="section-31"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-31">&#182;</a> </div> <p>if the route doesn't match the regex, gtfo</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">self</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">path</span><span class="p">))</span> <span class="p">{</span>
<span class="k">return</span> <span class="kc">false</span>
@@ -197,7 +193,7 @@
<span class="kd">var</span> <span class="nx">keysAndRoutes</span> <span class="o">=</span> <span class="nx">self</span><span class="p">.</span><span class="nx">keysAndRoutes</span><span class="p">()</span>
- <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">keysAndRoutes</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">keysAndRoutes</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">keysAndRoutes</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="k">instanceof</span> <span class="nx">Key</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">keysAndRoutes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">test</span><span class="p">(</span><span class="nx">parts</span><span class="p">[</span><span class="nx">j</span><span class="p">]))</span> <span class="p">{</span>
<span class="nx">params</span><span class="p">[</span><span class="nx">keysAndRoutes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">parts</span><span class="p">[</span><span class="nx">j</span><span class="p">]</span>
@@ -206,7 +202,7 @@
<span class="k">if</span> <span class="p">(</span><span class="nx">keysAndRoutes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">test</span><span class="p">(</span><span class="nx">parts</span><span class="p">[</span><span class="nx">j</span><span class="p">]))</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-33"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-33">&#182;</a> </div> <p>parse the subroute</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">subparams</span> <span class="o">=</span> <span class="nx">keysAndRoutes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">parse</span><span class="p">(</span><span class="nx">parts</span><span class="p">[</span><span class="nx">j</span><span class="p">],</span> <span class="nx">method</span><span class="p">)</span>
<span class="nx">mixin</span><span class="p">(</span><span class="nx">params</span><span class="p">,</span> <span class="nx">subparams</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-34"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-34">&#182;</a> </div> <p>advance the parts pointer by the number of submatches</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">j</span><span class="o">+=</span> <span class="nx">parts</span><span class="p">[</span><span class="nx">j</span><span class="p">].</span><span class="nx">match</span><span class="p">(</span><span class="nx">keysAndRoutes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">regexString</span><span class="p">()).</span><span class="nx">length</span><span class="o">-</span><span class="mi">2</span> <span class="o">||</span> <span class="mi">0</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
- <span class="nx">j</span><span class="o">++</span><span class="p">;</span>
+ <span class="nx">j</span><span class="o">++</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="nx">j</span><span class="o">++</span><span class="p">;</span>
@@ -215,8 +211,7 @@
<span class="k">return</span> <span class="nx">params</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-35"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-35">&#182;</a> </div> <p>path parsing</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">while</span> <span class="p">(</span><span class="nx">part</span> <span class="o">=</span> <span class="nx">PARTS</span><span class="p">.</span><span class="nx">exec</span><span class="p">(</span><span class="nx">path</span><span class="p">))</span> <span class="p">{</span>
<span class="nx">self</span><span class="p">.</span><span class="nx">parts</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">part</span><span class="p">)</span>
- <span class="p">}</span>
- </pre></div> </td> </tr> <tr id="section-36"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-36">&#182;</a> </div> <p>have to do this in two passes due to RegExp execution limits </p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">self</span><span class="p">.</span><span class="nx">parts</span><span class="p">)</span> <span class="p">{</span>
+ <span class="p">}</span></pre></div> </td> </tr> <tr id="section-36"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-36">&#182;</a> </div> <p>have to do this in two passes due to RegExp execution limits</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">self</span><span class="p">.</span><span class="nx">parts</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">OGRP</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">self</span><span class="p">.</span><span class="nx">parts</span><span class="p">[</span><span class="nx">i</span><span class="p">]))</span> <span class="p">{</span> <span class="c1">// optional group</span>
<span class="nx">self</span><span class="p">.</span><span class="nx">parts</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Route</span><span class="p">(</span><span class="nx">OGRP</span><span class="p">.</span><span class="nx">exec</span><span class="p">(</span><span class="nx">self</span><span class="p">.</span><span class="nx">parts</span><span class="p">[</span><span class="nx">i</span><span class="p">])[</span><span class="mi">1</span><span class="p">],</span> <span class="kc">true</span><span class="p">)</span>
<span class="nx">self</span><span class="p">.</span><span class="nx">parts</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">optional</span> <span class="o">=</span> <span class="kc">true</span>
@@ -227,7 +222,7 @@
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="c1">// string</span>
<span class="nx">self</span><span class="p">.</span><span class="nx">parts</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nb">String</span><span class="p">(</span><span class="nx">self</span><span class="p">.</span><span class="nx">parts</span><span class="p">[</span><span class="nx">i</span><span class="p">])</span>
<span class="p">}</span>
- <span class="p">}</span>
+ <span class="p">}</span>
<span class="k">return</span> <span class="nx">self</span>
View
53 docs/router.html
@@ -18,8 +18,7 @@
<span class="kd">var</span> <span class="nx">METHODS</span> <span class="o">=</span> <span class="sr">/^(GET|POST|PUT|DELETE)$/i</span><span class="p">,</span>
<span class="nx">self</span> <span class="o">=</span> <span class="k">this</span>
- <span class="k">this</span><span class="p">.</span><span class="nx">routes</span> <span class="o">=</span> <span class="p">[]</span>
- </pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <h2>router.match( path [, method] )</h2>
+ <span class="k">this</span><span class="p">.</span><span class="nx">routes</span> <span class="o">=</span> <span class="p">[]</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <h2>router.match( path [, method] )</h2>
<pre><code>router.match('/:controller/:action(/:id)(.:format)', 'GET')
.to(......)
@@ -84,9 +83,7 @@
<span class="nx">self</span><span class="p">.</span><span class="nx">put</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="o">+</span><span class="nx">controller_slug</span><span class="o">+</span><span class="s1">&#39;/:id(.:format)&#39;</span><span class="p">,</span> <span class="s1">&#39;PUT&#39;</span><span class="p">).</span><span class="nx">to</span><span class="p">(</span><span class="nx">controller</span><span class="o">+</span><span class="s1">&#39;.update&#39;</span><span class="p">),</span>
<span class="nx">self</span><span class="p">.</span><span class="k">delete</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="o">+</span><span class="nx">controller_slug</span><span class="o">+</span><span class="s1">&#39;/:id(.:format)&#39;</span><span class="p">,</span> <span class="s1">&#39;DELETE&#39;</span><span class="p">).</span><span class="nx">to</span><span class="p">(</span><span class="nx">controller</span><span class="o">+</span><span class="s1">&#39;.destroy&#39;</span><span class="p">)</span>
<span class="p">];</span>
- <span class="p">};</span>
-
- </pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <h2>router.first( path, method, callback )</h2>
+ <span class="p">};</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <h2>router.first( path, method, callback )</h2>
<p>find the first route that match the path &amp; method</p>
@@ -101,21 +98,19 @@
<pre><code>callback( error, params )
</code></pre> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">first</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">path</span><span class="p">,</span> <span class="nx">method</span><span class="p">,</span> <span class="nx">cb</span> <span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">params</span> <span class="o">=</span> <span class="kc">false</span>
-
- <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">self</span><span class="p">.</span><span class="nx">routes</span><span class="p">)</span> <span class="p">{</span>
- </pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p>attempt the parse</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">params</span> <span class="o">=</span> <span class="nx">self</span><span class="p">.</span><span class="nx">routes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">parse</span><span class="p">(</span><span class="nx">path</span><span class="p">,</span> <span class="nx">method</span><span class="p">)</span>
+
+ <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">self</span><span class="p">.</span><span class="nx">routes</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p>attempt the parse</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">params</span> <span class="o">=</span> <span class="nx">self</span><span class="p">.</span><span class="nx">routes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">parse</span><span class="p">(</span><span class="nx">path</span><span class="p">,</span> <span class="nx">method</span><span class="p">)</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">params</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p>fire the callback if given</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">cb</span> <span class="o">==</span> <span class="s1">&#39;function&#39;</span><span class="p">)</span> <span class="nx">cb</span><span class="p">(</span><span class="kc">false</span><span class="p">,</span> <span class="nx">params</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">&#182;</a> </div> <p>may as well return this</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="nx">params</span>
<span class="p">}</span>
-
+
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">cb</span> <span class="o">==</span> <span class="s1">&#39;function&#39;</span><span class="p">)</span> <span class="nx">cb</span><span class="p">(</span><span class="s1">&#39;No matching routes found&#39;</span><span class="p">)</span>
<span class="k">return</span> <span class="kc">false</span>
- <span class="p">};</span>
- </pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">&#182;</a> </div> <h2>router.all( path [, method] )</h2>
+ <span class="p">};</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">&#182;</a> </div> <h2>router.all( path [, method] )</h2>
<p>find &amp; return a params hash from ALL routes that match</p>
-<pre><code>router.all( '/products/5' )
+<pre><code>router.all( '/products/5' )
=&gt; [
{ controller: 'products', action: 'show', id: 5, method: 'GET' },
@@ -133,39 +128,37 @@
<span class="k">if</span> <span class="p">(</span><span class="nx">params</span><span class="p">)</span> <span class="nx">ret</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">params</span><span class="p">)</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">ret</span><span class="p">;</span>
- <span class="p">};</span>
- </pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">&#182;</a> </div> <h2>router.url( params[, add_querystring=false] )</h2>
+ <span class="p">};</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">&#182;</a> </div> <h2>router.url( params[, add_querystring=false] )</h2>
<p>generates a URL from a params hash</p>
-<pre><code>router.url( {
- controller: 'products',
- action: 'show',
+<pre><code>router.url( {
+ controller: 'products',
+ action: 'show',
id: 5
} )
=&gt; '/products/5'
router.url( {
- controller: 'products',
- action: 'show',
- id: 5,
+ controller: 'products',
+ action: 'show',
+ id: 5,
format: 'json'
} )
=&gt; '/products/5.json'
-router.url({
- controller: 'products',
- action: 'show',
- id: 5,
- format: 'json',
- love: 'cheese'
+router.url({
+ controller: 'products',
+ action: 'show',
+ id: 5,
+ format: 'json',
+ love: 'cheese'
}, true )
=&gt; '/products/5.json?love=cheese'
</code></pre>
<p>returns false if there are no suitable routes</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">url</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">params</span><span class="p">,</span> <span class="nx">add_querystring</span> <span class="p">)</span> <span class="p">{</span>
- <span class="kd">var</span> <span class="nx">url</span> <span class="o">=</span> <span class="kc">false</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>iterate through the existing routes until a suitable match is found</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">self</span><span class="p">.</span><span class="nx">routes</span><span class="p">)</span> <span class="p">{</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>do the controller &amp; acton match?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span><span class="p">(</span><span class="nx">self</span><span class="p">.</span><span class="nx">routes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">params</span><span class="p">.</span><span class="nx">controller</span><span class="p">)</span> <span class="o">!=</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">&amp;&amp;</span>
+ <span class="kd">var</span> <span class="nx">url</span> <span class="o">=</span> <span class="kc">false</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>iterate through the existing routes until a suitable match is found</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">self</span><span class="p">.</span><span class="nx">routes</span><span class="p">)</span> <span class="p">{</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>do the controller &amp; acton match?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span><span class="p">(</span><span class="nx">self</span><span class="p">.</span><span class="nx">routes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">params</span><span class="p">.</span><span class="nx">controller</span><span class="p">)</span> <span class="o">!=</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">&amp;&amp;</span>
<span class="nx">self</span><span class="p">.</span><span class="nx">routes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">params</span><span class="p">.</span><span class="nx">controller</span> <span class="o">!=</span> <span class="nx">params</span><span class="p">.</span><span class="nx">controller</span><span class="p">)</span> <span class="p">{</span>
<span class="k">continue</span>
<span class="p">}</span>
@@ -177,7 +170,7 @@
<span class="k">if</span> <span class="p">(</span><span class="nx">url</span><span class="p">)</span> <span class="p">{</span>
<span class="k">break</span><span class="p">;</span>
<span class="p">}</span>
- <span class="p">}</span>
+ <span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">url</span><span class="p">)</span> <span class="k">return</span> <span class="kc">false</span> <span class="c1">// no love? return false</span>
@@ -199,7 +192,7 @@
<p><strong>THIS IS CURRENTLY COMPLETELY UNTESTED. IT MIGHT NOT EVEN WORK. SERIOUSLY.</strong></p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">defer</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">fn</span> <span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span> <span class="k">typeof</span><span class="p">(</span><span class="nx">fn</span><span class="p">)</span> <span class="o">!=</span> <span class="s1">&#39;function&#39;</span> <span class="p">)</span> <span class="k">throw</span> <span class="s1">&#39;Router.defer requires a function as the only argument&#39;</span>
-
+
<span class="kd">var</span> <span class="nx">route</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Route</span><span class="p">(</span><span class="s1">&#39;deferred&#39;</span><span class="p">)</span>
<span class="nx">route</span><span class="p">.</span><span class="nx">parse</span> <span class="o">=</span> <span class="nx">fn</span> <span class="c1">// add the custom parser</span>
<span class="k">delete</span> <span class="nx">route</span><span class="p">.</span><span class="nx">test</span> <span class="c1">// = function(){return false};</span>
View
13 lib/key.js
@@ -67,9 +67,18 @@ var Key = function( name, optional ) {
if (condition instanceof RegExp) self.regex = condition // e.g. /\d+/
- if (condition instanceof String) self.regex = new RegExp(condition) // e.g. "/\d+/"
+ if (condition instanceof String) self.regex = new RegExp(condition.replace(/^\//, '').replace(/\/[gis]?$/, '')) // e.g. "\d+"
+
// an array of allowed values, e.g. ['stop','play','pause']
- if (condition instanceof Array) self.regex = new RegExp('/'+condition.join('|')+'/')
+ if (condition instanceof Array) {
+ condition = condition.map(function(cond){
+ if (cond instanceof RegExp) return cond.toString()
+ return cond
+ }).map(function(cond){
+ return cond.replace(/^\//, '').replace(/\/[gis]?$/, '')
+ })
+ self.regex = new RegExp(condition.join('|'))
+ }
return self
}
View
60 tests/barista.test.js
@@ -115,6 +115,18 @@ RouterTests = {
assert.ok(route, this.fail)
},
+ // create a static route with key match requirements as an array of strings
+ 'test Route With An Array of String Reqs' : function() {
+ var route = router.match('/:controller/:action/:id').where( { id: [ 'bob', 'frank', 'ted' ] } );
+ assert.ok(route, this.fail)
+ },
+
+ // create a static route with key match requirements as a mixed array
+ 'test Route With An Array of Mixed Reqs' : function() {
+ var route = router.match('/:controller/:action/:id').where( { id: [ /\d{1}/, '\\d\\d', '123' ] } );
+ assert.ok(route, this.fail)
+ },
+
// create a static route with key match requirements AND a method
'test Route With Reqs And Method' : function() {
var route = router.match('/:controller/:action/:id', 'GET').where( { id: /\d+/ } );
@@ -146,7 +158,7 @@ RouterTests = {
},
// test that the route accepts a regexp parameter
- 'test Simple Route Parses with conditions' : function() {
+ 'test Simple Route Parses with regex condition' : function() {
var route = router.match('/:controller/:action/:id').where( { id: /\d+/ } );
var params = router.first('/products/show/1','GET');
assert.ok(params, this.fail);
@@ -160,6 +172,52 @@ RouterTests = {
});
},
+ // test that the route accepts a string regex condition
+ 'test Simple Route Parses with string regex condition' : function() {
+ var route = router.match('/:controller/:action/:id').where( { id: '\\d+' } );
+ var params = router.first('/products/show/1','GET');
+ assert.ok(params, this.fail);
+ assert.equal(params.controller, 'products', this.fail);
+ assert.equal(params.action, 'show', this.fail);
+ assert.equal(params.id, 1, this.fail);
+ assert.equal(params.method, 'GET', this.fail);
+
+ bench(function(){
+ router.first('/products/show/1','GET');
+ });
+ },
+
+ // test that the route accepts a string condition
+ 'test Simple Route Parses with string condition' : function() {
+ var route = router.match('/:controller/:action/:id').where( { id: '1' } );
+ var params = router.first('/products/show/1','GET');
+ assert.ok(params, this.fail);
+ assert.equal(params.controller, 'products', this.fail);
+ assert.equal(params.action, 'show', this.fail);
+ assert.equal(params.id, 1, this.fail);
+ assert.equal(params.method, 'GET', this.fail);
+
+ bench(function(){
+ router.first('/products/show/1','GET');
+ });
+ },
+
+ // test that the route accepts an array of mixed condition
+ 'test Simple Route Parses with an array of mixed conditions' : function() {
+ var route = router.match('/:controller/:action/:id')
+ .where({ id: [ '\\d\\d', /\d{1}/, '123' ] });
+ var params = router.first('/products/show/1','GET');
+ assert.ok(params, this.fail);
+ assert.equal(params.controller, 'products', this.fail);
+ assert.equal(params.action, 'show', this.fail);
+ assert.equal(params.id, 1, this.fail);
+ assert.equal(params.method, 'GET', this.fail);
+
+ bench(function(){
+ router.first('/products/show/1','GET');
+ });
+ },
+
// test that the route rejects a bad regexp parameter
'test Simple Route fails to Parse with bad conditions' : function() {
var route = router.match('/:controller/:action/:id').where( { id: /\d+/ } );

0 comments on commit 38958d8

Please sign in to comment.
Something went wrong with that request. Please try again.