diff options
-rw-r--r-- | src/Model/TimerEdition.elm | 17 | ||||
-rw-r--r-- | src/Update/Update.elm | 2 | ||||
-rw-r--r-- | src/Utils/Maybe.elm | 7 |
3 files changed, 20 insertions, 6 deletions
diff --git a/src/Model/TimerEdition.elm b/src/Model/TimerEdition.elm index 5feea33..2ec98b7 100644 --- a/src/Model/TimerEdition.elm +++ b/src/Model/TimerEdition.elm @@ -2,7 +2,7 @@ module Model.TimerEdition ( TimerEdition , Numbers , newTimerEdition - , keyCodeToChar + , keyCodeToNumberChar , toTime , toMinutesAndSeconds ) where @@ -16,6 +16,7 @@ import Keyboard (KeyCode) import Model.Id (..) import Utils.List (..) +import Utils.Maybe (..) type alias TimerEdition = { id : Id @@ -29,10 +30,16 @@ newTimerEdition id = , numbers = [] } -keyCodeToChar : KeyCode -> Maybe Char -keyCodeToChar code = - let zero = 96 - nine = zero + 9 +keyCodeToNumberChar : KeyCode -> Maybe Char +keyCodeToNumberChar code = + List.map (flip keyCodeToCharFromZero code) zeroKeyCodes + |> List.foldl orElse Nothing + +zeroKeyCodes = [48, 96] + +keyCodeToCharFromZero : KeyCode -> KeyCode -> Maybe Char +keyCodeToCharFromZero zero code = + let nine = zero + 9 in if code >= zero && code <= nine then ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] |> Array.fromList diff --git a/src/Update/Update.elm b/src/Update/Update.elm index 2cb45cb..5e8c13e 100644 --- a/src/Update/Update.elm +++ b/src/Update/Update.elm @@ -98,7 +98,7 @@ update action model = ReadOnly -> { model | timerEdition <- Nothing } KeyPressed keyCode -> - { model | timerEdition <- Maybe.map (updateTimerEdition (keyCodeToChar keyCode)) model.timerEdition } + { model | timerEdition <- Maybe.map (updateTimerEdition (keyCodeToNumberChar keyCode)) model.timerEdition } updateTimerTime : TimerEdition -> Dict Id Timer -> Dict Id Timer updateTimerTime timerEdition = diff --git a/src/Utils/Maybe.elm b/src/Utils/Maybe.elm index 25d02e7..355ded9 100644 --- a/src/Utils/Maybe.elm +++ b/src/Utils/Maybe.elm @@ -1,5 +1,6 @@ module Utils.Maybe ( filterMaybe + , orElse ) where filterMaybe : (a -> Bool) -> Maybe a -> Maybe a @@ -11,3 +12,9 @@ filterMaybe cond maybe = else Nothing Nothing -> Nothing + +orElse : Maybe a -> Maybe a -> Maybe a +orElse mb1 mb2 = + case mb1 of + Just x -> Just x + Nothing -> mb2 |