From: Michael Orlitzky Date: Fri, 13 Jun 2014 15:02:52 +0000 (-0400) Subject: Add the format_commas function and use it to implement the earnings pickler. X-Git-Tag: 0.0.6~212 X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=6062e1f8ae4d894699bb9d1f552b36af0151b5bb;p=dead%2Fhtsn-import.git Add the format_commas function and use it to implement the earnings pickler. Doctest TSN.Picklers. --- diff --git a/htsn-import.cabal b/htsn-import.cabal index b5b1517..57b00c8 100644 --- a/htsn-import.cabal +++ b/htsn-import.cabal @@ -87,6 +87,7 @@ executable htsn-import groundhog-th >= 0.5, MissingH >= 1.2, old-locale >= 1, + split >= 0.2, tasty >= 0.8, tasty-hunit >= 0.8, time >= 1.4, @@ -168,6 +169,7 @@ test-suite testsuite groundhog-th >= 0.5, MissingH >= 1.2, old-locale >= 1, + split >= 0.2, tasty >= 0.8, tasty-hunit >= 0.8, time >= 1.4, @@ -239,6 +241,7 @@ test-suite shelltests groundhog-th >= 0.5, MissingH >= 1.2, old-locale >= 1, + split >= 0.2, process >= 1.1, tasty >= 0.8, tasty-hunit >= 0.8, diff --git a/src/TSN/Picklers.hs b/src/TSN/Picklers.hs index 387f4cf..ff7b0d1 100644 --- a/src/TSN/Picklers.hs +++ b/src/TSN/Picklers.hs @@ -13,6 +13,8 @@ module TSN.Picklers ( where -- System imports. +import Data.List ( intercalate ) +import Data.List.Split ( chunksOf ) import Data.String.Utils ( replace ) import Data.Time.Clock ( NominalDiffTime, UTCTime, addUTCTime ) import Data.Time.Format ( formatTime, parseTime ) @@ -78,6 +80,31 @@ xp_date_padded = from_date = formatTime defaultTimeLocale date_format_padded + +-- | Format a number as a string using a comma as the thousands +-- separator. +-- +-- Examples: +-- +-- >>> format_commas 0 +-- "0" +-- >>> format_commas 10 +-- "10" +-- >>> format_commas 100 +-- "100" +-- >>> format_commas 1000 +-- "1,000" +-- >>> format_commas 10000 +-- "10,000" +-- >>> format_commas 100000 +-- "100,000" +-- >>> format_commas 1000000 +-- "1,000,000" +-- +format_commas :: Int -> String +format_commas x = + reverse (intercalate "," $ chunksOf 3 $ reverse $ show x) + -- | Parse \ from an 'AutoRaceResultsListing'. These are -- essentially 'Int's, but they look like, -- @@ -98,8 +125,8 @@ xp_earnings = | otherwise = (read . strip_commas . show) s from_earnings :: Maybe Int -> String - from_earnings Nothing = "" - from_earnings (Just i) = show i + from_earnings Nothing = "TBA" + from_earnings (Just i) = format_commas i -- | (Un)pickle a 'UTCTime' from a \ element in an diff --git a/test/Doctests.hs b/test/Doctests.hs index d76d272..af4bcb5 100644 --- a/test/Doctests.hs +++ b/test/Doctests.hs @@ -4,9 +4,10 @@ import Test.DocTest ( doctest ) -- | There's some kind of goddamned bug causing the doctests to commit -- suicide when run on src/Main.hs. Since we only have doctests in --- TSN.Codegen for now, just test that. +-- TSN.Codegen and TSN.Picklers for now, just test those. -- main :: IO () main = doctest [ "-isrc", "-idist/build/autogen", - "src/TSN/Codegen.hs" ] + "src/TSN/Codegen.hs", + "src/TSN/Picklers.hs"]