Homoglypher
Homoglypher is a utility library designed to normalize homoglyphs in strings to their ASCII equivalents based on confusables.txt
from https://www.unicode.org/Public/security/latest/confusables.txt
Features
- Utilizes the latest Unicode confusables list.
- Detects and replaces Unicode homoglyphs with ASCII characters.
- Allows custom mapping
- Optional event handling for detecting changes
Installation
npm install homoglypher
Data in confusables.txt
are automatically updated on install (via postinstall script).
Usage
Simple usage:
import { Homoglypher } from 'homoglypher';
const homoglypher = new Homoglypher();
const input = '𝓐𝓑𝓒 is fancy but should become ABC.';
const normalized = homoglypher.normalize(input);
console.log(normalized); // Output: ABC is fancy but should become ABC.
Custom mapping:
const homoglypher = new Homoglypher({
custom: { // can be ES6 Map too
'æ': 'ae'
}
});
Event handling:
const homoglypher = new Homoglypher();
const normalized = homoglypher.normalize(input, {
onChange: (slice, replacement) => {
console.log(`Replaced "${slice}" → "${replacement}"`);
}
});
License
ISC © Tonylus