Skip to content

Commit caafd9c

Browse files
committed
Add TypeScript .d.ts file for type checking
1 parent dc9a133 commit caafd9c

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

Readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ Path-To-RegExp breaks compatibility with Express <= `4.x`:
204204
* Other RegExp features are not support - no nested matching groups, non-capturing groups or look aheads
205205
* Parameters have suffixes that augment meaning - `*`, `+` and `?`. E.g. `/:user*`
206206

207+
## TypeScript
208+
209+
Includes a [`.d.ts`](index.d.ts) file for TypeScript users.
210+
207211
## Live Demo
208212

209213
You can see a live demo of this library in use at [express-route-tester](http://forbeslindesay.github.com/express-route-tester/).

index.d.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
declare function pathToRegexp (path: pathToRegexp.Path, options?: pathToRegexp.Options): pathToRegexp.PathRegExp;
2+
declare function pathToRegexp (path: pathToRegexp.Path, keys: pathToRegexp.Token[], options?: pathToRegexp.Options): pathToRegexp.PathRegExp;
3+
4+
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+
10+
export interface Options {
11+
// When `true` the route will be case sensitive. (default: `false`)
12+
sensitive: boolean;
13+
// When `false` the trailing slash is optional. (default: `false`)
14+
strict: boolean;
15+
// When `false` the path will match at the beginning. (default: `true`)
16+
end: boolean;
17+
}
18+
19+
/**
20+
* Parse an Express-style path into an array of tokens.
21+
*/
22+
export function parse (path: string): Token[];
23+
24+
/**
25+
* Transforming an Express-style path into a valid path.
26+
*/
27+
export function compile (path: string): PathFunction;
28+
29+
/**
30+
* Transform an array of tokens into a path generator function.
31+
*/
32+
export function tokensToFunction (tokens: Token[]): PathFunction;
33+
34+
/**
35+
* Transform an array of tokens into a matching regular expression.
36+
*/
37+
export function tokensToRegExp (tokens: Token[], options?: Options): PathRegExp;
38+
39+
export interface Key {
40+
name: string | number;
41+
prefix: string;
42+
delimiter: string;
43+
optional: boolean;
44+
repeat: boolean;
45+
pattern: string;
46+
}
47+
48+
export type Token = string | Key;
49+
export type Path = string | RegExp | Array<string | RegExp>;
50+
export type PathFunction = (data?: Object) => string;
51+
}
52+
53+
export = pathToRegexp;

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"name": "path-to-regexp",
33
"description": "Express style path to RegExp utility",
44
"version": "1.2.1",
5+
"main": "index.js",
6+
"typings": "index.d.ts",
57
"files": [
68
"index.js",
79
"LICENSE"

0 commit comments

Comments
 (0)