@temboplus/frontend-core 0.2.4 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +74 -20
- package/esm/src/data/countries.d.ts +36 -3
- package/esm/src/data/countries.js +2475 -974
- package/esm/src/models/amount/amount.d.ts.map +1 -1
- package/esm/src/models/amount/amount.js +1 -1
- package/esm/src/models/bank/bank.d.ts +127 -0
- package/esm/src/models/bank/bank.d.ts.map +1 -1
- package/esm/src/models/bank/bank.js +264 -7
- package/esm/src/models/bank/index.d.ts +0 -1
- package/esm/src/models/bank/index.d.ts.map +1 -1
- package/esm/src/models/bank/index.js +0 -1
- package/esm/src/models/country/country.d.ts +278 -24
- package/esm/src/models/country/country.d.ts.map +1 -1
- package/esm/src/models/country/country.js +603 -31
- package/esm/src/models/country/index.d.ts +1 -1
- package/esm/src/models/country/index.d.ts.map +1 -1
- package/esm/src/models/country/index.js +1 -1
- package/esm/src/models/country/types.d.ts +53 -0
- package/esm/src/models/country/types.d.ts.map +1 -0
- package/esm/src/models/country/types.js +43 -0
- package/esm/src/models/currency/currency.d.ts +127 -4
- package/esm/src/models/currency/currency.d.ts.map +1 -1
- package/esm/src/models/currency/currency.js +229 -11
- package/esm/src/models/currency/index.d.ts +1 -1
- package/esm/src/models/currency/index.d.ts.map +1 -1
- package/esm/src/models/currency/index.js +1 -1
- package/esm/src/models/currency/types.d.ts +20 -0
- package/esm/src/models/currency/types.d.ts.map +1 -0
- package/esm/src/models/currency/types.js +13 -0
- package/esm/src/models/phone_number/global/phone_number.d.ts +3 -2
- package/esm/src/models/phone_number/global/phone_number.d.ts.map +1 -1
- package/esm/src/models/phone_number/global/service.d.ts +27 -20
- package/esm/src/models/phone_number/global/service.d.ts.map +1 -1
- package/esm/src/models/phone_number/global/service.js +61 -40
- package/package.json +1 -1
- package/script/src/data/countries.d.ts +36 -3
- package/script/src/data/countries.js +2475 -974
- package/script/src/models/amount/amount.d.ts.map +1 -1
- package/script/src/models/amount/amount.js +4 -4
- package/script/src/models/bank/bank.d.ts +127 -0
- package/script/src/models/bank/bank.d.ts.map +1 -1
- package/script/src/models/bank/bank.js +273 -12
- package/script/src/models/bank/index.d.ts +0 -1
- package/script/src/models/bank/index.d.ts.map +1 -1
- package/script/src/models/bank/index.js +0 -1
- package/script/src/models/country/country.d.ts +278 -24
- package/script/src/models/country/country.d.ts.map +1 -1
- package/script/src/models/country/country.js +611 -35
- package/script/src/models/country/index.d.ts +1 -1
- package/script/src/models/country/index.d.ts.map +1 -1
- package/script/src/models/country/index.js +1 -1
- package/script/src/models/country/types.d.ts +53 -0
- package/script/src/models/country/types.d.ts.map +1 -0
- package/script/src/models/country/types.js +46 -0
- package/script/src/models/currency/currency.d.ts +127 -4
- package/script/src/models/currency/currency.d.ts.map +1 -1
- package/script/src/models/currency/currency.js +237 -15
- package/script/src/models/currency/index.d.ts +1 -1
- package/script/src/models/currency/index.d.ts.map +1 -1
- package/script/src/models/currency/index.js +1 -1
- package/script/src/models/currency/types.d.ts +20 -0
- package/script/src/models/currency/types.d.ts.map +1 -0
- package/script/src/models/currency/types.js +14 -0
- package/script/src/models/phone_number/global/phone_number.d.ts +3 -2
- package/script/src/models/phone_number/global/phone_number.d.ts.map +1 -1
- package/script/src/models/phone_number/global/service.d.ts +27 -20
- package/script/src/models/phone_number/global/service.d.ts.map +1 -1
- package/script/src/models/phone_number/global/service.js +61 -40
- package/esm/src/models/bank/service.d.ts +0 -106
- package/esm/src/models/bank/service.d.ts.map +0 -1
- package/esm/src/models/bank/service.js +0 -240
- package/esm/src/models/country/service.d.ts +0 -75
- package/esm/src/models/country/service.d.ts.map +0 -1
- package/esm/src/models/country/service.js +0 -267
- package/esm/src/models/currency/service.d.ts +0 -96
- package/esm/src/models/currency/service.d.ts.map +0 -1
- package/esm/src/models/currency/service.js +0 -194
- package/script/src/models/bank/service.d.ts +0 -106
- package/script/src/models/bank/service.d.ts.map +0 -1
- package/script/src/models/bank/service.js +0 -247
- package/script/src/models/country/service.d.ts +0 -75
- package/script/src/models/country/service.d.ts.map +0 -1
- package/script/src/models/country/service.js +0 -274
- package/script/src/models/currency/service.d.ts +0 -96
- package/script/src/models/currency/service.d.ts.map +0 -1
- package/script/src/models/currency/service.js +0 -201
|
@@ -1,7 +1,77 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview This file contains both the Country class and CountryService class.
|
|
4
|
+
*
|
|
5
|
+
* ARCHITECTURE NOTE: Country and CountryService Classes
|
|
6
|
+
* ======================================================
|
|
7
|
+
*
|
|
8
|
+
* These two classes have been intentionally placed in the same file to resolve
|
|
9
|
+
* a circular dependency issue. The original implementation had these in separate files:
|
|
10
|
+
*
|
|
11
|
+
* - Country class: Defines country properties and static accessors
|
|
12
|
+
* - CountryService class: Loads country data and provides instance methods
|
|
13
|
+
*
|
|
14
|
+
* The circular dependency occurred because:
|
|
15
|
+
* 1. Country needed CountryService to initialize its static properties
|
|
16
|
+
* 2. CountryService needed Country to create Country instances
|
|
17
|
+
*
|
|
18
|
+
* By combining both classes in a single file:
|
|
19
|
+
* - We ensure proper initialization order
|
|
20
|
+
* - All static properties are immediately available after import
|
|
21
|
+
* - The public API remains unchanged
|
|
22
|
+
*
|
|
23
|
+
* This approach also better encapsulates related functionality in a single module,
|
|
24
|
+
* making it easier to understand and maintain the country-related domain model.
|
|
25
|
+
* The addition of currency support through the getCurrency() method leverages
|
|
26
|
+
* the Currency model while maintaining a clean separation of concerns.
|
|
27
|
+
*/
|
|
28
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
29
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
30
|
+
};
|
|
2
31
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Country = void 0;
|
|
4
|
-
const
|
|
32
|
+
exports.CountryService = exports.Country = exports.SUB_REGION = exports.CONTINENT = void 0;
|
|
33
|
+
const currency_js_1 = require("../currency/currency.js");
|
|
34
|
+
const countries_js_1 = __importDefault(require("../../data/countries.js"));
|
|
35
|
+
/**
|
|
36
|
+
* Enum for continents
|
|
37
|
+
*/
|
|
38
|
+
var CONTINENT;
|
|
39
|
+
(function (CONTINENT) {
|
|
40
|
+
CONTINENT["AFRICA"] = "Africa";
|
|
41
|
+
CONTINENT["ANTARCTICA"] = "Antarctica";
|
|
42
|
+
CONTINENT["ASIA"] = "Asia";
|
|
43
|
+
CONTINENT["EUROPE"] = "Europe";
|
|
44
|
+
CONTINENT["NORTH_AMERICA"] = "North America";
|
|
45
|
+
CONTINENT["OCEANIA"] = "Oceania";
|
|
46
|
+
CONTINENT["SOUTH_AMERICA"] = "South America";
|
|
47
|
+
})(CONTINENT || (exports.CONTINENT = CONTINENT = {}));
|
|
48
|
+
/**
|
|
49
|
+
* Enum for sub-regions
|
|
50
|
+
*/
|
|
51
|
+
var SUB_REGION;
|
|
52
|
+
(function (SUB_REGION) {
|
|
53
|
+
SUB_REGION["AUSTRALIA_AND_NEW_ZEALAND"] = "Australia and New Zealand";
|
|
54
|
+
SUB_REGION["CARIBBEAN"] = "Caribbean";
|
|
55
|
+
SUB_REGION["CENTRAL_AMERICA"] = "Central America";
|
|
56
|
+
SUB_REGION["CENTRAL_ASIA"] = "Central Asia";
|
|
57
|
+
SUB_REGION["EASTERN_AFRICA"] = "Eastern Africa";
|
|
58
|
+
SUB_REGION["EASTERN_ASIA"] = "Eastern Asia";
|
|
59
|
+
SUB_REGION["EASTERN_EUROPE"] = "Eastern Europe";
|
|
60
|
+
SUB_REGION["MELANESIA"] = "Melanesia";
|
|
61
|
+
SUB_REGION["MICRONESIA"] = "Micronesia";
|
|
62
|
+
SUB_REGION["MIDDLE_AFRICA"] = "Middle Africa";
|
|
63
|
+
SUB_REGION["NORTHERN_AFRICA"] = "Northern Africa";
|
|
64
|
+
SUB_REGION["NORTHERN_AMERICA"] = "Northern America";
|
|
65
|
+
SUB_REGION["NORTHERN_EUROPE"] = "Northern Europe";
|
|
66
|
+
SUB_REGION["POLYNESIA"] = "Polynesia";
|
|
67
|
+
SUB_REGION["SOUTH_EASTERN_ASIA"] = "South-eastern Asia";
|
|
68
|
+
SUB_REGION["SOUTHERN_AFRICA"] = "Southern Africa";
|
|
69
|
+
SUB_REGION["SOUTHERN_ASIA"] = "Southern Asia";
|
|
70
|
+
SUB_REGION["SOUTHERN_EUROPE"] = "Southern Europe";
|
|
71
|
+
SUB_REGION["WESTERN_AFRICA"] = "Western Africa";
|
|
72
|
+
SUB_REGION["WESTERN_ASIA"] = "Western Asia";
|
|
73
|
+
SUB_REGION["WESTERN_EUROPE"] = "Western Europe";
|
|
74
|
+
})(SUB_REGION || (exports.SUB_REGION = SUB_REGION = {}));
|
|
5
75
|
/**
|
|
6
76
|
* Represents a country with essential details.
|
|
7
77
|
* @class Country
|
|
@@ -9,36 +79,130 @@ const service_js_1 = require("./service.js");
|
|
|
9
79
|
class Country {
|
|
10
80
|
/**
|
|
11
81
|
* Creates a new Country instance.
|
|
12
|
-
* @param {string} _name - The
|
|
13
|
-
* @param {string}
|
|
82
|
+
* @param {string} _name - The common name of the country
|
|
83
|
+
* @param {string} _iso2 - The ISO-2 country code
|
|
84
|
+
* @param {string} _nameOfficial - The official name of the country
|
|
85
|
+
* @param {string} _iso3 - The ISO-3 country code
|
|
86
|
+
* @param {string} _flagEmoji - The flag emoji of the country
|
|
87
|
+
* @param {CONTINENT} _continent - The continent where the country is located
|
|
88
|
+
* @param {SUB_REGION} _region - The region within the continent where the country is located
|
|
89
|
+
* @param {string | null} _currencyCode - The ISO currency code used in the country
|
|
14
90
|
*/
|
|
15
|
-
constructor(_name,
|
|
91
|
+
constructor(_name, _iso2, _nameOfficial = "", _iso3, _flagEmoji = "", _continent = CONTINENT.EUROPE, _region = SUB_REGION.NORTHERN_EUROPE, _currencyCode = null) {
|
|
16
92
|
Object.defineProperty(this, "_name", {
|
|
17
93
|
enumerable: true,
|
|
18
94
|
configurable: true,
|
|
19
95
|
writable: true,
|
|
20
96
|
value: _name
|
|
21
97
|
});
|
|
22
|
-
Object.defineProperty(this, "
|
|
98
|
+
Object.defineProperty(this, "_iso2", {
|
|
99
|
+
enumerable: true,
|
|
100
|
+
configurable: true,
|
|
101
|
+
writable: true,
|
|
102
|
+
value: _iso2
|
|
103
|
+
});
|
|
104
|
+
Object.defineProperty(this, "_nameOfficial", {
|
|
105
|
+
enumerable: true,
|
|
106
|
+
configurable: true,
|
|
107
|
+
writable: true,
|
|
108
|
+
value: _nameOfficial
|
|
109
|
+
});
|
|
110
|
+
Object.defineProperty(this, "_iso3", {
|
|
111
|
+
enumerable: true,
|
|
112
|
+
configurable: true,
|
|
113
|
+
writable: true,
|
|
114
|
+
value: _iso3
|
|
115
|
+
});
|
|
116
|
+
Object.defineProperty(this, "_flagEmoji", {
|
|
117
|
+
enumerable: true,
|
|
118
|
+
configurable: true,
|
|
119
|
+
writable: true,
|
|
120
|
+
value: _flagEmoji
|
|
121
|
+
});
|
|
122
|
+
Object.defineProperty(this, "_continent", {
|
|
23
123
|
enumerable: true,
|
|
24
124
|
configurable: true,
|
|
25
125
|
writable: true,
|
|
26
|
-
value:
|
|
126
|
+
value: _continent
|
|
127
|
+
});
|
|
128
|
+
Object.defineProperty(this, "_region", {
|
|
129
|
+
enumerable: true,
|
|
130
|
+
configurable: true,
|
|
131
|
+
writable: true,
|
|
132
|
+
value: _region
|
|
133
|
+
});
|
|
134
|
+
Object.defineProperty(this, "_currencyCode", {
|
|
135
|
+
enumerable: true,
|
|
136
|
+
configurable: true,
|
|
137
|
+
writable: true,
|
|
138
|
+
value: _currencyCode
|
|
27
139
|
});
|
|
28
140
|
}
|
|
29
141
|
/**
|
|
30
|
-
* Gets the
|
|
31
|
-
* @returns {string} The
|
|
142
|
+
* Gets the common name of the country.
|
|
143
|
+
* @returns {string} The common name of the country
|
|
32
144
|
*/
|
|
33
145
|
get name() {
|
|
34
146
|
return this._name;
|
|
35
147
|
}
|
|
36
148
|
/**
|
|
37
|
-
* Gets the ISO code of the country.
|
|
38
|
-
* @returns {
|
|
149
|
+
* Gets the ISO-2 code of the country.
|
|
150
|
+
* @returns {ISO2CountryCode} The ISO-2 code of the country
|
|
39
151
|
*/
|
|
40
152
|
get code() {
|
|
41
|
-
return this.
|
|
153
|
+
return this._iso2;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Gets the official name of the country.
|
|
157
|
+
* @returns {string} The official name of the country
|
|
158
|
+
*/
|
|
159
|
+
get nameOfficial() {
|
|
160
|
+
return this._nameOfficial;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Gets the ISO-3 code of the country.
|
|
164
|
+
* @returns {ISO3CountryCode} The ISO-3 code of the country
|
|
165
|
+
*/
|
|
166
|
+
get iso3() {
|
|
167
|
+
return this._iso3;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Gets the flag emoji of the country.
|
|
171
|
+
* @returns {string} The flag emoji of the country
|
|
172
|
+
*/
|
|
173
|
+
get flagEmoji() {
|
|
174
|
+
return this._flagEmoji;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Gets the continent where the country is located.
|
|
178
|
+
* @returns {CONTINENT} The continent where the country is located
|
|
179
|
+
*/
|
|
180
|
+
get continent() {
|
|
181
|
+
return this._continent;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Gets the region within the continent where the country is located.
|
|
185
|
+
* @returns {SUB_REGION} The region within the continent where the country is located
|
|
186
|
+
*/
|
|
187
|
+
get region() {
|
|
188
|
+
return this._region;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Gets the ISO currency code used in the country.
|
|
192
|
+
* @returns {string | null} The ISO currency code of the country, or null if not available
|
|
193
|
+
*/
|
|
194
|
+
get currencyCode() {
|
|
195
|
+
return this._currencyCode;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Gets the Currency instance for this country.
|
|
199
|
+
* @returns {Currency | undefined} The Currency instance or undefined if no currency is assigned
|
|
200
|
+
*/
|
|
201
|
+
getCurrency() {
|
|
202
|
+
if (!this._currencyCode) {
|
|
203
|
+
return undefined;
|
|
204
|
+
}
|
|
205
|
+
return currency_js_1.Currency.fromCode(this._currencyCode);
|
|
42
206
|
}
|
|
43
207
|
/**
|
|
44
208
|
* Creates a string representation of the country.
|
|
@@ -48,12 +212,27 @@ class Country {
|
|
|
48
212
|
return `${this.name} (${this.code})`;
|
|
49
213
|
}
|
|
50
214
|
/**
|
|
51
|
-
*
|
|
52
|
-
* @
|
|
215
|
+
* Creates a detailed string representation of the country including the flag.
|
|
216
|
+
* @returns {string} Detailed string representation of the country
|
|
217
|
+
*/
|
|
218
|
+
toDetailedString() {
|
|
219
|
+
return `${this.flagEmoji} ${this.name} (${this.code}, ${this.iso3})`;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Retrieves a country by its ISO-2 or ISO-3 code.
|
|
223
|
+
* @param {CountryCode} code The ISO-2 or ISO-3 code of the country.
|
|
53
224
|
* @returns {Country | undefined} The country corresponding to the ISO code or `undefined` if not found.
|
|
54
225
|
*/
|
|
55
226
|
static fromCode(code) {
|
|
56
|
-
return
|
|
227
|
+
return CountryService.getInstance().fromCode(code);
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Retrieves a country by its ISO-3 code.
|
|
231
|
+
* @param {ISO3CountryCode} iso3 The ISO-3 code of the country.
|
|
232
|
+
* @returns {Country | undefined} The country corresponding to the ISO-3 code or `undefined` if not found.
|
|
233
|
+
*/
|
|
234
|
+
static fromIso3(iso3) {
|
|
235
|
+
return CountryService.getInstance().fromIso3(iso3);
|
|
57
236
|
}
|
|
58
237
|
/**
|
|
59
238
|
* Retrieves a country by its name.
|
|
@@ -61,25 +240,44 @@ class Country {
|
|
|
61
240
|
* @returns {Country | undefined} The country corresponding to the name or `undefined` if not found.
|
|
62
241
|
*/
|
|
63
242
|
static fromName(countryName) {
|
|
64
|
-
return
|
|
243
|
+
return CountryService.getInstance().fromName(countryName);
|
|
65
244
|
}
|
|
66
245
|
/**
|
|
67
246
|
* Returns all available countries.
|
|
68
247
|
* @returns {Country[]} Array of all countries
|
|
69
248
|
*/
|
|
70
249
|
static getAll() {
|
|
71
|
-
return
|
|
250
|
+
return CountryService.getInstance().getAll();
|
|
72
251
|
}
|
|
73
252
|
/**
|
|
74
|
-
*
|
|
75
|
-
* @param
|
|
76
|
-
* @returns
|
|
253
|
+
* Returns countries from a specific continent.
|
|
254
|
+
* @param {CONTINENT} continent The continent enum value
|
|
255
|
+
* @returns {Country[]} Array of countries in the specified continent
|
|
77
256
|
*/
|
|
78
|
-
static
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
257
|
+
static getByContinent(continent) {
|
|
258
|
+
return CountryService.getInstance().getByContinent(continent);
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Returns countries from a specific region.
|
|
262
|
+
* @param {SUB_REGION} region The region enum value
|
|
263
|
+
* @returns {Country[]} Array of countries in the specified region
|
|
264
|
+
*/
|
|
265
|
+
static getByRegion(region) {
|
|
266
|
+
return CountryService.getInstance().getByRegion(region);
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Returns a list of all available continents.
|
|
270
|
+
* @returns {CONTINENT[]} Array of continent enum values
|
|
271
|
+
*/
|
|
272
|
+
static getContinents() {
|
|
273
|
+
return Object.values(CONTINENT);
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Returns a list of all available regions.
|
|
277
|
+
* @returns {SUB_REGION[]} Array of region enum values
|
|
278
|
+
*/
|
|
279
|
+
static getRegions() {
|
|
280
|
+
return Object.values(SUB_REGION);
|
|
83
281
|
}
|
|
84
282
|
/**
|
|
85
283
|
* Validates if a given country name is valid
|
|
@@ -99,7 +297,7 @@ class Country {
|
|
|
99
297
|
validate() {
|
|
100
298
|
try {
|
|
101
299
|
return (Country.fromName(this._name) !== undefined &&
|
|
102
|
-
Country.fromCode(this.
|
|
300
|
+
Country.fromCode(this._iso2) !== undefined);
|
|
103
301
|
}
|
|
104
302
|
catch (_) {
|
|
105
303
|
return false;
|
|
@@ -115,9 +313,14 @@ class Country {
|
|
|
115
313
|
const country1 = Country.fromName(input);
|
|
116
314
|
if (country1)
|
|
117
315
|
return country1;
|
|
316
|
+
// deno-lint-ignore no-explicit-any
|
|
118
317
|
const country2 = Country.fromCode(input);
|
|
119
318
|
if (country2)
|
|
120
319
|
return country2;
|
|
320
|
+
// deno-lint-ignore no-explicit-any
|
|
321
|
+
const country3 = Country.fromIso3(input);
|
|
322
|
+
if (country3)
|
|
323
|
+
return country3;
|
|
121
324
|
}
|
|
122
325
|
return undefined;
|
|
123
326
|
}
|
|
@@ -132,9 +335,14 @@ class Country {
|
|
|
132
335
|
const text = input.trim();
|
|
133
336
|
if (text.length === 0)
|
|
134
337
|
return false;
|
|
135
|
-
const countryFromCode = Country.fromCode(text);
|
|
136
338
|
const countryFromName = Country.fromName(text);
|
|
137
|
-
|
|
339
|
+
// deno-lint-ignore no-explicit-any
|
|
340
|
+
const countryFromCode = Country.fromCode(text);
|
|
341
|
+
// deno-lint-ignore no-explicit-any
|
|
342
|
+
const countryFromIso3 = Country.fromIso3(text);
|
|
343
|
+
return countryFromCode !== undefined ||
|
|
344
|
+
countryFromName !== undefined ||
|
|
345
|
+
countryFromIso3 !== undefined;
|
|
138
346
|
}
|
|
139
347
|
/**
|
|
140
348
|
* Checks if an unknown value is a Country instance
|
|
@@ -145,13 +353,14 @@ class Country {
|
|
|
145
353
|
if (!obj || typeof obj !== "object")
|
|
146
354
|
return false;
|
|
147
355
|
const maybeCountry = obj;
|
|
356
|
+
console.log(maybeCountry);
|
|
148
357
|
// Check private properties exist with correct types
|
|
149
358
|
if (typeof maybeCountry._name !== "string")
|
|
150
359
|
return false;
|
|
151
|
-
if (typeof maybeCountry.
|
|
360
|
+
if (typeof maybeCountry._iso2 !== "string")
|
|
152
361
|
return false;
|
|
153
362
|
// Validate against known countries
|
|
154
|
-
const countryFromCode = Country.from(maybeCountry.
|
|
363
|
+
const countryFromCode = Country.from(maybeCountry._iso2);
|
|
155
364
|
const countryFromName = Country.from(maybeCountry._name);
|
|
156
365
|
return Boolean(countryFromCode &&
|
|
157
366
|
countryFromName &&
|
|
@@ -159,11 +368,378 @@ class Country {
|
|
|
159
368
|
}
|
|
160
369
|
}
|
|
161
370
|
exports.Country = Country;
|
|
371
|
+
/**
|
|
372
|
+
* Service for managing country data.
|
|
373
|
+
* @class CountryService
|
|
374
|
+
*/
|
|
375
|
+
class CountryService {
|
|
376
|
+
constructor() {
|
|
377
|
+
Object.defineProperty(this, "countryList", {
|
|
378
|
+
enumerable: true,
|
|
379
|
+
configurable: true,
|
|
380
|
+
writable: true,
|
|
381
|
+
value: []
|
|
382
|
+
});
|
|
383
|
+
Object.defineProperty(this, "codeRecord", {
|
|
384
|
+
enumerable: true,
|
|
385
|
+
configurable: true,
|
|
386
|
+
writable: true,
|
|
387
|
+
value: {}
|
|
388
|
+
});
|
|
389
|
+
Object.defineProperty(this, "iso3Record", {
|
|
390
|
+
enumerable: true,
|
|
391
|
+
configurable: true,
|
|
392
|
+
writable: true,
|
|
393
|
+
value: {}
|
|
394
|
+
});
|
|
395
|
+
Object.defineProperty(this, "nameRecord", {
|
|
396
|
+
enumerable: true,
|
|
397
|
+
configurable: true,
|
|
398
|
+
writable: true,
|
|
399
|
+
value: {}
|
|
400
|
+
});
|
|
401
|
+
Object.defineProperty(this, "continentRecord", {
|
|
402
|
+
enumerable: true,
|
|
403
|
+
configurable: true,
|
|
404
|
+
writable: true,
|
|
405
|
+
value: {}
|
|
406
|
+
});
|
|
407
|
+
Object.defineProperty(this, "regionRecord", {
|
|
408
|
+
enumerable: true,
|
|
409
|
+
configurable: true,
|
|
410
|
+
writable: true,
|
|
411
|
+
value: {}
|
|
412
|
+
});
|
|
413
|
+
// Static references for direct access through Country class
|
|
414
|
+
Object.defineProperty(this, "staticReferences", {
|
|
415
|
+
enumerable: true,
|
|
416
|
+
configurable: true,
|
|
417
|
+
writable: true,
|
|
418
|
+
value: new Map()
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Gets the singleton instance of CountryService.
|
|
423
|
+
* Creates the instance if it doesn't exist.
|
|
424
|
+
* @static
|
|
425
|
+
* @returns {CountryService} The singleton instance
|
|
426
|
+
*/
|
|
427
|
+
static getInstance() {
|
|
428
|
+
if (!CountryService.instance) {
|
|
429
|
+
CountryService.instance = new CountryService();
|
|
430
|
+
CountryService.instance.initialize();
|
|
431
|
+
}
|
|
432
|
+
return CountryService.instance;
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Maps a string continent name to the CONTINENT enum
|
|
436
|
+
* @param continentName String continent name from JSON
|
|
437
|
+
* @returns The corresponding CONTINENT enum value
|
|
438
|
+
*/
|
|
439
|
+
mapContinent(continentName) {
|
|
440
|
+
switch (continentName) {
|
|
441
|
+
case "Africa":
|
|
442
|
+
return CONTINENT.AFRICA;
|
|
443
|
+
case "Antarctica":
|
|
444
|
+
return CONTINENT.ANTARCTICA;
|
|
445
|
+
case "Asia":
|
|
446
|
+
return CONTINENT.ASIA;
|
|
447
|
+
case "Europe":
|
|
448
|
+
return CONTINENT.EUROPE;
|
|
449
|
+
case "North America":
|
|
450
|
+
return CONTINENT.NORTH_AMERICA;
|
|
451
|
+
case "Oceania":
|
|
452
|
+
return CONTINENT.OCEANIA;
|
|
453
|
+
case "South America":
|
|
454
|
+
return CONTINENT.SOUTH_AMERICA;
|
|
455
|
+
default:
|
|
456
|
+
return CONTINENT.EUROPE; // Default value
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Maps a string region name to the SUB_REGION enum
|
|
461
|
+
* @param regionName String region name from JSON
|
|
462
|
+
* @returns The corresponding SUB_REGION enum value
|
|
463
|
+
*/
|
|
464
|
+
mapRegion(regionName) {
|
|
465
|
+
switch (regionName) {
|
|
466
|
+
case "Australia and New Zealand":
|
|
467
|
+
return SUB_REGION.AUSTRALIA_AND_NEW_ZEALAND;
|
|
468
|
+
case "Caribbean":
|
|
469
|
+
return SUB_REGION.CARIBBEAN;
|
|
470
|
+
case "Central America":
|
|
471
|
+
return SUB_REGION.CENTRAL_AMERICA;
|
|
472
|
+
case "Central Asia":
|
|
473
|
+
return SUB_REGION.CENTRAL_ASIA;
|
|
474
|
+
case "Eastern Africa":
|
|
475
|
+
return SUB_REGION.EASTERN_AFRICA;
|
|
476
|
+
case "Eastern Asia":
|
|
477
|
+
return SUB_REGION.EASTERN_ASIA;
|
|
478
|
+
case "Eastern Europe":
|
|
479
|
+
return SUB_REGION.EASTERN_EUROPE;
|
|
480
|
+
case "Melanesia":
|
|
481
|
+
return SUB_REGION.MELANESIA;
|
|
482
|
+
case "Micronesia":
|
|
483
|
+
return SUB_REGION.MICRONESIA;
|
|
484
|
+
case "Middle Africa":
|
|
485
|
+
return SUB_REGION.MIDDLE_AFRICA;
|
|
486
|
+
case "Northern Africa":
|
|
487
|
+
return SUB_REGION.NORTHERN_AFRICA;
|
|
488
|
+
case "Northern America":
|
|
489
|
+
return SUB_REGION.NORTHERN_AMERICA;
|
|
490
|
+
case "Northern Europe":
|
|
491
|
+
return SUB_REGION.NORTHERN_EUROPE;
|
|
492
|
+
case "Polynesia":
|
|
493
|
+
return SUB_REGION.POLYNESIA;
|
|
494
|
+
case "South-eastern Asia":
|
|
495
|
+
return SUB_REGION.SOUTH_EASTERN_ASIA;
|
|
496
|
+
case "Southern Africa":
|
|
497
|
+
return SUB_REGION.SOUTHERN_AFRICA;
|
|
498
|
+
case "Southern Asia":
|
|
499
|
+
return SUB_REGION.SOUTHERN_ASIA;
|
|
500
|
+
case "Southern Europe":
|
|
501
|
+
return SUB_REGION.SOUTHERN_EUROPE;
|
|
502
|
+
case "Western Africa":
|
|
503
|
+
return SUB_REGION.WESTERN_AFRICA;
|
|
504
|
+
case "Western Asia":
|
|
505
|
+
return SUB_REGION.WESTERN_ASIA;
|
|
506
|
+
case "Western Europe":
|
|
507
|
+
return SUB_REGION.WESTERN_EUROPE;
|
|
508
|
+
default:
|
|
509
|
+
return SUB_REGION.NORTHERN_EUROPE; // Default value
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Initializes the service with country data.
|
|
514
|
+
* Should be called once when your application starts.
|
|
515
|
+
*/
|
|
516
|
+
initialize() {
|
|
517
|
+
try {
|
|
518
|
+
// Parse the JSON data
|
|
519
|
+
const data = JSON.parse(JSON.stringify(countries_js_1.default));
|
|
520
|
+
// deno-lint-ignore no-explicit-any
|
|
521
|
+
const countriesData = data.countries || [];
|
|
522
|
+
// Initialize continent and region records
|
|
523
|
+
Object.values(CONTINENT).forEach((continent) => {
|
|
524
|
+
this.continentRecord[continent] = [];
|
|
525
|
+
});
|
|
526
|
+
Object.values(SUB_REGION).forEach((region) => {
|
|
527
|
+
this.regionRecord[region] = [];
|
|
528
|
+
});
|
|
529
|
+
// Create Country instances from the data
|
|
530
|
+
const countries = countriesData.map((c) => {
|
|
531
|
+
const continent = this.mapContinent(c.continent);
|
|
532
|
+
const region = this.mapRegion(c.region);
|
|
533
|
+
return new Country(c.name, c.iso_2, c.name_official, c.iso_3, c.flag_emoji, continent, region, c.currency_iso_iso2);
|
|
534
|
+
});
|
|
535
|
+
const code_record = {};
|
|
536
|
+
const iso3_record = {};
|
|
537
|
+
const name_record = {};
|
|
538
|
+
countries.forEach((country) => {
|
|
539
|
+
// Populate code records
|
|
540
|
+
code_record[country.code] = country;
|
|
541
|
+
iso3_record[country.iso3] = country;
|
|
542
|
+
// Add to record by name
|
|
543
|
+
// Generate uppercase full name with underscores
|
|
544
|
+
const nameKey = country.name
|
|
545
|
+
.toUpperCase()
|
|
546
|
+
.replace(/\s+/g, "_")
|
|
547
|
+
.replace(/[-(),.']/g, "")
|
|
548
|
+
.replace(/&/g, "AND");
|
|
549
|
+
name_record[nameKey] = country;
|
|
550
|
+
// Group countries by continent
|
|
551
|
+
this.continentRecord[country.continent].push(country);
|
|
552
|
+
// Group countries by region
|
|
553
|
+
this.regionRecord[country.region].push(country);
|
|
554
|
+
this.staticReferences.set(country.code, country);
|
|
555
|
+
this.staticReferences.set(nameKey, country);
|
|
556
|
+
});
|
|
557
|
+
// Add specific country mappings for special cases
|
|
558
|
+
// Cocos Islands
|
|
559
|
+
if (code_record["CC"]) {
|
|
560
|
+
name_record["COCOS_ISLANDS"] = code_record["CC"];
|
|
561
|
+
this.staticReferences.set("COCOS_ISLANDS", code_record["CC"]);
|
|
562
|
+
}
|
|
563
|
+
// Cote d'Ivoire
|
|
564
|
+
if (code_record["CI"]) {
|
|
565
|
+
name_record["COTE_DIVOIRE"] = code_record["CI"];
|
|
566
|
+
this.staticReferences.set("COTE_DIVOIRE", code_record["CI"]);
|
|
567
|
+
}
|
|
568
|
+
// Macedonia (North Macedonia)
|
|
569
|
+
if (code_record["MK"]) {
|
|
570
|
+
name_record["MACEDONIA"] = code_record["MK"];
|
|
571
|
+
this.staticReferences.set("MACEDONIA", code_record["MK"]);
|
|
572
|
+
}
|
|
573
|
+
// US Virgin Islands
|
|
574
|
+
if (code_record["VI"]) {
|
|
575
|
+
name_record["VIRGIN_ISLANDS_US"] = code_record["VI"];
|
|
576
|
+
this.staticReferences.set("VIRGIN_ISLANDS_US", code_record["VI"]);
|
|
577
|
+
}
|
|
578
|
+
// British Virgin Islands
|
|
579
|
+
if (code_record["VG"]) {
|
|
580
|
+
name_record["VIRGIN_ISLANDS_BRITISH"] = code_record["VG"];
|
|
581
|
+
this.staticReferences.set("VIRGIN_ISLANDS_BRITISH", code_record["VG"]);
|
|
582
|
+
}
|
|
583
|
+
// Democratic Republic of the Congo
|
|
584
|
+
if (code_record["CD"]) {
|
|
585
|
+
name_record["DEMOCRATIC_REPUBLIC_OF_CONGO"] = code_record["CD"];
|
|
586
|
+
this.staticReferences.set("DEMOCRATIC_REPUBLIC_OF_CONGO", code_record["CD"]);
|
|
587
|
+
}
|
|
588
|
+
// Falkland Islands (Malvinas)
|
|
589
|
+
if (code_record["FK"]) {
|
|
590
|
+
name_record["FALKLAND_ISLANDS"] = code_record["FK"];
|
|
591
|
+
this.staticReferences.set("FALKLAND_ISLANDS", code_record["FK"]);
|
|
592
|
+
}
|
|
593
|
+
// Lao
|
|
594
|
+
if (code_record["LA"]) {
|
|
595
|
+
name_record["LAO"] = code_record["LA"];
|
|
596
|
+
this.staticReferences.set("LAO", code_record["LA"]);
|
|
597
|
+
}
|
|
598
|
+
this.codeRecord = code_record;
|
|
599
|
+
this.iso3Record = iso3_record;
|
|
600
|
+
this.nameRecord = name_record;
|
|
601
|
+
this.countryList = countries;
|
|
602
|
+
// Initialize static properties on Country class
|
|
603
|
+
this.initializeCountryStatics();
|
|
604
|
+
}
|
|
605
|
+
catch (error) {
|
|
606
|
+
console.error("Failed to initialize CountryService:", error);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Initialize the static properties on the Country class
|
|
611
|
+
*/
|
|
612
|
+
initializeCountryStatics() {
|
|
613
|
+
// Initialize ISO-2 code properties
|
|
614
|
+
Object.entries(this.codeRecord).forEach(([code, country]) => {
|
|
615
|
+
// deno-lint-ignore no-explicit-any
|
|
616
|
+
Country[code.toUpperCase()] = country;
|
|
617
|
+
});
|
|
618
|
+
// Initialize full name properties
|
|
619
|
+
Object.entries(this.nameRecord).forEach(([fullName, country]) => {
|
|
620
|
+
// deno-lint-ignore no-explicit-any
|
|
621
|
+
Country[fullName] = country;
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Gets all countries.
|
|
626
|
+
* @returns {Country[]} Array of all countries
|
|
627
|
+
*/
|
|
628
|
+
getAll() {
|
|
629
|
+
return this.countryList;
|
|
630
|
+
}
|
|
631
|
+
/**
|
|
632
|
+
* Gets static country references to be used by the Country class.
|
|
633
|
+
* @returns {Map<string, Country>} Map of static references
|
|
634
|
+
*/
|
|
635
|
+
getStaticReferences() {
|
|
636
|
+
return this.staticReferences;
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Gets all countries as a record.
|
|
640
|
+
* @returns {Record<string, Country>} Record of country codes and country objects
|
|
641
|
+
*/
|
|
642
|
+
getAllAsRecord() {
|
|
643
|
+
return this.codeRecord;
|
|
644
|
+
}
|
|
645
|
+
/**
|
|
646
|
+
* Gets all countries from a specific continent.
|
|
647
|
+
* @param {CONTINENT} continent The continent enum value
|
|
648
|
+
* @returns {Country[]} Array of countries in the specified continent
|
|
649
|
+
*/
|
|
650
|
+
getByContinent(continent) {
|
|
651
|
+
return this.continentRecord[continent] || [];
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* Gets all countries from a specific region.
|
|
655
|
+
* @param {SUB_REGION} region The region enum value
|
|
656
|
+
* @returns {Country[]} Array of countries in the specified region
|
|
657
|
+
*/
|
|
658
|
+
getByRegion(region) {
|
|
659
|
+
return this.regionRecord[region] || [];
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* Gets the full name record mapping.
|
|
663
|
+
* @returns {Record<string, Country>} Record of uppercase full name keys to country objects
|
|
664
|
+
*/
|
|
665
|
+
getFullNameRecord() {
|
|
666
|
+
return this.nameRecord;
|
|
667
|
+
}
|
|
668
|
+
/**
|
|
669
|
+
* Retrieves a country by its ISO-2 code.
|
|
670
|
+
* @param {string} code The ISO-2 code of the country.
|
|
671
|
+
* @returns {Country | undefined} The country corresponding to the ISO code or `undefined` if not found.
|
|
672
|
+
*/
|
|
673
|
+
fromCode(code) {
|
|
674
|
+
return this.codeRecord[code.toUpperCase()];
|
|
675
|
+
}
|
|
676
|
+
/**
|
|
677
|
+
* Retrieves a country by its ISO-3 code.
|
|
678
|
+
* @param {ISO3CountryCode} iso3 The ISO-3 code of the country.
|
|
679
|
+
* @returns {Country | undefined} The country corresponding to the ISO-3 code or `undefined` if not found.
|
|
680
|
+
*/
|
|
681
|
+
fromIso3(iso3) {
|
|
682
|
+
return this.iso3Record[iso3.toUpperCase()];
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Retrieves a country by its name.
|
|
686
|
+
* @param {string} countryName The name of the country.
|
|
687
|
+
* @returns {Country | undefined} The country corresponding to the name or `undefined` if not found.
|
|
688
|
+
*/
|
|
689
|
+
fromName(countryName) {
|
|
690
|
+
const fullNameKey = countryName
|
|
691
|
+
.toUpperCase()
|
|
692
|
+
.replace(/\s+/g, "_")
|
|
693
|
+
.replace(/[-(),.']/g, "")
|
|
694
|
+
.replace(/&/g, "AND");
|
|
695
|
+
const fullNameMatch = this.nameRecord[fullNameKey];
|
|
696
|
+
if (fullNameMatch)
|
|
697
|
+
return fullNameMatch;
|
|
698
|
+
// If not found, try more lenient matching
|
|
699
|
+
return this.countryList.find((country) => country.name.toLowerCase() === countryName.toLowerCase());
|
|
700
|
+
}
|
|
701
|
+
/**
|
|
702
|
+
* Searches for countries that match the given search term.
|
|
703
|
+
* @param {string} searchTerm - The partial name or code to search for.
|
|
704
|
+
* @param {number} [limit=10] - Maximum number of results to return.
|
|
705
|
+
* @returns {Country[]} Array of matching countries, limited to specified count.
|
|
706
|
+
*/
|
|
707
|
+
search(searchTerm, limit = 10) {
|
|
708
|
+
if (!searchTerm || typeof searchTerm !== "string")
|
|
709
|
+
return [];
|
|
710
|
+
const term = searchTerm.toLowerCase().trim();
|
|
711
|
+
if (term.length === 0)
|
|
712
|
+
return [];
|
|
713
|
+
const results = this.countryList.filter((country) => country.name.toLowerCase().includes(term) ||
|
|
714
|
+
country.nameOfficial.toLowerCase().includes(term) ||
|
|
715
|
+
country.code.toLowerCase().includes(term) ||
|
|
716
|
+
country.iso3.toLowerCase().includes(term));
|
|
717
|
+
return results.slice(0, limit);
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* Compares two Country instances for equality by checking their name and code
|
|
721
|
+
*
|
|
722
|
+
* @param {Country} country1 - First country to compare
|
|
723
|
+
* @param {Country} country2 - Second country to compare
|
|
724
|
+
* @returns {boolean} True if countries are equal, false otherwise
|
|
725
|
+
* @private
|
|
726
|
+
*/
|
|
727
|
+
compare(country1, country2) {
|
|
728
|
+
return (country1.name === country2.name &&
|
|
729
|
+
country1.code === country2.code);
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
exports.CountryService = CountryService;
|
|
162
733
|
// Initialize static properties by applying the references from CountryService
|
|
163
734
|
(function setupStaticReferences() {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
735
|
+
try {
|
|
736
|
+
const refs = CountryService.getInstance().getStaticReferences();
|
|
737
|
+
refs.forEach((country, key) => {
|
|
738
|
+
// deno-lint-ignore no-explicit-any
|
|
739
|
+
Country[key] = country;
|
|
740
|
+
});
|
|
741
|
+
}
|
|
742
|
+
catch (error) {
|
|
743
|
+
console.log("Failed to set up static references: ", error);
|
|
744
|
+
}
|
|
169
745
|
})();
|