forked from michaelwittig/node-i18n-iso-countries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (147 loc) · 3.15 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
149
150
151
152
153
154
155
156
157
158
159
var fs = require("fs"),
path = require("path");
/*
* All codes map to ISO 3166-1 alpha-2
*/
var alpha2 = {},
alpha3 = {},
numeric = {};
/*jslint stupid: true */
fs.readFileSync(path.resolve(__dirname, "codes.csv"), {encoding: "utf8"}).replace(/\r/g, "").split("\n").forEach(function(line) {
"use strict";
var s = line.split(";");
alpha2[s[0]] = s[1];
alpha3[s[1]] = s[0];
numeric[parseInt(s[2], 10)] = s[0];
});
/*jslint stupid: false */
/*
* @param code Alpha-3 code
* @return Alpha-2 code or undefined
*/
function alpha3ToAlpha2(code) {
"use strict";
return alpha3[code];
}
exports.alpha3ToAlpha2 = alpha3ToAlpha2;
/*
* @param code Alpha-2 code
* @return Alpha-3 code or undefined
*/
function alpha2ToAlpha3(code) {
"use strict";
return alpha2[code];
}
exports.alpha2ToAlpha3 = alpha2ToAlpha3;
/*
* @param code Numeric code
* @return Alpha-3 code or undefined
*/
function numericToAlpha3(code) {
"use strict";
return alpha2ToAlpha3(numeric[parseInt(code, 10)]);
}
exports.numericToAlpha3 = numericToAlpha3;
/*
* @param code Numeric code
* @return Alpha-2 code or undefined
*/
function numericToAlpha2(code) {
"use strict";
return numeric[parseInt(code, 10)];
}
exports.numericToAlpha2 = numericToAlpha2;
/*
* @param code ISO 3166-1 alpha-2, alpha-3 or numeric code
* @return ISO 3166-1 alpha-3
*/
function toAlpha3(code) {
"use strict";
if (typeof code === "string") {
if (/^[0-9]*$/.test(code)) {
return numericToAlpha3(code);
}
if(code.length === 2) {
return alpha2ToAlpha3(code.toUpperCase());
}
if (code.length === 3) {
return code.toUpperCase();
}
}
if (typeof code === "number") {
return numericToAlpha3(code);
}
return undefined;
}
exports.toAlpha3 = toAlpha3;
/*
* @param code ISO 3166-1 alpha-2, alpha-3 or numeric code
* @return ISO 3166-1 alpha-2
*/
function toAlpha2(code) {
"use strict";
if (typeof code === "string") {
if (/^[0-9]*$/.test(code)) {
return numericToAlpha2(code);
}
if (code.length === 2) {
return code.toUpperCase();
}
if(code.length === 3) {
return alpha3ToAlpha2(code.toUpperCase());
}
}
if (typeof code === "number") {
return numericToAlpha2(code);
}
return undefined;
}
exports.toAlpha2 = toAlpha2;
/*
* @param code ISO 3166-1 alpha-2, alpha-3 or numeric code
* @param lang language for country name
* @return name or undefined
*/
exports.getName = function(code, lang) {
"use strict";
try {
var l = require("./" + lang.toLowerCase());
return l.i18n()[toAlpha2(code)];
} catch (err) {
return undefined;
}
};
/*
* @param lang language for country name
* @return hash
*/
exports.getNames = function(lang) {
"use strict";
try {
var l = require("./" + lang.toLowerCase());
return l.i18n();
} catch (err) {
return {};
}
};
/*
* @return hash (alpha-2 => alpha-3)
*/
exports.getAlpha2Codes = function() {
"use strict";
return alpha2;
};
/*
* @return hash (alpha-3 => alpha-2)
*/
exports.getAlpha3Codes = function() {
"use strict";
return alpha3;
};
/*
* @return hash (numeric => alpha-2)
*/
exports.getNumericCodes = function() {
"use strict";
return numeric;
};