tripokey/elm-fuzzy/5.0.2

NOTE: this package is not compatible with Elm 0.19.1

Fuzzy

This is library for performing fuzzy string matching.

Customization

addPenalty : Int -> Config

Create a penalty configuration that is applied to each additional character in hay.

removePenalty : Int -> Config

Create a penalty configuration that is applied to each additional character in needle.

movePenalty : Int -> Config

Create a penalty configuration that is applied to each out of order character in hay.

Matching

match : List Config -> List String -> String -> String -> Result

Perform fuzzy matching between a query String (needle) and a target String (hay). The order of the arguments are significant. Lower score is better. Specifying some separators will allow for partial matching within a sentence. The default configuration is movePenalty = 100, addPenalty = 1, removePenalty = 1000.

let
    simpleMatch config separators needle hay =
      match config separators needle hay |> .score
in
    simpleMatch [] [] "test" "test" == 0
    simpleMatch [] [] "tst" "test" == 1
    simpleMatch [addPenalty 10000] [] "tst" "test" == 10000
    simpleMatch [] [] "test" "tste" == 100
    simpleMatch [] [] "test" "tst" == 1000
    simpleMatch [] ["/"] "/u/b/s" "/usr/local/bin/sh" == 5
    simpleMatch [] [] "/u/b/s" "/usr/local/bin/sh" == 211
    List.sortBy (simpleMatch [] [] "hrdevi") ["screen", "disk", "harddrive", "keyboard", "mouse", "computer"] == ["harddrive","keyboard","disk","screen","computer","mouse"]
type alias Result =
{ score : Int
, matches : List Match
}

Represents the result of a match. score is the total score of the result. matches is a list of matching words within the hay.

type alias Match =
{ score : Int
, offset : Int
, length : Int
, keys : List Key
}

Represents a matching word in hay. score is the score that this Match contributes to the total score in a Result. offset is the index where this match starts in the hay. length is the length of the match. keys is a list of matching indexes within the word. The keys are relative to the offset.

type alias Key =
Int

Represents a matching character in a Match.