string-width
Advanced tools
+12
-8
@@ -8,3 +8,3 @@ import stripAnsi from 'strip-ansi'; | ||
| - Width rules: | ||
| 1. Skip non-printing clusters (Default_Ignorable, Control, pure Mark, lone Surrogates). Tabs are ignored by design. | ||
| 1. Skip non-printing clusters (Default_Ignorable, Control, pure nonspacing/enclosing Mark, lone Surrogates). Tabs are ignored by design. | ||
| 2. RGI emoji clusters (\p{RGI_Emoji}) are double-width. | ||
@@ -14,3 +14,3 @@ 3. Minimally-qualified/unqualified emoji clusters (ZWJ sequences with 2+ Extended_Pictographic, or keycap sequences) are double-width. | ||
| Unmatched repeated leading/vowel/trailing jamo stay additive because that matches how the terminals we target render them. | ||
| 5. Otherwise use East Asian Width of the cluster's first visible code point, and add widths for trailing Halfwidth/Fullwidth Forms within the same cluster (e.g., dakuten/handakuten/prolonged sound mark). | ||
| 5. Otherwise use East Asian Width of the cluster's first visible code point, and add widths for trailing spacing marks and Halfwidth/Fullwidth Forms within the same cluster (e.g., dakuten/handakuten/prolonged sound mark). | ||
| */ | ||
@@ -21,6 +21,7 @@ | ||
| // Whole-cluster zero-width | ||
| const zeroWidthClusterRegex = /^(?:\p{Default_Ignorable_Code_Point}|\p{Control}|\p{Format}|\p{Mark}|\p{Surrogate})+$/v; | ||
| const zeroWidthClusterRegex = /^(?:\p{Default_Ignorable_Code_Point}|\p{Control}|\p{Format}|\p{Nonspacing_Mark}|\p{Enclosing_Mark}|\p{Surrogate})+$/v; | ||
| // Pick the base scalar if the cluster starts with Prepend/Format/Marks | ||
| const leadingNonPrintingRegex = /^[\p{Default_Ignorable_Code_Point}\p{Control}\p{Format}\p{Mark}\p{Surrogate}]+/v; | ||
| const leadingNonPrintingRegex = /^[\p{Default_Ignorable_Code_Point}\p{Control}\p{Format}\p{Nonspacing_Mark}\p{Enclosing_Mark}\p{Surrogate}]+/v; | ||
| const spacingMarkRegex = /\p{Spacing_Mark}/v; | ||
@@ -131,3 +132,3 @@ // RGI emoji sequences | ||
| function trailingHalfwidthWidth(visibleSegment, eastAsianWidthOptions) { | ||
| function trailingWidth(visibleSegment, eastAsianWidthOptions) { | ||
| let extra = 0; | ||
@@ -142,3 +143,6 @@ let first = true; | ||
| if (character >= '\uFF00' && character <= '\uFFEF') { | ||
| if ( | ||
| spacingMarkRegex.test(character) | ||
| || (character >= '\uFF00' && character <= '\uFFEF') | ||
| ) { | ||
| extra += eastAsianWidth(character.codePointAt(0), eastAsianWidthOptions); | ||
@@ -203,4 +207,4 @@ } | ||
| // Add width for trailing Halfwidth and Fullwidth Forms (e.g., ゙, ゚, ー) | ||
| width += trailingHalfwidthWidth(visibleSegment, eastAsianWidthOptions); | ||
| // Add width for trailing spacing marks and Halfwidth/Fullwidth Forms (e.g., ゙, ゚, ー) | ||
| width += trailingWidth(visibleSegment, eastAsianWidthOptions); | ||
| } | ||
@@ -207,0 +211,0 @@ |
+1
-1
| { | ||
| "name": "string-width", | ||
| "version": "8.2.1", | ||
| "version": "8.2.2", | ||
| "description": "Get the visual width of a string - the number of columns required to display it", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
11733
1.63%193
2.12%