Skip to content

yuckabug/stringpea

@yuckabug/stringpea 🫛✨

codecov

A minimal library for detecting confusable strings.

Installation 📦

npm install @yuckabug/stringpea

Usage 🚀

import { 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

How It Works 🤔⚙️

  1. Normalize - Converts confusable characters to their canonical form using Unicode confusables (e.g., 0O, 1l)
  2. 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.

Common Patterns 🎯

Absolute Distance Threshold

const distance = getConfusableDistance("admin", "adm1n");
if (distance <= 1) {
  console.log("Strings are very similar");
}

Proportional Threshold (for different length strings)

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");
}

Acknowledgments 🙏💚

See the NOTICE.md file for details.

License 📜

This project is licensed under the MIT License. See the LICENSE file for details.

Packages

No packages published

Contributors 2

  •  
  •