1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
[](https://travis-ci.org/tildeio/route-recognizer)
# 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`](https://github.com/tildeio/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:
```javascript
var router = new RouteRecognizer();
```
Add a simple new route description:
```javascript
router.add([{ path: "/posts", handler: handler }]);
```
Every route can optionally have a name:
```javascript
router.add([{ path: "/posts", handler: handler }], { as: "routeName"});
```
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:
```javascript
router.add([
{ path: "/admin", handler: admin },
{ path: "/posts", handler: posts }
]);
```
Recognizing a route will return a list of the handlers and
their associated parameters:
```javascript
var result = router.recognize("/admin/posts");
result === [
{ handler: admin, params: {} },
{ handler: posts, params: {} }
];
```
Dynamic segments:
```javascript
router.add([
{ path: "/posts/:id", handler: posts },
{ path: "/comments", handler: comments }
]);
result = router.recognize("/posts/1/comments");
result === [
{ handler: posts, params: { id: "1" } },
{ handler: comments, params: {} }
];
```
A dynamic segment matches any character but `/`.
Star segments:
```javascript
router.add([{ path: "/pages/*path", handler: page }]);
result = router.recognize("/pages/hello/world");
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:
```javascript
router.add([{ path: "/posts/edit", handler: editPost }]);
router.add([{ path: "/posts/:id", handler: showPost }]);
router.add([{ path: "/posts/new", handler: newPost }]);
var result1 = router.recognize("/posts/edit");
result1 === [{ handler: editPost, params: {} }];
var result2 = router.recognize("/posts/1");
result2 === [{ handler: showPost, params: { id: "1" } }];
var result3 = router.recognize("/posts/new");
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:
```bash
npm install
```
## 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
```
|