Skip to content

Commit

Permalink
Move functions and helpers before place of use
Browse files Browse the repository at this point in the history
  • Loading branch information
fidel committed Apr 4, 2024
1 parent 10cf276 commit e2bdbe6
Showing 1 changed file with 56 additions and 56 deletions.
112 changes: 56 additions & 56 deletions ruby_event_store-browser/elm/src/Search.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Search exposing (..)
import FeatherIcons
import Html exposing (..)
import Html.Attributes exposing (autofocus, class, href, placeholder, value)
import Html.Events exposing (onInput, onSubmit, onClick)
import Html.Events exposing (onClick, onInput, onSubmit)
import List
import Task

Expand All @@ -25,6 +25,16 @@ type Msg
| GoToStream Stream


emptyStreamName : Stream
emptyStreamName =
""


emptyStreams : List Stream
emptyStreams =
[]


init : (Stream -> a) -> (Stream -> a) -> Model a
init onSelectMsg onQueryMsg =
{ streams = emptyStreams
Expand All @@ -34,6 +44,21 @@ init onSelectMsg onQueryMsg =
}


onSelectCmd : (Stream -> a) -> Stream -> Cmd a
onSelectCmd onSelectMsg stream =
Task.perform onSelectMsg (Task.succeed stream)


onQueryChangedCmd : (Stream -> a) -> Stream -> Cmd a
onQueryChangedCmd onQueryMsg stream =
Task.perform onQueryMsg (Task.succeed stream)


hasAtLeastThreeChars : Stream -> Bool
hasAtLeastThreeChars stream =
String.length stream >= 3


update : Msg -> Model a -> ( Model a, Cmd a )
update msg model =
case msg of
Expand All @@ -52,6 +77,31 @@ update msg model =
)


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


filterStreams : Stream -> List Stream -> List Stream
filterStreams stream streams =
if String.isEmpty stream then
emptyStreams

else
List.filterMap (caseInsensitiveContains stream) streams


view : Model a -> Html Msg
view model =
let
Expand Down Expand Up @@ -79,6 +129,11 @@ view model =
]


streamsPresent : List Stream -> Bool
streamsPresent streams =
not <| List.isEmpty streams


viewStreamList : List Stream -> Html Msg
viewStreamList streams =
if streams |> streamsPresent then
Expand All @@ -103,58 +158,3 @@ viewStreamListItem stream =
]
[ text stream ]
]


emptyStreamName : Stream
emptyStreamName =
""


emptyStreams : List Stream
emptyStreams =
[]


onSelectCmd : (Stream -> a) -> Stream -> Cmd a
onSelectCmd onSelectMsg stream =
Task.perform onSelectMsg (Task.succeed stream)


onQueryChangedCmd : (Stream -> a) -> Stream -> Cmd a
onQueryChangedCmd onQueryMsg stream =
Task.perform onQueryMsg (Task.succeed stream)


hasAtLeastThreeChars : Stream -> Bool
hasAtLeastThreeChars stream =
String.length stream >= 3


streamsPresent : List Stream -> Bool
streamsPresent streams =
not <| List.isEmpty streams


filterStreams : Stream -> List Stream -> List Stream
filterStreams stream streams =
if String.isEmpty stream then
emptyStreams

else
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

0 comments on commit e2bdbe6

Please sign in to comment.