path-to-regexp 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of path-to-regexp might be problematic. Click here for more details.
- package/History.md +5 -0
- package/component.json +1 -1
- package/index.js +21 -7
- package/package.json +1 -1
- package/test.js +117 -11
package/History.md
CHANGED
package/component.json
CHANGED
package/index.js
CHANGED
@@ -22,22 +22,33 @@ module.exports = pathtoRegexp;
|
|
22
22
|
|
23
23
|
function pathtoRegexp(path, keys, options) {
|
24
24
|
options = options || {};
|
25
|
-
var sensitive = options.sensitive;
|
26
25
|
var strict = options.strict;
|
27
26
|
var end = options.end !== false;
|
27
|
+
var flags = options.sensitive ? '' : 'i';
|
28
28
|
keys = keys || [];
|
29
29
|
|
30
|
-
if (path instanceof RegExp)
|
31
|
-
|
30
|
+
if (path instanceof RegExp) {
|
31
|
+
return path;
|
32
|
+
}
|
32
33
|
|
33
|
-
path
|
34
|
-
.
|
34
|
+
if (Array.isArray(path)) {
|
35
|
+
// Map array parts into regexps and return their source. We also pass
|
36
|
+
// the same keys and options instance into every generation to get
|
37
|
+
// consistent matching groups before we join the sources together.
|
38
|
+
path = path.map(function (value) {
|
39
|
+
return pathtoRegexp(value, keys, options).source;
|
40
|
+
});
|
41
|
+
|
42
|
+
return new RegExp('(?:' + path.join('|') + ')', flags);
|
43
|
+
}
|
44
|
+
|
45
|
+
path = ('^' + path + (strict ? '' : path[path.length - 1] === '/' ? '?' : '/?'))
|
35
46
|
.replace(/\/\(/g, '/(?:')
|
36
47
|
.replace(/([\/\.])/g, '\\$1')
|
37
48
|
.replace(/(\\\/)?(\\\.)?:(\w+)(\(.*?\))?(\*)?(\?)?/g, function (match, slash, format, key, capture, star, optional) {
|
38
49
|
slash = slash || '';
|
39
50
|
format = format || '';
|
40
|
-
capture = capture || '([
|
51
|
+
capture = capture || '([^\\/' + format + ']+?)';
|
41
52
|
optional = optional || '';
|
42
53
|
|
43
54
|
keys.push({ name: key, optional: !!optional });
|
@@ -52,5 +63,8 @@ function pathtoRegexp(path, keys, options) {
|
|
52
63
|
})
|
53
64
|
.replace(/\*/g, '(.*)');
|
54
65
|
|
55
|
-
|
66
|
+
// If the path is non-ending, match until the end or a slash.
|
67
|
+
path += (end ? '$' : (path[path.length - 1] === '/' ? '' : '(?=\\/|$)'));
|
68
|
+
|
69
|
+
return new RegExp(path, flags);
|
56
70
|
};
|
package/package.json
CHANGED
package/test.js
CHANGED
@@ -46,6 +46,30 @@ describe('path-to-regexp', function () {
|
|
46
46
|
assert.ok(!m);
|
47
47
|
});
|
48
48
|
|
49
|
+
it('should do strict matches with trailing slashes', function () {
|
50
|
+
var params = [];
|
51
|
+
var re = pathToRegExp('/:test/', params, { strict: true });
|
52
|
+
var m;
|
53
|
+
|
54
|
+
assert.equal(params.length, 1);
|
55
|
+
assert.equal(params[0].name, 'test');
|
56
|
+
assert.equal(params[0].optional, false);
|
57
|
+
|
58
|
+
m = re.exec('/route');
|
59
|
+
|
60
|
+
assert.ok(!m);
|
61
|
+
|
62
|
+
m = re.exec('/route/');
|
63
|
+
|
64
|
+
assert.equal(m.length, 2);
|
65
|
+
assert.equal(m[0], '/route/');
|
66
|
+
assert.equal(m[1], 'route');
|
67
|
+
|
68
|
+
m = re.exec('/route//');
|
69
|
+
|
70
|
+
assert.ok(!m);
|
71
|
+
});
|
72
|
+
|
49
73
|
it('should allow optional express format params', function () {
|
50
74
|
var params = [];
|
51
75
|
var re = pathToRegExp('/:test?', params);
|
@@ -388,19 +412,75 @@ describe('path-to-regexp', function () {
|
|
388
412
|
assert.equal(m[1], 'test');
|
389
413
|
});
|
390
414
|
|
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
|
+
|
443
|
+
it('should match trailing slashing in non-ending strict mode', function () {
|
444
|
+
var params = [];
|
445
|
+
var re = pathToRegExp('/route/', params, { end: false, strict: true });
|
446
|
+
|
447
|
+
assert.equal(params.length, 0);
|
448
|
+
|
449
|
+
m = re.exec('/route/');
|
450
|
+
|
451
|
+
assert.equal(m.length, 1);
|
452
|
+
assert.equal(m[0], '/route/');
|
453
|
+
|
454
|
+
m = re.exec('/route/test');
|
455
|
+
|
456
|
+
assert.equal(m.length, 1);
|
457
|
+
assert.equal(m[0], '/route/');
|
458
|
+
|
459
|
+
m = re.exec('/route');
|
460
|
+
|
461
|
+
assert.ok(!m);
|
462
|
+
|
463
|
+
m = re.exec('/route//');
|
464
|
+
|
465
|
+
assert.equal(m.length, 1);
|
466
|
+
assert.equal(m[0], '/route/');
|
467
|
+
});
|
468
|
+
|
391
469
|
it('should not match trailing slashes in non-ending strict mode', function () {
|
392
470
|
var params = [];
|
393
|
-
var re = pathToRegExp('
|
471
|
+
var re = pathToRegExp('/route', params, { end: false, strict: true });
|
394
472
|
|
395
|
-
assert.equal(params.length,
|
396
|
-
assert.equal(params[0].name, 'test');
|
397
|
-
assert.equal(params[0].optional, false);
|
473
|
+
assert.equal(params.length, 0);
|
398
474
|
|
399
|
-
m = re.exec('/
|
475
|
+
m = re.exec('/route');
|
400
476
|
|
401
|
-
assert.equal(m.length,
|
402
|
-
assert.equal(m[0], '/
|
403
|
-
|
477
|
+
assert.equal(m.length, 1);
|
478
|
+
assert.equal(m[0], '/route');
|
479
|
+
|
480
|
+
m = re.exec('/route/');
|
481
|
+
|
482
|
+
assert.ok(m.length, 1);
|
483
|
+
assert.equal(m[0], '/route');
|
404
484
|
});
|
405
485
|
|
406
486
|
it('should match text after an express param', function () {
|
@@ -502,9 +582,35 @@ describe('path-to-regexp', function () {
|
|
502
582
|
it('should join arrays parts', function () {
|
503
583
|
var re = pathToRegExp(['/test', '/route']);
|
504
584
|
|
505
|
-
assert.ok(re.
|
506
|
-
assert.ok(re.
|
507
|
-
assert.ok(!re.
|
585
|
+
assert.ok(re.test('/test'));
|
586
|
+
assert.ok(re.test('/route'));
|
587
|
+
assert.ok(!re.test('/else'));
|
588
|
+
});
|
589
|
+
|
590
|
+
it('should match parts properly', function () {
|
591
|
+
var params = [];
|
592
|
+
var re = pathToRegExp(['/:test', '/test/:route'], params);
|
593
|
+
var m;
|
594
|
+
|
595
|
+
assert.equal(params.length, 2);
|
596
|
+
assert.equal(params[0].name, 'test');
|
597
|
+
assert.equal(params[0].optional, false);
|
598
|
+
assert.equal(params[1].name, 'route');
|
599
|
+
assert.equal(params[1].optional, false);
|
600
|
+
|
601
|
+
m = re.exec('/route');
|
602
|
+
|
603
|
+
assert.equal(m.length, 3);
|
604
|
+
assert.equal(m[0], '/route');
|
605
|
+
assert.equal(m[1], 'route');
|
606
|
+
assert.equal(m[2], undefined);
|
607
|
+
|
608
|
+
m = re.exec('/test/path');
|
609
|
+
|
610
|
+
assert.equal(m.length, 3);
|
611
|
+
assert.equal(m[0], '/test/path');
|
612
|
+
assert.equal(m[1], undefined);
|
613
|
+
assert.equal(m[2], 'path');
|
508
614
|
});
|
509
615
|
});
|
510
616
|
});
|