diff --git a/src/Mtlstats/Format.hs b/src/Mtlstats/Format.hs index 050fe0d..72c5565 100644 --- a/src/Mtlstats/Format.hs +++ b/src/Mtlstats/Format.hs @@ -24,6 +24,7 @@ module Mtlstats.Format , left , right , centre + , month ) where -- | Pad an 'Int' with leading zeroes to fit a certain character width @@ -72,3 +73,19 @@ centre n str = let pLen = (n - sLen) `div` 2 pad = replicate pLen ' ' in take n $ pad ++ str ++ repeat ' ' + +-- | Converts a number to a three character month (e.g. @"JAN"@) +month :: Int -> String +month 1 = "JAN" +month 2 = "FEB" +month 3 = "MAR" +month 4 = "APR" +month 5 = "MAY" +month 6 = "JUN" +month 7 = "JUL" +month 8 = "AUG" +month 9 = "SEP" +month 10 = "OCT" +month 11 = "NOV" +month 12 = "DEC" +month _ = "" diff --git a/test/FormatSpec.hs b/test/FormatSpec.hs index c4d8484..1177376 100644 --- a/test/FormatSpec.hs +++ b/test/FormatSpec.hs @@ -31,6 +31,7 @@ spec = describe "Mtlstats.Format" $ do leftSpec rightSpec centreSpec + monthSpec padNumSpec :: Spec padNumSpec = describe "padNum" $ do @@ -87,3 +88,14 @@ centreSpec = describe "centre" $ do context "overflow" $ it "should truncate the text" $ centre 2 "foo" `shouldBe` "fo" + +monthSpec :: Spec +monthSpec = describe "month" $ do + + context "January" $ + it "should return \"JAN\"" $ + month 1 `shouldBe` "JAN" + + context "invalid" $ + it "should return an empty string" $ + month 0 `shouldBe` ""