FuzzySort

3.1.0 · active · verified Sun Apr 19

FuzzySort (fuzzysort) is a JavaScript library providing a fast, SublimeText-like fuzzy searching algorithm. It is designed for high performance, capable of searching thousands of items in under 1ms, while maintaining a small footprint (5kb, 0 dependencies). The current stable version is 3.1.0, with updates released as needed for performance improvements or bug fixes rather than a strict schedule. Key differentiators include its speed, minimal size, and an intuitive API for both simple string matching and complex object searching with multiple keys and custom scoring functions. It offers robust result objects that include score, target, original object reference, and index highlights, enabling rich UI feedback. It ships with TypeScript types for enhanced developer experience.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates both basic and advanced fuzzy searching for a list of objects. It shows how to search by a single key, multiple keys, and how to apply a custom score function to boost results based on object properties. It also illustrates how to use the `highlight` method on results.

import fuzzysort from 'fuzzysort';

interface MyItem {
  id: number;
  name: string;
  tags: string[];
}

const items: MyItem[] = [
  { id: 1, name: 'Apple Macintosh', tags: ['computer', 'vintage'] },
  { id: 2, name: 'Banana Republic', tags: ['clothing', 'brand'] },
  { id: 3, name: 'Microsoft Surface', tags: ['computer', 'modern'] },
  { id: 4, name: 'Cherry Pie', tags: ['food', 'dessert'] },
];

// Basic search by a single key 'name'
const resultsName = fuzzysort.go('mac', items, { key: 'name' });
console.log('Search by name for "mac":', resultsName.map(r => r.obj.name));

// Advanced search by multiple keys, with custom scoring
const resultsAdvanced = fuzzysort.go('surf comp', items, {
  keys: [
    'name',
    'tags'
  ],
  scoreFn: r => {
    // Boost score for items tagged 'computer'
    if (r.obj && r.obj.tags.includes('computer')) {
      return r.score * 1.5;
    }
    return r.score;
  },
  limit: 5 // Limit to top 5 results
});

console.log('Advanced search for "surf comp":', resultsAdvanced.map(r => ({
  name: r.obj.name,
  score: r.score,
  highlightedName: r[0] ? r[0].highlight('<b>', '</b>') : null,
  highlightedTags: r[1] ? r[1].highlight('<b>', '</b>') : null
})));

view raw JSON →