From a87d7f69519b115b3c93fbc79d9c5f02010cea7f Mon Sep 17 00:00:00 2001 From: Szymon Fiedler Date: Fri, 22 Mar 2024 21:07:19 +0100 Subject: [PATCH] Clientside filtering is case insensitive now I really enjoy the concept of filterMap combined with Maybe. --- ruby_event_store-browser/elm/src/Search.elm | 18 +++++++++++++++++- .../elm/tests/SearchTest.elm | 7 ++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/ruby_event_store-browser/elm/src/Search.elm b/ruby_event_store-browser/elm/src/Search.elm index 1d901feb73..759b7cc5ae 100644 --- a/ruby_event_store-browser/elm/src/Search.elm +++ b/ruby_event_store-browser/elm/src/Search.elm @@ -134,4 +134,20 @@ filterStreams stream streams = [] else - List.filter (String.contains stream) streams + List.filterMap (caseInsensitiveContains stream) streams + + +caseInsensitiveContains : Stream -> Stream -> Maybe Stream +caseInsensitiveContains needle haystack = + let + needleLower = + String.toLower needle + + haystackLower = + String.toLower haystack + in + if String.contains needleLower haystackLower then + Just haystack + + else + Nothing diff --git a/ruby_event_store-browser/elm/tests/SearchTest.elm b/ruby_event_store-browser/elm/tests/SearchTest.elm index 9d15d97d3a..19b53ddd74 100644 --- a/ruby_event_store-browser/elm/tests/SearchTest.elm +++ b/ruby_event_store-browser/elm/tests/SearchTest.elm @@ -1,7 +1,7 @@ module SearchTest exposing (suite) import Expect -import Search exposing (Stream, filterStreams) +import Search exposing (filterStreams) import Test exposing (..) @@ -29,5 +29,10 @@ suite = Expect.equal [ "DummyStream$78" ] (filterStreams "78" [ "DummyStream$78", "DummyStream$79" ]) + , test "filterStreams is case insensitive" <| + \_ -> + Expect.equal + [ "DummyStream$78", "DummyStream$79" ] + (filterStreams "stream" [ "DummyStream$78", "DummyStream$79" ]) ] ]