Using elm-format for all files

This commit is contained in:
Eike Kettner
2019-12-29 21:55:12 +01:00
parent 546f1a6ee3
commit 2001cca88b
84 changed files with 7668 additions and 5079 deletions

View File

@ -1,8 +1,9 @@
module Util.Address exposing (..)
module Util.Address exposing (toString)
import Api.Model.Address exposing (Address)
toString: Address -> String
toString : Address -> String
toString a =
[ a.street, a.zip, a.city, a.country ]
|> List.filter (String.isEmpty >> not)

View File

@ -1,8 +1,9 @@
module Util.Contact exposing (..)
module Util.Contact exposing (toString)
import Api.Model.Contact exposing (Contact)
toString: List Contact -> String
toString : List Contact -> String
toString contacts =
List.map (\c -> c.kind ++ ": " ++ c.value) contacts
|> List.intersperse ", "

View File

@ -2,46 +2,65 @@ module Util.Duration exposing (Duration, toHuman)
-- 486ms -> 12s -> 1:05 -> 59:45 -> 1:02:12
type alias Duration = Int
toHuman: Duration -> String
type alias Duration =
Int
toHuman : Duration -> String
toHuman dur =
fromMillis dur
-- implementation
fromMillis: Int -> String
fromMillis : Int -> String
fromMillis ms =
case ms // 1000 of
0 ->
(String.fromInt ms) ++ "ms"
String.fromInt ms ++ "ms"
n ->
fromSeconds n
fromSeconds: Int -> String
fromSeconds : Int -> String
fromSeconds sec =
case sec // 60 of
0 ->
(String.fromInt sec) ++ "s"
String.fromInt sec ++ "s"
n ->
let
s = sec - (n * 60)
s =
sec - (n * 60)
in
(fromMinutes n) ++ ":" ++ (num s)
fromMinutes n ++ ":" ++ num s
fromMinutes: Int -> String
fromMinutes : Int -> String
fromMinutes min =
case min // 60 of
0 ->
(num min)
num min
n ->
let
m = min - (n * 60)
m =
min - (n * 60)
in
(num n) ++ ":" ++ (num m)
num n ++ ":" ++ num m
num: Int -> String
num : Int -> String
num n =
String.fromInt n
|> (++) (if n < 10 then "0" else "")
|> (++)
(if n < 10 then
"0"
else
""
)

View File

@ -1,9 +1,12 @@
module Util.File exposing (..)
module Util.File exposing (makeFileId)
import File exposing (File)
import Util.String
makeFileId: File -> String
makeFileId : File -> String
makeFileId file =
(File.name file) ++ "-" ++ (File.size file |> String.fromInt)
File.name file
++ "-"
++ (File.size file |> String.fromInt)
|> Util.String.crazyEncode

View File

@ -1,10 +1,17 @@
module Util.Html exposing (..)
module Util.Html exposing
( KeyCode(..)
, classActive
, intToKeyCode
, onClickk
, onKeyUp
)
import Html exposing (Attribute)
import Html.Attributes exposing (class)
import Html.Events exposing (on, keyCode)
import Html.Events exposing (keyCode, on)
import Json.Decode as Decode
type KeyCode
= Up
| Down
@ -12,29 +19,52 @@ type KeyCode
| Right
| Enter
intToKeyCode: Int -> Maybe KeyCode
intToKeyCode : Int -> Maybe KeyCode
intToKeyCode code =
case code of
38 -> Just Up
40 -> Just Down
39 -> Just Right
37 -> Just Left
13 -> Just Enter
_ -> Nothing
38 ->
Just Up
40 ->
Just Down
39 ->
Just Right
37 ->
Just Left
13 ->
Just Enter
_ ->
Nothing
onKeyUp : (Int -> msg) -> Attribute msg
onKeyUp tagger =
on "keyup" (Decode.map tagger keyCode)
on "keyup" (Decode.map tagger keyCode)
onClickk : msg -> Attribute msg
onClickk msg =
Html.Events.preventDefaultOn "click" (Decode.map alwaysPreventDefault (Decode.succeed msg))
Html.Events.preventDefaultOn "click" (Decode.map alwaysPreventDefault (Decode.succeed msg))
alwaysPreventDefault : msg -> ( msg, Bool )
alwaysPreventDefault msg =
( msg, True )
( msg, True )
classActive: Bool -> String -> Attribute msg
classActive : Bool -> String -> Attribute msg
classActive active classes =
class (classes ++ (if active then " active" else ""))
class
(classes
++ (if active then
" active"
else
""
)
)

View File

@ -1,37 +1,55 @@
module Util.Http exposing (..)
module Util.Http exposing
( authDelete
, authGet
, authPost
, authPostTrack
, authPut
, authTask
, errorToString
, executeIn
, jsonResolver
)
import Api.Model.AuthResult exposing (AuthResult)
import Http
import Json.Decode as D
import Process
import Task exposing (Task)
import Api.Model.AuthResult exposing (AuthResult)
import Json.Decode as D
-- Authenticated Requests
authReq: {url: String
,account: AuthResult
,method: String
,headers: List Http.Header
,body: Http.Body
,expect: Http.Expect msg
,tracker: Maybe String
} -> Cmd msg
authReq :
{ url : String
, account : AuthResult
, method : String
, headers : List Http.Header
, body : Http.Body
, expect : Http.Expect msg
, tracker : Maybe String
}
-> Cmd msg
authReq req =
Http.request
{ url = req.url
, method = req.method
, headers = (Http.header "X-Docspell-Auth" (Maybe.withDefault "" req.account.token)) :: req.headers
, headers = Http.header "X-Docspell-Auth" (Maybe.withDefault "" req.account.token) :: req.headers
, expect = req.expect
, body = req.body
, timeout = Nothing
, tracker = req.tracker
}
authPost: {url: String
,account: AuthResult
,body: Http.Body
,expect: Http.Expect msg
} -> Cmd msg
authPost :
{ url : String
, account : AuthResult
, body : Http.Body
, expect : Http.Expect msg
}
-> Cmd msg
authPost req =
authReq
{ url = req.url
@ -43,12 +61,15 @@ authPost req =
, tracker = Nothing
}
authPostTrack: {url: String
,account: AuthResult
,body: Http.Body
,expect: Http.Expect msg
,tracker: String
} -> Cmd msg
authPostTrack :
{ url : String
, account : AuthResult
, body : Http.Body
, expect : Http.Expect msg
, tracker : String
}
-> Cmd msg
authPostTrack req =
authReq
{ url = req.url
@ -60,11 +81,14 @@ authPostTrack req =
, tracker = Just req.tracker
}
authPut: {url: String
,account: AuthResult
,body: Http.Body
,expect: Http.Expect msg
} -> Cmd msg
authPut :
{ url : String
, account : AuthResult
, body : Http.Body
, expect : Http.Expect msg
}
-> Cmd msg
authPut req =
authReq
{ url = req.url
@ -76,10 +100,13 @@ authPut req =
, tracker = Nothing
}
authGet: {url: String
,account: AuthResult
,expect: Http.Expect msg
} -> Cmd msg
authGet :
{ url : String
, account : AuthResult
, expect : Http.Expect msg
}
-> Cmd msg
authGet req =
authReq
{ url = req.url
@ -91,10 +118,13 @@ authGet req =
, tracker = Nothing
}
authDelete: {url: String
,account: AuthResult
,expect: Http.Expect msg
} -> Cmd msg
authDelete :
{ url : String
, account : AuthResult
, expect : Http.Expect msg
}
-> Cmd msg
authDelete req =
authReq
{ url = req.url
@ -110,69 +140,81 @@ authDelete req =
-- Error Utilities
errorToStringStatus: Http.Error -> (Int -> String) -> String
errorToStringStatus : Http.Error -> (Int -> String) -> String
errorToStringStatus error statusString =
case error of
Http.BadUrl url ->
"There is something wrong with this url: " ++ url
Http.Timeout ->
"There was a network timeout."
Http.NetworkError ->
"There was a network error."
Http.BadStatus status ->
statusString status
Http.BadBody str ->
"There was an error decoding the response: " ++ str
errorToString: Http.Error -> String
errorToString : Http.Error -> String
errorToString error =
let
f sc = case sc of
404 ->
"The requested resource doesn't exist."
_ ->
"There was an invalid response status: " ++ (String.fromInt sc)
f sc =
case sc of
404 ->
"The requested resource doesn't exist."
_ ->
"There was an invalid response status: " ++ String.fromInt sc
in
errorToStringStatus error f
errorToStringStatus error f
-- Http.Task Utilities
jsonResolver : D.Decoder a -> Http.Resolver Http.Error a
jsonResolver : D.Decoder a -> Http.Resolver Http.Error a
jsonResolver decoder =
Http.stringResolver <|
\response ->
case response of
Http.BadUrl_ url ->
Err (Http.BadUrl url)
Http.stringResolver <|
\response ->
case response of
Http.BadUrl_ url ->
Err (Http.BadUrl url)
Http.Timeout_ ->
Err Http.Timeout
Http.Timeout_ ->
Err Http.Timeout
Http.NetworkError_ ->
Err Http.NetworkError
Http.NetworkError_ ->
Err Http.NetworkError
Http.BadStatus_ metadata body ->
Err (Http.BadStatus metadata.statusCode)
Http.BadStatus_ metadata body ->
Err (Http.BadStatus metadata.statusCode)
Http.GoodStatus_ metadata body ->
case D.decodeString decoder body of
Ok value ->
Ok value
Http.GoodStatus_ metadata body ->
case D.decodeString decoder body of
Ok value ->
Ok value
Err err ->
Err (Http.BadBody (D.errorToString err))
Err err ->
Err (Http.BadBody (D.errorToString err))
executeIn: Float -> ((Result Http.Error a) -> msg) -> Task Http.Error a -> Cmd msg
executeIn : Float -> (Result Http.Error a -> msg) -> Task Http.Error a -> Cmd msg
executeIn delay receive task =
Process.sleep delay
|> Task.andThen (\_ -> task)
|> Task.attempt receive
authTask:
authTask :
{ method : String
, headers : List Http.Header
, account: AuthResult
, account : AuthResult
, url : String
, body : Http.Body
, resolver : Http.Resolver x a
@ -182,7 +224,7 @@ authTask:
authTask req =
Http.task
{ method = req.method
, headers = (Http.header "X-Docspell-Auth" (Maybe.withDefault "" req.account.token)) :: req.headers
, headers = Http.header "X-Docspell-Auth" (Maybe.withDefault "" req.account.token) :: req.headers
, url = req.url
, body = req.body
, resolver = req.resolver

View File

@ -1,51 +1,80 @@
module Util.List exposing ( find
, findIndexed
, get
, distinct
, findNext
, findPrev
)
module Util.List exposing
( distinct
, find
, findIndexed
, findNext
, findPrev
, get
)
get: List a -> Int -> Maybe a
get : List a -> Int -> Maybe a
get list index =
if index < 0 then Nothing
else case list of
[] ->
Nothing
x :: xs ->
if index == 0
then Just x
else get xs (index - 1)
if index < 0 then
Nothing
find: (a -> Bool) -> List a -> Maybe a
else
case list of
[] ->
Nothing
x :: xs ->
if index == 0 then
Just x
else
get xs (index - 1)
find : (a -> Bool) -> List a -> Maybe a
find pred list =
findIndexed pred list |> Maybe.map Tuple.first
findIndexed: (a -> Bool) -> List a -> Maybe (a, Int)
findIndexed : (a -> Bool) -> List a -> Maybe ( a, Int )
findIndexed pred list =
findIndexed1 pred list 0
findIndexed1: (a -> Bool) -> List a -> Int -> Maybe (a, Int)
findIndexed1 : (a -> Bool) -> List a -> Int -> Maybe ( a, Int )
findIndexed1 pred list index =
case list of
[] -> Nothing
x :: xs ->
if pred x then Just (x, index)
else findIndexed1 pred xs (index + 1)
[] ->
Nothing
distinct: List a -> List a
x :: xs ->
if pred x then
Just ( x, index )
else
findIndexed1 pred xs (index + 1)
distinct : List a -> List a
distinct list =
List.reverse <|
List.foldl (\a -> \r -> if (List.member a r) then r else a :: r) [] list
List.foldl
(\a ->
\r ->
if List.member a r then
r
findPrev: (a -> Bool) -> List a -> Maybe a
else
a :: r
)
[]
list
findPrev : (a -> Bool) -> List a -> Maybe a
findPrev pred list =
findIndexed pred list
|> Maybe.map Tuple.second
|> Maybe.map (\i -> i - 1)
|> Maybe.andThen (get list)
findNext: (a -> Bool) -> List a -> Maybe a
findNext : (a -> Bool) -> List a -> Maybe a
findNext pred list =
findIndexed pred list
|> Maybe.map Tuple.second

View File

@ -1,23 +1,40 @@
module Util.Maybe exposing (..)
module Util.Maybe exposing
( isEmpty
, nonEmpty
, or
, withDefault
)
nonEmpty: Maybe a -> Bool
nonEmpty : Maybe a -> Bool
nonEmpty ma =
Maybe.map (\_ -> True) ma
|> Maybe.withDefault False
not (isEmpty ma)
isEmpty: Maybe a -> Bool
isEmpty : Maybe a -> Bool
isEmpty ma =
not (nonEmpty ma)
ma == Nothing
withDefault: Maybe a -> Maybe a -> Maybe a
withDefault : Maybe a -> Maybe a -> Maybe a
withDefault ma1 ma2 =
if isEmpty ma2 then ma1 else ma2
if isEmpty ma2 then
ma1
or: List (Maybe a) -> Maybe a
else
ma2
or : List (Maybe a) -> Maybe a
or listma =
case listma of
[] -> Nothing
[] ->
Nothing
el :: els ->
case el of
Just _ -> el
Nothing -> or els
Just _ ->
el
Nothing ->
or els

View File

@ -1,24 +1,60 @@
module Util.Size exposing (..)
module Util.Size exposing
( SizeUnit(..)
, bytesReadable
)
type SizeUnit = G|M|K|B
prettyNumber: Float -> String
type SizeUnit
= G
| M
| K
| B
prettyNumber : Float -> String
prettyNumber n =
let
parts = String.split "." (String.fromFloat n)
parts =
String.split "." (String.fromFloat n)
in
case parts of
n0 :: d :: [] -> n0 ++ "." ++ (String.left 2 d)
_ -> String.join "." parts
case parts of
n0 :: d :: [] ->
n0 ++ "." ++ String.left 2 d
bytesReadable: SizeUnit -> Float -> String
_ ->
String.join "." parts
bytesReadable : SizeUnit -> Float -> String
bytesReadable unit n =
let
k = n / 1024
num = prettyNumber n
k =
n / 1024
num =
prettyNumber n
in
case unit of
G -> num ++ "G"
M -> if k > 1 then (bytesReadable G k) else num ++ "M"
K -> if k > 1 then (bytesReadable M k) else num ++ "K"
B -> if k > 1 then (bytesReadable K k) else num ++ "B"
G ->
num ++ "G"
M ->
if k > 1 then
bytesReadable G k
else
num ++ "M"
K ->
if k > 1 then
bytesReadable M k
else
num ++ "K"
B ->
if k > 1 then
bytesReadable K k
else
num ++ "B"

View File

@ -1,28 +1,45 @@
module Util.String exposing (..)
module Util.String exposing
( crazyEncode
, ellipsis
, withDefault
)
import Base64
crazyEncode: String -> String
crazyEncode : String -> String
crazyEncode str =
let
b64 = Base64.encode str
len = String.length b64
b64 =
Base64.encode str
len =
String.length b64
in
case (String.right 2 b64 |> String.toList) of
'=' :: '=' :: [] ->
(String.dropRight 2 b64) ++ "0"
case String.right 2 b64 |> String.toList of
'=' :: '=' :: [] ->
String.dropRight 2 b64 ++ "0"
_ :: '=' :: [] ->
(String.dropRight 1 b64) ++ "1"
_ :: '=' :: [] ->
String.dropRight 1 b64 ++ "1"
_ ->
b64
_ ->
b64
ellipsis: Int -> String -> String
ellipsis : Int -> String -> String
ellipsis len str =
if String.length str <= len then str
else (String.left (len - 3) str) ++ "..."
if String.length str <= len then
str
withDefault: String -> String -> String
else
String.left (len - 3) str ++ "..."
withDefault : String -> String -> String
withDefault default str =
if str == "" then default else str
if str == "" then
default
else
str

View File

@ -1,4 +1,9 @@
module Util.Time exposing (..)
module Util.Time exposing
( formatDate
, formatDateShort
, formatDateTime
, formatIsoDateTime
)
import DateFormat
import Time exposing (Posix, Zone, utc)
@ -16,7 +21,8 @@ dateFormatter =
, DateFormat.yearNumber
]
dateFormatterShort: Zone -> Posix -> String
dateFormatterShort : Zone -> Posix -> String
dateFormatterShort =
DateFormat.format
[ DateFormat.yearNumber
@ -26,7 +32,8 @@ dateFormatterShort =
, DateFormat.dayOfMonthFixed
]
timeFormatter: Zone -> Posix -> String
timeFormatter : Zone -> Posix -> String
timeFormatter =
DateFormat.format
[ DateFormat.hourMilitaryNumber
@ -34,7 +41,8 @@ timeFormatter =
, DateFormat.minuteFixed
]
isoDateTimeFormatter: Zone -> Posix -> String
isoDateTimeFormatter : Zone -> Posix -> String
isoDateTimeFormatter =
DateFormat.format
[ DateFormat.yearNumber
@ -51,37 +59,49 @@ isoDateTimeFormatter =
]
timeZone: Zone
timeZone : Zone
timeZone =
utc
{- Format millis into "Wed, 10. Jan 2018, 18:57"
-}
formatDateTime: Int -> String
formatDateTime millis =
(formatDate millis) ++ ", " ++ (formatTime millis)
formatIsoDateTime: Int -> String
{- Format millis into "Wed, 10. Jan 2018, 18:57" -}
formatDateTime : Int -> String
formatDateTime millis =
formatDate millis ++ ", " ++ formatTime millis
formatIsoDateTime : Int -> String
formatIsoDateTime millis =
Time.millisToPosix millis
|> isoDateTimeFormatter timeZone
{- Format millis into "18:57". The current time (not the duration of
the millis).
-}
formatTime: Int -> String
formatTime : Int -> String
formatTime millis =
Time.millisToPosix millis
|> timeFormatter timeZone
{- Format millis into "Wed, 10. Jan 2018"
-}
formatDate: Int -> String
{- Format millis into "Wed, 10. Jan 2018" -}
formatDate : Int -> String
formatDate millis =
Time.millisToPosix millis
|> dateFormatter timeZone
formatDateShort: Int -> String
formatDateShort : Int -> String
formatDateShort millis =
Time.millisToPosix millis
|> dateFormatterShort timeZone

View File

@ -1,15 +1,18 @@
module Util.Update exposing (..)
module Util.Update exposing (andThen1)
andThen1: List (a -> (a, Cmd b)) -> a -> (a, Cmd b)
andThen1 : List (a -> ( a, Cmd b )) -> a -> ( a, Cmd b )
andThen1 fs a =
let
init = (a, [])
init =
( a, [] )
update el tuple =
let
(a2, c2) = el (Tuple.first tuple)
( a2, c2 ) =
el (Tuple.first tuple)
in
(a2, c2 :: (Tuple.second tuple))
( a2, c2 :: Tuple.second tuple )
in
List.foldl update init fs
|> Tuple.mapSecond Cmd.batch
List.foldl update init fs
|> Tuple.mapSecond Cmd.batch