nounicode
v1.0.4
Published
A Utility to validate and enforce ASCII-only strings (modern, extended, or standard ASCII) in JavaScript/TypeScript.
Maintainers
Readme
nounicode
A JavaScript/TypeScript utility to detect and enforce ASCII-only text. Choose between modern (0–255), extended (128–255), or standard (0–127) ASCII checks.
Installation
npm install nounicodeUsage
Import the default function or named helpers:
import isNoUnicode, {
isModernASCII,
isExtendedASCII,
isStandardASCII,
ASCIIType
} from 'nounicode';API Reference
isNoUnicode(data: string, type: ASCIIType = 'MODERN'): boolean
Validate that every character in a string falls within one of three ASCII ranges.
Parameters
data
The input string to validate.type
One of the following modes (defaults toMODERN):MODERNChecks code points < 256EXTENDEDChecks code points > 127 and < 256STANDARDChecks code points < 128
Returns
trueif all characters satisfy the chosen ASCII range.falseas soon as a character falls outside the specified range.
Notes
- Uses
String.prototype.codePointAtto handle all Unicode code points. - Iterates through code units, so astral characters (code points > 0xFFFF) are split into two iterations.
Examples
isNoUnicode('Hello, World!'); // true (all < 256)
isNoUnicode('Résumé', 'STANDARD'); // false ('é' is 233)
isNoUnicode('Résumé', 'EXTENDED'); // true (233 > 127 && 233 < 256)
isNoUnicode('😊', 'MODERN'); // false (128522 ≥ 256)isModernASCII(cc: number): boolean
Check if a Unicode code point is within 0–255.
Parameters
cc
A Unicode code point (e.g., returned bystr.codePointAt(idx)).
Returns
trueif0 ≤ cc < 256falseotherwise
Example
isModernASCII(200); // true
isModernASCII(300); // falseisExtendedASCII(cc: number): boolean
Check if a Unicode code point is within the extended ASCII range 128–255.
Parameters
cc
A Unicode code point to test.
Returns
trueif128 < cc < 256falseotherwise
Example
isExtendedASCII(129); // true
isExtendedASCII(127); // falseisStandardASCII(cc: number): boolean
Check if a Unicode code point is within the standard ASCII range 0–127.
Parameters
cc
A Unicode code point to test.
Returns
trueif0 ≤ cc < 128falseotherwise
Example
isStandardASCII(65); // true ('A')
isStandardASCII(128); // falseTypes
export type ASCIIType = 'MODERN' | 'EXTENDED' | 'STANDARD';MODERNAll byte values (0–255)EXTENDEDUpper half of byte range (128–255)STANDARDClassic ASCII (0–127)
Performance
Validates a string in O(n) time, where n is the number of UTF-16 code units. Early exits on the first invalid character.
Examples
isNoUnicode('Hello World!'); // true
isNoUnicode('Café'); // false (contains 'é')
isNoUnicode('Café', 'EXTENDED'); // true