Skip to content

Commit e06109e

Browse files
committed
Add TypeScript .d.ts file for type checking (#72)
Tweaked the test suite into TypeScript for continuous integration and type checking of the public interface
1 parent 876222c commit e06109e

File tree

6 files changed

+110
-16
lines changed

6 files changed

+110
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
coverage
33
components
4+
typings

Readme.md

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

219+
## TypeScript
220+
221+
Includes a [`.d.ts`](index.d.ts) file for TypeScript users.
222+
219223
## Live Demo
220224

221225
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: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
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"
810
],
911
"scripts": {
1012
"lint": "standard",
11-
"test-spec": "mocha -R spec --bail",
12-
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- -R spec",
13+
"test-spec": "mocha --require ts-node/register -R spec --bail test.ts",
14+
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --require ts-node/register -R spec test.ts",
15+
"prepublish": "typings install",
1316
"test": "npm run lint && npm run test-cov"
1417
},
1518
"keywords": [
@@ -33,7 +36,10 @@
3336
"istanbul": "~0.3.0",
3437
"mocha": "~2.2.4",
3538
"pre-commit": "~1.0.5",
36-
"standard": "~3.7.3"
39+
"standard": "~3.7.3",
40+
"ts-node": "^0.5.5",
41+
"typescript": "^1.8.7",
42+
"typings": "^0.6.9"
3743
},
3844
"dependencies": {
3945
"isarray": "0.0.1"

test.js renamed to test.ts

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
/* global describe, it */
22

3-
var util = require('util')
4-
var expect = require('chai').expect
5-
var pathToRegexp = require('./')
3+
/// <reference path="typings/main.d.ts" />
4+
5+
import util = require('util')
6+
import chai = require('chai')
7+
import pathToRegexp = require('./index')
8+
9+
const expect = chai.expect
10+
11+
type Test = [
12+
pathToRegexp.Path,
13+
pathToRegexp.Options,
14+
pathToRegexp.Token[],
15+
Array<[string, string[]]>,
16+
Array<[any, string]>
17+
]
618

719
/**
820
* An array of test cases with expected inputs and outputs.
921
*
1022
* @type {Array}
1123
*/
12-
var TESTS = [
24+
var TESTS: Test[] = [
1325
/**
1426
* Simple paths.
1527
*/
@@ -342,7 +354,8 @@ var TESTS = [
342354
['/two', ['/two']],
343355
['/three', null],
344356
['/one/two', null]
345-
]
357+
],
358+
[]
346359
],
347360

348361
/**
@@ -1373,7 +1386,8 @@ var TESTS = [
13731386
[],
13741387
[
13751388
['/match/anything', ['/match/anything']]
1376-
]
1389+
],
1390+
[]
13771391
],
13781392
[
13791393
/(.*)/,
@@ -1390,7 +1404,8 @@ var TESTS = [
13901404
],
13911405
[
13921406
['/match/anything', ['/match/anything', '/match/anything']]
1393-
]
1407+
],
1408+
[]
13941409
],
13951410
[
13961411
/\/(\d+)/,
@@ -1408,7 +1423,8 @@ var TESTS = [
14081423
[
14091424
['/abc', null],
14101425
['/123', ['/123', '123']]
1411-
]
1426+
],
1427+
[]
14121428
],
14131429

14141430
/**
@@ -1429,7 +1445,8 @@ var TESTS = [
14291445
],
14301446
[
14311447
['/test', ['/test', undefined]]
1432-
]
1448+
],
1449+
[]
14331450
],
14341451
[
14351452
['/:test(\\d+)', /(.*)/],
@@ -1455,7 +1472,8 @@ var TESTS = [
14551472
[
14561473
['/123', ['/123', '123', undefined]],
14571474
['/abc', ['/abc', undefined, '/abc']]
1458-
]
1475+
],
1476+
[]
14591477
],
14601478

14611479
/**
@@ -1485,7 +1503,8 @@ var TESTS = [
14851503
[
14861504
['/test', ['/test', 'test', undefined]],
14871505
['/route/test', ['/route/test', undefined, 'test']]
1488-
]
1506+
],
1507+
[]
14891508
],
14901509
[
14911510
[/^\/([^\/]+)$/, /^\/route\/([^\/]+)$/],
@@ -1511,7 +1530,8 @@ var TESTS = [
15111530
[
15121531
['/test', ['/test', 'test', undefined]],
15131532
['/route/test', ['/route/test', undefined, 'test']]
1514-
]
1533+
],
1534+
[]
15151535
],
15161536

15171537
/**
@@ -1523,7 +1543,8 @@ var TESTS = [
15231543
[],
15241544
[
15251545
['/anything/you/want', ['/anything/you/want']]
1526-
]
1546+
],
1547+
[]
15271548
],
15281549

15291550
/**

typings.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"ambientDependencies": {
3+
"mocha": "github:DefinitelyTyped/DefinitelyTyped/mocha/mocha.d.ts#d6dd320291705694ba8e1a79497a908e9f5e6617",
4+
"node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#48c1e3c1d6baefa4f1a126f188c27c4fefd36bff"
5+
},
6+
"devDependencies": {
7+
"chai": "github:typed-typings/npm-chai#01a92b8efc0cfa0ac894d695abd06b8b11e3b75b"
8+
}
9+
}

0 commit comments

Comments
 (0)