Skip to content

Commit 53c648f

Browse files
committed
Major changes
* New feature! Ability to set `endsWith` to match paths like `/test?query=string` and ignore the query string * Remove `isarray` * Explicitly handle trailing delimiters instead of trimming them (e.g. `/test/` is now treated as `/test/` instead of `/test` when matching) * Remove overloaded `keys` argument that accepts `options` * Remove `keys` list attached to the `RegExp` output * Remove asterisk functionality (it's a real pain to properly encode) * Change `tokensToFunction` (e.g. `compile`) to accept an `encode` function for pretty encoding (e.g. pass your own implementation)
1 parent e97122d commit 53c648f

File tree

7 files changed

+185
-371
lines changed

7 files changed

+185
-371
lines changed

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
sudo: false
12
language: node_js
23

34
node_js:
4-
- "0.10"
5-
- "0.12"
65
- "4.0"
7-
- "4.1"
6+
- "stable"
87

98
after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"

Readme.md

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Path-to-RegExp
22

3-
> Turn an Express-style path string such as `/user/:name` into a regular expression.
3+
> Turn a path string such as `/user/:name` into a regular expression.
44
55
[![NPM version][npm-image]][npm-url]
66
[![Build status][travis-image]][travis-url]
@@ -20,18 +20,19 @@ npm install path-to-regexp --save
2020
```javascript
2121
var pathToRegexp = require('path-to-regexp')
2222

23-
// pathToRegexp(path, keys, options)
23+
// pathToRegexp(path, keys?, options?)
2424
// pathToRegexp.parse(path)
2525
// pathToRegexp.compile(path)
2626
```
2727

28-
- **path** An Express-style string, an array of strings, or a regular expression.
28+
- **path** A string, array of strings, or a regular expression.
2929
- **keys** An array to be populated with the keys found in the path.
3030
- **options**
3131
- **sensitive** When `true` the route will be case sensitive. (default: `false`)
3232
- **strict** When `false` the trailing slash is optional. (default: `false`)
3333
- **end** When `false` the path will match at the beginning. (default: `true`)
3434
- **delimiter** Set the default delimiter for repeat parameters. (default: `'/'`)
35+
- **endsWith** Optional character, or list of characters, to treat as "end" characters
3536

3637
```javascript
3738
var keys = []
@@ -40,18 +41,18 @@ var re = pathToRegexp('/foo/:bar', keys)
4041
// keys = [{ name: 'bar', prefix: '/', delimiter: '/', optional: false, repeat: false, pattern: '[^\\/]+?' }]
4142
```
4243

43-
**Please note:** The `RegExp` returned by `path-to-regexp` is intended for use with pathnames or hostnames. It can not handle the query strings or fragments of a URL.
44+
**Please note:** The `RegExp` returned by `path-to-regexp` is intended for ordered data (e.g. pathnames, hostnames). It does not handle arbitrary data (e.g. query strings, URL fragments, JSON, etc).
4445

4546
### Parameters
4647

47-
The path string can be used to define parameters and populate the keys.
48+
The path argument is used to define parameters and populate the list of keys.
4849

4950
#### Named Parameters
5051

5152
Named parameters are defined by prefixing a colon to the parameter name (`:foo`). By default, the parameter will match until the following path segment.
5253

5354
```js
54-
var re = pathToRegexp('/:foo/:bar', keys)
55+
var re = pathToRegexp('/:foo/:bar')
5556
// keys = [{ name: 'foo', prefix: '/', ... }, { name: 'bar', prefix: '/', ... }]
5657

5758
re.exec('/test/route')
@@ -61,21 +62,21 @@ re.exec('/test/route')
6162
**Please note:** Named parameters must be made up of "word characters" (`[A-Za-z0-9_]`).
6263

6364
```js
64-
var re = pathToRegexp('/(apple-)?icon-:res(\\d+).png', keys)
65+
var re = pathToRegexp('/(apple-)?icon-:res(\\d+).png')
6566
// keys = [{ name: 0, prefix: '/', ... }, { name: 'res', prefix: '', ... }]
6667

6768
re.exec('/icon-76.png')
6869
//=> ['/icon-76.png', undefined, '76']
6970
```
7071

71-
#### Modified Parameters
72+
#### Parameter Modifiers
7273

7374
##### Optional
7475

75-
Parameters can be suffixed with a question mark (`?`) to make the parameter optional. This will also make the prefix optional.
76+
Parameters can be suffixed with a question mark (`?`) to make the parameter optional.
7677

7778
```js
78-
var re = pathToRegexp('/:foo/:bar?', keys)
79+
var re = pathToRegexp('/:foo/:bar?')
7980
// keys = [{ name: 'foo', ... }, { name: 'bar', delimiter: '/', optional: true, repeat: false }]
8081

8182
re.exec('/test')
@@ -85,12 +86,14 @@ re.exec('/test/route')
8586
//=> ['/test', 'test', 'route']
8687
```
8788

89+
**Tip:** If the parameter is the _only_ value in the segment, the prefix is also optional.
90+
8891
##### Zero or more
8992

9093
Parameters can be suffixed with an asterisk (`*`) to denote a zero or more parameter matches. The prefix is taken into account for each match.
9194

9295
```js
93-
var re = pathToRegexp('/:foo*', keys)
96+
var re = pathToRegexp('/:foo*')
9497
// keys = [{ name: 'foo', delimiter: '/', optional: true, repeat: true }]
9598

9699
re.exec('/')
@@ -105,7 +108,7 @@ re.exec('/bar/baz')
105108
Parameters can be suffixed with a plus sign (`+`) to denote a one or more parameter matches. The prefix is taken into account for each match.
106109

107110
```js
108-
var re = pathToRegexp('/:foo+', keys)
111+
var re = pathToRegexp('/:foo+')
109112
// keys = [{ name: 'foo', delimiter: '/', optional: false, repeat: true }]
110113

111114
re.exec('/')
@@ -120,7 +123,7 @@ re.exec('/bar/baz')
120123
All parameters can be provided a custom regexp, which overrides the default (`[^\/]+`).
121124

122125
```js
123-
var re = pathToRegexp('/:foo(\\d+)', keys)
126+
var re = pathToRegexp('/:foo(\\d+)')
124127
// keys = [{ name: 'foo', ... }]
125128

126129
re.exec('/123')
@@ -137,7 +140,7 @@ re.exec('/abc')
137140
It is possible to write an unnamed parameter that only consists of a matching group. It works the same as a named parameter, except it will be numerically indexed.
138141

139142
```js
140-
var re = pathToRegexp('/:foo/(.*)', keys)
143+
var re = pathToRegexp('/:foo/(.*)')
141144
// keys = [{ name: 'foo', ... }, { name: 0, ... }]
142145

143146
re.exec('/test/route')
@@ -149,7 +152,7 @@ re.exec('/test/route')
149152
An asterisk can be used for matching everything. It is equivalent to an unnamed matching group of `(.*)`.
150153

151154
```js
152-
var re = pathToRegexp('/foo/*', keys)
155+
var re = pathToRegexp('/foo/*')
153156
// keys = [{ name: '0', ... }]
154157

155158
re.exec('/foo/bar/baz')
@@ -173,11 +176,11 @@ console.log(tokens[2])
173176
//=> { name: 0, prefix: '/', delimiter: '/', optional: false, repeat: false, pattern: '.*' }
174177
```
175178

176-
**Note:** This method only works with Express-style strings.
179+
**Note:** This method only works with strings.
177180

178181
### Compile ("Reverse" Path-To-RegExp)
179182

180-
Path-To-RegExp exposes a compile function for transforming an Express-style path into a valid path.
183+
Path-To-RegExp exposes a compile function for transforming a string into a valid path.
181184

182185
```js
183186
var toPath = pathToRegexp.compile('/user/:id')
@@ -225,11 +228,10 @@ Path-To-RegExp exposes the two functions used internally that accept an array of
225228

226229
Path-To-RegExp breaks compatibility with Express <= `4.x`:
227230

228-
* No longer a direct conversion to a RegExp with sugar on top - it's a path matcher with named and unnamed matching groups
229-
* It's unlikely you previously abused this feature, it's rare and you could always use a RegExp instead
230-
* All matching RegExp special characters can be used in a matching group. E.g. `/:user(.*)`
231-
* Other RegExp features are not support - no nested matching groups, non-capturing groups or look aheads
231+
* RegExp special characters can only be used in a parameter
232+
* Express.js 4.x used all `RegExp` special characters regardless of position - this considered a bug
232233
* Parameters have suffixes that augment meaning - `*`, `+` and `?`. E.g. `/:user*`
234+
* No wildcard asterisk (`*`) - use parameters instead (`(.*)`)
233235

234236
## TypeScript
235237

index.d.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
declare function pathToRegexp (path: pathToRegexp.Path, options?: pathToRegexp.RegExpOptions & pathToRegexp.ParseOptions): pathToRegexp.PathRegExp;
2-
declare function pathToRegexp (path: pathToRegexp.Path, keys?: pathToRegexp.Key[], options?: pathToRegexp.RegExpOptions & pathToRegexp.ParseOptions): pathToRegexp.PathRegExp;
1+
declare function pathToRegexp (path: pathToRegexp.Path, keys?: pathToRegexp.Key[], options?: pathToRegexp.RegExpOptions & pathToRegexp.ParseOptions): RegExp;
32

43
declare namespace pathToRegexp {
5-
export interface PathRegExp extends RegExp {
6-
// An array to be populated with the keys found in the path.
7-
keys: Key[];
8-
}
9-
104
export interface RegExpOptions {
115
/**
126
* When `true` the route will be case sensitive. (default: `false`)
@@ -24,13 +18,21 @@ declare namespace pathToRegexp {
2418
* Sets the final character for non-ending optimistic matches. (default: `/`)
2519
*/
2620
delimiter?: string;
21+
/**
22+
* List of characters that can also be "end" characters.
23+
*/
24+
endsWith?: string | string[];
2725
}
2826

2927
export interface ParseOptions {
3028
/**
3129
* Set the default delimiter for repeat parameters. (default: `'/'`)
3230
*/
3331
delimiter?: string;
32+
/**
33+
* Encode a string for matching.
34+
*/
35+
encode?: (value: string) => string;
3436
}
3537

3638
/**
@@ -51,8 +53,7 @@ declare namespace pathToRegexp {
5153
/**
5254
* Transform an array of tokens into a matching regular expression.
5355
*/
54-
export function tokensToRegExp (tokens: Token[], options?: RegExpOptions): PathRegExp;
55-
export function tokensToRegExp (tokens: Token[], keys?: Key[], options?: RegExpOptions): PathRegExp;
56+
export function tokensToRegExp (tokens: Token[], keys?: Key[], options?: RegExpOptions): RegExp;
5657

5758
export interface Key {
5859
name: string | number;
@@ -62,11 +63,13 @@ declare namespace pathToRegexp {
6263
repeat: boolean;
6364
pattern: string;
6465
partial: boolean;
65-
asterisk: boolean;
6666
}
6767

6868
interface PathFunctionOptions {
69-
pretty?: boolean;
69+
/**
70+
* Function for encoding input strings for output.
71+
*/
72+
encode?: (value: string) => string;
7073
}
7174

7275
export type Token = string | Key;

0 commit comments

Comments
 (0)