Fuzzy String Scoring Utility

0.1.2 · abandoned · verified Sun Apr 19

Command-score is a JavaScript utility designed for fuzzy string matching, providing a numerical 'matchiness' score between 0 and 1 for a given query and target string. It was developed for use in autocompletion contexts within the Superhuman email client, where the set of potential results is relatively bounded. The library's scoring algorithm prioritizes various factors, including exact matches, case sensitivity, prefix matches, and penalties for word or character jumps and transpositions within the query. Scores are primarily comparable when the query remains constant across different target strings, allowing for a secondary sort on top of matchiness. The latest version, 0.1.2, was published in June 2016, and the project has seen no significant code updates since, indicating it is no longer actively maintained. As such, it does not support modern JavaScript module systems (ESM) natively.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to import `command-score`, use it to calculate fuzzy match scores for a list of items against a query, filter out non-matching results, and then sort them by score (and alphabetically for ties).

const commandScore = require('command-score');

function getMatches(query) {
  const items = ["red", "green", "gold", "blue", "reindeer", "great-green-macaw", "goldenrod"];
  const results = [];

  items.forEach(function (item) {
    const score = commandScore(item, query);
    if (score > 0) {
      results.push({ score: score, item: item });
    }
  });

  return results.sort(function (a, b) {
    if (a.score === b.score) {
      return a.item.localeCompare(b.item);
    }
    return b.score - a.score;
  }).map(function (suggestion) {
    return suggestion.item;
  });
}

console.log(getMatches('re')); // Expected: ['red', 'reindeer', 'great-green-macaw']
console.log(getMatches('go')); // Expected: ['gold', 'goldenrod']
console.log(getMatches('bl')); // Expected: ['blue']

view raw JSON →