fuzzymatchingjs
v0.5.0
Published
String Fuzzy Matching
Maintainers
Readme
fuzzymatchingjs
A fast and lightweight JavaScript library for fuzzy string matching. Perfect for search functionality, autocomplete features, and finding approximate matches in text.
Features
- 🚀 Fast: Optimized algorithms for quick string matching
- 🎯 Accurate: Confidence scoring for match quality
- 🌐 Unicode Support: Works with emojis and international characters
- 📦 Lightweight: Minimal dependencies, small bundle size
- 🔧 Flexible: Multiple output formats (UMD, ESM, CommonJS)
- ✅ Well-tested: Comprehensive test coverage
Installation
npm install fuzzymatchingjsQuick Start
ES Modules
import fuzzyMatching from 'fuzzymatchingjs';
// Basic pattern matching
const position = fuzzyMatching.fuzzyMatchPattern('hello world', 'wor');
console.log(position); // Returns the position of the match
// Confidence scoring
const confidence = fuzzyMatching.confidenceScore('JavaScript', 'JS');
console.log(confidence); // Returns a confidence score between -1 and 1CommonJS
const fuzzyMatching = require('fuzzymatchingjs');
const result = fuzzyMatching.fuzzyMatchPattern('search text', 'sea');Browser (UMD)
<script src="https://unpkg.com/fuzzymatchingjs/dist/fuzzymatchingjs.umd.js"></script>
<script>
const result = fuzzymatchingjs.fuzzyMatchPattern('hello', 'hel');
</script>API Reference
fuzzyMatchPattern(text, pattern)
Finds the best match location for a pattern within a text string.
Parameters:
text(string): The text to search withinpattern(string): The pattern to search for
Returns: Number - The starting position of the best match, or -1 if no match found
Example:
fuzzyMatching.fuzzyMatchPattern('abcdef', 'cd'); // Returns 2
fuzzyMatching.fuzzyMatchPattern('hello world', 'wor'); // Returns 6
fuzzyMatching.fuzzyMatchPattern('🐶🐱🐶🐶🐶', '🐱'); // Returns 1confidenceScore(text, pattern)
Calculates a confidence score for how well a pattern matches a text.
Parameters:
text(string): The text to match againstpattern(string): The pattern to match
Returns: Number - Confidence score between -1 (no match) and 1 (perfect match)
Example:
fuzzyMatching.confidenceScore('Stacee Lima', 'SL'); // Returns ~0.5
fuzzyMatching.confidenceScore('exact match', 'exact match'); // Returns 1.0
fuzzyMatching.confidenceScore('no match here', 'xyz'); // Returns -1Use Cases
- Search functionality: Implement forgiving search that finds results even with typos
- Autocomplete: Suggest completions based on partial input
- Data deduplication: Find similar entries in datasets
- Command palettes: Match user input to available commands
- File/folder search: Locate files with approximate names
Acknowledgements
The fuzzy matching algorithms are based on Neil Fraser's google-diff-match-patch library.
Development
Prerequisites
- Node.js 20.0.0 or higher
- npm 9.0.0 or higher
Setup
Clone the repository:
git clone https://github.com/seanoshea/fuzzymatchingjs.git cd fuzzymatchingjsInstall dependencies:
npm installRun tests to ensure everything works:
npm test
Available Scripts
npm run build- Build the library for productionnpm run dev- Build in watch mode for developmentnpm test- Run the test suitenpm run test:watch- Run tests in watch modenpm run lint- Check code style and qualitynpm run lint:fix- Automatically fix linting issuesnpm run generate-docs- Generate JSDoc documentation
Project Structure
fuzzymatchingjs/
├── src/ # Source code
│ ├── lib/ # Core library files
│ └── index.js # Main entry point
├── test/ # Jest test files
├── dist/ # Built files (generated)
├── docs/ # Documentation
└── .github/ # GitHub workflows and templatesBrowser Compatibility
- Chrome 90+
- Firefox 88+
- Safari 14+
- Edge 90+
- Node.js 20+
Performance
The library is optimized for performance with:
- Efficient string matching algorithms
- Minimal memory allocation
- Fast execution for typical use cases
- Bundle size < 10KB (minified + gzipped)
Contributing
We welcome contributions! Please see our Contributing Guide for details.
Development Workflow
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Make your changes and add tests
- Run
npm testandnpm run lint - Commit your changes:
git commit -m 'Add feature' - Push to your fork:
git push origin feature-name - Create a Pull Request
License
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.
Author
Sean O'Shea - [email protected]
Related Projects
- Swift version - Swift implementation of this library
