@hyrious/fuzzy-match
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

@hyrious/fuzzy-match

A string match function that help implementing fuzzy search like Sublime Text.

The algorithm is derived from this C implementation of fts_fuzzy_match.

Install

npm add @hyrious/fuzzy-match

Usage

import { match, match_trace } from "@hyrious/fuzzy-match";

match("th", "tth-hash");
// => 45 (score, maybe negative)

match("not found", "string");
// => -Infinity

// Match with backtrack, useful when we want to highlight the matching chars.
match_trace("th", "tth-hash");
// => { score: 45, stops: [0, 4] }

match_trace("not found", "string");
// => null

Possible Fuzzy Search Implementation

import { match } from "@hyrious/fuzzy-match";

function search(text, list) {
  return list
    .map((item) => ({
      item,
      score: match(text, item),
    }))
    .filter(({ score }) => score > -Infinity)
    .sort((a, b) => b.score - a.score);
}

License

MIT @ hyrious

Package Sidebar

Install

npm i @hyrious/fuzzy-match

Weekly Downloads

3

Version

0.1.1

License

MIT

Unpacked Size

16.1 kB

Total Files

9

Last publish

Collaborators

  • hyrious