Skip to content

Commit

Permalink
Add TypeScript .d.ts file for type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Mar 2, 2016
1 parent dc9a133 commit caafd9c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ Path-To-RegExp breaks compatibility with Express <= `4.x`:
* Other RegExp features are not support - no nested matching groups, non-capturing groups or look aheads
* Parameters have suffixes that augment meaning - `*`, `+` and `?`. E.g. `/:user*`

## TypeScript

Includes a [`.d.ts`](index.d.ts) file for TypeScript users.

## Live Demo

You can see a live demo of this library in use at [express-route-tester](http://forbeslindesay.github.com/express-route-tester/).
Expand Down
53 changes: 53 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
declare function pathToRegexp (path: pathToRegexp.Path, options?: pathToRegexp.Options): pathToRegexp.PathRegExp;
declare function pathToRegexp (path: pathToRegexp.Path, keys: pathToRegexp.Token[], options?: pathToRegexp.Options): pathToRegexp.PathRegExp;

declare namespace pathToRegexp {
export interface PathRegExp extends RegExp {
// An array to be populated with the keys found in the path.
keys: Key[];
}

export interface Options {
// When `true` the route will be case sensitive. (default: `false`)
sensitive: boolean;
// When `false` the trailing slash is optional. (default: `false`)
strict: boolean;
// When `false` the path will match at the beginning. (default: `true`)
end: boolean;
}

/**
* Parse an Express-style path into an array of tokens.
*/
export function parse (path: string): Token[];

/**
* Transforming an Express-style path into a valid path.
*/
export function compile (path: string): PathFunction;

/**
* Transform an array of tokens into a path generator function.
*/
export function tokensToFunction (tokens: Token[]): PathFunction;

/**
* Transform an array of tokens into a matching regular expression.
*/
export function tokensToRegExp (tokens: Token[], options?: Options): PathRegExp;

export interface Key {
name: string | number;
prefix: string;
delimiter: string;
optional: boolean;
repeat: boolean;
pattern: string;
}

export type Token = string | Key;
export type Path = string | RegExp | Array<string | RegExp>;
export type PathFunction = (data?: Object) => string;
}

export = pathToRegexp;
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"name": "path-to-regexp",
"description": "Express style path to RegExp utility",
"version": "1.2.1",
"main": "index.js",
"typings": "index.d.ts",
"files": [
"index.js",
"LICENSE"
Expand Down

0 comments on commit caafd9c

Please sign in to comment.