Skip to content

Commit

Permalink
add option to toggle case sensitivity. see #53
Browse files Browse the repository at this point in the history
  • Loading branch information
millermedeiros committed Aug 12, 2012
1 parent 785c07b commit c5b8e65
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
@@ -1,5 +1,11 @@
# Crossroads.js Changelog #

## ignorecase branch

- changed `Route.rules` array validation to be case insensitive (#49)
- route matching is case insensitive. (#53)
- added way to toggle case sensitivity `Route.ignoreCase` (#53)


## Next ##

Expand All @@ -18,8 +24,6 @@
- query string support affected old segment rules, now `?` is considered as
a segment divisor as `/` otherwise optional query string RegExp wouldn't
match proper segment if following a required segment. (#33)
- changed `Route.rules` array validation to be case insensitive (#49)
- route matching is case insensitive. (#53)



Expand Down
2 changes: 2 additions & 0 deletions dev/src/crossroads.js
Expand Up @@ -14,6 +14,8 @@

Crossroads.prototype = {

ignoreCase : true,

greedy : false,

greedyEnabled : true,
Expand Down
4 changes: 2 additions & 2 deletions dev/src/pattern_lexer.js
Expand Up @@ -104,7 +104,7 @@
return captureVals(TOKENS.OP.rgx, pattern);
}

function compilePattern(pattern) {
function compilePattern(pattern, ignoreCase) {
pattern = pattern || '';

if(pattern){
Expand All @@ -131,7 +131,7 @@
//single slash is treated as empty and end slash is optional
pattern += '\\/?';
}
return new RegExp('^'+ pattern + '$', 'i');
return new RegExp('^'+ pattern + '$', ignoreCase? 'i' : '');
}

function replaceTokens(pattern, regexpName, replaceName) {
Expand Down
8 changes: 6 additions & 2 deletions dev/src/route.js
Expand Up @@ -12,7 +12,7 @@
this._pattern = pattern;
this._paramsIds = isRegexPattern? null : patternLexer.getParamIds(this._pattern);
this._optionalParamsIds = isRegexPattern? null : patternLexer.getOptionalParamsIds(this._pattern);
this._matchRegexp = isRegexPattern? pattern : patternLexer.compilePattern(pattern);
this._matchRegexp = isRegexPattern? pattern : patternLexer.compilePattern(pattern, router.ignoreCase);
this.matched = new signals.Signal();
this.switched = new signals.Signal();
if (callback) {
Expand Down Expand Up @@ -74,6 +74,10 @@
},

_isValidArrayRule : function (arr, val) {
if (! this._router.ignoreCase) {
return arrayIndexOf(arr, val) !== -1;
}

if (typeof val === 'string') {
val = val.toLowerCase();
}
Expand All @@ -82,7 +86,7 @@
item,
compareVal;

while (n-- > 0) {
while (n--) {
item = arr[n];
compareVal = (typeof item === 'string')? item.toLowerCase() : item;
if (compareVal === val) {
Expand Down
33 changes: 32 additions & 1 deletion dev/tests/spec/match.spec.js
Expand Up @@ -9,6 +9,7 @@ describe('Match', function(){

afterEach(function(){
crossroads.removeAllRoutes();
crossroads.ignoreCase = true;
});


Expand Down Expand Up @@ -85,7 +86,7 @@ describe('Match', function(){
expect( s.match('/123/asd/45/qwe') ).toBe( false );
});

it('should be case insensitive', function () {
it('should be case insensitive by default', function () {
var s = crossroads.addRoute('foo/bar');
expect( s.match('foo') ).toBe( false );
expect( s.match('Foo') ).toBe( false );
Expand All @@ -94,6 +95,16 @@ describe('Match', function(){
expect( s.match('FoO/BAR') ).toBe( true );
});

it('should be allow toggling case sensitivity', function () {
crossroads.ignoreCase = false;
var s = crossroads.addRoute('foo/bar');
expect( s.match('foo') ).toBe( false );
expect( s.match('Foo') ).toBe( false );
expect( s.match('foo/bar') ).toBe( true );
expect( s.match('Foo/Bar') ).toBe( false );
expect( s.match('FoO/BAR') ).toBe( false );
});

describe('rest params', function () {
it('should support rest params', function () {
var s = crossroads.addRoute('/123/{bar}/:ipsum*:');
Expand Down Expand Up @@ -462,6 +473,26 @@ describe('Match', function(){

});

it('should change array validation behavior when ignoreCase is false', function(){
crossroads.ignoreCase = false;
var s = crossroads.addRoute('/{foo}/{bar}');

s.rules = {
foo : ['lorem-ipsum', '123'],
bar : ['DoLoR', '45']
};

expect( s.match('/lorem-ipsum') ).toBe( false );
expect( s.match('/lorem-ipsum/DoLoR') ).toBe( true );
expect( s.match('/LoReM-IpSuM/DOLoR') ).toBe( false );
expect( s.match('lorem-ipsum') ).toBe( false );
expect( s.match('/123') ).toBe( false );
expect( s.match('123') ).toBe( false );
expect( s.match('/123/123') ).toBe( false );
expect( s.match('/123/45') ).toBe( true );
});


it('should allow RegExp options', function(){
var s = crossroads.addRoute('/{foo}/{bar}');

Expand Down

0 comments on commit c5b8e65

Please sign in to comment.