forked from vanwagonet/elm-intl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Currency.elm
108 lines (80 loc) · 1.93 KB
/
Currency.elm
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
module Intl.Currency
exposing
( Currency
, fromCurrencyCode
, toCurrencyCode
, usd
, eur
, jpy
, gbp
, chf
, cad
)
{-| A Currency is used to the display numeric data as money. Note that
`NumberFormat` does *not* do exchange currency conversion, it only uses the
currency for displaying symbols and other conventions to mark the number as
money.
@docs Currency, fromCurrencyCode, toCurrencyCode
# Predefined Currencies
A few currencies have been pre-defined for convenience.
@docs usd, eur, jpy, gbp, chf, cad
-}
import Regex exposing (regex, contains)
import String exposing (toUpper)
{-| The Currency type holds a valid ISO 4217 currency code.
-}
type Currency
= Currency String
{-| Checks the string as a valid currency code, and returns a currency if it is.
-- Going back to the Gold Standard
fromCurrencyCode "XAU"
If a string is passed that is not a valid currency code, `Nothing` will be
returned.
Note that any 3-letter string will work, but if it isn't a defined currency
code, then the formatting will have no symbol or currency name. It will just use
the code you provided.
-}
fromCurrencyCode : String -> Maybe Currency
fromCurrencyCode code =
if contains (regex "^[A-Za-z]{3}$") code then
Just (Currency (toUpper code))
else
Nothing
{-| Gets the string currency code from a Currency
toCurrencyCode Currency.eur == "EUR"
-}
toCurrencyCode : Currency -> String
toCurrencyCode currency =
case currency of
Currency tag ->
tag
{-| United States Dollar
-}
usd : Currency
usd =
Currency "USD"
{-| Euro
-}
eur : Currency
eur =
Currency "EUR"
{-| Japanese Yen
-}
jpy : Currency
jpy =
Currency "JPY"
{-| Pound Sterling
-}
gbp : Currency
gbp =
Currency "GBP"
{-| Swiss Franc
-}
chf : Currency
chf =
Currency "CHF"
{-| Canadian Dollar
-}
cad : Currency
cad =
Currency "CAD"