is-char
v1.1.5
Published
Check if a value is exactly one character.
Maintainers
Readme
is-char
is-char is a focused utility for one job: checking whether a value is a single-character string.
In many codebases, this check appears in validators, parsers, CLIs, text filters, and protocol handlers. Keeping it in a dedicated package makes that intent explicit and reusable across projects.
Why this package matters
- It standardizes a common validation rule used in input boundaries.
- It removes repeated ad-hoc checks from application code.
- It keeps behavior consistent across services and libraries.
- It is minimal, dependency-free, and safe to use in performance-sensitive paths.
Install
npm
npm i is-charJSR
npx jsr add @arvid/is-charUsage
import isChar from "is-char";
isChar("a"); // true
isChar("ab"); // false
isChar(""); // false
isChar(1); // false
isChar("a", { is: "a" }); // true
isChar("a", { is: "b" }); // falseAPI
isChar(value)
Returns true when:
valueis a stringvalue.length === 1
Otherwise returns false.
isChar(value, { is })
If is is provided, isChar returns true only when:
valueis a single-character stringisis a single-character stringvalue === is
Character semantics
is-char follows JavaScript string semantics and checks value.length === 1 (UTF-16 code units).
This means some multi-code-unit characters (for example many emoji) return false.
TypeScript
import isChar from "is-char";Type definitions are included out of the box.
