Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/tolemac/route-recognizer
Browse files Browse the repository at this point in the history
…into tolemac-master
  • Loading branch information
EisenbergEffect committed May 9, 2016
2 parents 71e52d2 + d89cde4 commit a48e6d1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ interface RouteHandler {
interface ConfigurableRoute {
path: string;
handler: RouteHandler;
caseSensitive: boolean;
}

interface HandlerEntry {
Expand Down
8 changes: 4 additions & 4 deletions src/route-recognizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class RouteRecognizer {
let names = [];
let routeName = route.handler.name;
let isEmpty = true;
let segments = parse(route.path, names, types);
let segments = parse(route.path, names, types, route.caseSensitive);

for (let i = 0, ii = segments.length; i < ii; i++) {
let segment = segments[i];
Expand Down Expand Up @@ -73,7 +73,7 @@ export class RouteRecognizer {
}

currentState.handlers = handlers;
currentState.regex = new RegExp(regex + '$');
currentState.regex = new RegExp(regex + '$', route.caseSensitive ? '' : 'i');
currentState.types = types;

return currentState;
Expand Down Expand Up @@ -227,7 +227,7 @@ class RecognizeResults {
}
}

function parse(route, names, types) {
function parse(route, names, types, caseSensitive) {
// normalize route as not starting with a '/'. Recognition will
// also normalize.
let normalizedRoute = route;
Expand Down Expand Up @@ -256,7 +256,7 @@ function parse(route, names, types) {
} else if (segment === '') {
results.push(new EpsilonSegment());
} else {
results.push(new StaticSegment(segment));
results.push(new StaticSegment(segment, caseSensitive));
types.statics++;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/segments.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ const escapeRegex = new RegExp('(\\' + specials.join('|\\') + ')', 'g');
// * `repeat`: true if the character specification can repeat

export class StaticSegment {
constructor(string: string) {
constructor(string: string, caseSensitive: boolean) {
this.string = string;
this.caseSensitive = caseSensitive;
}

eachChar(callback: (spec: CharSpec) => void): void {
let s = this.string;
for (let i = 0, ii = s.length; i < ii; ++i) {
let ch = s[i];
callback({ validChars: ch });
callback({ validChars: this.caseSensitive ? ch : ch.toUpperCase() + ch.toLowerCase() });
}
}

Expand Down
29 changes: 29 additions & 0 deletions test/route-recognizer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ describe('route recognizer', () => {
expect(result[0].params).toEqual(routeTest.params);
});

it(`its case insensitive by default ${routeTest.title}`, () => {
let recognizer = new RouteRecognizer();
recognizer.add([routeTest.route]);

let result = recognizer.recognize(routeTest.path.toUpperCase());
expect(result).toBeTruthy();
expect(result.length).toBe(1);
expect(result[0].handler).toEqual(routeTest.route.handler);
expect(result[0].isDynamic).toBe(routeTest.isDynamic);
Object.keys(result[0].params).forEach((property) => {
expect(result[0].params[property].toUpperCase()).toEqual(routeTest.params[property].toUpperCase());
});
});

it(`should generate ${routeTest.title}`, () => {
let recognizer = new RouteRecognizer();
recognizer.add([routeTest.route]);
Expand Down Expand Up @@ -127,5 +141,20 @@ describe('route recognizer', () => {

expect(recognizer.handlersFor('static-multiple')[0].handler)
.toEqual(recognizer.handlersFor('static-multiple-alias')[0].handler)
});

it(`can set case sensitive route and fails`, () => {
let recognizer = new RouteRecognizer();
const routeTest = {
title: 'case sensitive route',
route: { 'path': 'CasE/InSeNsItIvE', 'handler': { 'name': 'static' }, 'caseSensitive': true },
isDynamic: false,
path: 'CasE/iNsEnSiTiVe',
params: {}
};
recognizer.add([routeTest.route]);

let result = recognizer.recognize(routeTest.path);
expect(result).toBeUndefined();
});
});

0 comments on commit a48e6d1

Please sign in to comment.