Skip to content

Commit 1c65dfa

Browse files
BendingBendersindresorhus
authored andcommittedApr 11, 2019
Add TypeScript definition (#13)
1 parent 27ed208 commit 1c65dfa

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed
 

‎index.d.ts

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
declare namespace cliTruncate {
2+
interface Options {
3+
/**
4+
Position to truncate the string.
5+
6+
@default 'end'
7+
*/
8+
readonly position?: 'start' | 'middle' | 'end';
9+
}
10+
}
11+
12+
/**
13+
Truncate a string to a specific width in the terminal.
14+
15+
@param input - Text to truncate.
16+
@param columns - Columns to occupy in the terminal.
17+
18+
@example
19+
```
20+
import cliTruncate = require('cli-truncate');
21+
22+
cliTruncate('unicorn', 4);
23+
//=> 'uni…'
24+
25+
// Truncate at different positions
26+
cliTruncate('unicorn', 4, {position: 'start'});
27+
//=> '…orn'
28+
29+
cliTruncate('unicorn', 4, {position: 'middle'});
30+
//=> 'un…n'
31+
32+
cliTruncate('\u001B[31municorn\u001B[39m', 4);
33+
//=> '\u001B[31muni\u001B[39m…'
34+
35+
// Truncate Unicode surrogate pairs
36+
cliTruncate('uni\uD83C\uDE00corn', 5);
37+
//=> 'uni\uD83C\uDE00…'
38+
39+
// Truncate fullwidth characters
40+
cliTruncate('안녕하세요', 3);
41+
//=> '안…'
42+
43+
// Truncate the paragraph to the terminal width
44+
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
45+
cliTruncate(paragraph, process.stdout.columns));
46+
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
47+
```
48+
*/
49+
declare function cliTruncate(
50+
input: string,
51+
columns: number,
52+
options?: cliTruncate.Options
53+
): string;
54+
55+
export = cliTruncate;

‎index.test-d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {expectType} from 'tsd';
2+
import cliTruncate = require('.');
3+
4+
expectType<string>(cliTruncate('unicorn', 4));
5+
expectType<string>(cliTruncate('unicorn', 4, {position: 'start'}));
6+
expectType<string>(cliTruncate('unicorn', 4, {position: 'middle'}));
7+
expectType<string>(cliTruncate('unicorn', 4, {position: 'end'}));

‎package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
"node": ">=8"
1414
},
1515
"scripts": {
16-
"test": "xo && ava"
16+
"test": "xo && ava && tsd"
1717
},
1818
"files": [
19-
"index.js"
19+
"index.js",
20+
"index.d.ts"
2021
],
2122
"keywords": [
2223
"truncate",
@@ -37,6 +38,7 @@
3738
},
3839
"devDependencies": {
3940
"ava": "^1.4.1",
41+
"tsd": "^0.7.2",
4042
"xo": "^0.24.0"
4143
}
4244
}

0 commit comments

Comments
 (0)