Skip to content

Commit 93908d6

Browse files
committed
Add path regex support.
1 parent c177488 commit 93908d6

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

lib/matchRoutes.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ function matchRoutes(routes, path) {
3636
if (match) {
3737
page = current;
3838
}
39+
// Regex matches are not named, so they go in the `_` array, much like splats.
40+
if (Array.isArray(match)) {
41+
match = {_: match};
42+
}
3943
}
4044
}
4145
if (!notFound && current.path === null) {

tests/browser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'use strict';
12
var assert = require('assert');
23
var ReactAsync = require('react-async');
34
var React = require('react');

tests/matchRoutes.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'use strict';
12
var assert = require('assert');
23
var matchRoutes = require('../lib/matchRoutes');
34

@@ -11,6 +12,7 @@ describe('matchRoutes', function() {
1112
{path: '(/)', handler: handler({name: 'root'})},
1213
{path: '/cat/:id', handler: handler({name: 'cat'})},
1314
{path: '/mod/*', handler: handler({name: 'mod'})},
15+
{path: /\/regex\/([a-zA-Z]*)$/, handler: handler({name: 'regex'})},
1416
{path: null, handler: handler({name: 'notfound'})}
1517
];
1618

@@ -54,6 +56,26 @@ describe('matchRoutes', function() {
5456
assert.strictEqual(match.unmatchedPath, 'wow/here');
5557
});
5658

59+
it('matches /regex/text', function() {
60+
var match = matchRoutes(routes, '/regex/text');
61+
assert(match.route);
62+
assert.strictEqual(match.route.handler.name, 'regex');
63+
assert.deepEqual(match.match, {_: ['text']});
64+
assert.strictEqual(match.path, '/regex/text');
65+
assert.strictEqual(match.matchedPath, '/regex/');
66+
assert.strictEqual(match.unmatchedPath, 'text');
67+
});
68+
69+
it('does not match /regex/1text', function() {
70+
var match = matchRoutes(routes, '/regex/1text');
71+
assert(match.route);
72+
assert.strictEqual(match.route.handler.name, 'notfound');
73+
assert.deepEqual(match.match, null);
74+
assert.strictEqual(match.path, '/regex/1text');
75+
assert.strictEqual(match.matchedPath, '/regex/1text');
76+
assert.strictEqual(match.unmatchedPath, null);
77+
});
78+
5779
it('handles not found', function() {
5880
var match = matchRoutes(routes, '/hm');
5981
assert(match.route);

tests/server.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'use strict';
12
var assert = require('assert');
23
var React = require('react');
34
var Router = require('../index');
@@ -16,6 +17,10 @@ describe('react-router-component (on server)', function() {
1617
path: '/x/:slug',
1718
handler: function(props) { return React.DOM.div(null, props.slug); }
1819
}),
20+
Router.Location({
21+
path: /\/y(.*)/,
22+
handler: function(props) { return React.DOM.div(null, props._[0]);}
23+
}),
1924
Router.NotFound({
2025
handler: function(props) { return React.DOM.div(null, 'not_found'); }
2126
})
@@ -35,6 +40,12 @@ describe('react-router-component (on server)', function() {
3540
assert(markup.match(/hello/));
3641
});
3742

43+
it('renders with regex', function() {
44+
var markup = React.renderComponentToString(App({path: '/y/ohhai'}));
45+
assert(markup.match(/class="App"/));
46+
assert(markup.match(/ohhai/));
47+
})
48+
3849
it('renders to empty on notfound', function() {
3950
var markup = React.renderComponentToString(App({path: '/notfound'}));
4051
assert(markup.match(/class="App"/));

0 commit comments

Comments
 (0)