Skip to content

Commit 2244c2c

Browse files
committed
Add SearchResults.filterMatches
1 parent f8cecbd commit 2244c2c

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/Lib/Search.elm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Lib.Search exposing
44
, debounce
55
, debounce_
66
, empty
7+
, fromList
78
, fromResult
89
, hasSubstantialQuery
910
, isEmptyQuery
@@ -64,6 +65,11 @@ search query_ =
6465
NotAsked query_
6566

6667

68+
fromList : String -> List a -> Search a
69+
fromList q matches =
70+
Success q (SearchResults.fromList matches)
71+
72+
6773

6874
-- TRANSFORM
6975

src/Lib/SearchResults.elm

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Lib.SearchResults exposing
55
, cycleNext
66
, cyclePrev
77
, empty
8+
, filterMatches
89
, focus
910
, focusOn
1011
, from
@@ -110,6 +111,24 @@ mapToList f results =
110111
mapMatchesToList f matches
111112

112113

114+
filterMatches : (a -> Bool) -> SearchResults a -> SearchResults a
115+
filterMatches f results =
116+
case results of
117+
Empty ->
118+
Empty
119+
120+
SearchResults (Matches matches) ->
121+
SearchResults
122+
(Matches
123+
(matches
124+
|> Zipper.toList
125+
|> List.filter f
126+
|> Zipper.fromList
127+
|> Maybe.withDefault matches
128+
)
129+
)
130+
131+
113132
toMaybe : SearchResults a -> Maybe (Matches a)
114133
toMaybe results =
115134
case results of

0 commit comments

Comments
 (0)