-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
148 lines (119 loc) · 3.87 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'use strict';
var _ = require('underscore');
var continents = require('./data/continents');
var regions = require('./data/regions');
var allCountries = require('./data/countries.json');
var allCurrencies = require('./data/currencies.json');
var allLanguages = require('./data/languages.json');
var getSymbol = require('currency-symbol-map');
var searchObjectforString = function (searchterm, data, property1, property2) {
var searchItem = searchterm.toLowerCase();
var found = false;
_.each(data, function (item) {
var match1 = item[property1] ? item[property1].toLowerCase() : '';
var match2 = item[property2] ? item[property2].toLowerCase() : '';
var statusCode = item.status || '';
if( ((searchItem === match1) || (searchItem === match2)) && statusCode !== 'deleted') {
found = item;
return false;
}
});
return found;
};
_.each(allCurrencies, 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;
});
var callingCountries = {all: []};
var allCallingCodes = _.reduce(allCountries, 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
allCallingCodes.sort(function (a, b) {
var parse = function (str) { return parseInt(str, 10) };
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;
}
}
});
var getAllCountries = function () {
return allCountries;
};
var getAllCurrencies = function () {
return allCurrencies;
};
var getAllLanguages = function () {
return allLanguages;
};
var getAllCallingCodes = function () {
return allCallingCodes;
};
var getAllCallingCountries = function () {
return callingCountries;
};
var getAllRegions = function () {
return regions;
};
var getAllContinents = function () {
return continents;
};
var getCountryInfoByCode = function (code) {
return searchObjectforString(code, allCountries, 'alpha2', 'alpha3');
};
var getCountryInfoByName = function (name) {
return searchObjectforString(name, allCountries, 'name');
};
var getCurrencyInfoByCode = function (code) {
return searchObjectforString(code, allCurrencies, 'code');
};
var getLanguageInfoByCode = function (code) {
return searchObjectforString(code, allLanguages, 'alpha2', 'alpha3');
};
var getLanguageInfoByBibliographic = function (code) {
return searchObjectforString(code, allLanguages, 'bibliographic');
};
module.exports = {
getAllCountries: getAllCountries,
getAllCurrencies: getAllCurrencies,
getAllLanguages: getAllLanguages,
getAllCallingCodes: getAllCallingCodes,
getAllCallingCountries: getAllCallingCountries,
getAllContinents: getAllContinents,
getAllRegions: getAllRegions,
getCountryInfoByCode: getCountryInfoByCode,
getCountryInfoByName: getCountryInfoByName,
getCurrencyInfoByCode: getCurrencyInfoByCode,
getLanguageInfoByCode: getLanguageInfoByCode,
getLanguageInfoByBibliographic: getLanguageInfoByBibliographic,
};