Skip to content

Commit bafa535

Browse files
Leon Sisindresorhus
andauthored
Add TypeScript definition (#50)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 99ada39 commit bafa535

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

index.d.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export type Options = {
2+
/**
3+
By default the wrap is soft, meaning long words may extend past the column width. Setting this to `true` will make it hard wrap at the column width.
4+
5+
@default false
6+
*/
7+
readonly hard?: boolean;
8+
9+
/**
10+
By default, an attempt is made to split words at spaces, ensuring that they don't extend past the configured columns. If wordWrap is `false`, each column will instead be completely filled splitting words as necessary.
11+
12+
@default true
13+
*/
14+
readonly wordWrap?: boolean;
15+
16+
/**
17+
Whitespace on all lines is removed by default. Set this option to `false` if you don't want to trim.
18+
19+
@default true
20+
*/
21+
readonly trim?: boolean;
22+
};
23+
24+
/**
25+
Wrap words to the specified column width.
26+
27+
@param string - String with ANSI escape codes. Like one styled by [`chalk`](https://github.com/chalk/chalk). Newline characters will be normalized to `\n`.
28+
@param columns - Number of columns to wrap the text to.
29+
30+
@example
31+
```
32+
import chalk from 'chalk';
33+
import wrapAnsi from 'wrap-ansi';
34+
35+
const input = 'The quick brown ' + chalk.red('fox jumped over ') +
36+
'the lazy ' + chalk.green('dog and then ran away with the unicorn.');
37+
38+
console.log(wrapAnsi(input, 20));
39+
```
40+
*/
41+
export default function wrapAnsi(string: string, columns: number, options?: Options): string;

index.test-d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {expectType} from 'tsd';
2+
import wrapAnsi from './index.js';
3+
4+
expectType<string>(wrapAnsi('input', 80));
5+
expectType<string>(wrapAnsi('input', 80, {}));
6+
expectType<string>(wrapAnsi('input', 80, {hard: true}));
7+
expectType<string>(wrapAnsi('input', 80, {hard: false}));
8+
expectType<string>(wrapAnsi('input', 80, {trim: true}));
9+
expectType<string>(wrapAnsi('input', 80, {trim: false}));
10+
expectType<string>(wrapAnsi('input', 80, {wordWrap: true}));
11+
expectType<string>(wrapAnsi('input', 80, {wordWrap: false}));

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@
1111
"url": "https://sindresorhus.com"
1212
},
1313
"type": "module",
14-
"exports": "./index.js",
14+
"exports": {
15+
"types": "./index.d.ts",
16+
"default": "./index.js"
17+
},
1518
"engines": {
1619
"node": ">=12"
1720
},
1821
"scripts": {
19-
"test": "xo && nyc ava"
22+
"test": "xo && nyc ava && tsd"
2023
},
2124
"files": [
22-
"index.js"
25+
"index.js",
26+
"index.d.ts"
2327
],
2428
"keywords": [
2529
"wrap",
@@ -59,6 +63,7 @@
5963
"coveralls": "^3.1.1",
6064
"has-ansi": "^5.0.1",
6165
"nyc": "^15.1.0",
66+
"tsd": "^0.25.0",
6267
"xo": "^0.44.0"
6368
}
6469
}

0 commit comments

Comments
 (0)