-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
126 lines (116 loc) · 3.55 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
var fs = require('fs'),
path = require('path');
var dataFile = 'country_names_and_code_elements_txt',
fromCode = {},
fromName = {};
// Read data from ISO's file
fs.readFileSync(path.join(__dirname, dataFile), 'ascii')
.split('\r\n')
.forEach(function(line) {
var row = line.split(';'),
code = row.pop(),
nameParts = row.pop(),
name = '';
if (!code || !nameParts) {
return;
}
// Make sure we know both "Iran" and "Iran, Islamic Republic of"
nameParts.split(/,\s+/).forEach(function(namePart) {
name += namePart;
fromCode[code.toUpperCase()] = name;
fromName[name.toUpperCase()] = code;
name += ', ';
});
});
// Register a number of name variants and special cases
fromName['KOSOVO'] = fromName['REPUBLIC OF KOSOVO'] = fromName['KOSOVO AND METOHIJA'] = 'XK';
fromName['MACEDONIA (FYROM)'] = fromName['MACEDONIA'];
fromName['SYRIA'] = fromName['SYRIAN ARAB REPUBLIC'];
fromName['REPUBLIC OF THE PHILIPPINES'] = fromName['PHILIPPINES'];
fromName['UNITED STATES OF AMERICA'] = fromName['USA'] = fromName['UNITED STATES'];
fromName['RUSSIA'] = fromName['RUSSIAN FEDERATION'];
fromName['SOUTH KOREA'] = fromName['KOREA, REPUBLIC OF'];
fromName['THE NETHERLANDS'] = fromName['NETHERLANDS'];
fromName['SÃO TOMÉ AND PRÍNCIPE'] = fromName['SAO TOME AND PRINCIPE'];
/**
* Get a country code for a country name. Case-insensitive.
*
* Examples:
*
* // Returns 'CH'
* countrynames.getCode('Switzerland')
* // Returns 'BB'
* countrynames.getCode('BarbaDOS')
*
* @param {String} English country name as of the ISO standard
* @return {String} Two-letter country code as of the ISO standard, uppercase
* @api public
*/
exports.getCode = function(name) {
return fromName[name.toUpperCase()];
};
/**
* Get a country name for a country code. Case-insensitive.
*
* Examples:
*
* // Returns 'TONGA'
* countrynames.getCode('TO')
* // Returns 'RÉUNION'
* countrynames.getCode('re')
*
* @param {String} Two-letter country code as of the ISO standard
* @return {String} English country name as of the ISO standard, uppercase
* @api public
*/
exports.getName = function(code) {
return fromCode[code.toUpperCase()];
};
/**
* Get all country information in an array of objects.
*
* Example:
*
* // Returns an array [ { code: "AD", name: "ANDORA" }, ..., { code: "ZW", name: "ZIMBABWE" } ]
* countrynames.getAll();
*
* @return {Array} All two-letter country codes and names defined in the ISO standard
* @api public
*/
exports.getAll = function() {
return Object.keys(fromCode).slice(1).sort().map(function(code, index){
return { 'code': code, 'name': fromCode[code] };
});
}
/**
* Get a country name for a country code. Case-insensitive.
*
* Example:
*
* // Returns an array ["AD", ... "ZW"]
* countrynames.getAllCodes();
*
* @return {Array} All two-letter country codes defined in the ISO standard
* @api public
*/
exports.getAllCodes = function() {
return Object.keys(fromCode).slice(1).sort();
};
/**
* Get a country name for a country code. Case-insensitive.
*
* Example:
*
* // Returns an array ["AFGHANISTAN", ... "ZIMBABWE", "ÅLAND ISLANDS"]
* countrynames.getAllCodes();
*
* @return {Array} All country names in the ISO standard, sorted alphabetically in your locale, if available
* @api public
*/
exports.getAllNames = function() {
return Object.keys(fromName)
.slice(1)
.sort(function(a, b) {
return a.localeCompare(b);
});
};