Skip to content

Commit 5d677b9

Browse files
committed
Make given trailing slash optional in non-strict mode
1 parent 0c0c7da commit 5d677b9

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function pathtoRegexp(path, keys, options) {
4242
return new RegExp('(?:' + path.join('|') + ')', flags);
4343
}
4444

45-
path = ('^' + path + (strict ? '' : '/?'))
45+
path = ('^' + path + (strict ? '' : path[path.length - 1] === '/' ? '?' : '/?'))
4646
.replace(/\/\(/g, '/(?:')
4747
.replace(/([\/\.])/g, '\\$1')
4848
.replace(/(\\\/)?(\\\.)?:(\w+)(\(.*?\))?(\*)?(\?)?/g, function (match, slash, format, key, capture, star, optional) {

test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,34 @@ describe('path-to-regexp', function () {
412412
assert.equal(m[1], 'test');
413413
});
414414

415+
it('should match trailing slashes in non-ending non-strict mode', function () {
416+
var params = [];
417+
var re = pathToRegExp('/route/', params, { end: false });
418+
var m;
419+
420+
assert.equal(params.length, 0);
421+
422+
m = re.exec('/route/');
423+
424+
assert.equal(m.length, 1);
425+
assert.equal(m[0], '/route/');
426+
427+
m = re.exec('/route/test');
428+
429+
assert.equal(m.length, 1);
430+
assert.equal(m[0], '/route');
431+
432+
m = re.exec('/route');
433+
434+
assert.equal(m.length, 1);
435+
assert.equal(m[0], '/route');
436+
437+
m = re.exec('/route//');
438+
439+
assert.equal(m.length, 1);
440+
assert.equal(m[0], '/route/');
441+
});
442+
415443
it('should match trailing slashing in non-ending strict mode', function () {
416444
var params = [];
417445
var re = pathToRegExp('/route/', params, { end: false, strict: true });

0 commit comments

Comments
 (0)