string-width
Advanced tools
Comparing version
34
index.js
@@ -7,2 +7,4 @@ import stripAnsi from 'strip-ansi'; | ||
const defaultIgnorableCodePointRegex = /^\p{Default_Ignorable_Code_Point}$/u; | ||
export default function stringWidth(string, options = {}) { | ||
@@ -37,7 +39,37 @@ if (typeof string !== 'string' || string.length === 0) { | ||
// Ignore zero-width characters | ||
if ( | ||
(codePoint >= 0x20_0B && codePoint <= 0x20_0F) // Zero-width space, non-joiner, joiner, left-to-right mark, right-to-left mark | ||
|| codePoint === 0xFE_FF // Zero-width no-break space | ||
) { | ||
continue; | ||
} | ||
// Ignore combining characters | ||
if (codePoint >= 0x3_00 && codePoint <= 0x3_6F) { | ||
if ( | ||
(codePoint >= 0x3_00 && codePoint <= 0x3_6F) // Combining diacritical marks | ||
|| (codePoint >= 0x1A_B0 && codePoint <= 0x1A_FF) // Combining diacritical marks extended | ||
|| (codePoint >= 0x1D_C0 && codePoint <= 0x1D_FF) // Combining diacritical marks supplement | ||
|| (codePoint >= 0x20_D0 && codePoint <= 0x20_FF) // Combining diacritical marks for symbols | ||
|| (codePoint >= 0xFE_20 && codePoint <= 0xFE_2F) // Combining half marks | ||
) { | ||
continue; | ||
} | ||
// Ignore surrogate pairs | ||
if (codePoint >= 0xD8_00 && codePoint <= 0xDF_FF) { | ||
continue; | ||
} | ||
// Ignore variation selectors | ||
if (codePoint >= 0xFE_00 && codePoint <= 0xFE_0F) { | ||
continue; | ||
} | ||
// This covers some of the above cases, but we still keep them for performance reasons. | ||
if (defaultIgnorableCodePointRegex.test(character)) { | ||
continue; | ||
} | ||
// TODO: Use `/\p{RGI_Emoji}/v` when targeting Node.js 20. | ||
if (emojiRegex().test(character)) { | ||
@@ -44,0 +76,0 @@ width += 2; |
{ | ||
"name": "string-width", | ||
"version": "7.1.0", | ||
"version": "7.2.0", | ||
"description": "Get the visual width of a string - the number of columns required to display it", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
7773
17.56%95
39.71%