• Stars
    star
    461
  • Rank 95,028 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 12 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A lightweight JavaScript library that matches paths against registered routes. It includes support for dynamic and star segments and nested handlers.

Build Status

About

route-recognizer is a lightweight JavaScript library (under 4kB gzipped!) 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 = new RouteRecognizer();

Add a simple new route description:

router.add([{ path: "/posts", handler: handler }]);

Every route can optionally have a name:

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:

router.add([
  { path: "/admin", handler: admin },
  { path: "/posts", handler: posts }
]);

Recognizing a route will return a list of the handlers and their associated parameters:

var result = router.recognize("/admin/posts");
result === [
  { handler: admin, params: {} },
  { handler: posts, params: {} }
];

Dynamic segments:

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:

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:

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:

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

More Repositories

1

rsvp.js

A lightweight library that provides tools for organizing asynchronous code
JavaScript
3,613
star
2

helix

Native Ruby extensions without fear
Rust
1,978
star
3

htmlbars

A variant of Handlebars that emits DOM and allows you to write helpers that manipulate live DOM nodes
JavaScript
1,605
star
4

router.js

TypeScript
1,346
star
5

bloggr-client

The Source for the Ember Get Excited Video
JavaScript
310
star
6

oasis.js

A lightweight library for embedding untrusted content and exposing capabilities. I guess it does two things? But it does them well.
JavaScript
281
star
7

conductor.js

JavaScript
150
star
8

simple-html-tokenizer

A lightweight JavaScript library for tokenizing non-`<script>` HTML expected to be found in the `<body>` of a document
TypeScript
84
star
9

learning-rust

Rust
67
star
10

kamino.js

Kamino.js is a library for passing data structures between sandboxed environments in the browser via `postMessage`.
JavaScript
48
star
11

ember-element-helper

Dynamic element helper for Glimmer templates.
JavaScript
42
star
12

ember-async-await-helper

Awaits a promise, then yields its result to a block. 👌
JavaScript
40
star
13

slackathon

A simple way to build slack interations inside a Rails app.
Ruby
31
star
14

MessageChannel.js

JavaScript
27
star
15

bound-templates.js

Bound Templates provides an interface similar to Polymer's MDV less invasively on more browsers (by using wrapper objects and an HTML parser written in JavaScript)
JavaScript
25
star
16

json-normalizer

A small, lightweight library for normalizing JSON properties and values.
JavaScript
24
star
17

broccoli-typescript-compiler

TypeScript
24
star
18

helix-rails

Helix for Rails
Ruby
22
star
19

libkit

JavaScript
17
star
20

ts-std

A standard library for TypeScript, extracted from Glimmer and other Tilde projects
TypeScript
10
star
21

buffoon

A protobuf library for Rust
Rust
10
star
22

selector-generator

A lightweight JavaScript library that generates CSS selectors from DOM nodes.
JavaScript
10
star
23

canal.js

Canal.js is a lightweight JavaScript library for creating channels, which are objects that represent a stream of events.
JavaScript
10
star
24

ember-control-flow-component

The superclass for all your "control-flow" component needs
9
star
25

ember-women

The Ember Women Helping Women Program
CSS
8
star
26

libcruby-sys

Rust
8
star
27

helix-website

Website for the Helix project
Ruby
8
star
28

kafka-librato-reporter

Report Kafka metrics to Librato
Java
8
star
29

sync.js

JavaScript
6
star
30

range-serializer

A lightweight JavaScript library that serializes/deserializes DOM range objects.
JavaScript
6
star
31

helix-flipper

A simple Rails app to get you started with Helix
Ruby
5
star
32

jsframe.js

Convert JavaScript to (near) JavaScript/HTML polyglot.
JavaScript
4
star
33

oasis-website

Oasis.js helps you structure communication between multiple untrusted sandboxes
CSS
3
star
34

ember-template-string-interpolation

JavaScript
3
star
35

rustconf

Annual Rust Conference
HTML
3
star
36

ember-training-june-day2

JavaScript
3
star
37

discourse-refcount-resource-spike

JavaScript
2
star
38

skiplist.js

JavaScript
2
star
39

ember-training-nyc-june

Material for the Tilde training course in NYC
JavaScript
2
star
40

emberconf-meetups

A list of unofficial EmberConf meetups
2
star
41

ember-swappable-service

JavaScript
2
star
42

ember-better-attributes

An Ember plugin that provides a better attribute syntax, building on simple-html-tokenizer. It is expected to be merged into Ember in the future.
JavaScript
2
star
43

fastboot-test

JavaScript
1
star
44

rails_todo_app

Ruby
1
star
45

ruby-rust-musl

Test of building musl Rust extensions for Ruby
Ruby
1
star