UNPKG

1.34 kBTypeScriptView Raw
1export type Options = {
2 /**
3 Count [ambiguous width characters](https://www.unicode.org/reports/tr11/#Ambiguous) as having narrow width (count of 1) instead of wide width (count of 2).
4
5 @default true
6
7 > Ambiguous characters behave like wide or narrow characters depending on the context (language tag, script identification, associated font, source of data, or explicit markup; all can provide the context). __If the context cannot be established reliably, they should be treated as narrow characters by default.__
8 > - http://www.unicode.org/reports/tr11/
9 */
10 readonly ambiguousIsNarrow?: boolean;
11
12 /**
13 Whether [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) should be counted.
14
15 @default false
16 */
17 readonly countAnsiEscapeCodes?: boolean;
18};
19
20/**
21Get the visual width of a string - the number of columns required to display it.
22
23Some Unicode characters are [fullwidth](https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms) and use double the normal width. [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) are stripped and doesn't affect the width.
24
25@example
26```
27import stringWidth from 'string-width';
28
29stringWidth('a');
30//=> 1
31
32stringWidth('古');
33//=> 2
34
35stringWidth('\u001B[1m古\u001B[22m');
36//=> 2
37```
38*/
39export default function stringWidth(string: string, options?: Options): number;