@temboplus/frontend-core 0.2.5 → 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 +63 -14
- package/esm/src/data/countries.d.ts +12 -2
- package/esm/src/data/countries.js +496 -248
- 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 +162 -24
- package/esm/src/models/country/country.d.ts.map +1 -1
- package/esm/src/models/country/country.js +442 -42
- 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 +12 -2
- package/script/src/data/countries.js +496 -248
- 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 +162 -24
- package/script/src/models/country/country.d.ts.map +1 -1
- package/script/src/models/country/country.js +453 -49
- 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 -111
- package/esm/src/models/country/service.d.ts.map +0 -1
- package/esm/src/models/country/service.js +0 -366
- 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 -111
- package/script/src/models/country/service.d.ts.map +0 -1
- package/script/src/models/country/service.js +0 -373
- 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,31 @@
|
|
|
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";
|
|
2
29
|
/**
|
|
3
30
|
* Enum for continents
|
|
4
31
|
*/
|
|
@@ -47,25 +74,26 @@ export class Country {
|
|
|
47
74
|
/**
|
|
48
75
|
* Creates a new Country instance.
|
|
49
76
|
* @param {string} _name - The common name of the country
|
|
50
|
-
* @param {string}
|
|
77
|
+
* @param {string} _iso2 - The ISO-2 country code
|
|
51
78
|
* @param {string} _nameOfficial - The official name of the country
|
|
52
79
|
* @param {string} _iso3 - The ISO-3 country code
|
|
53
80
|
* @param {string} _flagEmoji - The flag emoji of the country
|
|
54
81
|
* @param {CONTINENT} _continent - The continent where the country is located
|
|
55
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
|
|
56
84
|
*/
|
|
57
|
-
constructor(_name,
|
|
85
|
+
constructor(_name, _iso2, _nameOfficial = "", _iso3, _flagEmoji = "", _continent = CONTINENT.EUROPE, _region = SUB_REGION.NORTHERN_EUROPE, _currencyCode = null) {
|
|
58
86
|
Object.defineProperty(this, "_name", {
|
|
59
87
|
enumerable: true,
|
|
60
88
|
configurable: true,
|
|
61
89
|
writable: true,
|
|
62
90
|
value: _name
|
|
63
91
|
});
|
|
64
|
-
Object.defineProperty(this, "
|
|
92
|
+
Object.defineProperty(this, "_iso2", {
|
|
65
93
|
enumerable: true,
|
|
66
94
|
configurable: true,
|
|
67
95
|
writable: true,
|
|
68
|
-
value:
|
|
96
|
+
value: _iso2
|
|
69
97
|
});
|
|
70
98
|
Object.defineProperty(this, "_nameOfficial", {
|
|
71
99
|
enumerable: true,
|
|
@@ -97,6 +125,12 @@ export class Country {
|
|
|
97
125
|
writable: true,
|
|
98
126
|
value: _region
|
|
99
127
|
});
|
|
128
|
+
Object.defineProperty(this, "_currencyCode", {
|
|
129
|
+
enumerable: true,
|
|
130
|
+
configurable: true,
|
|
131
|
+
writable: true,
|
|
132
|
+
value: _currencyCode
|
|
133
|
+
});
|
|
100
134
|
}
|
|
101
135
|
/**
|
|
102
136
|
* Gets the common name of the country.
|
|
@@ -107,10 +141,10 @@ export class Country {
|
|
|
107
141
|
}
|
|
108
142
|
/**
|
|
109
143
|
* Gets the ISO-2 code of the country.
|
|
110
|
-
* @returns {
|
|
144
|
+
* @returns {ISO2CountryCode} The ISO-2 code of the country
|
|
111
145
|
*/
|
|
112
146
|
get code() {
|
|
113
|
-
return this.
|
|
147
|
+
return this._iso2;
|
|
114
148
|
}
|
|
115
149
|
/**
|
|
116
150
|
* Gets the official name of the country.
|
|
@@ -121,7 +155,7 @@ export class Country {
|
|
|
121
155
|
}
|
|
122
156
|
/**
|
|
123
157
|
* Gets the ISO-3 code of the country.
|
|
124
|
-
* @returns {
|
|
158
|
+
* @returns {ISO3CountryCode} The ISO-3 code of the country
|
|
125
159
|
*/
|
|
126
160
|
get iso3() {
|
|
127
161
|
return this._iso3;
|
|
@@ -147,6 +181,23 @@ export class Country {
|
|
|
147
181
|
get region() {
|
|
148
182
|
return this._region;
|
|
149
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);
|
|
200
|
+
}
|
|
150
201
|
/**
|
|
151
202
|
* Creates a string representation of the country.
|
|
152
203
|
* @returns {string} String representation of the country
|
|
@@ -162,8 +213,8 @@ export class Country {
|
|
|
162
213
|
return `${this.flagEmoji} ${this.name} (${this.code}, ${this.iso3})`;
|
|
163
214
|
}
|
|
164
215
|
/**
|
|
165
|
-
* Retrieves a country by its ISO-2 code.
|
|
166
|
-
* @param {
|
|
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.
|
|
167
218
|
* @returns {Country | undefined} The country corresponding to the ISO code or `undefined` if not found.
|
|
168
219
|
*/
|
|
169
220
|
static fromCode(code) {
|
|
@@ -171,7 +222,7 @@ export class Country {
|
|
|
171
222
|
}
|
|
172
223
|
/**
|
|
173
224
|
* Retrieves a country by its ISO-3 code.
|
|
174
|
-
* @param {
|
|
225
|
+
* @param {ISO3CountryCode} iso3 The ISO-3 code of the country.
|
|
175
226
|
* @returns {Country | undefined} The country corresponding to the ISO-3 code or `undefined` if not found.
|
|
176
227
|
*/
|
|
177
228
|
static fromIso3(iso3) {
|
|
@@ -222,28 +273,6 @@ export class Country {
|
|
|
222
273
|
static getRegions() {
|
|
223
274
|
return Object.values(SUB_REGION);
|
|
224
275
|
}
|
|
225
|
-
/**
|
|
226
|
-
* Validates if a given ISO-2 country code is valid
|
|
227
|
-
* @param code The country code to validate
|
|
228
|
-
* @returns True if the country code is valid
|
|
229
|
-
*/
|
|
230
|
-
static isValidCode(code) {
|
|
231
|
-
if (!code)
|
|
232
|
-
return false;
|
|
233
|
-
const country = Country.fromCode(code);
|
|
234
|
-
return !!country;
|
|
235
|
-
}
|
|
236
|
-
/**
|
|
237
|
-
* Validates if a given ISO-3 country code is valid
|
|
238
|
-
* @param iso3 The ISO-3 country code to validate
|
|
239
|
-
* @returns True if the ISO-3 country code is valid
|
|
240
|
-
*/
|
|
241
|
-
static isValidIso3(iso3) {
|
|
242
|
-
if (!iso3)
|
|
243
|
-
return false;
|
|
244
|
-
const country = Country.fromIso3(iso3);
|
|
245
|
-
return !!country;
|
|
246
|
-
}
|
|
247
276
|
/**
|
|
248
277
|
* Validates if a given country name is valid
|
|
249
278
|
* @param countryName The country name to validate
|
|
@@ -262,7 +291,7 @@ export class Country {
|
|
|
262
291
|
validate() {
|
|
263
292
|
try {
|
|
264
293
|
return (Country.fromName(this._name) !== undefined &&
|
|
265
|
-
Country.fromCode(this.
|
|
294
|
+
Country.fromCode(this._iso2) !== undefined);
|
|
266
295
|
}
|
|
267
296
|
catch (_) {
|
|
268
297
|
return false;
|
|
@@ -278,9 +307,11 @@ export class Country {
|
|
|
278
307
|
const country1 = Country.fromName(input);
|
|
279
308
|
if (country1)
|
|
280
309
|
return country1;
|
|
310
|
+
// deno-lint-ignore no-explicit-any
|
|
281
311
|
const country2 = Country.fromCode(input);
|
|
282
312
|
if (country2)
|
|
283
313
|
return country2;
|
|
314
|
+
// deno-lint-ignore no-explicit-any
|
|
284
315
|
const country3 = Country.fromIso3(input);
|
|
285
316
|
if (country3)
|
|
286
317
|
return country3;
|
|
@@ -298,8 +329,10 @@ export class Country {
|
|
|
298
329
|
const text = input.trim();
|
|
299
330
|
if (text.length === 0)
|
|
300
331
|
return false;
|
|
301
|
-
const countryFromCode = Country.fromCode(text);
|
|
302
332
|
const countryFromName = Country.fromName(text);
|
|
333
|
+
// deno-lint-ignore no-explicit-any
|
|
334
|
+
const countryFromCode = Country.fromCode(text);
|
|
335
|
+
// deno-lint-ignore no-explicit-any
|
|
303
336
|
const countryFromIso3 = Country.fromIso3(text);
|
|
304
337
|
return countryFromCode !== undefined ||
|
|
305
338
|
countryFromName !== undefined ||
|
|
@@ -314,24 +347,391 @@ export class Country {
|
|
|
314
347
|
if (!obj || typeof obj !== "object")
|
|
315
348
|
return false;
|
|
316
349
|
const maybeCountry = obj;
|
|
350
|
+
console.log(maybeCountry);
|
|
317
351
|
// Check private properties exist with correct types
|
|
318
352
|
if (typeof maybeCountry._name !== "string")
|
|
319
353
|
return false;
|
|
320
|
-
if (typeof maybeCountry.
|
|
354
|
+
if (typeof maybeCountry._iso2 !== "string")
|
|
321
355
|
return false;
|
|
322
356
|
// Validate against known countries
|
|
323
|
-
const countryFromCode = Country.from(maybeCountry.
|
|
357
|
+
const countryFromCode = Country.from(maybeCountry._iso2);
|
|
324
358
|
const countryFromName = Country.from(maybeCountry._name);
|
|
325
359
|
return Boolean(countryFromCode &&
|
|
326
360
|
countryFromName &&
|
|
327
361
|
countryFromCode.code === countryFromName.code);
|
|
328
362
|
}
|
|
329
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
|
+
}
|
|
330
725
|
// Initialize static properties by applying the references from CountryService
|
|
331
726
|
(function setupStaticReferences() {
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
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
|
+
}
|
|
337
737
|
})();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/src/models/country/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/src/models/country/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export * from "./country.js";
|
|
2
|
-
export * from "./
|
|
2
|
+
export * from "./index.js";
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Type definitions for ISO country codes
|
|
3
|
+
*
|
|
4
|
+
* These types provide type-safe representations of standard ISO codes:
|
|
5
|
+
* - CountryCode: ISO 3166-1 alpha-2 country codes
|
|
6
|
+
* - CountryCodeISO3: ISO 3166-1 alpha-3 country codes (three letters)
|
|
7
|
+
*
|
|
8
|
+
* Benefits:
|
|
9
|
+
* - Type-safety at compile time
|
|
10
|
+
* - Zero runtime overhead (compile away to strings)
|
|
11
|
+
* - Autocompletion in modern IDEs
|
|
12
|
+
* - Protection against typos and invalid values
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* ISO 3166-1 alpha-2 country codes
|
|
16
|
+
*
|
|
17
|
+
* These two-letter codes represent countries and territories based on the ISO 3166 standard.
|
|
18
|
+
* Example: "US" for United States, "JP" for Japan, "DE" for Germany
|
|
19
|
+
*/
|
|
20
|
+
export type ISO2CountryCode = "AF" | "AX" | "AL" | "DZ" | "AS" | "AD" | "AO" | "AI" | "AQ" | "AG" | "AR" | "AM" | "AW" | "AU" | "AT" | "AZ" | "BS" | "BH" | "BD" | "BB" | "BY" | "BE" | "BZ" | "BJ" | "BM" | "BT" | "BO" | "BA" | "BW" | "BV" | "BR" | "IO" | "VG" | "BN" | "BG" | "BF" | "BI" | "CV" | "KH" | "CM" | "CA" | "KY" | "CF" | "TD" | "CL" | "CN" | "CX" | "CC" | "CO" | "KM" | "CG" | "CK" | "CR" | "CI" | "HR" | "CU" | "CY" | "CZ" | "CD" | "DK" | "DJ" | "DM" | "DO" | "EC" | "EG" | "SV" | "GQ" | "ER" | "EE" | "SZ" | "ET" | "FK" | "FO" | "FJ" | "FI" | "FR" | "GF" | "PF" | "TF" | "GA" | "GM" | "GE" | "DE" | "GH" | "GI" | "GR" | "GL" | "GD" | "GP" | "GU" | "GT" | "GG" | "GN" | "GW" | "GY" | "HT" | "HM" | "VA" | "HN" | "HK" | "HU" | "IS" | "IN" | "ID" | "IR" | "IQ" | "IE" | "IM" | "IL" | "IT" | "JM" | "JP" | "JE" | "JO" | "KZ" | "KE" | "KI" | "KP" | "KR" | "KW" | "KG" | "LA" | "LV" | "LB" | "LS" | "LR" | "LY" | "LI" | "LT" | "LU" | "MO" | "MG" | "MW" | "MY" | "MV" | "ML" | "MT" | "MH" | "MQ" | "MR" | "MU" | "YT" | "MX" | "FM" | "MD" | "MC" | "MN" | "ME" | "MS" | "MA" | "MZ" | "MM" | "NA" | "NR" | "NP" | "NL" | "NC" | "NZ" | "NI" | "NE" | "NG" | "NU" | "NF" | "MK" | "MP" | "NO" | "OM" | "PK" | "PW" | "PS" | "PA" | "PG" | "PY" | "PE" | "PH" | "PN" | "PL" | "PT" | "PR" | "QA" | "RE" | "RO" | "RU" | "RW" | "SH" | "KN" | "LC" | "PM" | "VC" | "WS" | "SM" | "ST" | "SA" | "SN" | "RS" | "SC" | "SL" | "SG" | "SK" | "SI" | "SB" | "SO" | "ZA" | "GS" | "SS" | "ES" | "LK" | "SD" | "SR" | "SJ" | "SE" | "CH" | "SY" | "TW" | "TJ" | "TZ" | "TH" | "TL" | "TG" | "TK" | "TO" | "TT" | "TN" | "TR" | "TM" | "TC" | "TV" | "VI" | "UG" | "UA" | "AE" | "GB" | "US" | "UM" | "UY" | "UZ" | "VU" | "VE" | "VN" | "WF" | "EH" | "YE" | "ZM" | "ZW";
|
|
21
|
+
/**
|
|
22
|
+
* ISO 3166-1 alpha-3 country codes
|
|
23
|
+
*
|
|
24
|
+
* These three-letter codes represent countries and territories based on the ISO 3166 standard.
|
|
25
|
+
* Example: "USA" for United States, "JPN" for Japan, "DEU" for Germany
|
|
26
|
+
*/
|
|
27
|
+
export type ISO3CountryCode = "AFG" | "ALA" | "ALB" | "DZA" | "ASM" | "AND" | "AGO" | "AIA" | "ATA" | "ATG" | "ARG" | "ARM" | "ABW" | "AUS" | "AUT" | "AZE" | "BHS" | "BHR" | "BGD" | "BRB" | "BLR" | "BEL" | "BLZ" | "BEN" | "BMU" | "BTN" | "BOL" | "BIH" | "BWA" | "BVT" | "BRA" | "IOT" | "VGB" | "BRN" | "BGR" | "BFA" | "BDI" | "CPV" | "KHM" | "CMR" | "CAN" | "CYM" | "CAF" | "TCD" | "CHL" | "CHN" | "CXR" | "CCK" | "COL" | "COM" | "COG" | "COK" | "CRI" | "CIV" | "HRV" | "CUB" | "CYP" | "CZE" | "COD" | "DNK" | "DJI" | "DMA" | "DOM" | "ECU" | "EGY" | "SLV" | "GNQ" | "ERI" | "EST" | "SWZ" | "ETH" | "FLK" | "FRO" | "FJI" | "FIN" | "FRA" | "GUF" | "PYF" | "ATF" | "GAB" | "GMB" | "GEO" | "DEU" | "GHA" | "GIB" | "GRC" | "GRL" | "GRD" | "GLP" | "GUM" | "GTM" | "GGY" | "GIN" | "GNB" | "GUY" | "HTI" | "HMD" | "VAT" | "HND" | "HKG" | "HUN" | "ISL" | "IND" | "IDN" | "IRN" | "IRQ" | "IRL" | "IMN" | "ISR" | "ITA" | "JAM" | "JPN" | "JEY" | "JOR" | "KAZ" | "KEN" | "KIR" | "KWT" | "KGZ" | "LAO" | "LVA" | "LBN" | "LSO" | "LBR" | "LBY" | "LIE" | "LTU" | "LUX" | "MAC" | "MDG" | "MWI" | "MYS" | "MDV" | "MLI" | "MLT" | "MHL" | "MTQ" | "MRT" | "MUS" | "MYT" | "MEX" | "FSM" | "MDA" | "MCO" | "MNG" | "MNE" | "MSR" | "MAR" | "MOZ" | "MMR" | "NAM" | "NRU" | "NPL" | "NLD" | "NCL" | "NZL" | "NIC" | "NER" | "NGA" | "NIU" | "NFK" | "PRK" | "MKD" | "MNP" | "NOR" | "OMN" | "PAK" | "PLW" | "PSE" | "PAN" | "PNG" | "PRY" | "PER" | "PHL" | "PCN" | "POL" | "PRT" | "PRI" | "QAT" | "REU" | "ROU" | "RUS" | "RWA" | "SHN" | "KNA" | "LCA" | "SPM" | "VCT" | "WSM" | "SMR" | "STP" | "SAU" | "SEN" | "SRB" | "SYC" | "SLE" | "SGP" | "SVK" | "SVN" | "SLB" | "SOM" | "ZAF" | "SGS" | "KOR" | "SSD" | "ESP" | "LKA" | "SDN" | "SUR" | "SJM" | "SWE" | "CHE" | "SYR" | "TWN" | "TJK" | "TZA" | "THA" | "TLS" | "TGO" | "TKL" | "TON" | "TTO" | "TUN" | "TUR" | "TKM" | "TCA" | "TUV" | "VIR" | "UGA" | "UKR" | "ARE" | "GBR" | "USA" | "UMI" | "URY" | "UZB" | "VUT" | "VEN" | "VNM" | "WLF" | "ESH" | "YEM" | "ZMB" | "ZWE";
|
|
28
|
+
/**
|
|
29
|
+
* Maps ISO-2 country codes to ISO-3 country codes
|
|
30
|
+
*/
|
|
31
|
+
export declare const ISO2_TO_ISO3_MAP: Record<ISO2CountryCode, ISO3CountryCode>;
|
|
32
|
+
/**
|
|
33
|
+
* Represents an ISO country code, accepting either ISO 3166-1 alpha-2 (two-letter)
|
|
34
|
+
* or ISO 3166-1 alpha-3 (three-letter) formats.
|
|
35
|
+
*
|
|
36
|
+
* This union type allows functions to accept either format for flexibility,
|
|
37
|
+
* while still maintaining type safety and validation.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* // Valid uses:
|
|
41
|
+
* const usa1: CountryCode = "US"; // ISO2
|
|
42
|
+
* const usa2: CountryCode = "USA"; // ISO3
|
|
43
|
+
* const japan1: CountryCode = "JP"; // ISO2
|
|
44
|
+
* const japan2: CountryCode = "JPN"; // ISO3
|
|
45
|
+
*
|
|
46
|
+
* // Type checking will catch invalid codes:
|
|
47
|
+
* const invalid: CountryCode = "INVALID"; // Error
|
|
48
|
+
*
|
|
49
|
+
* @see ISO2CountryCode - For two-letter country codes only
|
|
50
|
+
* @see ISO3CountryCode - For three-letter country codes only
|
|
51
|
+
*/
|
|
52
|
+
export type CountryCode = ISO2CountryCode | ISO3CountryCode;
|
|
53
|
+
//# sourceMappingURL=types.d.ts.map
|