-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.go
More file actions
86 lines (72 loc) · 1.97 KB
/
Copy pathjavascript.go
File metadata and controls
86 lines (72 loc) · 1.97 KB
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
package javascript
import (
"errors"
"fmt"
"os"
"github.com/flowcommerce/json-reference/common"
)
type JavascriptFormat struct {
Symbol string `json:"symbol"`
Decimal string `json:"decimal"`
Group string `json:"group"`
Precision int `json:"precision"`
Format string `json:"format"`
}
type CommonData struct {
Countries []common.Country
Currencies []common.Currency
Locales []common.Locale
}
func Generate() {
data := CommonData{
Countries: common.Countries(),
Currencies: common.Currencies(),
Locales: common.Locales(),
}
common.WriteJson("data/javascript/currency-format.json", generateFormatsByLocale(data))
}
func generateFormatsByLocale(data CommonData) map[string]JavascriptFormat {
all := map[string]JavascriptFormat{}
for _, l := range data.Locales {
currency, err := findCurrencyByLocale(data, l)
if err == nil && currency.Symbols != nil {
all[l.Id] = JavascriptFormat{
Symbol: currency.Symbols.Primary,
Decimal: l.Numbers.Decimal,
Group: l.Numbers.Group,
Precision: currency.NumberDecimals,
Format: "%s%v",
}
}
}
return all
}
func findCurrencyByLocale(data CommonData, locale common.Locale) (common.Currency, error) {
country := findCountryByIso31663(data, locale.Country)
if country.DefaultCurrency == "" {
return common.Currency{}, errors.New("Country has no default currency")
} else {
c := findCurrencyByIso42173(data, country.DefaultCurrency)
return c, nil
}
}
func findCountryByIso31663(data CommonData, country string) common.Country {
for _, c := range data.Countries {
if c.Iso_3166_3 == country {
return c
}
}
fmt.Printf("ERROR: Country[%s] not found\n", country)
os.Exit(1)
return common.Country{}
}
func findCurrencyByIso42173(data CommonData, currency string) common.Currency {
for _, c := range data.Currencies {
if c.Iso_4217_3 == currency {
return c
}
}
fmt.Printf("ERROR: Currency[%s] not found\n", currency)
os.Exit(1)
return common.Currency{}
}