char-info
A library that gives you information about individual Unicode characters. It also provides a list of Unicode groupings and their names, including lists of categories, blocks, and scripts. This is also reflected in the library's type definition files.
You can use for stuff like:
- Find out what language a character is in, such as Greek (α), Latin (a), Hebrew (א), and so on.
- Whether it's a kind of punctuation, digit, letter, emoji, spacing mark, or something else
- What Unicode character block it inhabits
- If it's upper-case or lower-case
The library only supports characters in the BMP. There are no plans to expanding it beyond the BMP.
In addition, the library provides basic ASCII character indicator functions, such as isLetter
, isUpper
, and so forth. These have really simple implementations and don't have the overhead of the Unicode indicators.
The library comes bundled with Unicode character information tables that originally came from the Unicode Character Database. This data can make bundles with the library being quite heavy. To counteract this, the library is built to leverage "tree shaking" or dead code elimination, which is used in all modern bundlers. Provided you only import the members you'll use, only some of the data will end up in your bundle. For example, if you just import the ASCII character indicators your bundle won't contain any Unicode data at all.
Enabling dead code elimination may require switching to ES2015 native modules. You'll need to look at your bundler's documentation for more information.
Imports
The package has three paths you can import from:
char-info/ascii
, which contains indicator functions for ASCII characters and character codes.char-info/unicode
, which contain the special Unicode character indicators.char-info
, which re-exports everything.
If you're using a properly configured bundler, you can import what you need from char-info
and still take advantage of tree shaking.
Usage - ASCII indicators
import {isLetter, isUpper, isUpperCode} from "char-info"
assert.isTrue(isLetter("a"));
assert.isTrue(isUpper("A"));
assert.isTrue(isUpperCode("A"));
import * as AsciiInfo from "char-info/ascii";
assert.isTrue(AsciiInfo.isLetterCode("a".charCodeAt(0));
Usage - Unicode Indicators
Unicode indicators work a bit differently, and aren't just plain functions. Instead, each indicator has two members, char
and code
, for testing characters (in the form of strings) and character codes, respectively.
import { uniIsLetter, uniIsDigit } from "char-info";
assert.isTrue(uniIsLetter.char("א"));
assert.isTrue(uniIsDigit.char("٩"));