UNPKG

3.45 kBMarkdownView Raw
1[![Build Status](https://travis-ci.org/tildeio/route-recognizer.svg)](https://travis-ci.org/tildeio/route-recognizer)
2
3# About
4`route-recognizer` is a lightweight JavaScript library (under 2k!) that
5can be used as the recognizer for a more comprehensive router system
6(such as [`router.js`](https://github.com/tildeio/router.js)).
7
8In keeping with the Unix philosophy, it is a modular library that does one
9thing and does it well.
10
11# Usage
12
13Create a new router:
14
15```javascript
16var router = new RouteRecognizer();
17```
18
19Add a simple new route description:
20
21```javascript
22router.add([{ path: "/posts", handler: handler }]);
23```
24
25Every route can optionally have a name:
26```javascript
27router.add([{ path: "/posts", handler: handler }], { as: "routeName"});
28```
29
30The handler is an opaque object with no specific meaning to
31`route-recognizer`. A module using `route-recognizer` could
32use functions or other objects with domain-specific semantics
33for what to do with the handler.
34
35A route description can have handlers at various points along
36the path:
37
38```javascript
39router.add([
40 { path: "/admin", handler: admin },
41 { path: "/posts", handler: posts }
42]);
43```
44
45Recognizing a route will return a list of the handlers and
46their associated parameters:
47
48```javascript
49var result = router.recognize("/admin/posts");
50result === [
51 { handler: admin, params: {} },
52 { handler: posts, params: {} }
53];
54```
55
56Dynamic segments:
57
58```javascript
59router.add([
60 { path: "/posts/:id", handler: posts },
61 { path: "/comments", handler: comments }
62]);
63
64result = router.recognize("/posts/1/comments");
65result === [
66 { handler: posts, params: { id: "1" } },
67 { handler: comments, params: {} }
68];
69```
70
71A dynamic segment matches any character but `/`.
72
73Star segments:
74
75```javascript
76router.add([{ path: "/pages/*path", handler: page }]);
77
78result = router.recognize("/pages/hello/world");
79result === [{ handler: page, params: { path: "hello/world" } }];
80```
81
82# Sorting
83
84If multiple routes all match a path, `route-recognizer`
85will pick the one with the fewest dynamic segments:
86
87```javascript
88router.add([{ path: "/posts/edit", handler: editPost }]);
89router.add([{ path: "/posts/:id", handler: showPost }]);
90router.add([{ path: "/posts/new", handler: newPost }]);
91
92var result1 = router.recognize("/posts/edit");
93result1 === [{ handler: editPost, params: {} }];
94
95var result2 = router.recognize("/posts/1");
96result2 === [{ handler: showPost, params: { id: "1" } }];
97
98var result3 = router.recognize("/posts/new");
99result3 === [{ handler: newPost, params: {} }];
100```
101
102As you can see, this has the expected result. Explicit
103static paths match more closely than dynamic paths.
104
105This is also true when comparing star segments and other
106dynamic segments. The recognizer will prefer fewer star
107segments and prefer using them for less of the match (and,
108consequently, using dynamic and static segments for more
109of the match).
110
111# Building / Running Tests
112
113This project uses Ember CLI and Broccoli for building and testing.
114
115## Getting Started
116
117Run the following commands to get going:
118
119```bash
120npm install
121bower install
122```
123
124The above assumes that you have `bower` installed globally (you can install
125via `npm install -g bower` if you do not).
126
127## Running Tests
128
129Run the following:
130
131```
132npm start
133```
134
135At this point you can navigate to the url specified in the Testem UI (usually
136http://localhost:7357/). As you change the project the tests will rerun.
137
138## Building
139
140```
141npm run build
142```