Skip to content
This repository was archived by the owner on Dec 15, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"babel-preset-stage-2": "^6.5.0",
"coveralls": "^2.11.9",
"history": "^3.0.0-2",
"lodash.find": "^4.3.0",
"rimraf": "^2.5.2",
"url-pattern": "^1.0.1",
"webpack": "^1.12.15"
Expand Down
5 changes: 3 additions & 2 deletions src/create-matcher.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import UrlPattern from 'url-pattern';
import find from 'lodash.find';

export default routes => {
const routeCache = Object.keys(routes).map(key => ({
Expand All @@ -7,8 +8,8 @@ export default routes => {
}));

return incomingRoute => {
const match = routeCache.find(
route => route.pattern.match(incomingRoute)
const match = find(routeCache, route =>
route.pattern.match(incomingRoute)
);
return {
params: match.pattern.match(incomingRoute),
Expand Down
55 changes: 55 additions & 0 deletions test/spec/create-matcher.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { createMatcher } from 'src';

const routes = {
'/home': {
name: 'home'
},
'/home/messages': {
name: 'messages'
},
'/home/messages/:team': {
name: 'team'
},
'/home/messages/:team/:channel': {
name: 'channel'
}
};

describe('createMatcher', () => {
it('matches URLs and returns both their params and their value in the route hash', () => {
const matchRoute = createMatcher(routes);

expect(matchRoute('/home')).to.deep.equal({
params: {},
result: {
name: 'home'
}
});

expect(matchRoute('/home/messages')).to.deep.equal({
params: {},
result: {
name: 'messages'
}
});

expect(matchRoute('/home/messages/a-team')).to.deep.equal({
params: {
team: 'a-team'
},
result: {
name: 'team'
}
});

expect(matchRoute('/home/messages/a-team/the-wat-channel')).to.deep.equal({
params: {
team: 'a-team',
channel: 'the-wat-channel'
},
result: {
name: 'channel'
}
});
});
});