FastFzy
The lua implementation of the fzy string matching algorithm. This algorithm is optimized for matching stuff on the terminal, but should serve well as a baseline search algorithm within a game too.
See:
Modified from the initial code to fit this codebase. While this definitely messes with some naming which may have been better, it also keeps usage of this library consistent with other libraries.
Notes:
- A higher score is better than a lower score
-
Scoring time is
O(n*m)wherenis the length of the needle andmis the length of the haystack. - Scoring memory is also
O(n*m) - Should do quite well with small lists
This was ported from Quenty's Fzy.
TODO: Support UTF8
Types
FzyConfiguration
interface FzyConfiguration {CapitalMatchScore: numberCaseSensitive: booleanConsecutiveMatchScore: numberDotMatchScore: numberGapInnerScore: numberGapLeadingScore: numberGapTrailingScore: numberMaxMatchLength: numberSlashMatchScore: numberWordMatchScore: number}Configuration for FastFzy. See FastFzy.CreateConfiguration for details. This affects scoring and how the matching is done.
FilterResult
interface FilterResult {Index: number--
The index of the line in haystacks.
Positions: {int}--
The positions of the needle in haystack.
String: string--
The line in haystacks.
}An interface returned by FastFzy.BetterFilter.
Functions
CreateConfiguration
Creates a new configuration for Fzy.
IsFzyConfiguration
FastFzy.IsFzyConfiguration(configuration: any) → booleanReturns true if it is a FzyConfiguration.
HasMatch
Check if needle is a subsequence of the haystack.
Usually called before FastFzy.Score or FastFzy.Positions.
IsPerfectMatch
FastFzy.IsPerfectMatch(needle: string,--
must be a subsequence of haystack, or the result is undefined.
haystack: string) → booleanComputes whether a needle or haystack are a perfect match or not
Score
FastFzy.Score(needle: string,--
must be a subsequence of haystack, or the result is undefined.
haystack: string) → number--
higher scores indicate better matches. See also FastFzy.GetMinScore and FastFzy.GetMaxScore.
Compute a matching score.
Positions
FastFzy.Positions(needle: string,--
must be a subsequence of haystack, or the result is undefined.
haystack: string) → ({int},--
indices, where indices[n] is the location of the nth character of needle in haystack.
number--
the same matching score returned by score
)Compute the locations where fzy matches a string.
Determine where each character of the needle is matched to the haystack
in the optimal match.
Filter
FastFzy.Filter() → {{idx,positions,score},...}Apply FastFzy.HasMatch and FastFzy.Positions to an array of haystacks.
Returns an array with one entry per matching line in haystacks,
each entry giving the index of the line in haystacks as well as
the equivalent to the return value of positions for that line.
Notice
For Luau, I'd recommend using FastFzy.BetterFilter instead, as this specific function doesn't really have the best Luau support.
BetterFilter
An alternative to FastFzy.Filter that returns an array of FastFzy.FilterResult. This is more ideal for Luau at the cost of some speed, as Luau does not support strict arrays the same way that TypeScript would
GetMinScore
FastFzy.GetMinScore() → numberThe lowest value returned by score.
In two special cases:
- an empty
needle, or - a
needleorhaystacklarger than than FastFzy.GetMaxLength,
the FastFzy.Score function will return this exact value, which can be used as a sentinel. This is the lowest possible score.
GetMaxScore
FastFzy.GetMaxScore() → numberThe score returned for exact matches. This is the highest possible score.
GetMaxLength
The maximum size for which fzy will evaluate scores.
GetScoreFloor
The minimum score returned for normal matches.
For matches that don't return FastFzy.GetMinScore, their score will be greater than than this value.
GetScoreCeiling
The maximum score for non-exact matches.
For matches that don't return FastFzy.GetMaxScore, their score will be less than this value.