The core functionality involves calculating similarity ratios between strings or extracting the best matches from a list of choices.
By default, fuzz.extract returns an array of tuples: [choice, score, index/key]. To receive an array of objects instead, set options.returnObjects = true to get [{choice, score, key}, ...].
For asynchronous operations, use fuzz.extractAsPromised. This supports cancellation via an AbortController passed in the options.abortController field.
fuzz = require('fuzzball');
// Simple ratio
fuzz.ratio("hello world", "hiyyo wyrld"); // 64
// Token set ratio
fuzz.token_set_ratio("fuzzy was a bear", "a fuzzy bear fuzzy was"); // 100
// Extracting best matches from choices
const options = {scorer: fuzz.token_set_ratio};
const choices = ["Hood, Harry", "Mr. Minor", "Mr. Henry Hood"];
const results = fuzz.extract("mr. harry hood", choices, options);
// Results: [ [ 'Hood, Harry', 100, 0 ], [ 'Mr. Henry Hood', 85, 2 ], [ 'Mr. Minor', 40, 1 ] ]
// Async extraction with cancellation
const abortController = new AbortController();
const asyncOptions = { ...options, abortController };
fuzz.extractAsPromised("gonna get canceled", choices, asyncOptions)
.then(res => {/* do stuff */})
.catch((e) => {
if (e.message === 'aborted') console.log('Search was aborted!');
});
abortController.abort();