Skip to content

Commit

Permalink
Create gh-pages branch via GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
haggen committed Mar 13, 2013
1 parent a6d2606 commit 32b1f3a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 21 deletions.
78 changes: 58 additions & 20 deletions index.html
Expand Up @@ -34,23 +34,45 @@ <h1>WayJS</h1>

<p>Lightweight and flexible URL pattern matching in JavaScript.</p>

<h2>Tests:</h2>
<h2>Test:</h2>

<p>You'll need <a href="https://github.com/visionmedia/mocha">mocha</a> to run the tests.</p>

<p>Run <code>$ npm install -g mocha</code>. The <code>-g</code> flag tells the NPM to install the module globally so mocha's binaries goes in your <code>/usr/bin</code> directory.</p>
<p>If you already have <code>mocha</code> installed globally you can simply run <code>$ mocha</code>.</p>

<p>Then run <code>$ mocha</code>.</p>
<p>If not, you can install the dependencies with <code>$ npm install</code> and then run <code>$ npm test</code>. No need to install <code>mocha</code> with <code>-g</code> or anything.</p>

<h2>Minify:</h2>

<p>You'll need <a href="https://github.com/mishoo/UglifyJS">uglify-js</a> to minify the script.</p>

<p>Install it with <code>$ npm install -g uglify-js</code>, then run <code>$ make</code>.</p>
<p>Install the dependencies with <code>$ npm install</code> and then run <code>$ npm run minify</code>.</p>

<h2>Usage:</h2>

<p>Register a new route:</p>
<p>First thing is to create a new instance.</p>

<p>In browser:</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">way</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Way</span><span class="p">();</span>
</pre></div>

<p>With AMD/require.js:</p>

<div class="highlight"><pre><span class="nx">require</span><span class="p">(</span><span class="s1">'way'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">Way</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">way</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Way</span><span class="p">();</span>
<span class="p">});</span>
</pre></div>

<p>Or with Node:</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">Way</span><span class="p">,</span> <span class="nx">way</span><span class="p">;</span>

<span class="nx">Way</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'way'</span><span class="p">);</span>
<span class="nx">way</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Way</span><span class="p">();</span>
</pre></div>

<p>Now, to register a new route:</p>

<div class="highlight"><pre><span class="nx">way</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="s1">'/hello/world'</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'Hello, world'</span><span class="p">);</span>
Expand All @@ -62,31 +84,39 @@ <h2>Usage:</h2>
<div class="highlight"><pre><span class="kd">var</span> <span class="nx">match</span> <span class="o">=</span> <span class="nx">way</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="s1">'/hello/world'</span><span class="p">);</span>
</pre></div>

<p>This will return the first route to match or <code>undefined</code> if none.</p>

<p>The route object has the collection of actions and eventually parsed parameters.</p>
<p>This will return an <code>object</code> or <code>undefined</code> if no route matches. This object has the collection of actions and parameters.</p>

<div class="highlight"><pre><span class="nx">match</span><span class="p">.</span><span class="nx">actions</span><span class="p">;</span> <span class="c1">//-&gt; [function() { console.log('Hello, world'); }]</span>
<span class="nx">match</span><span class="p">.</span><span class="nx">params</span><span class="p">;</span> <span class="c1">//-&gt; {}</span>
</pre></div>

<p>You can provide multiple actions.</p>
<p>You can provide <code>n</code> actions when mapping a route.</p>

<div class="highlight"><pre><span class="nx">way</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="s1">'/hello/world'</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><span class="cm">/* 1 */</span><span class="p">},</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><span class="cm">/* 2 */</span><span class="p">});</span>
<div class="highlight"><pre><span class="nx">way</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="s1">'/hello/world'</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><span class="cm">/* 1 */</span><span class="p">},</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span><span class="cm">/* 2 */</span><span class="p">},</span> <span class="p">...);</span>
</pre></div>

<p>That way you have control over the flow and do things like if the first action do not return true, it won't call the next one, or pass the returning value from the current action to the next one, or anything else that suits you. I told you it was flexible. :)</p>
<p>Once you have a match, WayJS get out of the way. What to do with the action and parameters is totally up to you.</p>

<p>WayJS works in both browser and Node.</p>
<p>A common approach is to iterate over the actions, passing the parameters, and if one of them return false, you break the chain:</p>

<div class="highlight"><pre><span class="k">for</span><span class="p">(</span><span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">t</span> <span class="o">=</span> <span class="nx">match</span><span class="p">.</span><span class="nx">actions</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">t</span><span class="o">--</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">if</span><span class="p">(</span><span class="nx">match</span><span class="p">.</span><span class="nx">actions</span><span class="p">[</span><span class="nx">i</span><span class="p">](</span><span class="nx">match</span><span class="p">.</span><span class="nx">params</span><span class="p">)</span> <span class="o">===</span> <span class="kc">false</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>

</pre></div>

<p>But that's just a simple suggestion. You can do whatever you need.</p>

<h2>Pattern syntax:</h2>

<h3>Named parameters</h3>

<p>Capture anything except forward slashes and save in <code>way.params</code> with given name.</p>
<p>Capture anything except forward slashes and save in <code>params</code> with given name.</p>

<pre><code>way.map('/log/:message', function() {
console.log(way.params.message);
<pre><code>way.map('/log/:message', function(params) {
console.log(params.message);
});
</code></pre>

Expand All @@ -101,18 +131,26 @@ <h3>Optional groups</h3>

<h3>Splats</h3>

<p>Capture everything, including slashes and save in <code>way.params.splat</code>. You can include multiple splats, returning an array.</p>
<p>Capture everything, including slashes and save in <code>params.splat</code>. You can include multiple splats, returning an array.</p>

<pre><code>way.map('/goto/*', function() {
console.log('Goto: ', way.params.splat[0]);
<pre><code>way.map('/goto/*', function(params) {
console.log('Goto: ', params.splat[0]);
});
</code></pre>

<p>All the special syntaxes above can be combined to create powerful routing patterns.</p>
<p>All the special syntaxes above can be combined to create powerful matching patterns.</p>

<h2>Changelog:</h2>

<h3>v0.3.4 2012-09-06</h3>
<h3>v0.4.0 2013-03-13</h3>

<ul>
<li>Now Way constructor is properly exposed instead of an instance</li>
<li>Moved <code>routes</code> out of the constructor to the prototype</li>
<li>Fixed docs typos and outdated information</li>
<li>Updated tests accordingly</li>
<li>Dropped Makefile and in favor of npm scripts</li>
</ul><h3>v0.3.4 2012-09-06</h3>

<ul>
<li>Fixed bug when casting #map arguments as array</li>
Expand Down
2 changes: 1 addition & 1 deletion params.json
@@ -1 +1 @@
{"name":"WayJS","tagline":"Lightweight and flexible URL pattern matching in JavaScript.","body":"# WayJS\r\n\r\nLightweight and flexible URL pattern matching in JavaScript.\r\n\r\n## Tests:\r\n\r\nYou'll need [mocha](https://github.com/visionmedia/mocha) to run the tests.\r\n\r\nRun `$ npm install -g mocha`. The `-g` flag tells the NPM to install the module globally so mocha's binaries goes in your `/usr/bin` directory.\r\n\r\nThen run `$ mocha`.\r\n\r\n## Minify:\r\n\r\nYou'll need [uglify-js](https://github.com/mishoo/UglifyJS) to minify the script.\r\n\r\nInstall it with `$ npm install -g uglify-js`, then run `$ make`.\r\n\r\n## Usage:\r\n\r\nRegister a new route:\r\n\r\n```javascript\r\nway.map('/hello/world', function() {\r\n console.log('Hello, world');\r\n});\r\n```\r\n\r\nThen match some path against route table:\r\n\r\n```javascript\r\nvar match = way.match('/hello/world');\r\n```\r\n\r\nThis will return the first route to match or `undefined` if none.\r\n\r\nThe route object has the collection of actions and eventually parsed parameters.\r\n\r\n```javascript\r\nmatch.actions; //-> [function() { console.log('Hello, world'); }]\r\nmatch.params; //-> {}\r\n```\r\n\r\nYou can provide multiple actions.\r\n\r\n```javascript\r\nway.map('/hello/world', function() {/* 1 */}, function() {/* 2 */});\r\n```\r\n\r\nThat way you have control over the flow and do things like if the first action do not return true, it won't call the next one, or pass the returning value from the current action to the next one, or anything else that suits you. I told you it was flexible. :)\r\n\r\nWayJS works in both browser and Node.\r\n\r\n## Pattern syntax:\r\n\r\n### Named parameters\r\n\r\nCapture anything except forward slashes and save in `way.params` with given name.\r\n\r\n way.map('/log/:message', function() {\r\n console.log(way.params.message);\r\n });\r\n\r\n### Optional groups\r\n\r\nMatches with or without the snippet inside the parenthesis.\r\n\r\n way.map('(/good)/bye', function() {\r\n console.log('Farewell!!');\r\n })\r\n\r\n### Splats\r\n\r\nCapture everything, including slashes and save in `way.params.splat`. You can include multiple splats, returning an array.\r\n\r\n way.map('/goto/*', function() {\r\n console.log('Goto: ', way.params.splat[0]);\r\n });\r\n\r\nAll the special syntaxes above can be combined to create powerful routing patterns.\r\n\r\n## Changelog:\r\n\r\n### v0.3.4 2012-09-06\r\n\r\n- Fixed bug when casting #map arguments as array\r\n\r\n### v0.3.3 2012-09-05\r\n\r\n- Added support for [requirejs](http://requirejs.org/)\r\n\r\n### v0.3.2 2012-09-05\r\n\r\n- Changed to multiple actions instead of allowing multiple matches\r\n- Accepts multiple splats\r\n- Tests updated accordingly\r\n\r\n### v0.2.1 2012-08-24\r\n\r\n- Changed patterns regex to do exact matches\r\n\r\n### v0.2.0 2012-08-23\r\n\r\n- Accepts multiple matches, returning a collection of them\r\n- Fixed bug with parameter values being sliced\r\n\r\n### v0.1.0 2012-08-21\r\n\r\n- First version\r\n","google":"UA-38812126-1","note":"Don't delete this file! It's used internally to help with page regeneration."}
{"name":"WayJS","tagline":"Lightweight and flexible URL pattern matching in JavaScript.","body":"# WayJS\r\n\r\nLightweight and flexible URL pattern matching in JavaScript.\r\n\r\n## Test:\r\n\r\nYou'll need [mocha](https://github.com/visionmedia/mocha) to run the tests.\r\n\r\nIf you already have `mocha` installed globally you can simply run `$ mocha`.\r\n\r\nIf not, you can install the dependencies with `$ npm install` and then run `$ npm test`. No need to install `mocha` with `-g` or anything.\r\n\r\n## Minify:\r\n\r\nYou'll need [uglify-js](https://github.com/mishoo/UglifyJS) to minify the script.\r\n\r\nInstall the dependencies with `$ npm install` and then run `$ npm run minify`.\r\n\r\n## Usage:\r\n\r\nFirst thing is to create a new instance.\r\n\r\nIn browser:\r\n\r\n```javascript\r\nvar way = new Way();\r\n```\r\n\r\nWith AMD/require.js:\r\n\r\n```javascript\r\nrequire('way', function(Way) {\r\n var way = new Way();\r\n});\r\n```\r\n\r\nOr with Node:\r\n\r\n```javascript\r\nvar Way, way;\r\n\r\nWay = require('way');\r\nway = new Way();\r\n```\r\n\r\nNow, to register a new route:\r\n\r\n```javascript\r\nway.map('/hello/world', function() {\r\n console.log('Hello, world');\r\n});\r\n```\r\n\r\nThen match some path against route table:\r\n\r\n```javascript\r\nvar match = way.match('/hello/world');\r\n```\r\n\r\nThis will return an `object` or `undefined` if no route matches. This object has the collection of actions and parameters.\r\n\r\n```javascript\r\nmatch.actions; //-> [function() { console.log('Hello, world'); }]\r\nmatch.params; //-> {}\r\n```\r\n\r\nYou can provide `n` actions when mapping a route.\r\n\r\n```javascript\r\nway.map('/hello/world', function() {/* 1 */}, function() {/* 2 */}, ...);\r\n```\r\n\r\nOnce you have a match, WayJS get out of the way. What to do with the action and parameters is totally up to you.\r\n\r\nA common approach is to iterate over the actions, passing the parameters, and if one of them return false, you break the chain:\r\n\r\n```javascript\r\nfor(i = 0, t = match.actions.length; t--; i++) {\r\n if(match.actions[i](match.params) === false) {\r\n break;\r\n }\r\n}\r\n\r\n```\r\n\r\nBut that's just a simple suggestion. You can do whatever you need.\r\n\r\n## Pattern syntax:\r\n\r\n### Named parameters\r\n\r\nCapture anything except forward slashes and save in `params` with given name.\r\n\r\n way.map('/log/:message', function(params) {\r\n console.log(params.message);\r\n });\r\n\r\n### Optional groups\r\n\r\nMatches with or without the snippet inside the parenthesis.\r\n\r\n way.map('(/good)/bye', function() {\r\n console.log('Farewell!!');\r\n })\r\n\r\n### Splats\r\n\r\nCapture everything, including slashes and save in `params.splat`. You can include multiple splats, returning an array.\r\n\r\n way.map('/goto/*', function(params) {\r\n console.log('Goto: ', params.splat[0]);\r\n });\r\n\r\nAll the special syntaxes above can be combined to create powerful matching patterns.\r\n\r\n## Changelog:\r\n\r\n### v0.4.0 2013-03-13\r\n\r\n- Now Way constructor is properly exposed instead of an instance\r\n- Moved `routes` out of the constructor to the prototype\r\n- Fixed docs typos and outdated information\r\n- Updated tests accordingly\r\n- Dropped Makefile and in favor of npm scripts\r\n\r\n### v0.3.4 2012-09-06\r\n\r\n- Fixed bug when casting #map arguments as array\r\n\r\n### v0.3.3 2012-09-05\r\n\r\n- Added support for [requirejs](http://requirejs.org/)\r\n\r\n### v0.3.2 2012-09-05\r\n\r\n- Changed to multiple actions instead of allowing multiple matches\r\n- Accepts multiple splats\r\n- Tests updated accordingly\r\n\r\n### v0.2.1 2012-08-24\r\n\r\n- Changed patterns regex to do exact matches\r\n\r\n### v0.2.0 2012-08-23\r\n\r\n- Accepts multiple matches, returning a collection of them\r\n- Fixed bug with parameter values being sliced\r\n\r\n### v0.1.0 2012-08-21\r\n\r\n- First version\r\n","google":"UA-38812126-1","note":"Don't delete this file! It's used internally to help with page regeneration."}

0 comments on commit 32b1f3a

Please sign in to comment.