Socket
Book a DemoInstallSign in
Socket

@hyrious/fuzzy-match

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hyrious/fuzzy-match

Match string like Sublime Text

latest
Source
npmnpm
Version
0.1.1
Version published
Maintainers
1
Created
Source

@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

Keywords

string

FAQs

Package last updated on 26 Sep 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts