Skip to content

susisu/meaw

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
October 12, 2020 19:56
September 13, 2023 00:36
September 23, 2023 01:38
src
September 23, 2023 02:25
September 19, 2023 21:47
September 29, 2023 00:53
September 29, 2023 01:01
September 23, 2023 02:45
September 23, 2023 02:25
September 29, 2023 00:53
September 29, 2023 00:53

meaw

CI

Utilities for Unicode East Asian Width (EAW).

Installation

# npm
npm i --save meaw
# yarn
yarn add meaw
# pnpm
pnpm add meaw

Usage

getEAW()

Gets the EAW property of a character.

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");

computeWidth()

Deprecated. To calculate the visual width of a string, it is more accurate and recommended to split the string into graphemes (using libraries like graphemer) and calculate the width for those graphemes.

Computes an approximate 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).

import { computeWidth } from "meaw";

assert(computeWidth("Aあ🍣Ω") === 6);
// character width for each EAW property can be customized
assert(computeWidth("Aあ🍣Ω", { "A": 2 }) === 7);

Development

Setup

git clone https://github.com/susisu/meaw.git
cd meaw
pnpm i

Scripts

Name Description
fetch fetch the latest version of the EAW definition file
generate generate source script from the EAW definition file
typecheck run typechecker
lint run lint tool
test run tests
build build script
clean remove built script

License

MIT License

Author

Susisu (GitHub, Twitter)