Skip to content

Commit

Permalink
feat(UrlMatcher): Add support for case insensitive url matching
Browse files Browse the repository at this point in the history
  • Loading branch information
mauroservienti committed Mar 11, 2014
1 parent e3ba1bf commit 642d524
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* * `'/files/*path'` - ditto.
*
* @param {string} pattern the pattern to compile into a matcher.
* @param {bool} caseInsensitiveMatch true if url matching should be case insensitive, otherwise false, the default value (for backward compatibility) is false.
*
* @property {string} prefix A static prefix of this pattern. The matcher guarantees that any
* URL matching this matcher (i.e. any string for which {@link ui.router.util.type:UrlMatcher#methods_exec exec()} returns
Expand All @@ -55,7 +56,7 @@
*
* @returns {Object} New UrlMatcher object
*/
function UrlMatcher(pattern) {
function UrlMatcher(pattern, caseInsensitiveMatch) {

// Find all placeholders and create a compiled pattern, using either classic or curly syntax:
// '*' name
Expand Down Expand Up @@ -119,7 +120,12 @@ function UrlMatcher(pattern) {

compiled += quoteRegExp(segment) + '$';
segments.push(segment);
this.regexp = new RegExp(compiled);
if(caseInsensitiveMatch){
this.regexp = new RegExp(compiled, 'i');
}else{
this.regexp = new RegExp(compiled);
}

this.prefix = segments[0];
}

Expand Down Expand Up @@ -263,6 +269,22 @@ UrlMatcher.prototype.format = function (values) {
*/
function $UrlMatcherFactory() {

var useCaseInsensitiveMatch = false;

/**
* @ngdoc function
* @name ui.router.util.$urlMatcherFactory#caseInsensitiveMatch
* @methodOf ui.router.util.$urlMatcherFactory
*
* @description
* Define if url matching should be case sensistive, the default behavior, or not.
*
* @param {bool} value false to match URL in a case sensitive manner; otherwise true;
*/
this.caseInsensitiveMatch = function(value){
useCaseInsensitiveMatch = value;
};

/**
* @ngdoc function
* @name ui.router.util.$urlMatcherFactory#compile
Expand All @@ -275,7 +297,7 @@ function $UrlMatcherFactory() {
* @returns {ui.router.util.type:UrlMatcher} The UrlMatcher.
*/
this.compile = function (pattern) {
return new UrlMatcher(pattern);
return new UrlMatcher(pattern, useCaseInsensitiveMatch);
};

/**
Expand Down
13 changes: 13 additions & 0 deletions test/urlMatcherFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ describe("UrlMatcher", function () {
expect(new UrlMatcher('/hello/world').exec('/hello/world')).toEqual({});
});

it("matches static case insensitive URLs", function () {
expect(new UrlMatcher('/hello/world', true).exec('/heLLo/World')).toEqual({});
});

it("matches against the entire path", function () {
var matcher = new UrlMatcher('/hello/world');
expect(matcher.exec('/hello/world/')).toBeNull();
Expand Down Expand Up @@ -139,4 +143,13 @@ describe("urlMatcherFactory", function () {
it("recognizes matchers", function () {
expect($umf.isMatcher(new UrlMatcher('/'))).toBe(true);
});

it("should handle case sensistive URL by default", function () {
expect($umf.compile('/hello/world').exec('/heLLo/WORLD')).toBeNull();
});

it("should handle case insensistive URL", function () {
$umf.caseInsensitiveMatch(true);
expect($umf.compile('/hello/world').exec('/heLLo/WORLD')).toEqual({});
});
});

0 comments on commit 642d524

Please sign in to comment.