A minimal library for detecting confusable strings.
npm install @yuckabug/stringpeaimport { getConfusableDistance } from "@yuckabug/stringpea";
// Returns the confusable distance between two strings
console.log(getConfusableDistance("HELL0", "HELLO")); // 0 - confusably identical
console.log(getConfusableDistance("paypa1", "paypal")); // 0 - '1' looks like 'l'
console.log(getConfusableDistance("admin", "adm1n")); // 1 - one character different- Normalize - Converts confusable characters to their canonical form using Unicode confusables (e.g.,
0→O,1→l) - Calculate Distance - Computes the Levenshtein (edit) distance between the normalized strings
The returned distance represents the minimum number of single-character edits needed to change one string into another, after accounting for confusable similarity.
const distance = getConfusableDistance("admin", "adm1n");
if (distance <= 1) {
console.log("Strings are very similar");
}const a = "category";
const b = "bategory";
const distance = getConfusableDistance(a, b);
const similarity = 1 - (distance / Math.max(a.length, b.length));
if (similarity >= 0.85) {
console.log("Strings are 85% similar");
}See the NOTICE.md file for details.
This project is licensed under the MIT License. See the LICENSE file for details.