0.1.1 • Published 2 years ago

@hyrious/fuzzy-match v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@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

0.1.1

2 years ago

0.1.0

2 years ago