Skip to content

swarn/fzy-lua

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fzy-lua

Actions Status

A Lua port of fzy's fuzzy string matching algorithm. This includes both a pure Lua implementation and a compiled C implementation with a Lua wrapper.

Why

From the original fzy:

fzy is faster and shows better results than other fuzzy finders.

Most other fuzzy matchers sort based on the length of a match. fzy tries to find the result the user intended. It does this by favouring matches on consecutive letters and starts of words. This allows matching using acronyms or different parts of the path.

Install

luarocks install fzy

Or, just download a copy of fzy_lua.lua and drop it in your project.

Usage

score(needle, haystack)

local fzy = require('fzy')

fzy.score("amuser", "app/models/user.rb")     -- 5.595
fzy.score("amuser", "app/models/customer.rb") -- 3.655

positions(needle, haystack)

fzy.positions("amuser", "app/models/user.rb")     -- { 1, 5, 12, 13, 14, 15 }
--                       ^   ^      ^^^^
fzy.positions("amuser", "app/models/customer.rb") -- { 1, 5, 13, 14, 18, 19 }
--                       ^   ^       ^^   ^^

NB: score and positions should only be called with a needle that is a subsequence of the haystack, which you can check with the has_match function.

See the docs for more information.

Testing

busted test/test.lua

Thanks

John Hawthorn wrote the original fzy. The native implementation here is basically his code with a few tweaks, and the lua implementation is derived from his fzy.js implementation.

Rom Grk made several useful suggestions, and has a lua C implemenation using the luajit ffi library.

About

A lua implementation of the fzy fuzzy matching algorithm

Topics

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •