diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..d7a5fc6 --- /dev/null +++ b/index.d.ts @@ -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; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..17567cc --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,7 @@ +import {expectType} from 'tsd'; +import cliTruncate = require('.'); + +expectType(cliTruncate('unicorn', 4)); +expectType(cliTruncate('unicorn', 4, {position: 'start'})); +expectType(cliTruncate('unicorn', 4, {position: 'middle'})); +expectType(cliTruncate('unicorn', 4, {position: 'end'})); diff --git a/package.json b/package.json index 94d0336..cb5df75 100644 --- a/package.json +++ b/package.json @@ -13,10 +13,11 @@ "node": ">=8" }, "scripts": { - "test": "xo && ava" + "test": "xo && ava && tsd" }, "files": [ - "index.js" + "index.js", + "index.d.ts" ], "keywords": [ "truncate", @@ -37,6 +38,7 @@ }, "devDependencies": { "ava": "^1.4.1", + "tsd": "^0.7.2", "xo": "^0.24.0" } }