meaw
Version:
Utilities for Unicode East Asian Width
65 lines (60 loc) • 2.13 kB
text/typescript
type EastAsianWidth = "N" | "Na" | "W" | "F" | "H" | "A";
declare const version: string;
/**
* Gets the EAW property of a code point.
* @param codePoint Code point
* @return The EAW property of the code point
* @example
* import { getEAWOfCodePoint } from "meaw";
*
* // 0x3042 is the code point of 'あ' (U+3042)
* assert(getEAWOfCodePoint(0x3042) === "W");
*/
declare function getEAWOfCodePoint(codePoint: number): EastAsianWidth | undefined;
/**
* Gets the EAW property of a character.
* @param str Character string
* @param pos Character position (in code unit) (default = 0)
* @return The EAW property of the character
* @example
* import { getEAW } from "meaw";
*
* // Narrow
* assert(getEAW("A") === "Na");
* // Wide
* assert(getEAW("あ") === "W");
* assert(getEAW("安") === "W");
* assert(getEAW("🍣") === "W");
* // Fullwidth
* assert(getEAW("A") === "F");
* // Halfwidth
* assert(getEAW("ア") === "H");
* // Ambiguous
* assert(getEAW("∀") === "A");
* assert(getEAW("→") === "A");
* assert(getEAW("Ω") === "A");
* assert(getEAW("Я") === "A");
* // Neutral
* assert(getEAW("ℵ") === "N");
*
* // character position (in code unit) can be specified
* assert(getEAW("ℵAあAア∀", 2) === "W");
*/
declare function getEAW(str: string, pos?: number): EastAsianWidth | undefined;
/**
* Computes the width of a string based on the EAW properties of the characters.
* By default, characters with property Wide (W) or Fullwidth (F) are treated as wide (= 2)
* and others are as narrow (= 1).
* @deprecated
* @param str Character string
* @param widths An object that maps EAW properties to character widths
* @return The computed width
* @example
* import { computeWidth } from "meaw";
*
* assert(computeWidth("Aあ🍣Ω") === 6);
* // character width for each EAW property can be customized
* assert(computeWidth("Aあ🍣Ω", { "A": 2 }) === 7);
*/
declare function computeWidth(str: string, widths?: Readonly<Partial<Record<EastAsianWidth, number>>>): number;
export { type EastAsianWidth, computeWidth, version as eawVersion, getEAW, getEAWOfCodePoint };