Skip to content

Commit e77ea17

Browse files
Akim95sindresorhus
andcommittedOct 4, 2019
Add TypeScript definition (#32)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 166a0d5 commit e77ea17

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed
 

‎index.d.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
declare namespace ansiRegex {
2+
interface Options {
3+
/**
4+
Match only the first ANSI escape.
5+
6+
@default false
7+
*/
8+
onlyFirst: boolean;
9+
}
10+
}
11+
12+
/**
13+
Regular expression for matching ANSI escape codes.
14+
15+
@example
16+
```
17+
import ansiRegex = require('ansi-regex');
18+
19+
ansiRegex().test('\u001B[4mcake\u001B[0m');
20+
//=> true
21+
22+
ansiRegex().test('cake');
23+
//=> false
24+
25+
'\u001B[4mcake\u001B[0m'.match(ansiRegex());
26+
//=> ['\u001B[4m', '\u001B[0m']
27+
28+
'\u001B[4mcake\u001B[0m'.match(ansiRegex({onlyFirst: true}));
29+
//=> ['\u001B[4m']
30+
31+
'\u001B]8;;https://github.com\u0007click\u001B]8;;\u0007'.match(ansiRegex());
32+
//=> ['\u001B]8;;https://github.com\u0007', '\u001B]8;;\u0007']
33+
```
34+
*/
35+
declare function ansiRegex(options?: ansiRegex.Options): RegExp;
36+
37+
export = ansiRegex;

‎index.test-d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {expectType} from 'tsd';
2+
import ansiRegex from '.';
3+
4+
expectType<RegExp>(ansiRegex());
5+
expectType<RegExp>(ansiRegex({onlyFirst: true}));

‎package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
"node": ">=8"
1414
},
1515
"scripts": {
16-
"test": "xo && ava",
16+
"test": "xo && ava && tsd",
1717
"view-supported": "node fixtures/view-codes.js"
1818
},
1919
"files": [
20-
"index.js"
20+
"index.js",
21+
"index.d.ts"
2122
],
2223
"keywords": [
2324
"ansi",
@@ -48,6 +49,7 @@
4849
],
4950
"devDependencies": {
5051
"ava": "^1.4.1",
52+
"tsd": "^0.9.0",
5153
"xo": "^0.24.0"
5254
}
5355
}

0 commit comments

Comments
 (0)
Please sign in to comment.