-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.elm
36 lines (24 loc) · 909 Bytes
/
Util.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
port module Util exposing ((<$>), (<**>), label, onChange, pretty, toPrecision)
import Html
import Html.Attributes exposing (attribute, value, selected)
import Html.Events as Html
import Json.Decode as Json
import Time exposing (Time, second)
import String exposing (slice, padRight)
(<$>) : (a -> b) -> Maybe a -> Maybe b
(<$>) = Maybe.map
(<**>) : Maybe a -> (a -> b) -> Maybe b
(<**>) = flip (<$>)
label : String -> Html.Attribute a
label = attribute "label"
onChange : (String -> a) -> Html.Attribute a
onChange tg = Html.on "change" (Json.map tg Html.targetValue)
pretty : Time -> String
pretty time =
let x = (toFloat <| floor (time / 10)) / 100
in toPrecision 2 x
floatPart : Float -> Float
floatPart i = i - (toFloat <| truncate i)
toPrecision : Int -> Float -> String
toPrecision n x =
(toString <| truncate x) ++ "." ++ (padRight n '0' <| slice 2 (2+n) <| toString <| floatPart x)