Skip to content

Commit

Permalink
Add TypeScript .d.ts file for type checking
Browse files Browse the repository at this point in the history
Tweaked the test suite into TypeScript for continuous integration and type checking of the public interface
  • Loading branch information
blakeembrey committed Mar 2, 2016
1 parent 876222c commit 78f0a5d
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
coverage
components
typings
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,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;
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
"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"
],
"scripts": {
"lint": "standard",
"test-spec": "mocha -R spec --bail",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- -R spec",
"test-spec": "mocha --require ts-node/register -R spec --bail test.ts",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --require ts-node/register -R spec test.ts",
"prepublish": "typings install",
"test": "npm run lint && npm run test-cov"
},
"keywords": [
Expand All @@ -33,7 +36,10 @@
"istanbul": "~0.3.0",
"mocha": "~2.2.4",
"pre-commit": "~1.0.5",
"standard": "~3.7.3"
"standard": "~3.7.3",
"ts-node": "^0.5.5",
"typescript": "^1.8.7",
"typings": "^0.6.9"
},
"dependencies": {
"isarray": "0.0.1"
Expand Down
47 changes: 34 additions & 13 deletions test.js → test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
/* global describe, it */

var util = require('util')
var expect = require('chai').expect
var pathToRegexp = require('./')
/// <reference path="typings/main.d.ts" />

import util = require('util')
import chai = require('chai')
import pathToRegexp = require('./index')

const expect = chai.expect

type Test = [
pathToRegexp.Path,
pathToRegexp.Options,
pathToRegexp.Token[],
Array<[string, string[]]>,
Array<[any, string]>
]

/**
* An array of test cases with expected inputs and outputs.
*
* @type {Array}
*/
var TESTS = [
var TESTS: Test[] = [
/**
* Simple paths.
*/
Expand Down Expand Up @@ -342,7 +354,8 @@ var TESTS = [
['/two', ['/two']],
['/three', null],
['/one/two', null]
]
],
[]
],

/**
Expand Down Expand Up @@ -1373,7 +1386,8 @@ var TESTS = [
[],
[
['/match/anything', ['/match/anything']]
]
],
[]
],
[
/(.*)/,
Expand All @@ -1390,7 +1404,8 @@ var TESTS = [
],
[
['/match/anything', ['/match/anything', '/match/anything']]
]
],
[]
],
[
/\/(\d+)/,
Expand All @@ -1408,7 +1423,8 @@ var TESTS = [
[
['/abc', null],
['/123', ['/123', '123']]
]
],
[]
],

/**
Expand All @@ -1429,7 +1445,8 @@ var TESTS = [
],
[
['/test', ['/test', undefined]]
]
],
[]
],
[
['/:test(\\d+)', /(.*)/],
Expand All @@ -1455,7 +1472,8 @@ var TESTS = [
[
['/123', ['/123', '123', undefined]],
['/abc', ['/abc', undefined, '/abc']]
]
],
[]
],

/**
Expand Down Expand Up @@ -1485,7 +1503,8 @@ var TESTS = [
[
['/test', ['/test', 'test', undefined]],
['/route/test', ['/route/test', undefined, 'test']]
]
],
[]
],
[
[/^\/([^\/]+)$/, /^\/route\/([^\/]+)$/],
Expand All @@ -1511,7 +1530,8 @@ var TESTS = [
[
['/test', ['/test', 'test', undefined]],
['/route/test', ['/route/test', undefined, 'test']]
]
],
[]
],

/**
Expand All @@ -1523,7 +1543,8 @@ var TESTS = [
[],
[
['/anything/you/want', ['/anything/you/want']]
]
],
[]
],

/**
Expand Down
9 changes: 9 additions & 0 deletions typings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"ambientDependencies": {
"mocha": "github:DefinitelyTyped/DefinitelyTyped/mocha/mocha.d.ts#d6dd320291705694ba8e1a79497a908e9f5e6617",
"node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#48c1e3c1d6baefa4f1a126f188c27c4fefd36bff"
},
"devDependencies": {
"chai": "github:typed-typings/npm-chai#01a92b8efc0cfa0ac894d695abd06b8b11e3b75b"
}
}

0 comments on commit 78f0a5d

Please sign in to comment.