Skip to content

Commit

Permalink
feat: make matching case insensitive, add support for 'caseSentive' o…
Browse files Browse the repository at this point in the history
…ption

BREAKING CHANGE: 'test' and 'partialTest' are now case insensitive by default, use 'caseSensitive' option to make it case sensitive
  • Loading branch information
troch committed Mar 25, 2018
1 parent 89049b1 commit 063aca6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 5 additions & 4 deletions modules/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ export default class Path {
this.queryParamsBr.indexOf(name) !== -1;
}

_urlTest(path, regex) {
let match = path.match(regex);
_urlTest(path, source, { caseSensitive = false } = {}) {
const regex = new RegExp('^' + source, caseSensitive ? '' : 'i');
const match = path.match(regex);
if (!match) return null;
else if (!this.urlParams.length) return {};
// Reduce named params to key-value pairs
Expand All @@ -196,7 +197,7 @@ export default class Path {
// trailingSlash: falsy => non optional, truthy => optional
const source = optTrailingSlash(this.source, options.trailingSlash);
// Check if exact match
const matched = this._urlTest(path, new RegExp('^' + source + (this.hasQueryParams ? '(\\?.*$|$)' : '$')));
const matched = this._urlTest(path, source + (this.hasQueryParams ? '(\\?.*$|$)' : '$'), opts);
// If no match, or no query params, no need to go further
if (!matched || !this.hasQueryParams) return matched;
// Extract query params
Expand All @@ -220,7 +221,7 @@ export default class Path {
// Check if partial match (start of given path matches regex)
// trailingSlash: falsy => non optional, truthy => optional
let source = upToDelimiter(this.source, options.delimited);
let match = this._urlTest(path, new RegExp('^' + source));
let match = this._urlTest(path, source, opts);

if (!match) return match;

Expand Down
8 changes: 8 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,12 @@ describe('Path', function () {
path.build({ name: 'he:re' }).should.eql('/test/he:re/test2');
path.build({ name: 'he\'re' }).should.eql('/test/he\'re/test2');
});

it('should be case insensitive', () => {
var path = new Path('/test');

path.test('/test').should.eql({})
path.test('/Test').should.eql({})
should.not.exist(path.test('/TEST', { caseSensitive: true }))
});
});

0 comments on commit 063aca6

Please sign in to comment.