forked from marchah/node-countries
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
109 lines (93 loc) · 3.87 KB
/
Copy pathindex.js
File metadata and controls
109 lines (93 loc) · 3.87 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use strict';
const _ = require('lodash');
const countries = require('./data/countries.json');
const toExport = {
JSON: countries,
};
/**
* Find the country object of the given country name
*
* @param {String} name country name
* @param {Boolean} [useAlias] use alias flag, default `false`
* @return {Object} country country object
*/
function getCountryByName(name, useAlias) {
if (!_.isString(name)) return undefined;
return _.find(countries, function (country) {
if (useAlias) {
return country.name.toUpperCase() === name.toUpperCase()
|| _.find(country.alias, function (alias) { return alias.toUpperCase() === name.toUpperCase() });
}
return country.name.toUpperCase() === name.toUpperCase();
});
}
/**
* Find the country object of the given country name or short name
*
* @param {String} name country name or short name (alpha2)
* @param {Boolean} [useAlias] use alias flag, default `false`
* @return {Object} country country object
*/
function getCountryByNameOrShortName(name, useAlias) {
if (!_.isString(name)) return undefined;
return _.find(countries, function (country) {
if (useAlias) {
return country.name.toUpperCase() === name.toUpperCase()
|| country.alpha2.toUpperCase() === name.toUpperCase()
|| _.find(country.alias, function (alias) { return alias.toUpperCase() === name.toUpperCase() });
}
return country.name.toUpperCase() === name.toUpperCase() || country.alpha2.toUpperCase() === name.toUpperCase();
});
}
toExport.getCountryByName = getCountryByName;
toExport.findCountryByName = getCountryByName;
toExport.getCountryByNameOrShortName = getCountryByNameOrShortName;
toExport.findCountryByNameOrShortName = getCountryByNameOrShortName;
/**
* Find the province object of the given province name
*
* @param {String} name english province name
* @param {Boolean} [useAlias] use alias flag, default `false`
* @return {Object} province province object
*/
function getProvinceByName(name, useAlias) {
if (!_.isString(name) || !_.isArray(this.provinces)) return undefined;
return _.find(this.provinces, function (province) {
if (useAlias) {
return province.name.toUpperCase() === name.toUpperCase()
|| _.find(province.alias, function (alias) { return alias.toUpperCase() === name.toUpperCase() });
}
return province.name.toUpperCase() === name.toUpperCase();
});
}
/**
* Find the province object of the given province name or short name
*
* @param {String} name english province name or short name
* @param {Boolean} [useAlias] use alias flag, default `false`
* @return {Object} province province object
*/
function getProvinceByNameOrShortName(name, useAlias) {
if (!_.isString(name) || !_.isArray(this.provinces)) return undefined;
return _.find(this.provinces, function (province) {
if (useAlias) {
return province.name.toUpperCase() === name.toUpperCase()
|| (province.short && province.short.toUpperCase() === name.toUpperCase())
|| _.find(province.alias, function (alias) { return alias.toUpperCase() === name.toUpperCase() });
}
return province.name.toUpperCase() === name.toUpperCase() || (province.short && province.short.toUpperCase() === name.toUpperCase());
});
}
/**
*
* Add search function to each country and map each country by alpha2
*/
const listCountries = _.keyBy(_.cloneDeep(countries), 'alpha2');
_.forEach(listCountries, function (country, key) {
country.getProvinceByName = _.bind(getProvinceByName, country);
country.findProvinceByName = _.bind(getProvinceByName, country);
country.getProvinceByNameOrShortName = _.bind(getProvinceByNameOrShortName, country);
country.findProvinceByNameOrShortName = _.bind(getProvinceByNameOrShortName, country);
toExport[key] = country;
});
module.exports = toExport;