Przeglądaj źródła

make the prettyDate implementation generic

there is a `RelativeUnit` type now, which is pattern-matched on and then
the result of that is used in the generic formatting.
Lucas Stadler 11 lat temu
rodzic
commit
a3ab85f33a
1 zmienionych plików z 39 dodań i 39 usunięć
  1. 39 39
      elm/PrettyDate.elm

+ 39 - 39
elm/PrettyDate.elm

@ -10,39 +10,39 @@ prettyDate reference date =
10 10
        diffSeconds = referenceSeconds - dateSeconds
11 11
    in format << toRelativeTime <| diffSeconds
12 12
13
type RelativeTime = JustNow
14
                  | Minute Int
15
                  | Hour Int
16
                  | Day Int
17
                  | Week Int
18
                  | Month Int
19
                  | Year Int
20
                  | Future RelativeTime
13
type RelativeUnit = JustNow
14
                  | Minute
15
                  | Hour
16
                  | Day
17
                  | Week
18
                  | Month
19
                  | Year
20
21
type RelativeTime = Past RelativeUnit Int
22
                  | Future RelativeUnit Int
23
24
stringForm relativeUnit =
25
    case relativeUnit of
26
      Minute  -> ("a minute", "minutes")
27
      Hour    -> ("an hour", "hours")
28
      Day     -> ("a day", "days")
29
      Week    -> ("a week", "weeks")
30
      Month   -> ("a month", "months")
31
      Year    -> ("a year", "years")
21 32
22 33
format : RelativeTime -> String
23 34
format relativeTime =
24 35
    case relativeTime of
25
      JustNow -> "moments ago"
26
      Minute 1 -> "a minute ago"
27
      Minute n -> toString n ++ " minutes ago"
28
      Hour 1 -> "hour ago"
29
      Hour n -> toString n ++ " hours ago"
30
      Day 1 -> "a day ago"
31
      Day n -> toString n ++ " years ago"
32
      Week 1 -> "a week ago"
33
      Week n -> toString n ++ " weeks ago"
34
      Month 1 -> "a month ago"
35
      Month n -> toString n ++ " months ago"
36
      Year 1 -> "a year ago"
37
      Year n -> toString n ++ "years ago"
38
      Future (JustNow) -> "in a moment"
39
      Future (Minute n) -> "in " ++ toString n ++ " hours"
40
      Future (Hour n) -> "in " ++ toString n ++ " hours"
41
      Future (Day n) -> "in " ++ toString n ++ " days"
42
      Future (Week n) -> "in " ++ toString n ++ " weeks"
43
      Future (Month n) -> "in " ++ toString n ++ " months"
44
      Future (Year n) -> "in " ++ toString n ++ " years"
45
      _ -> "???"
36
      Past   JustNow _ -> "moments ago"
37
      Future JustNow _ -> "in a moment"
38
      Past   unit n    -> format' ""    " ago" unit n
39
      Future unit n    -> format' "in " ""     unit n
40
41
format' prefix suffix unit n =
42
    let (singular, plural) = stringForm unit
43
    in if n == 1
44
       then prefix ++ singular
45
       else prefix ++ toString n ++ " " ++ plural ++ suffix
46 46
47 47
toRelativeTime : Float -> RelativeTime
48 48
toRelativeTime origDiff =
@ -55,14 +55,14 @@ toRelativeTime origDiff =
55 55
        roundToString = toString << ceiling
56 56
        inThePast = origDiff > 0
57 57
        diff = abs origDiff
58
        relativeTime =
59
            if | diff < minute -> JustNow
60
               | diff < hour   -> Minute <| round (diff / minute)
61
               | diff < day    -> Hour <| round (diff / hour)
62
               | diff < week   -> Day <| round (diff / day)
63
               | diff < month  -> Week <| round (diff / week)
64
               | diff < year   -> Month <| round (diff / month)
65
               | otherwise     -> Year <| round (diff / year)
58
        (relativeUnit, n) =
59
            if | diff < minute -> (JustNow, 0)
60
               | diff < hour   -> (Minute, round (diff / minute))
61
               | diff < day    -> (Hour, round (diff / hour))
62
               | diff < week   -> (Day, round (diff / day))
63
               | diff < month  -> (Week, round (diff / week))
64
               | diff < year   -> (Month, round (diff / month))
65
               | otherwise     -> (Year, round (diff / year))
66 66
    in if inThePast
67
       then relativeTime
68
       else Future relativeTime
67
       then Past relativeUnit n
68
       else Future relativeUnit n