Skip to content

Commit 84c77e8

Browse files
committed
Add SearchResults.uniqueMatchesBy
1 parent 2244c2c commit 84c77e8

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/Lib/SearchResults.elm

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module Lib.SearchResults exposing
2323
, prev
2424
, toList
2525
, toMaybe
26+
, uniqueMatchesBy
2627
)
2728

2829
import List.Extra as ListE
@@ -111,6 +112,24 @@ mapToList f results =
111112
mapMatchesToList f matches
112113

113114

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

tests/Lib/SearchResultsTests.elm

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,29 @@ getAt =
154154

155155

156156

157+
-- MAP
158+
159+
160+
uniqueMatchesBy : Test
161+
uniqueMatchesBy =
162+
describe "SearchResults.uniqueMatchesBy"
163+
[ test "Removes duplicates based on the function" <|
164+
\_ ->
165+
let
166+
result =
167+
SearchResults.from [ "a", "b" ] "b" [ "c", "a", "c" ]
168+
|> SearchResults.uniqueMatchesBy
169+
(\x -> x)
170+
|> SearchResults.toList
171+
172+
expected =
173+
[ "a", "b", "c" ]
174+
in
175+
Expect.equal expected result
176+
]
177+
178+
179+
157180
-- MAP
158181

159182

0 commit comments

Comments
 (0)