@@ -1,182 +1,40 @@
1−module Search exposing
1+module Search exposing (searchList)
11−import Set exposing (Set)
3+import Html exposing (..)
12−
4+import Html.Lazy as Lazy
14−ignoredCharacters : Set Char
16− Set.fromList [ '_', '-', '.' ]
19−prepareString : String -> String
21− String.filter (\c -> Set.member c ignoredCharacters |> not)
27− | NoMatch {- | A search result for an empty query. It is for the most part just ignored, hance the name. -}
31−{-| Used to combine different search results within one search entry.
33−combine : List SearchResult -> SearchResult
40− if List.all ((==) Ignore) ress then
45− go : List SearchResult -> SearchResult
54− (Match score) :: xs ->
60− Match (score + score_)
75type alias Needle =
7type alias Needle =
79−type Query
11+type alias Haystack =
80− = Query Needle
12+ String
83−{-| Constructs queries from an input string. (Splits on spaces and requires all words to match)
85−constructQueries : String -> List Query
87− prepareString >> String.words >> List.filter (String.isEmpty >> not) >> List.filter (String.length >> (<) 1) >> List.map Query
90−{-| Search a search term within a search entry.
92−search : Query -> Haystack -> SearchResult
93−search (Query needle) haystack =
94− if String.contains needle (prepareString haystack) then
101−{-| Requires all search results to be matched
15+search : List Needle -> Haystack -> Bool
102−-}
16+search needles haystack =
103−searchAll : List SearchResult -> SearchResult
106− combineQueries : SearchResult -> SearchResult -> SearchResult
18+ haystack_ =
107− combineQueries x y =
19+ haystack |> String.toLower
121− ( Match a, Match b ) ->
124− List.foldl combineQueries Ignore
21+ List.all (\needle -> String.contains needle haystack_) needles
127−{-| A list of search inputs with the definition of what it means to search for that query.
130− = Search (List ( String, Query -> a -> SearchResult ))
133−contramap : (b -> a) -> Search a -> Search b
134−contramap f (Search s) =
135− List.map (\( q, g ) -> ( q, \query b -> g query (f b) )) s
139−{-| The main function of the module. Search a list with the given `Search`.
24+prepareNeedles : String -> List Needle
140−-}
25+prepareNeedles =
141−searchList : Search a -> List a -> List a
26+ String.toLower
142−searchList (Search searches) aa =
27+ >> String.words
152− List.map (\q a -> f q a) qq
28+ >> List.map String.trim
153− )
29+ >> List.filter (String.isEmpty >> not)
154−
30+ >> List.filter (String.length >> (<) 1)
156− List.map (\f -> f a) searchByTerm |> searchAll
159− |> List.map (\a -> ( searchEntry a, a ))
160− |> sortResult Tuple.first
161− |> List.map Tuple.second
164−sortResult : (a -> SearchResult) -> List a -> List a
33+searchList : Needle -> (a -> Haystack) -> (List a -> Html msg) -> List a -> Html msg
165−sortResult f =
34+searchList needle getHaystack show aa =
166− List.filterMap
35+ case prepareNeedles needle of
168− case f a of
37+ Lazy.lazy show aa
175− Ignore ->
39+ needles ->
176− Just ( 0, a )
40+ aa |> List.filter (\a -> search needles (getHaystack a)) |> show
178− -- Here we assume that the sort is stable which is not true for old
179− -- versions of ECMAScript which would result in undeterministic
180− -- ordering for empty queries.
181− >> List.sortBy Tuple.first
182− >> List.map Tuple.second