-
Notifications
You must be signed in to change notification settings - Fork 4
/
Utils.elm
67 lines (52 loc) · 1.28 KB
/
Utils.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module Utils
( unsafeExtract
, randomListItem
, queryParams
, dictGetInt
, listGet
) where
import UrlParameterParser exposing (ParseResult(..), parseSearchString)
import Random exposing (Seed)
import Dict exposing (Dict)
import String
import Debug
unsafeExtract : Maybe a -> a
unsafeExtract maybe =
case maybe of
Just a ->
a
_ ->
Debug.crash "unsafeExtract failed"
randomListItem : Seed -> List a -> (a, Seed)
randomListItem seed list =
let
gen = Random.int 0 ((List.length list) - 1)
(randomIndex, newSeed) = Random.generate gen seed
randomItem = List.drop randomIndex list
|> List.head
|> unsafeExtract
in
(randomItem, newSeed)
queryParams : String -> Dict String String
queryParams locationSearch =
case (parseSearchString locationSearch) of
Error _ ->
Dict.empty
UrlParams dict ->
dict
dictGetInt : String -> Int -> Int -> Int -> Dict String String -> Int
dictGetInt key default min max dict =
case Dict.get key dict of
Nothing ->
default
Just value ->
case String.toInt value of
Err _ ->
default
Ok intValue ->
clamp min max intValue
listGet : Int -> List a -> a
listGet index list =
List.drop index list
|> List.head
|> unsafeExtract