g1.fuzzysearch

g1.fuzzysearch(data, options) returns a fuzzy search function that filters the data based on the text.

For example:

var data = [
  { product: "Cider Apple Vinegar" },
  { product: "JBL In-Ear Headphones" },
  { product: "Vaseline Body Lotion" },
  { product: "Redux Men's Watch" },
  { product: "Omega3 Fish Oil" },
];

var search = g1.fuzzysearch(data, {
  keys: ["product"], // Search within these keys
  limit: 2, //  Return only the top 2 results
});

search("omega");
// Returns {product: "Omega3 Fish Oil", ...} since it's the only one
search("red");
// Returns {product: "Redux Men's Watch"} and {product: "JBL In-Ear Headphones"}
// The second matches r (in "Ear"), followed by e then d in "Headphones"

It matches with the following priority. For example, if the string is “alpha beta”, then:

  1. Match the exact phrase (“alpha beta”)
  2. Match all words in the same order (“alp bet”)
  3. Match words in any order (“bet alp”)
  4. Match partial words in any order (“ba aph”)
  5. Match letters in order (“abt”)

It accepts an options dict with these keys: