diff --git a/.travis.yml b/.travis.yml index 4a83e22..e2d609e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,3 +3,4 @@ node_js: - "0.11" - "0.10" - "0.8" +after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" diff --git a/Readme.md b/Readme.md index 27f5d3d..09b2efe 100644 --- a/Readme.md +++ b/Readme.md @@ -2,8 +2,15 @@ Turn an Express-style path string such as `/user/:name` into a regular expression. -[![Build Status](https://img.shields.io/travis/component/path-to-regexp/master.svg)](https://travis-ci.org/component/path-to-regexp) -[![NPM version](https://img.shields.io/npm/v/path-to-regexp.svg)](https://www.npmjs.org/package/path-to-regexp) +[![NPM version][npm-image]][npm-url] +[![Build status][travis-image]][travis-url] +[![Test coverage][coveralls-image]][coveralls-url] + +## Installation + +``` +npm install path-to-regexp --save +``` ## Usage @@ -119,12 +126,13 @@ re.exec('/test/route'); ## Compatibility with Express <= 4.x -Path-To-RegExp breaks compatibility with Express 3.x in a few ways: +Path-To-RegExp breaks compatibility with Express <= 4.x in a few ways: * RegExp special characters can now be used in the regular path. E.g. `/user[(\\d+)]` * All RegExp special characters can now be used inside the custom match. E.g. `/:user(.*)` * No more support for asterisk matching - use an explicit parameter instead. E.g. `/(.*)` * Parameters can have suffixes that augment meaning - `*`, `+` and `?`. E.g. `/:user*` +* Strings aren't interpreted as literal regexp strings - no more non-capturing groups, lookaheads, lookbehinds or nested matching groups (but you can still pass a regexp manually) ## Live Demo @@ -133,3 +141,10 @@ You can see a live demo of this library in use at [express-route-tester](http:// ## License MIT + +[npm-image]: https://img.shields.io/npm/v/path-to-regexp.svg?style=flat +[npm-url]: https://npmjs.org/package/path-to-regexp +[travis-image]: https://img.shields.io/travis/component/path-to-regexp.svg?style=flat +[travis-url]: https://travis-ci.org/component/path-to-regexp +[coveralls-image]: https://img.shields.io/coveralls/component/path-to-regexp.svg?style=flat +[coveralls-url]: https://coveralls.io/r/component/path-to-regexp?branch=master diff --git a/index.js b/index.js index 143bffe..25c9af3 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,11 @@ */ module.exports = pathtoRegexp; +/** + * The main path matching regexp utility. + * + * @type {RegExp} + */ var PATH_REGEXP = new RegExp([ // Match already escaped characters that would otherwise incorrectly appear // in future matches. This allows the user to escape special characters that @@ -28,6 +33,19 @@ function escapeGroup (group) { return group.replace(/([=!:$\/()])/g, '\\$1'); } +/** + * Attach the keys as a property of the regexp. + * + * @param {RegExp} re + * @param {Array} keys + * @return {RegExp} + */ +var attachKeys = function (re, keys) { + re.keys = keys; + + return re; +}; + /** * Normalize the given path string, returning a regular expression. * @@ -63,7 +81,7 @@ function pathtoRegexp (path, keys, options) { })); // Return the source back to the user. - return path; + return attachKeys(path, keys); } if (Array.isArray(path)) { @@ -75,7 +93,7 @@ function pathtoRegexp (path, keys, options) { }); // Generate a new regexp instance by joining all the parts together. - return new RegExp('(?:' + path.join('|') + ')', flags); + return attachKeys(new RegExp('(?:' + path.join('|') + ')', flags), keys); } // Alter the path string into a usable regexp. @@ -140,5 +158,5 @@ function pathtoRegexp (path, keys, options) { path += strict && endsWithSlash ? '' : '(?=\\/|$)'; } - return new RegExp('^' + path + (end ? '$' : ''), flags); + return attachKeys(new RegExp('^' + path + (end ? '$' : ''), flags), keys); }; diff --git a/package.json b/package.json index 243a077..f2822c2 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "path-to-regexp", "description": "Express style path to RegExp utility", - "version": "0.2.3", + "version": "0.2.4", "scripts": { "test": "istanbul cover node_modules/mocha/bin/_mocha -- -R spec" }, diff --git a/test.js b/test.js index 5a150c8..15ea500 100644 --- a/test.js +++ b/test.js @@ -814,8 +814,17 @@ var TESTS = [ ], /** - * Regressions. + * Real world examples. */ + [ + '/:foo/:bar', + [ + { name: 'foo', delimiter: '/', optional: false, repeat: false }, + { name: 'bar', delimiter: '/', optional: false, repeat: false } + ], + '/match/route', + ['/match/route', 'match', 'route'] + ], [ '/:remote([\\w-.]+)/:user([\\w-]+)', [ @@ -831,12 +840,6 @@ var TESTS = [ * Dynamically generate the entire test suite. */ describe('path-to-regexp', function () { - it('should not break when keys aren\'t provided', function () { - var re = pathToRegexp('/:foo/:bar'); - - assert.deepEqual(exec(re, '/test/route'), ['/test/route', 'test', 'route']); - }); - TESTS.forEach(function (test) { var description = ''; var options = test[4] || {}; @@ -860,7 +863,8 @@ describe('path-to-regexp', function () { var params = []; var re = pathToRegexp(test[0], params, test[4]); - // Check the params are as expected. + // Check the keys are as expected. + assert.equal(re.keys, params); assert.deepEqual(params, test[1]); // Run the regexp and check the result is expected.