What is get-east-asian-width?
The get-east-asian-width npm package is used to determine the East Asian Width properties of Unicode characters. This is useful for applications that need to handle text rendering, alignment, or width calculations in East Asian languages.
What are get-east-asian-width's main functionalities?
Get East Asian Width of a character
This feature allows you to get the East Asian Width property of a single character. In this example, the character 'あ' has a width property of 'W' (Wide).
const getEastAsianWidth = require('get-east-asian-width');
const width = getEastAsianWidth('あ');
console.log(width); // Output: 'W'
Get East Asian Width of a string
This feature allows you to get the East Asian Width properties of each character in a string. In this example, each character in the string 'こんにちは' has a width property of 'W' (Wide).
const getEastAsianWidth = require('get-east-asian-width');
const widths = 'こんにちは'.split('').map(getEastAsianWidth);
console.log(widths); // Output: ['W', 'W', 'W', 'W', 'W']
Check if a character is Fullwidth or Halfwidth
This feature allows you to check if a character is Fullwidth or Halfwidth. In this example, the character 'A' is Fullwidth, while 'A' is not.
const getEastAsianWidth = require('get-east-asian-width');
const isFullwidth = (char) => getEastAsianWidth(char) === 'F';
console.log(isFullwidth('A')); // Output: true
console.log(isFullwidth('A')); // Output: false
Other packages similar to get-east-asian-width
east-asian-width
The east-asian-width package provides similar functionality to get-east-asian-width by determining the East Asian Width properties of Unicode characters. It offers a comprehensive set of utilities for handling East Asian text rendering and alignment.
unicode-width
The unicode-width package calculates the display width of Unicode strings, taking into account East Asian Width properties. It is useful for applications that need to handle text alignment and rendering in a variety of languages, including East Asian languages.
string-width
The string-width package determines the display width of a string, considering East Asian Width properties and other factors. It is commonly used in CLI applications to ensure proper text alignment and formatting.
get-east-asian-width
Determine the East Asian Width of a Unicode character
East Asian Width categorizes Unicode characters based on their occupied space in East Asian typography, which helps in text layout and alignment, particularly in languages like Japanese, Chinese, and Korean.
Unlike other similar packages, this package uses the latest Unicode data (which changes each year).
Install
npm install get-east-asian-width
Usage
import {eastAsianWidth, eastAsianWidthType} from 'get-east-asian-width';
const codePoint = '字'.codePointAt(0);
console.log(eastAsianWidth(codePoint));
console.log(eastAsianWidthType(codePoint));
eastAsianWidth(codePoint: number, options?: object): 1 | 2
Returns the width as a number for the given code point.
options
Type: object
ambiguousAsWide
Type: boolean
Default: false
Whether to treat an 'ambiguous'
character as wide.
import {eastAsianWidth} from 'get-east-asian-width';
const codePoint = '⛣'.codePointAt(0);
console.log(eastAsianWidth(codePoint));
console.log(eastAsianWidth(codePoint, {ambiguousAsWide: true}));
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.
eastAsianWidthType(codePoint: number): 'fullwidth' | 'halfwidth' | 'wide' | 'narrow' | 'neutral' | 'ambiguous'
Returns the type of “East Asian Width” for the given code point.
Related