Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fixed where array bug, added docs for conditions

  • Loading branch information...
commit 38958d850cb276be7cdda5cb08a0d004efd238ad 1 parent 3ab715b
Kieran Huggins authored
43 Readme.md
Source Rendered
... ... @@ -1,4 +1,4 @@
1   -Barista is a simple URL router for nodejs.
  1 +Barista is a simple URL router for NodeJS.
2 2
3 3 Getting Barista
4 4 ===============
@@ -18,23 +18,54 @@ var Router = require('barista').Router;
18 18 var router = new Router;
19 19 ```
20 20
  21 +Adding routes
  22 +-------------
21 23
22   -### Adding routes
  24 +### A simple example
23 25
24 26 ```javascript
25   -// a basic example
26 27 router.match( '/products', 'GET' )
27 28 .to( 'products.index' )
  29 +```
  30 +
  31 +### Rails-esque variable names
28 32
29   -// Rails-style variables
  33 +```javascript
30 34 router.match( '/products/:id', 'GET' )
31 35 .to( 'products.show' )
32 36
33   -// optional parts
  37 +router.match( '/profiles/:username', 'GET' )
  38 + .to( 'users.show' )
  39 +
34 40 router.match( '/products/:id(.:format)', 'GET' )
35 41 .to( 'products.show' )
  42 +```
36 43
37   -// convenience methods
  44 +### Match conditions
  45 +
  46 +```javascript
  47 +router.match( '/:beverage/near/:zipcode', 'GET' )
  48 + .to( 'beverage.byZipCode' )
  49 + .where({
  50 + // an array of options
  51 + beverage: [ 'coffee', 'tea', 'beer', 'warm_sake' ],
  52 + // a regex pattern
  53 + zipcode: /^\d{5}(-\d{4})?$/
  54 + })
  55 +
  56 +router.match( '/:beverage/near/:location', 'GET' )
  57 + .to( 'beverage.byLocation' )
  58 + .where({
  59 + // could be a postal code
  60 + // OR a zip code
  61 + // OR the word 'me' (geolocation FTW)
  62 + location: [ /^\d{5}(-\d{4})?$/, /^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/, 'me' ]
  63 + })
  64 +```
  65 +
  66 +### Convenience methods
  67 +
  68 +```javascript
38 69 router.get( '/products/:id(.:format)' )
39 70 .to( 'products.show' )
40 71
2  docs/helpers.html
@@ -15,7 +15,7 @@
15 15 <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>
16 16
17 17 <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>
18   -
  18 +
19 19 <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>
20 20 <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>
21 21 <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>
17 docs/key.html
@@ -34,15 +34,14 @@
34 34 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>
35 35 <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>
36 36 <span class="cm">/*</span>
37   -<span class="cm"> -- no longer needed </span>
  37 +<span class="cm"> -- no longer needed</span>
38 38 <span class="cm"> snake_caseify the controller, if there is one</span>
39 39 <span class="cm"> if (self.name == &#39;controller&#39;) return snakeize(string)</span>
40 40 <span class="cm"> */</span>
41 41 <span class="k">return</span> <span class="nx">string</span>
42 42 <span class="p">}</span>
43 43 <span class="k">return</span> <span class="kc">false</span> <span class="c1">// doesn&#39;t match, let it go</span>
44   - <span class="p">};</span>
45   - </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>
  44 + <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>
46 45
47 46 <p>adds conditions that the key must match</p>
48 47
@@ -51,8 +50,16 @@
51 50 <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>
52 51
53 52 <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>
54   -
55   - <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>
  53 +
  54 + <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>
  55 + <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>
  56 + <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>
  57 + <span class="k">return</span> <span class="nx">cond</span>
  58 + <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>
  59 + <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>
  60 + <span class="p">})</span>
  61 + <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>
  62 + <span class="p">}</span>
56 63
57 64 <span class="k">return</span> <span class="nx">self</span>
58 65 <span class="p">}</span>
47 docs/route.html
@@ -36,7 +36,7 @@
36 36
37 37 <p>builds &amp; tests on a full regex of the entire path</p>
38 38
39   -<pre><code>route.test( '/products/19/edit' )
  39 +<pre><code>route.test( '/products/19/edit' )
40 40 =&gt; true
41 41 </code></pre>
42 42
@@ -63,9 +63,9 @@
63 63 <span class="nx">endpoint</span> <span class="o">=</span> <span class="kc">undefined</span>
64 64 <span class="p">}</span>
65 65
66   - <span class="cm">/* </span>
67   -<span class="cm"> TODO: make endpoint optional, since you can have the </span>
68   -<span class="cm"> controller &amp; action in the URL utself, </span>
  66 + <span class="cm">/*</span>
  67 +<span class="cm"> TODO: make endpoint optional, since you can have the</span>
  68 +<span class="cm"> controller &amp; action in the URL utself,</span>
69 69 <span class="cm"> even though that&#39;s a terrible idea...</span>
70 70 <span class="cm"> */</span>
71 71
@@ -110,8 +110,7 @@
110 110 <span class="p">}</span>
111 111
112 112 <span class="k">return</span> <span class="nx">self</span> <span class="c1">// chainable</span>
113   - <span class="p">};</span>
114   - </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>
  113 + <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>
115 114
116 115 <p>builds a string url for this Route from a params object</p>
117 116
@@ -119,13 +118,13 @@
119 118
120 119 <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>
121 120 <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>
122   -
  121 +
123 122 <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>
124 123 <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>
125 124 <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>
126 125 <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>
127   - <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>
128   - <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>
  126 + <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>
  127 + <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>
129 128 <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>
130 129 <span class="p">}</span>
131 130 <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
@@ -133,22 +132,20 @@
133 132 <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="c1">// string</span>
134 133 <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>
135 134 <span class="p">}</span>
136   - <span class="p">}</span>
137   - </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>
138   - <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>
139   - <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>
  135 + <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>
  136 + <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>
  137 + <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>
140 138 <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
141 139 <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>
142 140 <span class="p">}</span>
143 141 <span class="p">}</span>
144 142 <span class="p">}</span>
145   -
  143 +
146 144 <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>
147 145 <span class="p">}</span>
148 146
149 147 <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>
150   - <span class="p">};</span>
151   - </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>
  148 + <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>
152 149
153 150 <p>just the parts that aren't strings. basically</p>
154 151
@@ -182,13 +179,12 @@
182 179
183 180 <p>returns: a params hash || false (if the route doesn't match)</p>
184 181
185   -<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>
186   - </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,
187   -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>
  182 +<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,
  183 +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>
188 184 <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>
189 185 <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>
190   -
191   - <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>
  186 +
  187 + <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>
192 188
193 189 <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>
194 190 <span class="k">return</span> <span class="kc">false</span>
@@ -197,7 +193,7 @@
197 193
198 194 <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>
199 195
200   - <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>
  196 + <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>
201 197 <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>
202 198 <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>
203 199 <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 @@
206 202 <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>
207 203 <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>
208 204 <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
209   - <span class="nx">j</span><span class="o">++</span><span class="p">;</span>
  205 + <span class="nx">j</span><span class="o">++</span><span class="p">;</span>
210 206 <span class="p">}</span>
211 207 <span class="p">}</span>
212 208 <span class="nx">j</span><span class="o">++</span><span class="p">;</span>
@@ -215,8 +211,7 @@
215 211 <span class="k">return</span> <span class="nx">params</span>
216 212 <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>
217 213 <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>
218   - <span class="p">}</span>
219   - </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>
  214 + <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>
220 215 <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>
221 216 <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>
222 217 <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 @@
227 222 <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="c1">// string</span>
228 223 <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>
229 224 <span class="p">}</span>
230   - <span class="p">}</span>
  225 + <span class="p">}</span>
231 226
232 227 <span class="k">return</span> <span class="nx">self</span>
233 228
53 docs/router.html
@@ -18,8 +18,7 @@
18 18 <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>
19 19 <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span>
20 20
21   - <span class="k">this</span><span class="p">.</span><span class="nx">routes</span> <span class="o">=</span> <span class="p">[]</span>
22   - </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>
  21 + <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>
23 22
24 23 <pre><code>router.match('/:controller/:action(/:id)(.:format)', 'GET')
25 24 .to(......)
@@ -84,9 +83,7 @@
84 83 <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>
85 84 <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>
86 85 <span class="p">];</span>
87   - <span class="p">};</span>
88   -
89   - </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>
  86 + <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>
90 87
91 88 <p>find the first route that match the path &amp; method</p>
92 89
@@ -101,21 +98,19 @@
101 98 <pre><code>callback( error, params )
102 99 </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>
103 100 <span class="kd">var</span> <span class="nx">params</span> <span class="o">=</span> <span class="kc">false</span>
104   -
105   - <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>
106   - </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>
  101 +
  102 + <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>
107 103 <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>
108 104 <span class="p">}</span>
109   -
  105 +
110 106 <span class="p">}</span>
111 107 <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>
112 108 <span class="k">return</span> <span class="kc">false</span>
113   - <span class="p">};</span>
114   - </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>
  109 + <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>
115 110
116 111 <p>find &amp; return a params hash from ALL routes that match</p>
117 112
118   -<pre><code>router.all( '/products/5' )
  113 +<pre><code>router.all( '/products/5' )
119 114
120 115 =&gt; [
121 116 { controller: 'products', action: 'show', id: 5, method: 'GET' },
@@ -133,39 +128,37 @@
133 128 <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>
134 129 <span class="p">}</span>
135 130 <span class="k">return</span> <span class="nx">ret</span><span class="p">;</span>
136   - <span class="p">};</span>
137   - </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>
  131 + <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>
138 132
139 133 <p>generates a URL from a params hash</p>
140 134
141   -<pre><code>router.url( {
142   - controller: 'products',
143   - action: 'show',
  135 +<pre><code>router.url( {
  136 + controller: 'products',
  137 + action: 'show',
144 138 id: 5
145 139 } )
146 140 =&gt; '/products/5'
147 141
148 142 router.url( {
149   - controller: 'products',
150   - action: 'show',
151   - id: 5,
  143 + controller: 'products',
  144 + action: 'show',
  145 + id: 5,
152 146 format: 'json'
153 147 } )
154 148 =&gt; '/products/5.json'
155 149
156   -router.url({
157   - controller: 'products',
158   - action: 'show',
159   - id: 5,
160   - format: 'json',
161   - love: 'cheese'
  150 +router.url({
  151 + controller: 'products',
  152 + action: 'show',
  153 + id: 5,
  154 + format: 'json',
  155 + love: 'cheese'
162 156 }, true )
163 157 =&gt; '/products/5.json?love=cheese'
164 158 </code></pre>
165 159
166 160 <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>
167   - <span class="kd">var</span> <span class="nx">url</span> <span class="o">=</span> <span class="kc">false</span>
168   - </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>
  161 + <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>
169 162 <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>
170 163 <span class="k">continue</span>
171 164 <span class="p">}</span>
@@ -177,7 +170,7 @@
177 170 <span class="k">if</span> <span class="p">(</span><span class="nx">url</span><span class="p">)</span> <span class="p">{</span>
178 171 <span class="k">break</span><span class="p">;</span>
179 172 <span class="p">}</span>
180   - <span class="p">}</span>
  173 + <span class="p">}</span>
181 174
182 175 <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>
183 176
@@ -199,7 +192,7 @@
199 192
200 193 <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>
201 194 <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>
202   -
  195 +
203 196 <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>
204 197 <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>
205 198 <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>
13 lib/key.js
@@ -67,9 +67,18 @@ var Key = function( name, optional ) {
67 67
68 68 if (condition instanceof RegExp) self.regex = condition // e.g. /\d+/
69 69
70   - if (condition instanceof String) self.regex = new RegExp(condition) // e.g. "/\d+/"
  70 + if (condition instanceof String) self.regex = new RegExp(condition.replace(/^\//, '').replace(/\/[gis]?$/, '')) // e.g. "\d+"
  71 +
71 72 // an array of allowed values, e.g. ['stop','play','pause']
72   - if (condition instanceof Array) self.regex = new RegExp('/'+condition.join('|')+'/')
  73 + if (condition instanceof Array) {
  74 + condition = condition.map(function(cond){
  75 + if (cond instanceof RegExp) return cond.toString()
  76 + return cond
  77 + }).map(function(cond){
  78 + return cond.replace(/^\//, '').replace(/\/[gis]?$/, '')
  79 + })
  80 + self.regex = new RegExp(condition.join('|'))
  81 + }
73 82
74 83 return self
75 84 }
60 tests/barista.test.js
@@ -115,6 +115,18 @@ RouterTests = {
115 115 assert.ok(route, this.fail)
116 116 },
117 117
  118 + // create a static route with key match requirements as an array of strings
  119 + 'test Route With An Array of String Reqs' : function() {
  120 + var route = router.match('/:controller/:action/:id').where( { id: [ 'bob', 'frank', 'ted' ] } );
  121 + assert.ok(route, this.fail)
  122 + },
  123 +
  124 + // create a static route with key match requirements as a mixed array
  125 + 'test Route With An Array of Mixed Reqs' : function() {
  126 + var route = router.match('/:controller/:action/:id').where( { id: [ /\d{1}/, '\\d\\d', '123' ] } );
  127 + assert.ok(route, this.fail)
  128 + },
  129 +
118 130 // create a static route with key match requirements AND a method
119 131 'test Route With Reqs And Method' : function() {
120 132 var route = router.match('/:controller/:action/:id', 'GET').where( { id: /\d+/ } );
@@ -146,7 +158,7 @@ RouterTests = {
146 158 },
147 159
148 160 // test that the route accepts a regexp parameter
149   - 'test Simple Route Parses with conditions' : function() {
  161 + 'test Simple Route Parses with regex condition' : function() {
150 162 var route = router.match('/:controller/:action/:id').where( { id: /\d+/ } );
151 163 var params = router.first('/products/show/1','GET');
152 164 assert.ok(params, this.fail);
@@ -160,6 +172,52 @@ RouterTests = {
160 172 });
161 173 },
162 174
  175 + // test that the route accepts a string regex condition
  176 + 'test Simple Route Parses with string regex condition' : function() {
  177 + var route = router.match('/:controller/:action/:id').where( { id: '\\d+' } );
  178 + var params = router.first('/products/show/1','GET');
  179 + assert.ok(params, this.fail);
  180 + assert.equal(params.controller, 'products', this.fail);
  181 + assert.equal(params.action, 'show', this.fail);
  182 + assert.equal(params.id, 1, this.fail);
  183 + assert.equal(params.method, 'GET', this.fail);
  184 +
  185 + bench(function(){
  186 + router.first('/products/show/1','GET');
  187 + });
  188 + },
  189 +
  190 + // test that the route accepts a string condition
  191 + 'test Simple Route Parses with string condition' : function() {
  192 + var route = router.match('/:controller/:action/:id').where( { id: '1' } );
  193 + var params = router.first('/products/show/1','GET');
  194 + assert.ok(params, this.fail);
  195 + assert.equal(params.controller, 'products', this.fail);
  196 + assert.equal(params.action, 'show', this.fail);
  197 + assert.equal(params.id, 1, this.fail);
  198 + assert.equal(params.method, 'GET', this.fail);
  199 +
  200 + bench(function(){
  201 + router.first('/products/show/1','GET');
  202 + });
  203 + },
  204 +
  205 + // test that the route accepts an array of mixed condition
  206 + 'test Simple Route Parses with an array of mixed conditions' : function() {
  207 + var route = router.match('/:controller/:action/:id')
  208 + .where({ id: [ '\\d\\d', /\d{1}/, '123' ] });
  209 + var params = router.first('/products/show/1','GET');
  210 + assert.ok(params, this.fail);
  211 + assert.equal(params.controller, 'products', this.fail);
  212 + assert.equal(params.action, 'show', this.fail);
  213 + assert.equal(params.id, 1, this.fail);
  214 + assert.equal(params.method, 'GET', this.fail);
  215 +
  216 + bench(function(){
  217 + router.first('/products/show/1','GET');
  218 + });
  219 + },
  220 +
163 221 // test that the route rejects a bad regexp parameter
164 222 'test Simple Route fails to Parse with bad conditions' : function() {
165 223 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.