path-to-regexp 0.1.3 → 0.1.7

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 CHANGED
@@ -1,3 +1,23 @@
1
+ 0.1.7 / 2015-07-28
2
+ ==================
3
+
4
+ * Fixed regression with escaped round brackets and matching groups.
5
+
6
+ 0.1.6 / 2015-06-19
7
+ ==================
8
+
9
+ * Replace `index` feature by outputting all parameters, unnamed and named.
10
+
11
+ 0.1.5 / 2015-05-08
12
+ ==================
13
+
14
+ * Add an index property for position in match result.
15
+
16
+ 0.1.4 / 2015-03-05
17
+ ==================
18
+
19
+ * Add license information
20
+
1
21
  0.1.3 / 2014-07-06
2
22
  ==================
3
23
 
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Blake Embrey (hello@blakeembrey.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/Readme.md CHANGED
@@ -1,13 +1,15 @@
1
-
2
1
  # Path-to-RegExp
3
2
 
4
- Turn an Express-style path string such as `/user/:name` into a regular expression.
3
+ Turn an Express-style path string such as `/user/:name` into a regular expression.
4
+
5
+ **Note:** This is a legacy branch. You should upgrade to `1.x`.
5
6
 
6
7
  ## Usage
7
8
 
8
9
  ```javascript
9
10
  var pathToRegexp = require('path-to-regexp');
10
11
  ```
12
+
11
13
  ### pathToRegexp(path, keys, options)
12
14
 
13
15
  - **path** A string in the express format, an array of such strings, or a regular expression
@@ -30,4 +32,4 @@ You can see a live demo of this library in use at [express-route-tester](http://
30
32
 
31
33
  ## License
32
34
 
33
- MIT
35
+ MIT
package/index.js CHANGED
@@ -4,6 +4,11 @@
4
4
 
5
5
  module.exports = pathtoRegexp;
6
6
 
7
+ /**
8
+ * Match matching groups in a regular expression.
9
+ */
10
+ var MATCHING_GROUP_REGEXP = /\((?!\?)/g;
11
+
7
12
  /**
8
13
  * Normalize the given path string,
9
14
  * returning a regular expression.
@@ -22,12 +27,25 @@ module.exports = pathtoRegexp;
22
27
 
23
28
  function pathtoRegexp(path, keys, options) {
24
29
  options = options || {};
30
+ keys = keys || [];
25
31
  var strict = options.strict;
26
32
  var end = options.end !== false;
27
33
  var flags = options.sensitive ? '' : 'i';
28
- keys = keys || [];
34
+ var extraOffset = 0;
35
+ var keysOffset = keys.length;
36
+ var i = 0;
37
+ var name = 0;
38
+ var m;
29
39
 
30
40
  if (path instanceof RegExp) {
41
+ while (m = MATCHING_GROUP_REGEXP.exec(path.source)) {
42
+ keys.push({
43
+ name: name++,
44
+ optional: false,
45
+ offset: m.index
46
+ });
47
+ }
48
+
31
49
  return path;
32
50
  }
33
51
 
@@ -45,23 +63,64 @@ function pathtoRegexp(path, keys, options) {
45
63
  path = ('^' + path + (strict ? '' : path[path.length - 1] === '/' ? '?' : '/?'))
46
64
  .replace(/\/\(/g, '/(?:')
47
65
  .replace(/([\/\.])/g, '\\$1')
48
- .replace(/(\\\/)?(\\\.)?:(\w+)(\(.*?\))?(\*)?(\?)?/g, function (match, slash, format, key, capture, star, optional) {
66
+ .replace(/(\\\/)?(\\\.)?:(\w+)(\(.*?\))?(\*)?(\?)?/g, function (match, slash, format, key, capture, star, optional, offset) {
49
67
  slash = slash || '';
50
68
  format = format || '';
51
69
  capture = capture || '([^\\/' + format + ']+?)';
52
70
  optional = optional || '';
53
71
 
54
- keys.push({ name: key, optional: !!optional });
72
+ keys.push({
73
+ name: key,
74
+ optional: !!optional,
75
+ offset: offset + extraOffset
76
+ });
55
77
 
56
- return ''
78
+ var result = ''
57
79
  + (optional ? '' : slash)
58
80
  + '(?:'
59
81
  + format + (optional ? slash : '') + capture
60
82
  + (star ? '((?:[\\/' + format + '].+?)?)' : '')
61
83
  + ')'
62
84
  + optional;
85
+
86
+ extraOffset += result.length - match.length;
87
+
88
+ return result;
63
89
  })
64
- .replace(/\*/g, '(.*)');
90
+ .replace(/\*/g, function (star, index) {
91
+ var len = keys.length
92
+
93
+ while (len-- > keysOffset && keys[len].offset > index) {
94
+ keys[len].offset += 3; // Replacement length minus asterisk length.
95
+ }
96
+
97
+ return '(.*)';
98
+ });
99
+
100
+ // This is a workaround for handling unnamed matching groups.
101
+ while (m = MATCHING_GROUP_REGEXP.exec(path)) {
102
+ var escapeCount = 0;
103
+ var index = m.index;
104
+
105
+ while (path.charAt(--index) === '\\') {
106
+ escapeCount++;
107
+ }
108
+
109
+ // It's possible to escape the bracket.
110
+ if (escapeCount % 2 === 1) {
111
+ continue;
112
+ }
113
+
114
+ if (keysOffset + i === keys.length || keys[keysOffset + i].offset > m.index) {
115
+ keys.splice(keysOffset + i, 0, {
116
+ name: name++, // Unnamed matching groups must be consistently linear.
117
+ optional: false,
118
+ offset: m.index
119
+ });
120
+ }
121
+
122
+ i++;
123
+ }
65
124
 
66
125
  // If the path is non-ending, match until the end or a slash.
67
126
  path += (end ? '$' : (path[path.length - 1] === '/' ? '' : '(?=\\/|$)'));
package/package.json CHANGED
@@ -1,7 +1,11 @@
1
1
  {
2
2
  "name": "path-to-regexp",
3
3
  "description": "Express style path to RegExp utility",
4
- "version": "0.1.3",
4
+ "version": "0.1.7",
5
+ "files": [
6
+ "index.js",
7
+ "LICENSE"
8
+ ],
5
9
  "scripts": {
6
10
  "test": "istanbul cover _mocha -- -R spec"
7
11
  },
@@ -14,6 +18,7 @@
14
18
  "path-to-regexp": "index.js"
15
19
  }
16
20
  },
21
+ "license": "MIT",
17
22
  "repository": {
18
23
  "type": "git",
19
24
  "url": "https://github.com/component/path-to-regexp.git"
package/.npmignore DELETED
@@ -1,2 +0,0 @@
1
- node_modules
2
- coverage
package/component.json DELETED
@@ -1,15 +0,0 @@
1
- {
2
- "name": "path-to-regexp",
3
- "description": "Express style path to RegExp utility",
4
- "version": "0.1.3",
5
- "keywords": [
6
- "express",
7
- "regexp",
8
- "route",
9
- "routing"
10
- ],
11
- "scripts": [
12
- "index.js"
13
- ],
14
- "license": "MIT"
15
- }
package/test.js DELETED
@@ -1,616 +0,0 @@
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 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
-
73
- it('should allow optional express format params', function () {
74
- var params = [];
75
- var re = pathToRegExp('/:test?', params);
76
- var m;
77
-
78
- assert.equal(params.length, 1);
79
- assert.equal(params[0].name, 'test');
80
- assert.equal(params[0].optional, true);
81
-
82
- m = re.exec('/route');
83
-
84
- assert.equal(m.length, 2);
85
- assert.equal(m[0], '/route');
86
- assert.equal(m[1], 'route');
87
-
88
- m = re.exec('/');
89
-
90
- assert.equal(m.length, 2);
91
- assert.equal(m[0], '/');
92
- assert.equal(m[1], undefined);
93
- });
94
-
95
- it('should allow express format param regexps', function () {
96
- var params = [];
97
- var m = pathToRegExp('/:page(\\d+)', params).exec('/56');
98
-
99
- assert.equal(params.length, 1);
100
- assert.equal(params[0].name, 'page');
101
- assert.equal(params[0].optional, false);
102
-
103
- assert.equal(m.length, 2);
104
- assert.equal(m[0], '/56');
105
- assert.equal(m[1], '56');
106
- });
107
-
108
- it('should match without a prefixed slash', function () {
109
- var params = [];
110
- var m = pathToRegExp(':test', params).exec('string');
111
-
112
- assert.equal(params.length, 1);
113
- assert.equal(params[0].name, 'test');
114
- assert.equal(params[0].optional, false);
115
-
116
- assert.equal(m.length, 2);
117
- assert.equal(m[0], 'string');
118
- assert.equal(m[1], 'string');
119
- });
120
-
121
- it('should not match format parts', function () {
122
- var params = [];
123
- var m = pathToRegExp('/:test.json', params).exec('/route.json');
124
-
125
- assert.equal(params.length, 1);
126
- assert.equal(params[0].name, 'test');
127
- assert.equal(params[0].optional, false);
128
-
129
- assert.equal(m.length, 2);
130
- assert.equal(m[0], '/route.json');
131
- assert.equal(m[1], 'route');
132
- });
133
-
134
- it('should match format parts', function () {
135
- var params = [];
136
- var re = pathToRegExp('/:test.:format', params);
137
- var m;
138
-
139
- assert.equal(params.length, 2);
140
- assert.equal(params[0].name, 'test');
141
- assert.equal(params[0].optional, false);
142
- assert.equal(params[1].name, 'format');
143
- assert.equal(params[1].optional, false);
144
-
145
- m = re.exec('/route.json');
146
-
147
- assert.equal(m.length, 3);
148
- assert.equal(m[0], '/route.json');
149
- assert.equal(m[1], 'route');
150
- assert.equal(m[2], 'json');
151
-
152
- m = re.exec('/route');
153
-
154
- assert.ok(!m);
155
- });
156
-
157
- it('should match route parts with a trailing format', function () {
158
- var params = [];
159
- var m = pathToRegExp('/:test.json', params).exec('/route.json');
160
-
161
- assert.equal(params.length, 1);
162
- assert.equal(params[0].name, 'test');
163
- assert.equal(params[0].optional, false);
164
-
165
- assert.equal(m.length, 2);
166
- assert.equal(m[0], '/route.json');
167
- assert.equal(m[1], 'route');
168
- });
169
-
170
- it('should match optional trailing routes', function () {
171
- var params = [];
172
- var m = pathToRegExp('/test*', params).exec('/test/route');
173
-
174
- assert.equal(params.length, 0);
175
-
176
- assert.equal(m.length, 2);
177
- assert.equal(m[0], '/test/route');
178
- assert.equal(m[1], '/route');
179
- });
180
-
181
- it('should match optional trailing routes after a param', function () {
182
- var params = [];
183
- var re = pathToRegExp('/:test*', params);
184
- var m;
185
-
186
- assert.equal(params.length, 1);
187
- assert.equal(params[0].name, 'test');
188
- assert.equal(params[0].optional, false);
189
-
190
- m = re.exec('/test/route');
191
-
192
- assert.equal(m.length, 3);
193
- assert.equal(m[0], '/test/route');
194
- assert.equal(m[1], 'test');
195
- assert.equal(m[2], '/route');
196
-
197
- m = re.exec('/testing');
198
-
199
- assert.equal(m.length, 3);
200
- assert.equal(m[0], '/testing');
201
- assert.equal(m[1], 'testing');
202
- assert.equal(m[2], '');
203
- });
204
-
205
- it('should match optional trailing routes before a format', function () {
206
- var params = [];
207
- var re = pathToRegExp('/test*.json', params);
208
- var m;
209
-
210
- assert.equal(params.length, 0);
211
-
212
- m = re.exec('/test.json');
213
-
214
- assert.equal(m.length, 2);
215
- assert.equal(m[0], '/test.json');
216
- assert.equal(m[1], '');
217
-
218
- m = re.exec('/testing.json');
219
-
220
- assert.equal(m.length, 2);
221
- assert.equal(m[0], '/testing.json');
222
- assert.equal(m[1], 'ing');
223
-
224
- m = re.exec('/test/route.json');
225
-
226
- assert.equal(m.length, 2);
227
- assert.equal(m[0], '/test/route.json');
228
- assert.equal(m[1], '/route');
229
- });
230
-
231
- it('should match optional trailing routes after a param and before a format', function () {
232
- var params = [];
233
- var re = pathToRegExp('/:test*.json', params);
234
- var m;
235
-
236
- assert.equal(params.length, 1);
237
- assert.equal(params[0].name, 'test');
238
- assert.equal(params[0].optional, false);
239
-
240
- m = re.exec('/testing.json');
241
-
242
- assert.equal(m.length, 3);
243
- assert.equal(m[0], '/testing.json');
244
- assert.equal(m[1], 'testing');
245
- assert.equal(m[2], '');
246
-
247
- m = re.exec('/test/route.json');
248
-
249
- assert.equal(m.length, 3);
250
- assert.equal(m[0], '/test/route.json');
251
- assert.equal(m[1], 'test');
252
- assert.equal(m[2], '/route');
253
-
254
- m = re.exec('.json');
255
-
256
- assert.ok(!m);
257
- });
258
-
259
- it('should match optional trailing routes between a normal param and a format param', function () {
260
- var params = [];
261
- var re = pathToRegExp('/:test*.:format', params);
262
- var m;
263
-
264
- assert.equal(params.length, 2);
265
- assert.equal(params[0].name, 'test');
266
- assert.equal(params[0].optional, false);
267
- assert.equal(params[1].name, 'format');
268
- assert.equal(params[1].optional, false);
269
-
270
- m = re.exec('/testing.json');
271
-
272
- assert.equal(m.length, 4);
273
- assert.equal(m[0], '/testing.json');
274
- assert.equal(m[1], 'testing');
275
- assert.equal(m[2], '');
276
- assert.equal(m[3], 'json');
277
-
278
- m = re.exec('/test/route.json');
279
-
280
- assert.equal(m.length, 4);
281
- assert.equal(m[0], '/test/route.json');
282
- assert.equal(m[1], 'test');
283
- assert.equal(m[2], '/route');
284
- assert.equal(m[3], 'json');
285
-
286
- m = re.exec('/test');
287
-
288
- assert.ok(!m);
289
-
290
- m = re.exec('.json');
291
-
292
- assert.ok(!m);
293
- });
294
-
295
- it('should match optional trailing routes after a param and before an optional format param', function () {
296
- var params = [];
297
- var re = pathToRegExp('/:test*.:format?', params);
298
- var m;
299
-
300
- assert.equal(params.length, 2);
301
- assert.equal(params[0].name, 'test');
302
- assert.equal(params[0].optional, false);
303
- assert.equal(params[1].name, 'format');
304
- assert.equal(params[1].optional, true);
305
-
306
- m = re.exec('/testing.json');
307
-
308
- assert.equal(m.length, 4);
309
- assert.equal(m[0], '/testing.json');
310
- assert.equal(m[1], 'testing');
311
- assert.equal(m[2], '');
312
- assert.equal(m[3], 'json');
313
-
314
- m = re.exec('/test/route.json');
315
-
316
- assert.equal(m.length, 4);
317
- assert.equal(m[0], '/test/route.json');
318
- assert.equal(m[1], 'test');
319
- assert.equal(m[2], '/route');
320
- assert.equal(m[3], 'json');
321
-
322
- m = re.exec('/test');
323
-
324
- assert.equal(m.length, 4);
325
- assert.equal(m[0], '/test');
326
- assert.equal(m[1], 'test');
327
- assert.equal(m[2], '');
328
- assert.equal(m[3], undefined);
329
-
330
- m = re.exec('.json');
331
-
332
- assert.ok(!m);
333
- });
334
-
335
- it('should match optional trailing routes inside optional express param', function () {
336
- var params = [];
337
- var re = pathToRegExp('/:test*?', params);
338
- var m;
339
-
340
- assert.equal(params.length, 1);
341
- assert.equal(params[0].name, 'test');
342
- assert.equal(params[0].optional, true);
343
-
344
- m = re.exec('/test/route');
345
-
346
- assert.equal(m.length, 3);
347
- assert.equal(m[0], '/test/route');
348
- assert.equal(m[1], 'test');
349
- assert.equal(m[2], '/route');
350
-
351
- m = re.exec('/test');
352
-
353
- assert.equal(m.length, 3);
354
- assert.equal(m[0], '/test');
355
- assert.equal(m[1], 'test');
356
- assert.equal(m[2], '');
357
-
358
- m = re.exec('/');
359
-
360
- assert.equal(m.length, 3);
361
- assert.equal(m[0], '/');
362
- assert.equal(m[1], undefined);
363
- assert.equal(m[2], undefined);
364
- });
365
-
366
- it('should do case insensitive matches', function () {
367
- var m = pathToRegExp('/test').exec('/TEST');
368
-
369
- assert.equal(m[0], '/TEST');
370
- });
371
-
372
- it('should do case sensitive matches', function () {
373
- var re = pathToRegExp('/test', null, { sensitive: true });
374
- var m;
375
-
376
- m = re.exec('/test');
377
-
378
- assert.equal(m.length, 1);
379
- assert.equal(m[0], '/test');
380
-
381
- m = re.exec('/TEST');
382
-
383
- assert.ok(!m);
384
- });
385
-
386
- it('should do non-ending matches', function () {
387
- var params = [];
388
- var m = pathToRegExp('/:test', params, { end: false }).exec('/test/route');
389
-
390
- assert.equal(params.length, 1);
391
- assert.equal(params[0].name, 'test');
392
- assert.equal(params[0].optional, false);
393
-
394
- assert.equal(m.length, 2);
395
- assert.equal(m[0], '/test');
396
- assert.equal(m[1], 'test');
397
- });
398
-
399
- it('should match trailing slashes in non-ending non-strict mode', function () {
400
- var params = [];
401
- var re = pathToRegExp('/:test', params, { end: false });
402
- var m;
403
-
404
- assert.equal(params.length, 1);
405
- assert.equal(params[0].name, 'test');
406
- assert.equal(params[0].optional, false);
407
-
408
- m = re.exec('/test/');
409
-
410
- assert.equal(m.length, 2);
411
- assert.equal(m[0], '/test/');
412
- assert.equal(m[1], 'test');
413
- });
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
-
469
- it('should not match trailing slashes in non-ending strict mode', function () {
470
- var params = [];
471
- var re = pathToRegExp('/route', params, { end: false, strict: true });
472
-
473
- assert.equal(params.length, 0);
474
-
475
- m = re.exec('/route');
476
-
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');
484
- });
485
-
486
- it('should match text after an express param', function () {
487
- var params = [];
488
- var re = pathToRegExp('/(:test)route', params);
489
-
490
- assert.equal(params.length, 1);
491
- assert.equal(params[0].name, 'test');
492
- assert.equal(params[0].optional, false);
493
-
494
- m = re.exec('/route');
495
-
496
- assert.ok(!m);
497
-
498
- m = re.exec('/testroute');
499
-
500
- assert.equal(m.length, 2);
501
- assert.equal(m[0], '/testroute');
502
- assert.equal(m[1], 'test');
503
-
504
- m = re.exec('testroute');
505
-
506
- assert.ok(!m);
507
- });
508
-
509
- it('should match text after an optional express param', function () {
510
- var params = [];
511
- var re = pathToRegExp('/(:test?)route', params);
512
- var m;
513
-
514
- assert.equal(params.length, 1);
515
- assert.equal(params[0].name, 'test');
516
- assert.equal(params[0].optional, true);
517
-
518
- m = re.exec('/route');
519
-
520
- assert.equal(m.length, 2);
521
- assert.equal(m[0], '/route');
522
- assert.equal(m[1], undefined);
523
-
524
- m = re.exec('/testroute');
525
-
526
- assert.equal(m.length, 2);
527
- assert.equal(m[0], '/testroute');
528
- assert.equal(m[1], 'test');
529
-
530
- m = re.exec('route');
531
-
532
- assert.ok(!m);
533
- });
534
-
535
- it('should match optional formats', function () {
536
- var params = [];
537
- var re = pathToRegExp('/:test.:format?', params);
538
- var m;
539
-
540
- assert.equal(params.length, 2);
541
- assert.equal(params[0].name, 'test');
542
- assert.equal(params[0].optional, false);
543
- assert.equal(params[1].name, 'format');
544
- assert.equal(params[1].optional, true);
545
-
546
- m = re.exec('/route');
547
-
548
- assert.equal(m.length, 3);
549
- assert.equal(m[0], '/route');
550
- assert.equal(m[1], 'route');
551
- assert.equal(m[2], undefined);
552
-
553
- m = re.exec('/route.json');
554
-
555
- assert.equal(m.length, 3);
556
- assert.equal(m[0], '/route.json');
557
- assert.equal(m[1], 'route');
558
- assert.equal(m[2], 'json');
559
- });
560
-
561
- it('should match full paths with format by default', function () {
562
- var params = [];
563
- var m = pathToRegExp('/:test', params).exec('/test.json');
564
-
565
- assert.equal(params.length, 1);
566
- assert.equal(params[0].name, 'test');
567
- assert.equal(params[0].optional, false);
568
-
569
- assert.equal(m.length, 2);
570
- assert.equal(m[0], '/test.json');
571
- assert.equal(m[1], 'test.json');
572
- });
573
- });
574
-
575
- describe('regexps', function () {
576
- it('should return the regexp', function () {
577
- assert.deepEqual(pathToRegExp(/.*/), /.*/);
578
- });
579
- });
580
-
581
- describe('arrays', function () {
582
- it('should join arrays parts', function () {
583
- var re = pathToRegExp(['/test', '/route']);
584
-
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');
614
- });
615
- });
616
- });