Skip to content

Commit

Permalink
Merge 0aaf0c4 into feb5c1f
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimoulis committed Oct 1, 2017
2 parents feb5c1f + 0aaf0c4 commit 808a312
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Simple route matcher using minimatch.

[Changelog](https://github.com/cdimoulis/simple-routes/blob/master/changelog.md)

[![npm][npm]][npm-url]
[![node][node]][node-url]
[![deps][deps]][deps-url]
Expand Down Expand Up @@ -46,7 +48,7 @@ router.addRoutes([

router.hasRoute('/index.html'); // true

let action = router.getAction('/blog/articles/cheese'); // returns main_controller.articles
let action = router.getAction('/blog/articles/cheese'); // returns main_controller.articles, undefined if not found
```

**NOTE:**
Expand All @@ -69,6 +71,16 @@ You can get an array of all the actions currently in the router:
router.actions();
```

Get the action if the passed route string matches a pattern. Undefined if not found:
```js
router.getAction('/...');
```

Get the route pattern that the passed route string matches. Undefined if not found:
```js
router.getRouteMatch('/...');
```

View a neater layout of the current routes in the router. If the function passed is a named function statement the function name will show. Otherwise the function will print.

```js
Expand Down
11 changes: 11 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
2.1
====

* Added `getRouteMatch(...)` function to return the matched pattern.
* Router `getAction(..)` function returns undefined if not found, not -1.


2.0
====

* Converted simple-routes router to an extendible class.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-routes",
"version": "0.2.0",
"version": "0.2.1",
"description": "A simple route matcher",
"keywords": [
"router",
Expand Down
13 changes: 12 additions & 1 deletion src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,18 @@ class Router {
let rq = route.split('?');
// Index of path only
let index = _findMatch(rq[0]);
return _actions[index] || -1;
return _actions[index];
};

// Get the route patter that is matched

// Returns null if not found
getRouteMatch(route) {
// Separate potential query string
let rq = route.split('?');
// Index of path only
let index = _findMatch(rq[0]);
return _routes[index];
};

// Remove a route
Expand Down
27 changes: 22 additions & 5 deletions test/router.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,34 @@ test('Add routes', () => {
f.x = () => {return 3;};
router.addRoutes([
['/test', action1],
['/love', action2],
['/love/*', action2],
['/temp', f.x],
]);
let routes = router.routes();
let actions = router.actions();
expect(routes.length).toBe(3);
expect(actions.length).toBe(3);
expect(routes[1]).toBe('/love');
expect(routes[1]).toBe('/love/*');
expect(actions[1]).toBe(action2);
});

test('Get Action', () => {
expect(router.getAction('/test')).toBe(action1);
expect(router.getAction('/love')).toBe(action2);
expect(router.getAction('/not_found')).toBe(-1);
expect(router.getAction('/love/food')).toBe(action2);
expect(router.getAction('/not_found')).toBeUndefined();
});

test('Get Route Match', () => {
router.addRoutes([
['/a/b/*/c', action1],
['/*/b/*/c', action1]
]);
expect(router.getRouteMatch('/a/b/z/c')).toBe('/a/b/*/c');
expect(router.getRouteMatch('/x/b/z/c')).toBe('/*/b/*/c');
expect(router.getRouteMatch('/love/q')).toBe('/love/*');
expect(router.getRouteMatch('/a/b/z/c/d')).toBeUndefined();
router.removeRoute('/a/b/*/c');
router.removeRoute('/*/b/*/c');
});

test('Remove route', () => {
Expand Down Expand Up @@ -74,10 +87,14 @@ test('Remove route with query', () => {
});

test('Has Route', () => {
expect(router.hasRoute('/love')).toBeTruthy();
expect(router.hasRoute('/love/*')).toBeTruthy();
expect(router.hasRoute('/not_found')).toBeFalsy();
});

test('Get Route Match', () => {
expect(router.getRouteMatch('/love/family')).toBe('/love/*')
});

test('To String', () => {
expect(typeof router.toString()).toBe('string');
});
Expand Down

0 comments on commit 808a312

Please sign in to comment.