-
Notifications
You must be signed in to change notification settings - Fork 4
/
ISO_countries.js
143 lines (129 loc) · 4.43 KB
/
ISO_countries.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
/**
* ISO_countries.js
*
* Load and check country codes
*/
import { readFile, readFileSync } from "fs";
import chalk from "chalk";
import fetchS from "sync-fetch";
import handleErrors from "./fetch_err_handler.js";
import { isHTTPURL } from "./pattern_checks.js";
/**
* load the countries list into the allowedCountries global array from the specified text
*
* @param {String} countryData the text of the country JSON data
* @returns {object} processed JSON object of countries
*/
function loadCountryData(countryData) {
return JSON.parse(countryData, function (key, value) {
if (key == "numeric") return Number(value);
else if (key == "alpha2") {
if (value.length != 2) return "**";
else return value;
} else if (key == "alpha3") {
if (value.length != 3) return "***";
else return value;
} else return value;
});
}
export default class ISOcountries {
#countriesList;
#use2CharCountries;
#use3CharCountries;
/**
* constructor
*
* @param {boolean} use2 allow the 2 digit country codes to be searched
* @param {boolean} use3 allow the 3 digit country codes to be searched
*/
constructor(use2 = true, use3 = false) {
loadCountryData.bind(this);
this.#countriesList = [];
this.#use2CharCountries = use2;
this.#use3CharCountries = use3;
}
/**
* load the countries list into the allowedCountries global array from the specified JSON file
*
* @param {String} countriesFile the file name to load
* @param {boolean} purge erase the existing values before loading new
*/
#loadCountriesFromFile(countriesFile, purge = false, async = true) {
console.log(chalk.yellow(`reading countries from ${countriesFile}`));
if (purge) this.reset();
if (async)
readFile(
countriesFile,
{ encoding: "utf-8" },
function (err, data) {
if (!err) this.#countriesList = loadCountryData(data);
else console.log(chalk.red(err.error));
}.bind(this)
);
else {
let langs = readFileSync(countriesFile, { encoding: "utf-8" }).toString();
this.#countriesList = loadCountryData(langs);
}
}
/**
* load the countries list into the allowedCountries global array from the specified JSON file
*
* @param {String} countriesURL the URL to the file to load
* @param {boolean} purge erase the existing values before loading new
*/
#loadCountriesFromURL(countriesURL, purge = false, async = true) {
let isHTTPurl = isHTTPURL(countriesURL);
console.log(chalk.yellow(`${isHTTPurl ? "" : "--> NOT "}retrieving countries from ${countriesURL} using fetch()`));
if (!isHTTPurl) return;
if (purge) this.reset();
if (async)
fetch(countriesURL)
.then(handleErrors)
.then((response) => response.text())
.then((responseText) => (this.#countriesList = loadCountryData(responseText)))
.catch((error) => console.log(chalk.red(`error (${error}) retrieving ${countriesURL}`)));
else {
let resp = null;
try {
resp = fetchS(countriesURL);
} catch (error) {
console.log(chalk.red(error.message));
}
if (resp) {
if (resp.ok) this.#countriesList = loadCountryData(response.text);
else console.log(chalk.red(`error (${error}) retrieving ${languagesURL}`));
}
}
}
loadCountries(options, async = true) {
if (!options) options = {};
if (!options.purge) options.purge = true;
if (options.file) this.#loadCountriesFromFile(options.file, options.purge, async);
else if (options.url) this.#loadCountriesFromURL(options.url, options.purge, async);
}
reset() {
this.#countriesList.length = 0;
}
count() {
return this.#countriesList.length;
}
/**
* determine if the argument contains a valid ISO 3166 country code
*
* @param {String} countryCode the country code to be checked for validity
* @param {Boolean} caseSensitive ignore case
* @return {boolean} true if countryCode is known else false
*/
isISO3166code(countryCode, caseSensitive = true) {
let found = false;
const countryCode_lc = countryCode.toLowerCase();
if (this.#use3CharCountries && countryCode.length == 3) {
if (caseSensitive ? this.#countriesList.find((elem) => elem.alpha3 == countryCode) : this.#countriesList.find((elem) => elem.alpha3.toLowerCase() == countryCode_lc))
found = true;
} else if (this.#use2CharCountries && countryCode.length == 2) {
if (caseSensitive ? this.#countriesList.find((elem) => elem.alpha2 == countryCode) : this.#countriesList.find((elem) => elem.alpha2.toLowerCase() == countryCode_lc))
found = true;
}
return found;
}
}