Skip to content

Commit 642d524

Browse files
feat(UrlMatcher): Add support for case insensitive url matching
1 parent e3ba1bf commit 642d524

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/urlMatcherFactory.js

+25-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* * `'/files/*path'` - ditto.
4040
*
4141
* @param {string} pattern the pattern to compile into a matcher.
42+
* @param {bool} caseInsensitiveMatch true if url matching should be case insensitive, otherwise false, the default value (for backward compatibility) is false.
4243
*
4344
* @property {string} prefix A static prefix of this pattern. The matcher guarantees that any
4445
* URL matching this matcher (i.e. any string for which {@link ui.router.util.type:UrlMatcher#methods_exec exec()} returns
@@ -55,7 +56,7 @@
5556
*
5657
* @returns {Object} New UrlMatcher object
5758
*/
58-
function UrlMatcher(pattern) {
59+
function UrlMatcher(pattern, caseInsensitiveMatch) {
5960

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

120121
compiled += quoteRegExp(segment) + '$';
121122
segments.push(segment);
122-
this.regexp = new RegExp(compiled);
123+
if(caseInsensitiveMatch){
124+
this.regexp = new RegExp(compiled, 'i');
125+
}else{
126+
this.regexp = new RegExp(compiled);
127+
}
128+
123129
this.prefix = segments[0];
124130
}
125131

@@ -263,6 +269,22 @@ UrlMatcher.prototype.format = function (values) {
263269
*/
264270
function $UrlMatcherFactory() {
265271

272+
var useCaseInsensitiveMatch = false;
273+
274+
/**
275+
* @ngdoc function
276+
* @name ui.router.util.$urlMatcherFactory#caseInsensitiveMatch
277+
* @methodOf ui.router.util.$urlMatcherFactory
278+
*
279+
* @description
280+
* Define if url matching should be case sensistive, the default behavior, or not.
281+
*
282+
* @param {bool} value false to match URL in a case sensitive manner; otherwise true;
283+
*/
284+
this.caseInsensitiveMatch = function(value){
285+
useCaseInsensitiveMatch = value;
286+
};
287+
266288
/**
267289
* @ngdoc function
268290
* @name ui.router.util.$urlMatcherFactory#compile
@@ -275,7 +297,7 @@ function $UrlMatcherFactory() {
275297
* @returns {ui.router.util.type:UrlMatcher} The UrlMatcher.
276298
*/
277299
this.compile = function (pattern) {
278-
return new UrlMatcher(pattern);
300+
return new UrlMatcher(pattern, useCaseInsensitiveMatch);
279301
};
280302

281303
/**

test/urlMatcherFactorySpec.js

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ describe("UrlMatcher", function () {
33
expect(new UrlMatcher('/hello/world').exec('/hello/world')).toEqual({});
44
});
55

6+
it("matches static case insensitive URLs", function () {
7+
expect(new UrlMatcher('/hello/world', true).exec('/heLLo/World')).toEqual({});
8+
});
9+
610
it("matches against the entire path", function () {
711
var matcher = new UrlMatcher('/hello/world');
812
expect(matcher.exec('/hello/world/')).toBeNull();
@@ -139,4 +143,13 @@ describe("urlMatcherFactory", function () {
139143
it("recognizes matchers", function () {
140144
expect($umf.isMatcher(new UrlMatcher('/'))).toBe(true);
141145
});
146+
147+
it("should handle case sensistive URL by default", function () {
148+
expect($umf.compile('/hello/world').exec('/heLLo/WORLD')).toBeNull();
149+
});
150+
151+
it("should handle case insensistive URL", function () {
152+
$umf.caseInsensitiveMatch(true);
153+
expect($umf.compile('/hello/world').exec('/heLLo/WORLD')).toEqual({});
154+
});
142155
});

0 commit comments

Comments
 (0)