Skip to content

Commit

Permalink
Allow a non-lookahead regex (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctcpip committed Aug 22, 2024
1 parent 51a1955 commit c4272e4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
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);
};
65 changes: 65 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,19 @@ describe('path-to-regexp', function () {
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');
assert.equal(params[0].optional, false);

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

it('should match trailing slashes in non-ending non-strict mode', function () {
var params = [];
var re = pathToRegExp('/:test', params, { end: false });
Expand Down Expand Up @@ -571,6 +584,34 @@ describe('path-to-regexp', function () {
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/');

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 slashing in non-ending strict mode', function () {
var params = [];
var re = pathToRegExp('/route/', params, { end: false, strict: true });
Expand Down Expand Up @@ -616,6 +657,24 @@ describe('path-to-regexp', function () {
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/');
});

it('should match text after an express param', function () {
var params = [];
var re = pathToRegExp('/(:test)route', params);
Expand Down Expand Up @@ -728,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

0 comments on commit c4272e4

Please sign in to comment.