Skip to content

feat(UrlMatcher): Add support for case insensitive url matching #957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2014
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
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({});
});
});