Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/susisu/meaw
Utility for Unicode East Asian Width :cat2:
https://github.com/susisu/meaw
eastasianwidth javascript javascript-library unicode
Last synced: 2 months ago
JSON representation
Utility for Unicode East Asian Width :cat2:
- Host: GitHub
- URL: https://github.com/susisu/meaw
- Owner: susisu
- License: other
- Created: 2017-04-14T03:26:05.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-09-22T01:43:03.000Z (3 months ago)
- Last Synced: 2024-10-11T21:19:36.848Z (3 months ago)
- Topics: eastasianwidth, javascript, javascript-library, unicode
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/meaw
- Size: 1020 KB
- Stars: 17
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# meaw
[![CI](https://github.com/susisu/meaw/workflows/CI/badge.svg)](https://github.com/susisu/meaw/actions?query=workflow%3ACI)
Utilities for [Unicode East Asian Width (EAW)](http://www.unicode.org/reports/tr11/).
## Installation
``` shell
# npm
npm i --save meaw
# yarn
yarn add meaw
# pnpm
pnpm add meaw
```## Usage
### `getEAW()`
Gets the [EAW property](http://www.unicode.org/reports/tr11/) of a character.
``` javascript
import { getEAW } from "meaw";// Narrow
assert(getEAW("A") === "Na");
// Wide
assert(getEAW("ć") === "W");
assert(getEAW("å®") === "W");
assert(getEAW("š£") === "W");
// Fullwidth
assert(getEAW("ļ¼”") === "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ćļ¼”ļ½±ā", 2) === "W");
```### `computeWidth()`
**Deprecated.** To calculate the visual width of a string, it is recommended to split the string into graphemes (using [`Intl.Segmenter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter) or libraries like [graphemer](https://github.com/flmnt/graphemer)) and then calculate the widths of them.
Computes an approximate width of a string based on the [EAW properties](http://www.unicode.org/reports/tr11/) of the characters.
By default, characters with property Wide (W) or Fullwidth (F) are treated as wide (= 2) and others are as narrow (= 1).``` javascript
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
``` shell
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 |
| `format` | run Prettier |
| `lint` | run ESLint |
| `typecheck` | run `tsc --noEmit` |
| `test` | execute tests |
| `build` | build scripts |## License
[MIT License](http://opensource.org/licenses/mit-license.php)
## Author
Susisu ([GitHub](https://github.com/susisu), [Twitter](https://twitter.com/susisu2413))