About
route-recognizer
is a lightweight JavaScript library (under 2k!) that
can be used as the recognizer for a more comprehensive router system
(such as router.js
).
In keeping with the Unix philosophy, it is a modular library that does one thing and does it well.
Usage
Create a new router:
var router = ;
Add a simple new route description:
router;
Every route can optionally have a name:
router;
The handler is an opaque object with no specific meaning to
route-recognizer
. A module using route-recognizer
could
use functions or other objects with domain-specific semantics
for what to do with the handler.
A route description can have handlers at various points along the path:
router;
Recognizing a route will return a list of the handlers and their associated parameters:
var result = router;result === handler: admin params: {} handler: posts params: {} ;
Dynamic segments:
router; result = router;result === handler: posts params: id: "1" handler: comments params: {} ;
A dynamic segment matches any character but /
.
Star segments:
router; result = router;result === handler: page params: path: "hello/world" ;
Sorting
If multiple routes all match a path, route-recognizer
will pick the one with the fewest dynamic segments:
router;router;router; var result1 = router;result1 === handler: editPost params: {} ; var result2 = router;result2 === handler: showPost params: id: "1" ; var result3 = router;result3 === handler: newPost params: {} ;
As you can see, this has the expected result. Explicit static paths match more closely than dynamic paths.
This is also true when comparing star segments and other dynamic segments. The recognizer will prefer fewer star segments and prefer using them for less of the match (and, consequently, using dynamic and static segments for more of the match).
Building / Running Tests
This project uses Ember CLI and Broccoli for building and testing.
Getting Started
Run the following commands to get going:
npm installbower install
The above assumes that you have bower
installed globally (you can install
via npm install -g bower
if you do not).
Running Tests
Run the following:
npm start
At this point you can navigate to the url specified in the Testem UI (usually http://localhost:7357/). As you change the project the tests will rerun.
Building
npm run build