ddedic / fuzzysort

Fast SublimeText-like fuzzy search for JavaScript.

Home Page:https://rawgit.com/farzher/fuzzysort/master/test.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fast SublimeText-like fuzzy search for JavaScript.

Sublime's fuzzy search is... sublime. I wish everything used it. So here's an open source js version.

https://rawgit.com/farzher/fuzzysort/master/test.html

Installation Node

npm i fuzzysort
node
> require('fuzzysort').single('t', 'test')
{ score: 3, highlighted: '<b>t</b>est' }

Installation Browser

<script src="https://rawgit.com/farzher/fuzzysort/master/fuzzysort.js"></script>
<script> console.log(fuzzysort.single('t', 'test')) </script>

Usage

fuzzysort.single(search, target)

fuzzysort.single('query', 'some string that contains my query.')
// {score: 59, highlighted: "some string that contains my <b>query</b>."}

fuzzysort.single('query', 'irrelevant string') // null

// exact match returns a score of 0. lower score is better
fuzzysort.single('query', 'query') // {score: 0, highlighted: "<b>query</b>"}

fuzzysort.go(search, targets)

fuzzysort.go('mr', ['Monitor.cpp', 'MeshRenderer.cpp'])
// [{score: 18, highlighted: "<b>M</b>esh<b>R</b>enderer.cpp"}
// ,{score: 6009, highlighted: "<b>M</b>onito<b>r</b>.cpp"}]

fuzzysort.goAsync(search, targets)

let promise = fuzzysort.goAsync('mr', ['Monitor.cpp', 'MeshRenderer.cpp'])
promise.then(results => console.log(results))
if(invalidated) promise.cancel()
Options
  • fuzzysort.highlightMatches = true Turn this off if you don't care about highlighted (faster)
  • fuzzysort.highlightOpen = '<b>'
  • fuzzysort.highlightClose = '</b>'
  • fuzzysort.threshold = null Don't return matches worse than this (lower is faster) (irrelevant for single)
  • fuzzysort.limit = null Don't return more results than this (faster) (irrelevant for single)

How To Go Fast

You can help the algorithm go fast by providing prepared targets instead of raw strings. Preparing strings is slow, do this ahead of time and only prepare each target once.

myObj.titlePrepared = fuzzysort.prepare(myObj.title)
fuzzysort.single('gotta', myObj.titlePrepared)
fuzzysort.single('go', myObj.titlePrepared)
fuzzysort.single('fast', myObj.titlePrepared)

Advanced Usage

Search a list of objects, by multiple fields, with custom weights.

let objects = [{title:'Favorite Color', desc:'Chrome'}, {title:'Google Chrome', desc:'Launch Chrome'}]
let search = 'chr'
let results = []
for(const myObj of objects) {
  const titleInfo = fuzzysort.single(search, myObj.title)
  const descInfo = fuzzysort.single(search, myObj.desc)

  // Create a custom combined score to sort by. +100 to the desc score makes it a worse match
  const myScore = Math.min(titleInfo?titleInfo.score:1000, descInfo?descInfo.score+100:1000)
  if(myScore >= 1000) continue

  results.push({
    myObj,
    myScore,
    titleHighlighted: titleInfo ? titleInfo.highlighted : myObj.title,
    descHighlighted: descInfo ? descInfo.highlighted : myObj.desc,
  })
}
results.sort((a, b) => a.myScore - b.myScore)
console.log(results)

Multiple instances, each with different options.

const strictsort = fuzzysort.new()
strictsort.threshold = 999

Get the matched Indexes.

fuzzysort.single('tt', 'test').indexes // [0, 3]

About

Fast SublimeText-like fuzzy search for JavaScript.

https://rawgit.com/farzher/fuzzysort/master/test.html


Languages

Language:JavaScript 99.8%Language:HTML 0.2%