Skip to content

Commit

Permalink
Add TypeScript definition (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 11, 2019
1 parent 27ed208 commit 1c65dfa
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
55 changes: 55 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
declare namespace cliTruncate {
interface Options {
/**
Position to truncate the string.
@default 'end'
*/
readonly position?: 'start' | 'middle' | 'end';
}
}

/**
Truncate a string to a specific width in the terminal.
@param input - Text to truncate.
@param columns - Columns to occupy in the terminal.
@example
```
import cliTruncate = require('cli-truncate');
cliTruncate('unicorn', 4);
//=> 'uni…'
// Truncate at different positions
cliTruncate('unicorn', 4, {position: 'start'});
//=> '…orn'
cliTruncate('unicorn', 4, {position: 'middle'});
//=> 'un…n'
cliTruncate('\u001B[31municorn\u001B[39m', 4);
//=> '\u001B[31muni\u001B[39m…'
// Truncate Unicode surrogate pairs
cliTruncate('uni\uD83C\uDE00corn', 5);
//=> 'uni\uD83C\uDE00…'
// Truncate fullwidth characters
cliTruncate('안녕하세요', 3);
//=> '안…'
// Truncate the paragraph to the terminal width
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
cliTruncate(paragraph, process.stdout.columns));
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
```
*/
declare function cliTruncate(
input: string,
columns: number,
options?: cliTruncate.Options
): string;

export = cliTruncate;
7 changes: 7 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {expectType} from 'tsd';
import cliTruncate = require('.');

expectType<string>(cliTruncate('unicorn', 4));
expectType<string>(cliTruncate('unicorn', 4, {position: 'start'}));
expectType<string>(cliTruncate('unicorn', 4, {position: 'middle'}));
expectType<string>(cliTruncate('unicorn', 4, {position: 'end'}));
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"truncate",
Expand All @@ -37,6 +38,7 @@
},
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}

0 comments on commit 1c65dfa

Please sign in to comment.