regexparam
Advanced tools
Comparing version 1.0.2 to 1.1.0
module.exports = function (str) { | ||
var c, o, tmp, keys=[], pattern='', arr=str.split('/'); | ||
var c, o, tmp, ext, keys=[], pattern='', arr=str.split('/'); | ||
arr[0] || arr.shift(); | ||
@@ -11,5 +11,7 @@ | ||
} else if (c === ':') { | ||
o = tmp[tmp.length - 1] === '?'; // optional? | ||
keys.push( tmp.substring(1, o ? tmp.length - 1 : tmp.length) ); | ||
pattern += o ? '(?:/([^/]+?))?' : '/([^/]+?)'; | ||
o = tmp.indexOf('?', 1); | ||
ext = tmp.indexOf('.', 1); | ||
keys.push( tmp.substring(1, !!~o ? o : !!~ext ? ext : tmp.length) ); | ||
pattern += !!~o && !~ext ? '(?:/([^/]+?))?' : '/([^/]+?)'; | ||
if (!!~ext) pattern += (!!~o ? '?' : '') + '\\' + tmp.substring(ext); | ||
} else { | ||
@@ -19,7 +21,7 @@ pattern += '/' + tmp; | ||
} | ||
keys.length && (pattern += '(?:/)?'); | ||
return { | ||
keys: keys, | ||
pattern: new RegExp('^' + pattern + '\/?$', 'i') | ||
pattern: new RegExp('^' + pattern + (keys.length ? '(?:/)?' : '') + '\/?$', 'i') | ||
}; | ||
} |
{ | ||
"name": "regexparam", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"repository": "lukeed/regexparam", | ||
"description": "A tiny (252B) utility that converts route patterns into RegExp. Limited alternative to `path-to-regexp` 🙇", | ||
"description": "A tiny (285B) utility that converts route patterns into RegExp. Limited alternative to `path-to-regexp` 🙇", | ||
"module": "dist/regexparam.mjs", | ||
@@ -18,3 +18,3 @@ "main": "dist/regexparam.js", | ||
"scripts": { | ||
"build": "node builder", | ||
"build": "bundt", | ||
"pretest": "npm run build", | ||
@@ -32,9 +32,6 @@ "test": "tape test/*.js | tap-spec" | ||
"devDependencies": { | ||
"gzip-size": "^3.0.0", | ||
"mk-dirs": "^1.0.0", | ||
"pretty-bytes": "^4.0.2", | ||
"bundt": "^0.3.0", | ||
"tap-spec": "^4.1.1", | ||
"tape": "^4.8.0", | ||
"terser": "^3.13.1" | ||
"tape": "^4.8.0" | ||
} | ||
} |
# regexparam [![Build Status](https://travis-ci.org/lukeed/regexparam.svg?branch=master)](https://travis-ci.org/lukeed/regexparam) | ||
> A tiny (252B) utility that converts route patterns into RegExp. Limited alternative to [`path-to-regexp`](https://github.com/pillarjs/path-to-regexp) 🙇 | ||
> A tiny (285B) utility that converts route patterns into RegExp. Limited alternative to [`path-to-regexp`](https://github.com/pillarjs/path-to-regexp) 🙇 | ||
@@ -13,11 +13,10 @@ With `regexparam`, you may turn a pathing string (eg, `/users/:id`) into a regular expression. | ||
* Parameter (`/:title`, `/books/:title`, `/books/:genre/:title`) | ||
* Parameter w/ Suffix (`/movies/:title.mp4`, `/movies/:title.(mp4|mov)`) | ||
* Optional Parameters (`/:title?`, `/books/:title?`, `/books/:genre/:title?`) | ||
* Wildcards (`*`, `/books/*`, `/books/:genre/*`) | ||
Lastly, please note that while this route-parser is not slow, you should use [`matchit`](https://github.com/lukeed/matchit#benchmarks) or [`trouter`](https://github.com/lukeed/trouter) if performance is of critical importance. This is especially true for backend/server scenarios! | ||
This module exposes two module definitions: | ||
* **ES Module**: `dist/regexparam.mjs` | ||
* **CommonJS**: `dist/regexparam.js` | ||
* **ESModule**: `dist/regexparam.mjs` | ||
@@ -36,13 +35,2 @@ ## Install | ||
let foo = regexparam('users/*'); | ||
// foo.keys => ['wild'] | ||
// foo.pattern => /^\/users\/(.*)(?:\/)?\/?$/i | ||
let bar = regexparam('/books/:genre/:title?') | ||
// bar.keys => ['genre', 'title'] | ||
// bar.pattern => /^\/books\/([^\/]+?)(?:\/([^\/]+?))?(?:\/)?\/?$/i | ||
bar.pattern.test('/books/horror'); //=> true | ||
bar.pattern.test('/books/horror/goosebumps'); //=> true | ||
// Example param-assignment | ||
@@ -58,7 +46,44 @@ function exec(path, result) { | ||
exec('/books/horror', bar); | ||
//=> { genre:'horror', title:null } | ||
exec('/books/horror/goosebumps', bar); | ||
//=> { genre:'horror', title:'goosebumps' } | ||
// Parameter, with Optional Parameter | ||
// --- | ||
let foo = regexparam('/books/:genre/:title?') | ||
// foo.pattern => /^\/books\/([^\/]+?)(?:\/([^\/]+?))?(?:\/)?\/?$/i | ||
// foo.keys => ['genre', 'title'] | ||
foo.pattern.test('/books/horror'); //=> true | ||
foo.pattern.test('/books/horror/goosebumps'); //=> true | ||
exec('/books/horror', foo); | ||
//=> { genre: 'horror', title: null } | ||
exec('/books/horror/goosebumps', foo); | ||
//=> { genre: 'horror', title: 'goosebumps' } | ||
// Parameter, with suffix | ||
// --- | ||
let bar = regexparam('/movies/:title.(mp4|mov)'); | ||
// bar.pattern => /^\/movies\/([^\/]+?)\.(mp4|mov)(?:\/)?\/?$/i | ||
// bar.keys => ['title'] | ||
bar.pattern.test('/movies/narnia'); //=> false | ||
bar.pattern.test('/movies/narnia.mp3'); //=> false | ||
bar.pattern.test('/movies/narnia.mp4'); //=> true | ||
exec('/movies/narnia.mp4', bar); | ||
//=> { title: 'narnia' } | ||
// Wildcard | ||
// --- | ||
let baz = regexparam('users/*'); | ||
// baz.pattern => /^\/users\/(.*)(?:\/)?\/?$/i | ||
// baz.keys => ['wild'] | ||
baz.pattern.test('/users'); //=> false | ||
baz.pattern.test('/users/lukeed'); //=> true | ||
exec('/users/lukeed/repos/new', baz); | ||
//=> { wild: 'lukeed/repos/new' } | ||
``` | ||
@@ -68,2 +93,3 @@ | ||
## API | ||
@@ -83,4 +109,11 @@ | ||
## Related | ||
- [trouter](https://github.com/lukeed/trouter) - A server-side HTTP router that extends from this module. | ||
- [matchit](https://github.com/lukeed/matchit) - Similar (650B) library, but relies on String comparison instead of `RegExp`s. | ||
## License | ||
MIT © [Luke Edwards](https://lukeed.com) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6595
3
46
115