lib: add Hledger.Data.Currency, currencySymbolToCode, currencyCodeToSymbol

This commit is contained in:
Simon Michael 2024-10-11 14:40:27 -10:00
parent ceeea69264
commit 6dd9c92d32
3 changed files with 165 additions and 1 deletions

View File

@ -13,6 +13,7 @@ module Hledger.Data (
module Hledger.Data.AccountName,
module Hledger.Data.Amount,
module Hledger.Data.Balancing,
module Hledger.Data.Currency,
module Hledger.Data.Dates,
module Hledger.Data.Errors,
module Hledger.Data.Journal,
@ -38,6 +39,7 @@ import Hledger.Data.Account
import Hledger.Data.AccountName
import Hledger.Data.Amount
import Hledger.Data.Balancing
import Hledger.Data.Currency
import Hledger.Data.Dates
import Hledger.Data.Errors
import Hledger.Data.Journal
@ -58,8 +60,9 @@ import Hledger.Data.Valuation
tests_Data = testGroup "Data" [
tests_AccountName
,tests_Amount
,tests_Dates
,tests_Balancing
-- ,tests_Currency
,tests_Dates
,tests_Journal
,tests_Ledger
,tests_Posting

View File

@ -0,0 +1,160 @@
{-|
Currency names, symbols and codes.
Reference:
- https://www.xe.com/symbols
- https://www.xe.com/currency
-}
{-# LANGUAGE OverloadedStrings #-}
module Hledger.Data.Currency (
currencies,
currencySymbolToCode,
currencyCodeToSymbol,
)
where
import qualified Data.Map as M
import Data.Text (Text)
-- | An ISO 4217 currency code, like EUR. Usually three upper case letters.
type CurrencyCode = Text
-- | A traditional currency symbol, like $. Usually one character, sometimes more.
-- Different from hledger's more general "CommoditySymbol" type.
type CurrencySymbol = Text
-- | Look for a ISO 4217 currency code corresponding to this currency symbol.
--
-- >>> currencySymbolToCode ""
-- Nothing
-- >>> currencySymbolToCode "$"
-- Just "USD"
currencySymbolToCode :: CurrencySymbol -> Maybe CurrencyCode
currencySymbolToCode s = M.lookup s currencyCodesBySymbol
-- | Look for a currency symbol corresponding to this ISO 4217 currency code.
--
-- >>> currencyCodeToSymbol "CZK" -- Just "Kč"
-- Just "K\269"
currencyCodeToSymbol :: CurrencyCode -> Maybe CurrencySymbol
currencyCodeToSymbol c = M.lookup c currencySymbolsByCode
currencyCodesBySymbol = M.fromList [(s,c) | (_,c,s) <- currencies]
currencySymbolsByCode = M.fromList [(c,s) | (_,c,s) <- currencies]
currencies = [
-- country and currency name ISO 4217 code symbol
("Albania Lek", "ALL", "Lek"),
("Afghanistan Afghani", "AFN", "؋"),
("Argentina Peso", "ARS", "$"),
("Aruba Guilder", "AWG", "ƒ"),
("Australia Dollar", "AUD", "$"),
("Azerbaijan Manat", "AZN", ""),
("Bahamas Dollar", "BSD", "$"),
("Barbados Dollar", "BBD", "$"),
("Belarus Ruble", "BYN", "Br"),
("Belize Dollar", "BZD", "BZ$"),
("Bermuda Dollar", "BMD", "$"),
("Bolivia Bolíviano", "BOB", "$b"),
("Bosnia and Herzegovina Convertible Mark", "BAM", "KM"),
("Botswana Pula", "BWP", "P"),
("Bulgaria Lev", "BGN", "лв"),
("Brazil Real", "BRL", "R$"),
("Brunei Darussalam Dollar", "BND", "$"),
("Cambodia Riel", "KHR", ""),
("Canada Dollar", "CAD", "$"),
("Cayman Islands Dollar", "KYD", "$"),
("Chile Peso", "CLP", "$"),
("China Yuan Renminbi", "CNY", "¥"),
("Colombia Peso", "COP", "$"),
("Costa Rica Colon", "CRC", ""),
("Croatia Kuna", "HRK", "kn"),
("Cuba Peso", "CUP", ""),
("Czech Republic Koruna", "CZK", ""),
("Denmark Krone", "DKK", "kr"),
("Dominican Republic Peso", "DOP", "RD$"),
("East Caribbean Dollar", "XCD", "$"),
("Egypt Pound", "EGP", "£"),
("El Salvador Colon", "SVC", "$"),
("Euro Member Countries", "EUR", ""),
("Falkland Islands (Malvinas) Pound", "FKP", "£"),
("Fiji Dollar", "FJD", "$"),
("Ghana Cedi", "GHS", "¢"),
("Gibraltar Pound", "GIP", "£"),
("Guatemala Quetzal", "GTQ", "Q"),
("Guernsey Pound", "GGP", "£"),
("Guyana Dollar", "GYD", "$"),
("Honduras Lempira", "HNL", "L"),
("Hong Kong Dollar", "HKD", "$"),
("Hungary Forint", "HUF", "Ft"),
("Iceland Krona", "ISK", "kr"),
("India Rupee", "INR", ""),
("Indonesia Rupiah", "IDR", "Rp"),
("Iran Rial", "IRR", ""),
("Isle of Man Pound", "IMP", "£"),
("Israel Shekel", "ILS", ""),
("Jamaica Dollar", "JMD", "J$"),
("Japan Yen", "JPY", "¥"),
("Jersey Pound", "JEP", "£"),
("Kazakhstan Tenge", "KZT", "лв"),
("Korea (North) Won", "KPW", ""),
("Korea (South) Won", "KRW", ""),
("Kyrgyzstan Som", "KGS", "лв"),
("Laos Kip", "LAK", ""),
("Lebanon Pound", "LBP", "£"),
("Liberia Dollar", "LRD", "$"),
("Macedonia Denar", "MKD", "ден"),
("Malaysia Ringgit", "MYR", "RM"),
("Mauritius Rupee", "MUR", ""),
("Mexico Peso", "MXN", "$"),
("Mongolia Tughrik", "MNT", ""),
("Mozambique Metical", "MZN", "MT"),
("Namibia Dollar", "NAD", "$"),
("Nepal Rupee", "NPR", ""),
("Netherlands Antilles Guilder", "ANG", "ƒ"),
("New Zealand Dollar", "NZD", "$"),
("Nicaragua Cordoba", "NIO", "C$"),
("Nigeria Naira", "NGN", ""),
("Norway Krone", "NOK", "kr"),
("Oman Rial", "OMR", ""),
("Pakistan Rupee", "PKR", ""),
("Panama Balboa", "PAB", "B/."),
("Paraguay Guarani", "PYG", "Gs"),
("Peru Sol", "PEN", "S/."),
("Philippines Peso", "PHP", ""),
("Poland Zloty", "PLN", ""),
("Qatar Riyal", "QAR", ""),
("Romania Leu", "RON", "lei"),
("Russia Ruble", "RUB", ""),
("Saint Helena Pound", "SHP", "£"),
("Saudi Arabia Riyal", "SAR", ""),
("Serbia Dinar", "RSD", "Дин."),
("Seychelles Rupee", "SCR", ""),
("Singapore Dollar", "SGD", "$"),
("Solomon Islands Dollar", "SBD", "$"),
("Somalia Shilling", "SOS", "S"),
("South Africa Rand", "ZAR", "R"),
("Sri Lanka Rupee", "LKR", ""),
("Sweden Krona", "SEK", "kr"),
("Switzerland Franc", "CHF", "CHF"),
("Suriname Dollar", "SRD", "$"),
("Syria Pound", "SYP", "£"),
("Taiwan New Dollar", "TWD", "NT$"),
("Thailand Baht", "THB", "฿"),
("Trinidad and Tobago Dollar", "TTD", "TT$"),
("Turkey Lira", "TRY", ""),
("Tuvalu Dollar", "TVD", "$"),
("Ukraine Hryvnia", "UAH", ""),
("United Kingdom Pound", "GBP", "£"),
("United States Dollar", "USD", "$"),
("Uruguay Peso", "UYU", "$U"),
("Uzbekistan Som", "UZS", "лв"),
("Venezuela Bolívar", "VEF", "Bs"),
("Viet Nam Dong", "VND", ""),
("Yemen Rial", "YER", ""),
("Zimbabwe Dollar", "ZWD", "Z$")
]
-- tests_Currency = testGroup "Currency" []

View File

@ -122,6 +122,7 @@ library:
- Hledger.Data.AccountName
- Hledger.Data.Amount
- Hledger.Data.Balancing
- Hledger.Data.Currency
- Hledger.Data.Dates
- Hledger.Data.Errors
- Hledger.Data.Journal