Skip to content

Instantly share code, notes, and snippets.

@etherealHero
Created December 22, 2023 02:35
Show Gist options
  • Save etherealHero/d12f23fde8f9db239fcee81eef8d2a6d to your computer and use it in GitHub Desktop.
Save etherealHero/d12f23fde8f9db239fcee81eef8d2a6d to your computer and use it in GitHub Desktop.
Fuzzysort find word in list
import chalk from 'chalk'
import fuzzysort from 'fuzzysort'

const findWord = 'lorem'
const resource = ['lorem ipsum', 'lorei mpsum', 'ipsum dolor', 'ipsum lor', 'losum remip', 'lor', 'loiem iplor']

console.log(
  [...fuzzysort.go(findWord, resource.map(a => ({title: a})), { keys: ['title'] })]
    .map(a => fuzzysort.highlight(a[0], (m) => chalk.yellow(m)))
    .map(b => b.join(''))
    .join("\n")
)

Output:

lorem ipsum

lorei mpsum

losum remip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment