implemented month

This commit is contained in:
Jonathan Lamothe 2019-09-02 10:29:32 -04:00
parent 8b88cbff75
commit f0e0d644d2
2 changed files with 29 additions and 0 deletions

View File

@ -24,6 +24,7 @@ module Mtlstats.Format
, left , left
, right , right
, centre , centre
, month
) where ) where
-- | Pad an 'Int' with leading zeroes to fit a certain character width -- | 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 pLen = (n - sLen) `div` 2
pad = replicate pLen ' ' pad = replicate pLen ' '
in take n $ pad ++ str ++ repeat ' ' 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 _ = ""

View File

@ -31,6 +31,7 @@ spec = describe "Mtlstats.Format" $ do
leftSpec leftSpec
rightSpec rightSpec
centreSpec centreSpec
monthSpec
padNumSpec :: Spec padNumSpec :: Spec
padNumSpec = describe "padNum" $ do padNumSpec = describe "padNum" $ do
@ -87,3 +88,14 @@ centreSpec = describe "centre" $ do
context "overflow" $ context "overflow" $
it "should truncate the text" $ it "should truncate the text" $
centre 2 "foo" `shouldBe` "fo" 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` ""