From 49c2085e0302b9f0922e6fad6c273d27541edb38 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 5 May 2023 18:27:35 +0800 Subject: [PATCH 1/4] Add `countAnsiEscapeCodes` option --- index.d.ts | 7 +++++++ index.js | 5 ++++- index.test-d.ts | 4 ++-- readme.md | 9 ++++++++- test.js | 1 + 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index aed9fdf..bc1e295 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,6 +5,13 @@ export interface Options { @default true */ readonly ambiguousIsNarrow: boolean; + + /** + Whether [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) should be counted. They are ignored by default. + + @default false + */ + readonly countAnsiEscapeCodes: boolean; } /** diff --git a/index.js b/index.js index 9294488..0ff9fb6 100644 --- a/index.js +++ b/index.js @@ -9,10 +9,13 @@ export default function stringWidth(string, options = {}) { options = { ambiguousIsNarrow: true, + countAnsiEscapeCodes: false, ...options }; - string = stripAnsi(string); + if (!options.countAnsiEscapeCodes) { + string = stripAnsi(string); + } if (string.length === 0) { return 0; diff --git a/index.test-d.ts b/index.test-d.ts index a713789..d905cdd 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -2,5 +2,5 @@ import {expectType} from 'tsd'; import stringWidth from './index.js'; expectType(stringWidth('古')); - -expectType(stringWidth('★', {ambiguousIsNarrow: true})); +expectType(stringWidth('★', {ambiguousIsNarrow: false})); +expectType(stringWidth('\u001B[31m\u001B[39m', {countAnsiEscapeCodes: true})); diff --git a/readme.md b/readme.md index 52910df..e1093a5 100644 --- a/readme.md +++ b/readme.md @@ -44,10 +44,17 @@ Type: `object` ##### ambiguousIsNarrow Type: `boolean`\ -Default: `false` +Default: `true` 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). +##### countAnsiEscapeCodes + +Type: `boolean`\ +Default: `false` + +Whether [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) should be counted. They are ignored by default. + ## Related - [string-width-cli](https://github.com/sindresorhus/string-width-cli) - CLI for this module diff --git a/test.js b/test.js index df3f1c4..32d0882 100644 --- a/test.js +++ b/test.js @@ -13,6 +13,7 @@ test('main', t => { t.is(stringWidth('안녕하세요'), 10); t.is(stringWidth('A\uD83C\uDE00BC'), 5, 'surrogate'); t.is(stringWidth('\u001B[31m\u001B[39m'), 0); + t.is(stringWidth('\u001B[31m\u001B[39m', {countAnsiEscapeCodes: true}), 8); t.is(stringWidth('\u001B]8;;https://github.com\u0007Click\u001B]8;;\u0007'), 5); t.is(stringWidth('\u{231A}'), 2, '⌚ default emoji presentation character (Emoji_Presentation)'); t.is(stringWidth('\u{2194}\u{FE0F}'), 2, '↔️ default text presentation character rendered as emoji'); From 92ed7e8bf3bf1c8e0ccf1143876b65b63052e021 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Fri, 5 May 2023 21:11:39 +0700 Subject: [PATCH 2/4] Update index.d.ts --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index bc1e295..8c6ca2a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -7,7 +7,7 @@ export interface Options { readonly ambiguousIsNarrow: boolean; /** - Whether [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) should be counted. They are ignored by default. + Whether [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) should be counted. @default false */ From 59dc863f1d66d42143193dc1a6620c48379fcbf4 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Fri, 5 May 2023 21:11:51 +0700 Subject: [PATCH 3/4] Update readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index e1093a5..107e6b8 100644 --- a/readme.md +++ b/readme.md @@ -53,7 +53,7 @@ Count [ambiguous width characters](https://www.unicode.org/reports/tr11/#Ambiguo Type: `boolean`\ Default: `false` -Whether [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) should be counted. They are ignored by default. +Whether [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) should be counted. ## Related From fc28770d8146679011f93b44254e137e1fa3aa91 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sat, 6 May 2023 16:35:03 +0800 Subject: [PATCH 4/4] Mark property optional --- index.d.ts | 4 ++-- index.test-d.ts | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index e8f37d1..71ecc24 100644 --- a/index.d.ts +++ b/index.d.ts @@ -4,14 +4,14 @@ export type Options = { @default true */ - readonly ambiguousIsNarrow: boolean; + readonly ambiguousIsNarrow?: boolean; /** Whether [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) should be counted. @default false */ - readonly countAnsiEscapeCodes: boolean; + readonly countAnsiEscapeCodes?: boolean; }; /** diff --git a/index.test-d.ts b/index.test-d.ts index d905cdd..e02ebfe 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -2,5 +2,6 @@ import {expectType} from 'tsd'; import stringWidth from './index.js'; expectType(stringWidth('古')); +expectType(stringWidth('★', {})); expectType(stringWidth('★', {ambiguousIsNarrow: false})); expectType(stringWidth('\u001B[31m\u001B[39m', {countAnsiEscapeCodes: true}));