path-to-regexp 0.0.1 → 0.1.2

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/.npmignore CHANGED
@@ -1 +1,2 @@
1
- test
1
+ node_modules
2
+ coverage
package/History.md ADDED
@@ -0,0 +1,11 @@
1
+
2
+ 0.1.0 / 2014-03-06
3
+ ==================
4
+
5
+ * add options.end
6
+
7
+ 0.0.2 / 2013-02-10
8
+ ==================
9
+
10
+ * Update to match current express
11
+ * add .license property to component.json
package/Readme.md CHANGED
@@ -3,6 +3,31 @@
3
3
 
4
4
  Turn an Express-style path string such as `/user/:name` into a regular expression.
5
5
 
6
+ ## Usage
7
+
8
+ ```javascript
9
+ var pathToRegexp = require('path-to-regexp');
10
+ ```
11
+ ### pathToRegexp(path, keys, options)
12
+
13
+ - **path** A string in the express format, an array of such strings, or a regular expression
14
+ - **keys** An array to be populated with the keys present in the url. Once the function completes, this will be an array of strings.
15
+ - **options**
16
+ - **options.sensitive** Defaults to false, set this to true to make routes case sensitive
17
+ - **options.strict** Defaults to false, set this to true to make the trailing slash matter.
18
+ - **options.end** Defaults to true, set this to false to only match the prefix of the URL.
19
+
20
+ ```javascript
21
+ var keys = [];
22
+ var exp = pathToRegexp('/foo/:bar', keys);
23
+ //keys = ['bar']
24
+ //exp = /^\/foo\/(?:([^\/]+?))\/?$/i
25
+ ```
26
+
27
+ ## Live Demo
28
+
29
+ You can see a live demo of this library in use at [express-route-tester](http://forbeslindesay.github.com/express-route-tester/).
30
+
6
31
  ## License
7
32
 
8
33
  MIT
package/component.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "path-to-regexp",
3
+ "description": "Express style path to RegExp utility",
4
+ "version": "0.1.0",
5
+ "keywords": [
6
+ "express",
7
+ "regexp",
8
+ "route",
9
+ "routing"
10
+ ],
11
+ "scripts": [
12
+ "index.js"
13
+ ],
14
+ "license": "MIT"
15
+ }
package/index.js CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  /**
3
2
  * Expose `pathtoRegexp`.
4
3
  */
@@ -25,6 +24,7 @@ function pathtoRegexp(path, keys, options) {
25
24
  options = options || {};
26
25
  var sensitive = options.sensitive;
27
26
  var strict = options.strict;
27
+ var end = options.end !== false;
28
28
  keys = keys || [];
29
29
 
30
30
  if (path instanceof RegExp) return path;
@@ -32,21 +32,25 @@ function pathtoRegexp(path, keys, options) {
32
32
 
33
33
  path = path
34
34
  .concat(strict ? '' : '/?')
35
- .replace(/\/\(/g, '(?:/')
36
- .replace(/\+/g, '__plus__')
37
- .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, function(_, slash, format, key, capture, optional){
38
- keys.push({ name: key, optional: !! optional });
35
+ .replace(/\/\(/g, '/(?:')
36
+ .replace(/([\/\.])/g, '\\$1')
37
+ .replace(/(\\\/)?(\\\.)?:(\w+)(\(.*?\))?(\*)?(\?)?/g, function (match, slash, format, key, capture, star, optional) {
39
38
  slash = slash || '';
39
+ format = format || '';
40
+ capture = capture || '([^/' + format + ']+?)';
41
+ optional = optional || '';
42
+
43
+ keys.push({ name: key, optional: !!optional });
44
+
40
45
  return ''
41
46
  + (optional ? '' : slash)
42
47
  + '(?:'
43
- + (optional ? slash : '')
44
- + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'
45
- + (optional || '');
48
+ + format + (optional ? slash : '') + capture
49
+ + (star ? '((?:[\\/' + format + '].+?)?)' : '')
50
+ + ')'
51
+ + optional;
46
52
  })
47
- .replace(/([\/.])/g, '\\$1')
48
- .replace(/__plus__/g, '(.+)')
49
53
  .replace(/\*/g, '(.*)');
50
54
 
51
- return new RegExp('^' + path + '$', sensitive ? '' : 'i');
52
- };
55
+ return new RegExp('^' + path + (end ? '$' : '(?=\/|$)'), sensitive ? '' : 'i');
56
+ };
package/package.json CHANGED
@@ -1,11 +1,25 @@
1
1
  {
2
2
  "name": "path-to-regexp",
3
3
  "description": "Express style path to RegExp utility",
4
- "version": "0.0.1",
5
- "keywords": ["express", "regexp"],
4
+ "version": "0.1.2",
5
+ "scripts": {
6
+ "test": "istanbul cover _mocha -- -R spec"
7
+ },
8
+ "keywords": [
9
+ "express",
10
+ "regexp"
11
+ ],
6
12
  "component": {
7
13
  "scripts": {
8
14
  "path-to-regexp": "index.js"
9
15
  }
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/component/path-to-regexp.git"
20
+ },
21
+ "devDependencies": {
22
+ "mocha": "^1.17.1",
23
+ "istanbul": "^0.2.6"
10
24
  }
11
25
  }
package/test.js ADDED
@@ -0,0 +1,510 @@
1
+ var pathToRegExp = require('./');
2
+ var assert = require('assert');
3
+
4
+ describe('path-to-regexp', function () {
5
+ describe('strings', function () {
6
+ it('should match simple paths', function () {
7
+ var params = [];
8
+ var m = pathToRegExp('/test', params).exec('/test');
9
+
10
+ assert.equal(params.length, 0);
11
+
12
+ assert.equal(m.length, 1);
13
+ assert.equal(m[0], '/test');
14
+ });
15
+
16
+ it('should match express format params', function () {
17
+ var params = [];
18
+ var m = pathToRegExp('/:test', params).exec('/pathname');
19
+
20
+ assert.equal(params.length, 1);
21
+ assert.equal(params[0].name, 'test');
22
+ assert.equal(params[0].optional, false);
23
+
24
+ assert.equal(m.length, 2);
25
+ assert.equal(m[0], '/pathname');
26
+ assert.equal(m[1], 'pathname');
27
+ });
28
+
29
+ it('should do strict matches', function () {
30
+ var params = [];
31
+ var re = pathToRegExp('/:test', params, { strict: true });
32
+ var m;
33
+
34
+ assert.equal(params.length, 1);
35
+ assert.equal(params[0].name, 'test');
36
+ assert.equal(params[0].optional, false);
37
+
38
+ m = re.exec('/route');
39
+
40
+ assert.equal(m.length, 2);
41
+ assert.equal(m[0], '/route');
42
+ assert.equal(m[1], 'route');
43
+
44
+ m = re.exec('/route/');
45
+
46
+ assert.ok(!m);
47
+ });
48
+
49
+ it('should allow optional express format params', function () {
50
+ var params = [];
51
+ var re = pathToRegExp('/:test?', params);
52
+ var m;
53
+
54
+ assert.equal(params.length, 1);
55
+ assert.equal(params[0].name, 'test');
56
+ assert.equal(params[0].optional, true);
57
+
58
+ m = re.exec('/route');
59
+
60
+ assert.equal(m.length, 2);
61
+ assert.equal(m[0], '/route');
62
+ assert.equal(m[1], 'route');
63
+
64
+ m = re.exec('/');
65
+
66
+ assert.equal(m.length, 2);
67
+ assert.equal(m[0], '/');
68
+ assert.equal(m[1], undefined);
69
+ });
70
+
71
+ it('should allow express format param regexps', function () {
72
+ var params = [];
73
+ var m = pathToRegExp('/:page(\\d+)', params).exec('/56');
74
+
75
+ assert.equal(params.length, 1);
76
+ assert.equal(params[0].name, 'page');
77
+ assert.equal(params[0].optional, false);
78
+
79
+ assert.equal(m.length, 2);
80
+ assert.equal(m[0], '/56');
81
+ assert.equal(m[1], '56');
82
+ });
83
+
84
+ it('should match without a prefixed slash', function () {
85
+ var params = [];
86
+ var m = pathToRegExp(':test', params).exec('string');
87
+
88
+ assert.equal(params.length, 1);
89
+ assert.equal(params[0].name, 'test');
90
+ assert.equal(params[0].optional, false);
91
+
92
+ assert.equal(m.length, 2);
93
+ assert.equal(m[0], 'string');
94
+ assert.equal(m[1], 'string');
95
+ });
96
+
97
+ it('should not match format parts', function () {
98
+ var params = [];
99
+ var m = pathToRegExp('/:test.json', params).exec('/route.json');
100
+
101
+ assert.equal(params.length, 1);
102
+ assert.equal(params[0].name, 'test');
103
+ assert.equal(params[0].optional, false);
104
+
105
+ assert.equal(m.length, 2);
106
+ assert.equal(m[0], '/route.json');
107
+ assert.equal(m[1], 'route');
108
+ });
109
+
110
+ it('should match format parts', function () {
111
+ var params = [];
112
+ var re = pathToRegExp('/:test.:format', params);
113
+ var m;
114
+
115
+ assert.equal(params.length, 2);
116
+ assert.equal(params[0].name, 'test');
117
+ assert.equal(params[0].optional, false);
118
+ assert.equal(params[1].name, 'format');
119
+ assert.equal(params[1].optional, false);
120
+
121
+ m = re.exec('/route.json');
122
+
123
+ assert.equal(m.length, 3);
124
+ assert.equal(m[0], '/route.json');
125
+ assert.equal(m[1], 'route');
126
+ assert.equal(m[2], 'json');
127
+
128
+ m = re.exec('/route');
129
+
130
+ assert.ok(!m);
131
+ });
132
+
133
+ it('should match route parts with a trailing format', function () {
134
+ var params = [];
135
+ var m = pathToRegExp('/:test.json', params).exec('/route.json');
136
+
137
+ assert.equal(params.length, 1);
138
+ assert.equal(params[0].name, 'test');
139
+ assert.equal(params[0].optional, false);
140
+
141
+ assert.equal(m.length, 2);
142
+ assert.equal(m[0], '/route.json');
143
+ assert.equal(m[1], 'route');
144
+ });
145
+
146
+ it('should match optional trailing routes', function () {
147
+ var params = [];
148
+ var m = pathToRegExp('/test*', params).exec('/test/route');
149
+
150
+ assert.equal(params.length, 0);
151
+
152
+ assert.equal(m.length, 2);
153
+ assert.equal(m[0], '/test/route');
154
+ assert.equal(m[1], '/route');
155
+ });
156
+
157
+ it('should match optional trailing routes after a param', function () {
158
+ var params = [];
159
+ var re = pathToRegExp('/:test*', params);
160
+ var m;
161
+
162
+ assert.equal(params.length, 1);
163
+ assert.equal(params[0].name, 'test');
164
+ assert.equal(params[0].optional, false);
165
+
166
+ m = re.exec('/test/route');
167
+
168
+ assert.equal(m.length, 3);
169
+ assert.equal(m[0], '/test/route');
170
+ assert.equal(m[1], 'test');
171
+ assert.equal(m[2], '/route');
172
+
173
+ m = re.exec('/testing');
174
+
175
+ assert.equal(m.length, 3);
176
+ assert.equal(m[0], '/testing');
177
+ assert.equal(m[1], 'testing');
178
+ assert.equal(m[2], '');
179
+ });
180
+
181
+ it('should match optional trailing routes before a format', function () {
182
+ var params = [];
183
+ var re = pathToRegExp('/test*.json', params);
184
+ var m;
185
+
186
+ assert.equal(params.length, 0);
187
+
188
+ m = re.exec('/test.json');
189
+
190
+ assert.equal(m.length, 2);
191
+ assert.equal(m[0], '/test.json');
192
+ assert.equal(m[1], '');
193
+
194
+ m = re.exec('/testing.json');
195
+
196
+ assert.equal(m.length, 2);
197
+ assert.equal(m[0], '/testing.json');
198
+ assert.equal(m[1], 'ing');
199
+
200
+ m = re.exec('/test/route.json');
201
+
202
+ assert.equal(m.length, 2);
203
+ assert.equal(m[0], '/test/route.json');
204
+ assert.equal(m[1], '/route');
205
+ });
206
+
207
+ it('should match optional trailing routes after a param and before a format', function () {
208
+ var params = [];
209
+ var re = pathToRegExp('/:test*.json', params);
210
+ var m;
211
+
212
+ assert.equal(params.length, 1);
213
+ assert.equal(params[0].name, 'test');
214
+ assert.equal(params[0].optional, false);
215
+
216
+ m = re.exec('/testing.json');
217
+
218
+ assert.equal(m.length, 3);
219
+ assert.equal(m[0], '/testing.json');
220
+ assert.equal(m[1], 'testing');
221
+ assert.equal(m[2], '');
222
+
223
+ m = re.exec('/test/route.json');
224
+
225
+ assert.equal(m.length, 3);
226
+ assert.equal(m[0], '/test/route.json');
227
+ assert.equal(m[1], 'test');
228
+ assert.equal(m[2], '/route');
229
+
230
+ m = re.exec('.json');
231
+
232
+ assert.ok(!m);
233
+ });
234
+
235
+ it('should match optional trailing routes between a normal param and a format param', function () {
236
+ var params = [];
237
+ var re = pathToRegExp('/:test*.:format', params);
238
+ var m;
239
+
240
+ assert.equal(params.length, 2);
241
+ assert.equal(params[0].name, 'test');
242
+ assert.equal(params[0].optional, false);
243
+ assert.equal(params[1].name, 'format');
244
+ assert.equal(params[1].optional, false);
245
+
246
+ m = re.exec('/testing.json');
247
+
248
+ assert.equal(m.length, 4);
249
+ assert.equal(m[0], '/testing.json');
250
+ assert.equal(m[1], 'testing');
251
+ assert.equal(m[2], '');
252
+ assert.equal(m[3], 'json');
253
+
254
+ m = re.exec('/test/route.json');
255
+
256
+ assert.equal(m.length, 4);
257
+ assert.equal(m[0], '/test/route.json');
258
+ assert.equal(m[1], 'test');
259
+ assert.equal(m[2], '/route');
260
+ assert.equal(m[3], 'json');
261
+
262
+ m = re.exec('/test');
263
+
264
+ assert.ok(!m);
265
+
266
+ m = re.exec('.json');
267
+
268
+ assert.ok(!m);
269
+ });
270
+
271
+ it('should match optional trailing routes after a param and before an optional format param', function () {
272
+ var params = [];
273
+ var re = pathToRegExp('/:test*.:format?', params);
274
+ var m;
275
+
276
+ assert.equal(params.length, 2);
277
+ assert.equal(params[0].name, 'test');
278
+ assert.equal(params[0].optional, false);
279
+ assert.equal(params[1].name, 'format');
280
+ assert.equal(params[1].optional, true);
281
+
282
+ m = re.exec('/testing.json');
283
+
284
+ assert.equal(m.length, 4);
285
+ assert.equal(m[0], '/testing.json');
286
+ assert.equal(m[1], 'testing');
287
+ assert.equal(m[2], '');
288
+ assert.equal(m[3], 'json');
289
+
290
+ m = re.exec('/test/route.json');
291
+
292
+ assert.equal(m.length, 4);
293
+ assert.equal(m[0], '/test/route.json');
294
+ assert.equal(m[1], 'test');
295
+ assert.equal(m[2], '/route');
296
+ assert.equal(m[3], 'json');
297
+
298
+ m = re.exec('/test');
299
+
300
+ assert.equal(m.length, 4);
301
+ assert.equal(m[0], '/test');
302
+ assert.equal(m[1], 'test');
303
+ assert.equal(m[2], '');
304
+ assert.equal(m[3], undefined);
305
+
306
+ m = re.exec('.json');
307
+
308
+ assert.ok(!m);
309
+ });
310
+
311
+ it('should match optional trailing routes inside optional express param', function () {
312
+ var params = [];
313
+ var re = pathToRegExp('/:test*?', params);
314
+ var m;
315
+
316
+ assert.equal(params.length, 1);
317
+ assert.equal(params[0].name, 'test');
318
+ assert.equal(params[0].optional, true);
319
+
320
+ m = re.exec('/test/route');
321
+
322
+ assert.equal(m.length, 3);
323
+ assert.equal(m[0], '/test/route');
324
+ assert.equal(m[1], 'test');
325
+ assert.equal(m[2], '/route');
326
+
327
+ m = re.exec('/test');
328
+
329
+ assert.equal(m.length, 3);
330
+ assert.equal(m[0], '/test');
331
+ assert.equal(m[1], 'test');
332
+ assert.equal(m[2], '');
333
+
334
+ m = re.exec('/');
335
+
336
+ assert.equal(m.length, 3);
337
+ assert.equal(m[0], '/');
338
+ assert.equal(m[1], undefined);
339
+ assert.equal(m[2], undefined);
340
+ });
341
+
342
+ it('should do case insensitive matches', function () {
343
+ var m = pathToRegExp('/test').exec('/TEST');
344
+
345
+ assert.equal(m[0], '/TEST');
346
+ });
347
+
348
+ it('should do case sensitive matches', function () {
349
+ var re = pathToRegExp('/test', null, { sensitive: true });
350
+ var m;
351
+
352
+ m = re.exec('/test');
353
+
354
+ assert.equal(m.length, 1);
355
+ assert.equal(m[0], '/test');
356
+
357
+ m = re.exec('/TEST');
358
+
359
+ assert.ok(!m);
360
+ });
361
+
362
+ it('should do non-ending matches', function () {
363
+ var params = [];
364
+ var m = pathToRegExp('/:test', params, { end: false }).exec('/test/route');
365
+
366
+ assert.equal(params.length, 1);
367
+ assert.equal(params[0].name, 'test');
368
+ assert.equal(params[0].optional, false);
369
+
370
+ assert.equal(m.length, 2);
371
+ assert.equal(m[0], '/test');
372
+ assert.equal(m[1], 'test');
373
+ });
374
+
375
+ it('should match trailing slashes in non-ending non-strict mode', function () {
376
+ var params = [];
377
+ var re = pathToRegExp('/:test', params, { end: false });
378
+ var m;
379
+
380
+ assert.equal(params.length, 1);
381
+ assert.equal(params[0].name, 'test');
382
+ assert.equal(params[0].optional, false);
383
+
384
+ m = re.exec('/test/');
385
+
386
+ assert.equal(m.length, 2);
387
+ assert.equal(m[0], '/test/');
388
+ assert.equal(m[1], 'test');
389
+ });
390
+
391
+ it('should not match trailing slashes in non-ending strict mode', function () {
392
+ var params = [];
393
+ var re = pathToRegExp('/:test', params, { end: false, strict: true });
394
+
395
+ assert.equal(params.length, 1);
396
+ assert.equal(params[0].name, 'test');
397
+ assert.equal(params[0].optional, false);
398
+
399
+ m = re.exec('/test/');
400
+
401
+ assert.equal(m.length, 2);
402
+ assert.equal(m[0], '/test');
403
+ assert.equal(m[1], 'test');
404
+ });
405
+
406
+ it('should match text after an express param', function () {
407
+ var params = [];
408
+ var re = pathToRegExp('/(:test)route', params);
409
+
410
+ assert.equal(params.length, 1);
411
+ assert.equal(params[0].name, 'test');
412
+ assert.equal(params[0].optional, false);
413
+
414
+ m = re.exec('/route');
415
+
416
+ assert.ok(!m);
417
+
418
+ m = re.exec('/testroute');
419
+
420
+ assert.equal(m.length, 2);
421
+ assert.equal(m[0], '/testroute');
422
+ assert.equal(m[1], 'test');
423
+
424
+ m = re.exec('testroute');
425
+
426
+ assert.ok(!m);
427
+ });
428
+
429
+ it('should match text after an optional express param', function () {
430
+ var params = [];
431
+ var re = pathToRegExp('/(:test?)route', params);
432
+ var m;
433
+
434
+ assert.equal(params.length, 1);
435
+ assert.equal(params[0].name, 'test');
436
+ assert.equal(params[0].optional, true);
437
+
438
+ m = re.exec('/route');
439
+
440
+ assert.equal(m.length, 2);
441
+ assert.equal(m[0], '/route');
442
+ assert.equal(m[1], undefined);
443
+
444
+ m = re.exec('/testroute');
445
+
446
+ assert.equal(m.length, 2);
447
+ assert.equal(m[0], '/testroute');
448
+ assert.equal(m[1], 'test');
449
+
450
+ m = re.exec('route');
451
+
452
+ assert.ok(!m);
453
+ });
454
+
455
+ it('should match optional formats', function () {
456
+ var params = [];
457
+ var re = pathToRegExp('/:test.:format?', params);
458
+ var m;
459
+
460
+ assert.equal(params.length, 2);
461
+ assert.equal(params[0].name, 'test');
462
+ assert.equal(params[0].optional, false);
463
+ assert.equal(params[1].name, 'format');
464
+ assert.equal(params[1].optional, true);
465
+
466
+ m = re.exec('/route');
467
+
468
+ assert.equal(m.length, 3);
469
+ assert.equal(m[0], '/route');
470
+ assert.equal(m[1], 'route');
471
+ assert.equal(m[2], undefined);
472
+
473
+ m = re.exec('/route.json');
474
+
475
+ assert.equal(m.length, 3);
476
+ assert.equal(m[0], '/route.json');
477
+ assert.equal(m[1], 'route');
478
+ assert.equal(m[2], 'json');
479
+ });
480
+
481
+ it('should match full paths with format by default', function () {
482
+ var params = [];
483
+ var m = pathToRegExp('/:test', params).exec('/test.json');
484
+
485
+ assert.equal(params.length, 1);
486
+ assert.equal(params[0].name, 'test');
487
+ assert.equal(params[0].optional, false);
488
+
489
+ assert.equal(m.length, 2);
490
+ assert.equal(m[0], '/test.json');
491
+ assert.equal(m[1], 'test.json');
492
+ });
493
+ });
494
+
495
+ describe('regexps', function () {
496
+ it('should return the regexp', function () {
497
+ assert.deepEqual(pathToRegExp(/.*/), /.*/);
498
+ });
499
+ });
500
+
501
+ describe('arrays', function () {
502
+ it('should join arrays parts', function () {
503
+ var re = pathToRegExp(['/test', '/route']);
504
+
505
+ assert.ok(re.exec('/test'));
506
+ assert.ok(re.exec('/route'));
507
+ assert.ok(!re.exec('/else'));
508
+ });
509
+ });
510
+ });