Skip to content

Commit b5520e3

Browse files
committed
✨ add lookahead support as an option, skip test that cannot pass in node <10
1 parent dbe3e18 commit b5520e3

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function pathtoRegexp(path, keys, options) {
3131
var strict = options.strict;
3232
var end = options.end !== false;
3333
var flags = options.sensitive ? '' : 'i';
34+
var lookahead = options.lookahead !== false;
3435
var extraOffset = 0;
3536
var keysOffset = keys.length;
3637
var i = 0;
@@ -123,7 +124,11 @@ function pathtoRegexp(path, keys, options) {
123124
}
124125

125126
// If the path is non-ending, match until the end or a slash.
126-
path += (end ? '$' : (path[path.length - 1] === '/' ? '' : '(?:\/|$)'));
127+
if (end) {
128+
path += '$';
129+
} else if (path[path.length - 1] !== '/') {
130+
path += lookahead ? '(?=\\/|$)' : '(?:\/|$)';
131+
}
127132

128133
return new RegExp(path, flags);
129134
};

test.js

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,20 @@ describe('path-to-regexp', function () {
516516

517517
it('should do non-ending matches', function () {
518518
var params = [];
519-
var re = pathToRegExp('/:test', params, { end: false })
520-
var m = re.exec('/test/route');
519+
var m = pathToRegExp('/:test', params, { end: false }).exec('/test/route');
520+
521+
assert.equal(params.length, 1);
522+
assert.equal(params[0].name, 'test');
523+
assert.equal(params[0].optional, false);
524+
525+
assert.equal(m.length, 2);
526+
assert.equal(m[0], '/test');
527+
assert.equal(m[1], 'test');
528+
});
529+
530+
it('should do non-ending matches (no lookahead)', function () {
531+
var params = [];
532+
var m = pathToRegExp('/:test', params, { end: false, lookahead: false }).exec('/test/route');
521533

522534
assert.equal(params.length, 1);
523535
assert.equal(params[0].name, 'test');
@@ -558,6 +570,34 @@ describe('path-to-regexp', function () {
558570

559571
m = re.exec('/route/test');
560572

573+
assert.equal(m.length, 1);
574+
assert.equal(m[0], '/route');
575+
576+
m = re.exec('/route');
577+
578+
assert.equal(m.length, 1);
579+
assert.equal(m[0], '/route');
580+
581+
m = re.exec('/route//');
582+
583+
assert.equal(m.length, 1);
584+
assert.equal(m[0], '/route/');
585+
});
586+
587+
it('should match trailing slashes in non-ending non-strict mode (no lookahead)', function () {
588+
var params = [];
589+
var re = pathToRegExp('/route/', params, { end: false, lookahead: false });
590+
var m;
591+
592+
assert.equal(params.length, 0);
593+
594+
m = re.exec('/route/');
595+
596+
assert.equal(m.length, 1);
597+
assert.equal(m[0], '/route/');
598+
599+
m = re.exec('/route/test');
600+
561601
assert.equal(m.length, 1);
562602
assert.equal(m[0], '/route/');
563603

@@ -613,6 +653,24 @@ describe('path-to-regexp', function () {
613653

614654
m = re.exec('/route/');
615655

656+
assert.ok(m.length, 1);
657+
assert.equal(m[0], '/route');
658+
});
659+
660+
it('should not match trailing slashes in non-ending strict mode (no lookahead)', function () {
661+
var params = [];
662+
var re = pathToRegExp('/route', params, { end: false, strict: true, lookahead: false });
663+
var m;
664+
665+
assert.equal(params.length, 0);
666+
667+
m = re.exec('/route');
668+
669+
assert.equal(m.length, 1);
670+
assert.equal(m[0], '/route');
671+
672+
m = re.exec('/route/');
673+
616674
assert.ok(m.length, 1);
617675
assert.equal(m[0], '/route/');
618676
});
@@ -729,6 +787,12 @@ describe('path-to-regexp', function () {
729787
});
730788

731789
it('should pull out matching named groups', function () {
790+
const majorVersion = Number(process.version.split('.')[0].replace('v', ''));
791+
if (majorVersion < 10) {
792+
console.log('skipping test: node <10 does not support named capture groups');
793+
return;
794+
}
795+
732796
var params = [];
733797
var re = pathToRegExp(/\/(.*)\/(?<foo>.*)\/(.*)/, params);
734798
var m;

0 commit comments

Comments
 (0)