Home Reference Source Test

Function

Static Public Summary
public

computeWidth(str: string, widthMap: Object<string, number> | undefined): number

Computes width of a string based on the EAW properties of its characters.

public

getEAW(str: string, at: number): string

Returns the EAW property of a character.

Static Public

public computeWidth(str: string, widthMap: Object<string, number> | undefined): number source

import {computeWidth} from 'meaw'

Computes width of a string based on the EAW properties of its characters. By default characters with property Wide (W) or Fullwidth (F) are treated as wide (= 2) and the others are as narrow (= 1)

Params:

NameTypeAttributeDescription
str string

A string to compute width

widthMap Object<string, number> | undefined
  • optional
  • default: undefined

An object which represents a map from an EAW property to a character width

Return:

number

The computed width

Example:

import { computeWidth } from "meaw";

assert(computeWidth("Aあ🍣Ω") === 6);
// custom widths can be specified by an object
assert(computeWidth("Aあ🍣Ω", { "A": 2 }) === 7);

Test:

public getEAW(str: string, at: number): string source

import {getEAW} from 'meaw'

Returns the EAW property of a character.

Params:

NameTypeAttributeDescription
str string

A string in which the character is contained

at number
  • optional
  • default: 0

The position (in code unit) of the character in the string

Return:

string

The EAW property of the specified 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");

// a position (in code unit) can be specified
assert(getEAW("ℵAあAア∀", 2) === "W");

Test: