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