diff options
author | Joris | 2017-03-26 21:10:42 +0200 |
---|---|---|
committer | Joris | 2017-03-26 21:10:42 +0200 |
commit | 1e47a7754ca38bd1a6c74765d8378caf68ce4619 (patch) | |
tree | d0d9238479dc2529a1b558bbbcde346e7e8c2935 /src/client/Utils | |
parent | c0ac16a713c4e53cf6af8e72a6d5f6b8ac5d6456 (diff) |
Separate client and server watch
Diffstat (limited to 'src/client/Utils')
-rw-r--r-- | src/client/Utils/Cmd.elm | 16 | ||||
-rw-r--r-- | src/client/Utils/Dict.elm | 11 | ||||
-rw-r--r-- | src/client/Utils/Either.elm | 9 | ||||
-rw-r--r-- | src/client/Utils/Form.elm | 13 | ||||
-rw-r--r-- | src/client/Utils/Http.elm | 39 | ||||
-rw-r--r-- | src/client/Utils/Json.elm | 12 | ||||
-rw-r--r-- | src/client/Utils/List.elm | 17 | ||||
-rw-r--r-- | src/client/Utils/Maybe.elm | 34 | ||||
-rw-r--r-- | src/client/Utils/Search.elm | 10 | ||||
-rw-r--r-- | src/client/Utils/String.elm | 38 |
10 files changed, 199 insertions, 0 deletions
diff --git a/src/client/Utils/Cmd.elm b/src/client/Utils/Cmd.elm new file mode 100644 index 0000000..5f41cbe --- /dev/null +++ b/src/client/Utils/Cmd.elm @@ -0,0 +1,16 @@ +module Utils.Cmd exposing + ( pipeUpdate + , (:>) + ) + +import Platform.Cmd as Cmd + +pipeUpdate : (model, Cmd msg) -> (model -> (model, Cmd msg)) -> (model, Cmd msg) +pipeUpdate (model, cmd) f = + let (newModel, newCmd) = f model + in (newModel, Cmd.batch [ cmd, newCmd ]) + +(:>) : (m, Cmd a) -> (m -> (m, Cmd a)) -> (m, Cmd a) +(:>) = pipeUpdate + +infixl 0 :> diff --git a/src/client/Utils/Dict.elm b/src/client/Utils/Dict.elm new file mode 100644 index 0000000..7d708e2 --- /dev/null +++ b/src/client/Utils/Dict.elm @@ -0,0 +1,11 @@ +module Utils.Dict exposing + ( mapValues + ) + +import Dict as Dict exposing (..) + +mapValues : (a -> b) -> Dict comparable a -> Dict comparable b +mapValues f = Dict.fromList << List.map (onSecond f) << Dict.toList + +onSecond : (a -> b) -> (comparable, a) -> (comparable, b) +onSecond f tuple = case tuple of (x, y) -> (x, f y) diff --git a/src/client/Utils/Either.elm b/src/client/Utils/Either.elm new file mode 100644 index 0000000..275fc8c --- /dev/null +++ b/src/client/Utils/Either.elm @@ -0,0 +1,9 @@ +module Utils.Either exposing + ( toMaybeError + ) + +toMaybeError : Result a b -> Maybe a +toMaybeError result = + case result of + Ok _ -> Nothing + Err x -> Just x diff --git a/src/client/Utils/Form.elm b/src/client/Utils/Form.elm new file mode 100644 index 0000000..8d75a32 --- /dev/null +++ b/src/client/Utils/Form.elm @@ -0,0 +1,13 @@ +module Utils.Form exposing + ( fieldAsText + ) + +import Form exposing (Form) + +import Model.Payment exposing (Frequency(..)) + +fieldAsText : Form a b -> String -> String +fieldAsText form field = + Form.getFieldAsString field form + |> .value + |> Maybe.withDefault "" diff --git a/src/client/Utils/Http.elm b/src/client/Utils/Http.elm new file mode 100644 index 0000000..dd3870a --- /dev/null +++ b/src/client/Utils/Http.elm @@ -0,0 +1,39 @@ +module Utils.Http exposing + ( jsonRequest + , request + , errorKey + ) + +import Http exposing (..) +import Task exposing (..) +import Json.Decode as Decode exposing (Decoder, Value) +import Json.Encode as Encode + +jsonRequest : String -> String -> Expect a -> (Result Error a -> msg) -> Encode.Value -> Cmd msg +jsonRequest method url expect handleResult value = + requestWithBody method url (jsonBody value) expect handleResult + +request : String -> String -> Expect a -> (Result Error a -> msg) -> Cmd msg +request method url = requestWithBody method url emptyBody + +requestWithBody : String -> String -> Body -> Expect a -> (Result Error a -> msg) -> Cmd msg +requestWithBody method url body expect handleResult = + let req = Http.request + { method = method + , headers = [] + , url = url + , body = body + , expect = expect + , timeout = Nothing + , withCredentials = False + } + in send handleResult req + +errorKey : Error -> String +errorKey error = + case error of + BadUrl _ -> "BadUrl" + Timeout -> "Timeout" + NetworkError -> "NetworkError" + BadPayload _ _ -> "BadPayload" + BadStatus response -> response.body diff --git a/src/client/Utils/Json.elm b/src/client/Utils/Json.elm new file mode 100644 index 0000000..29e815b --- /dev/null +++ b/src/client/Utils/Json.elm @@ -0,0 +1,12 @@ +module Utils.Json exposing + ( dictDecoder + ) + +import Json.Decode as Decode exposing (Decoder) +import Dict exposing (Dict) + +dictDecoder : Decoder comparable -> Decoder a -> Decoder (Dict comparable a) +dictDecoder keyDecoder valueDecoder = + Decode.map2 (,) keyDecoder valueDecoder + |> Decode.list + |> Decode.map Dict.fromList diff --git a/src/client/Utils/List.elm b/src/client/Utils/List.elm new file mode 100644 index 0000000..cc57d9f --- /dev/null +++ b/src/client/Utils/List.elm @@ -0,0 +1,17 @@ +module Utils.List exposing + ( groupBy + , mean + ) + +import Dict + +groupBy : (a -> comparable) -> List a -> List (comparable, List a) +groupBy f xs = + let addItem item dict = + let groupItems = Dict.get (f item) dict |> Maybe.withDefault [] + in Dict.insert (f item) (item :: groupItems) dict + in List.foldr addItem Dict.empty xs + |> Dict.toList + +mean : List Int -> Int +mean xs = (List.sum xs) // (List.length xs) diff --git a/src/client/Utils/Maybe.elm b/src/client/Utils/Maybe.elm new file mode 100644 index 0000000..46456e1 --- /dev/null +++ b/src/client/Utils/Maybe.elm @@ -0,0 +1,34 @@ +module Utils.Maybe exposing + ( isJust + , cat + , toList + , orElse + ) + +isJust : Maybe a -> Bool +isJust maybe = + case maybe of + Just _ -> True + Nothing -> False + +cat : List (Maybe a) -> List a +cat = + List.foldr + (\mb xs -> + case mb of + Just x -> x :: xs + Nothing -> xs + ) + [] + +toList : Maybe a -> List a +toList mb = + case mb of + Just a -> [a] + Nothing -> [] + +orElse : Maybe a -> Maybe a -> Maybe a +orElse mb1 mb2 = + case mb1 of + Just x -> Just x + Nothing -> mb2 diff --git a/src/client/Utils/Search.elm b/src/client/Utils/Search.elm new file mode 100644 index 0000000..1b70387 --- /dev/null +++ b/src/client/Utils/Search.elm @@ -0,0 +1,10 @@ +module Utils.Search exposing + ( format + ) + +import String + +import Utils.String as String + +format : String -> String +format = String.unaccent << String.toLower diff --git a/src/client/Utils/String.elm b/src/client/Utils/String.elm new file mode 100644 index 0000000..90fe68e --- /dev/null +++ b/src/client/Utils/String.elm @@ -0,0 +1,38 @@ +module Utils.String exposing + ( unaccent + ) + +unaccent : String -> String +unaccent = String.map unaccentChar + +unaccentChar : Char -> Char +unaccentChar c = case c of + 'à' -> 'a' + 'á' -> 'a' + 'â' -> 'a' + 'ã' -> 'a' + 'ä' -> 'a' + 'ç' -> 'c' + 'è' -> 'e' + 'é' -> 'e' + 'ê' -> 'e' + 'ë' -> 'e' + 'ì' -> 'i' + 'í' -> 'i' + 'î' -> 'i' + 'ï' -> 'i' + 'ñ' -> 'n' + 'ò' -> 'o' + 'ó' -> 'o' + 'ô' -> 'o' + 'õ' -> 'o' + 'ö' -> 'o' + 'š' -> 's' + 'ù' -> 'u' + 'ú' -> 'u' + 'û' -> 'u' + 'ü' -> 'u' + 'ý' -> 'y' + 'ÿ' -> 'y' + 'ž' -> 'z' + _ -> c |