forked from OpenBookPrices/country-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (95 loc) · 3.02 KB
/
index.js
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
109
110
111
112
113
114
115
'use strict';
var _ = require('lodash');
var continents = require('./data/continents');
var regions = require('./data/regions');
var countriesAll = require('./data/countries.json');
var currenciesAll = require('./data/currencies.json');
var languagesAll = require('./data/languages.json');
var lookup = require('./lookup');
var getSymbol = require('currency-symbol-map')
exports.continents = continents;
exports.regions = regions;
exports.countries = {
all: countriesAll,
};
_.each(countriesAll, function (country) {
// prefer assigned country codes over inactive ones
var exportedAlpha2 = exports.countries[country.alpha2];
if (!exportedAlpha2 || exportedAlpha2.status === 'deleted') {
exports.countries[country.alpha2] = country;
}
var exportedAlpha3 = exports.countries[country.alpha3];
if (!exportedAlpha3 || exportedAlpha3.status === 'deleted') {
exports.countries[country.alpha3] = country;
}
});
exports.currencies = {
all: currenciesAll,
};
_.each(currenciesAll, function (currency) {
//If the symbol isn't available, default to the currency code
var symbol = getSymbol(currency.code);
if (symbol == '?') {
symbol = currency.code;
}
currency.symbol = symbol;
exports.currencies[currency.code] = currency;
});
exports.languages = {
all: languagesAll,
};
// Note that for the languages there are several entries with the same alpha3 -
// eg Dutch and Flemish. Not sure how to best deal with that - here whichever
// comes last wins.
_.each(languagesAll, function (language) {
exports.languages[language.alpha2] = language;
exports.languages[language.bibliographic] = language;
exports.languages[language.alpha3] = language;
});
exports.lookup = lookup({
countries: countriesAll,
currencies: currenciesAll,
languages: languagesAll
});
var callingCountries = {all: []};
var callingCodesAll = _.reduce(countriesAll, function (codes, country) {
if (country.countryCallingCodes && country.countryCallingCodes.length) {
callingCountries.all.push(country);
callingCountries[country.alpha2] = country;
callingCountries[country.alpha3] = country;
_.each(country.countryCallingCodes, function (code) {
if (codes.indexOf(code) == -1) {
codes.push(code);
}
});
}
return codes;
}, []);
delete callingCountries['']; // remove empty alpha3s
exports.callingCountries = callingCountries;
callingCodesAll.sort(function (a, b) {
var parse = function (str) { return parseInt(str) };
var splitA = _.map(a.split(' '), parse);
var splitB = _.map(b.split(' '), parse);
if (splitA[0] < splitB[0]) {
return -1;
} else if (splitA[0] > splitB[0]) {
return 1;
} else {
// Same - check split[1]
if (splitA[1] === undefined && splitB[1] !== undefined) {
return -1;
} else if (splitA[1] !== undefined && splitB[1] === undefined) {
return 1;
} else if (splitA[1] < splitB[1]) {
return -1;
} else if (splitA[1] > splitB[1]) {
return 1;
} else {
return 0;
}
}
});
exports.callingCodes = {
all: callingCodesAll
};