Skip to content

Getting started ​

Install ​

sh
npm install fuzzy-toolkit
sh
pnpm add fuzzy-toolkit
sh
yarn add fuzzy-toolkit
sh
bun add fuzzy-toolkit

Works with ES modules (import) and CommonJS (require), in Node 18+ and every modern browser or bundler. Types are included.

Without a bundler, load it from a CDN: the script gives a global fuzzyToolkit.

html
<script src="https://cdn.jsdelivr.net/npm/fuzzy-toolkit"></script>
<script>
  fuzzyToolkit.similarity('Molière', 'moliere') // 100
</script>

Or as a module: import { search } from 'https://cdn.jsdelivr.net/npm/fuzzy-toolkit/dist/browser/fuzzy-toolkit.min.js'.

Compare two texts ​

ts
import { similarity, isMatch } from 'fuzzy-toolkit'

similarity('Molière', 'moliere')                      // 100
similarity('stairway to heven', 'Stairway to Heaven') // 94.44
similarity('Smith John', 'John Smith')                // 100
similarity('Paris', 'Tokyo')                          // 0

isMatch('color', 'colour')     // true  (83.33 ≥ 80)
isMatch('color', 'colour', 90) // false

Scores go from 0 (nothing in common) to 100 (identical once normalized). Two texts match when the score reaches the threshold, 80 by default.

Pick the function for your use case ​

You want to…Use
Grade an answer in a quiz or a blind testcheck()
Filter a list as the user typessearch() / createIndex()
Suggest a correctionclosest()
Merge duplicates in a datasetdedupe() / group()
Spot a title or a name inside a sentencefind()
Highlight what matched in a resulthighlight()
Know how similar two texts aresimilarity(), isMatch()
Understand a scorecompare()

Reuse a configuration ​

When the same options are used many times, create a matcher. Options are validated and compiled once.

ts
import { createMatcher } from 'fuzzy-toolkit'

const quiz = createMatcher({ preset: 'music', threshold: 85 })

quiz.check('beatles', 'The Beatles').verdict // 'correct'
quiz.similarity('Hey Jude - Remastered', 'hey jude') // 100
quiz.search('queen', songs, { keys: 'artist' })

A matcher carries every method, so a bundler keeps all of them (about 14 kB min+gzip). When the size of a browser bundle matters, import only the functions you call and pass them the same options object: check and checkAll alone are about 10 kB. See Size.

Work with objects ​

List functions accept strings or objects. With objects, tell them where the text is with keys: a property, several properties, or a function. TypeScript only lets you name properties that hold text.

ts
interface Song { title: string; artist: string; aliases?: string[]; year: number }

search('queen', songs, { keys: ['title', 'artist', 'aliases'] }) // ✅
search('queen', songs, { keys: (song) => `${song.title} ${song.artist}` }) // ✅
search('queen', songs) // ❌ keys is required for objects
search('queen', songs, { keys: ['year'] }) // ❌ year is not text
search('queen', songs, { keys: ['title', { name: 'artist', weight: 0.8 }] }) // ✅ artist counts less

Each result is a Match: { item, score, index, text }, where text is the value that produced the score.

Next: Options.