summaryrefslogtreecommitdiff
path: root/sort-and-sanitize-the-other-kind-of-amex-export.hs
blob: adeac066650cadb3424bc71b22db2b46c79add15 (plain)
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
import Text.CSV
import Text.Printf (printf)

import Data.List (sort, isPrefixOf, groupBy)

import System.Environment (getArgs)

normAndInvertAmount :: Field -> Field
normAndInvertAmount amt | amt == "" = ""
    | otherwise = printf "%.2f" (0 - (read amt :: Double))

compressWhitespace :: String -> String
compressWhitespace x = map head $ groupSpaces x
	where groupSpaces "" = [""]
	      groupSpaces x = groupBy (\x y -> x==' ' && y==' ') x

massageAmount :: [Field] -> [Field]
massageAmount [a,b,c,d,e,f,g,h,i,j] = [a,b,c,d,compressWhitespace e,f,g,h,i,normAndInvertAmount j]
-- massageAmount [a,b,c,d,e,f,g,h] = [a,b,compressWhitespace c,d,e,f,g,normAndInvertAmount h]
massageAmount xs = xs

sanitySort :: [Record] -> [Record]
sanitySort ts = map (massageAmount) (sort ts)

main = do
        args <- getArgs
	result <- parseCSVFromFile (head args)
	case result of
		Left e -> putStrLn ("Parse fail: " ++ show e)
		Right ts -> putStrLn $ printCSV (sanitySort ts)