Skip to content

Commit

Permalink
✨ add lookahead support as an option, skip test that cannot pass in n…
Browse files Browse the repository at this point in the history
…ode <10
  • Loading branch information
ctcpip committed Aug 20, 2024
1 parent dbe3e18 commit 526268d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function pathtoRegexp(path, keys, options) {
var strict = options.strict;
var end = options.end !== false;
var flags = options.sensitive ? '' : 'i';
var lookahead = options.lookahead !== false;
var extraOffset = 0;
var keysOffset = keys.length;
var i = 0;
Expand Down Expand Up @@ -123,7 +124,11 @@ function pathtoRegexp(path, keys, options) {
}

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

return new RegExp(path, flags);
};
70 changes: 67 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,20 @@ describe('path-to-regexp', function () {

it('should do non-ending matches', function () {
var params = [];
var re = pathToRegExp('/:test', params, { end: false })
var m = re.exec('/test/route');
var m = pathToRegExp('/:test', params, { end: false }).exec('/test/route');

assert.equal(params.length, 1);
assert.equal(params[0].name, 'test');
assert.equal(params[0].optional, false);

assert.equal(m.length, 2);
assert.equal(m[0], '/test');
assert.equal(m[1], 'test');
});

it('should do non-ending matches (no lookahead)', function () {
var params = [];
var m = pathToRegExp('/:test', params, { end: false, lookahead: false }).exec('/test/route');

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

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

assert.equal(m.length, 1);
assert.equal(m[0], '/route');

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

assert.equal(m.length, 1);
assert.equal(m[0], '/route');

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

assert.equal(m.length, 1);
assert.equal(m[0], '/route/');
});

it('should match trailing slashes in non-ending non-strict mode (no lookahead)', function () {
var params = [];
var re = pathToRegExp('/route/', params, { end: false, lookahead: false });
var m;

assert.equal(params.length, 0);

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

assert.equal(m.length, 1);
assert.equal(m[0], '/route/');

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

assert.equal(m.length, 1);
assert.equal(m[0], '/route/');

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

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

assert.ok(m.length, 1);
assert.equal(m[0], '/route');
});

it('should not match trailing slashes in non-ending strict mode (no lookahead)', function () {
var params = [];
var re = pathToRegExp('/route', params, { end: false, strict: true, lookahead: false });
var m;

assert.equal(params.length, 0);

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

assert.equal(m.length, 1);
assert.equal(m[0], '/route');

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

assert.ok(m.length, 1);
assert.equal(m[0], '/route/');
});
Expand Down Expand Up @@ -729,6 +787,12 @@ describe('path-to-regexp', function () {
});

it('should pull out matching named groups', function () {
const majorVersion = Number(process.version.split('.')[0].replace('v', ''));
if (majorVersion < 10) {
console.log('skipping test: node <10 does not support named capture groups');
return;
}

var params = [];
var re = pathToRegExp(/\/(.*)\/(?<foo>.*)\/(.*)/, params);
var m;
Expand Down Expand Up @@ -830,4 +894,4 @@ describe('path-to-regexp', function () {
assert.equal(m[2], 'world');
});
});
});
});

0 comments on commit 526268d

Please sign in to comment.