A library for calculating the display width of Unicode strings.
npm install unicode-width-approximationA library for calculating the display width of Unicode strings in terminal/monospace environments.
``bash`
npm install unicode-width-approximation
`javascript
import {
getStringWidth,
getCodepointWidth,
isWideChar,
isZeroWidth
} from "unicode-width-approximation";
// Get width of strings
console.log(getStringWidth("hello")); // 5
console.log(getStringWidth("δΈζ")); // 4
console.log(getStringWidth("π¨βπ©βπ§βπ¦")); // 2
// Get width of single code points
console.log(getCodepointWidth(0x41)); // 1 (ASCII 'A')
console.log(getCodepointWidth(0x4E00)); // 2 (CJK)
console.log(getCodepointWidth(0x1F600)); // 2 (emoji)
`
TypeScript type definitions are included.
`typescript
import { getStringWidth, getCodepointWidth } from "unicode-width-approximation";
const width: number = getStringWidth("helloδΈη");
console.log(width); // 9
`
Calculate the total display width of a string.
Get the display width of a single Unicode code point (0, 1, or 2).
Check if a code point is a wide character (East Asian Wide or Fullwidth).
Check if a code point is a zero-width character.
Requires Emscripten to be installed.
`bash`
npm run build
`bash``
npm test
MIT License