@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,7 +1,37 @@
|
|
|
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 = exports.SUB_REGION = exports.CONTINENT = 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"));
|
|
5
35
|
/**
|
|
6
36
|
* Enum for continents
|
|
7
37
|
*/
|
|
@@ -50,25 +80,26 @@ class Country {
|
|
|
50
80
|
/**
|
|
51
81
|
* Creates a new Country instance.
|
|
52
82
|
* @param {string} _name - The common name of the country
|
|
53
|
-
* @param {string}
|
|
83
|
+
* @param {string} _iso2 - The ISO-2 country code
|
|
54
84
|
* @param {string} _nameOfficial - The official name of the country
|
|
55
85
|
* @param {string} _iso3 - The ISO-3 country code
|
|
56
86
|
* @param {string} _flagEmoji - The flag emoji of the country
|
|
57
87
|
* @param {CONTINENT} _continent - The continent where the country is located
|
|
58
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
|
|
59
90
|
*/
|
|
60
|
-
constructor(_name,
|
|
91
|
+
constructor(_name, _iso2, _nameOfficial = "", _iso3, _flagEmoji = "", _continent = CONTINENT.EUROPE, _region = SUB_REGION.NORTHERN_EUROPE, _currencyCode = null) {
|
|
61
92
|
Object.defineProperty(this, "_name", {
|
|
62
93
|
enumerable: true,
|
|
63
94
|
configurable: true,
|
|
64
95
|
writable: true,
|
|
65
96
|
value: _name
|
|
66
97
|
});
|
|
67
|
-
Object.defineProperty(this, "
|
|
98
|
+
Object.defineProperty(this, "_iso2", {
|
|
68
99
|
enumerable: true,
|
|
69
100
|
configurable: true,
|
|
70
101
|
writable: true,
|
|
71
|
-
value:
|
|
102
|
+
value: _iso2
|
|
72
103
|
});
|
|
73
104
|
Object.defineProperty(this, "_nameOfficial", {
|
|
74
105
|
enumerable: true,
|
|
@@ -100,6 +131,12 @@ class Country {
|
|
|
100
131
|
writable: true,
|
|
101
132
|
value: _region
|
|
102
133
|
});
|
|
134
|
+
Object.defineProperty(this, "_currencyCode", {
|
|
135
|
+
enumerable: true,
|
|
136
|
+
configurable: true,
|
|
137
|
+
writable: true,
|
|
138
|
+
value: _currencyCode
|
|
139
|
+
});
|
|
103
140
|
}
|
|
104
141
|
/**
|
|
105
142
|
* Gets the common name of the country.
|
|
@@ -110,10 +147,10 @@ class Country {
|
|
|
110
147
|
}
|
|
111
148
|
/**
|
|
112
149
|
* Gets the ISO-2 code of the country.
|
|
113
|
-
* @returns {
|
|
150
|
+
* @returns {ISO2CountryCode} The ISO-2 code of the country
|
|
114
151
|
*/
|
|
115
152
|
get code() {
|
|
116
|
-
return this.
|
|
153
|
+
return this._iso2;
|
|
117
154
|
}
|
|
118
155
|
/**
|
|
119
156
|
* Gets the official name of the country.
|
|
@@ -124,7 +161,7 @@ class Country {
|
|
|
124
161
|
}
|
|
125
162
|
/**
|
|
126
163
|
* Gets the ISO-3 code of the country.
|
|
127
|
-
* @returns {
|
|
164
|
+
* @returns {ISO3CountryCode} The ISO-3 code of the country
|
|
128
165
|
*/
|
|
129
166
|
get iso3() {
|
|
130
167
|
return this._iso3;
|
|
@@ -150,6 +187,23 @@ class Country {
|
|
|
150
187
|
get region() {
|
|
151
188
|
return this._region;
|
|
152
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);
|
|
206
|
+
}
|
|
153
207
|
/**
|
|
154
208
|
* Creates a string representation of the country.
|
|
155
209
|
* @returns {string} String representation of the country
|
|
@@ -165,20 +219,20 @@ class Country {
|
|
|
165
219
|
return `${this.flagEmoji} ${this.name} (${this.code}, ${this.iso3})`;
|
|
166
220
|
}
|
|
167
221
|
/**
|
|
168
|
-
* Retrieves a country by its ISO-2 code.
|
|
169
|
-
* @param {
|
|
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.
|
|
170
224
|
* @returns {Country | undefined} The country corresponding to the ISO code or `undefined` if not found.
|
|
171
225
|
*/
|
|
172
226
|
static fromCode(code) {
|
|
173
|
-
return
|
|
227
|
+
return CountryService.getInstance().fromCode(code);
|
|
174
228
|
}
|
|
175
229
|
/**
|
|
176
230
|
* Retrieves a country by its ISO-3 code.
|
|
177
|
-
* @param {
|
|
231
|
+
* @param {ISO3CountryCode} iso3 The ISO-3 code of the country.
|
|
178
232
|
* @returns {Country | undefined} The country corresponding to the ISO-3 code or `undefined` if not found.
|
|
179
233
|
*/
|
|
180
234
|
static fromIso3(iso3) {
|
|
181
|
-
return
|
|
235
|
+
return CountryService.getInstance().fromIso3(iso3);
|
|
182
236
|
}
|
|
183
237
|
/**
|
|
184
238
|
* Retrieves a country by its name.
|
|
@@ -186,14 +240,14 @@ class Country {
|
|
|
186
240
|
* @returns {Country | undefined} The country corresponding to the name or `undefined` if not found.
|
|
187
241
|
*/
|
|
188
242
|
static fromName(countryName) {
|
|
189
|
-
return
|
|
243
|
+
return CountryService.getInstance().fromName(countryName);
|
|
190
244
|
}
|
|
191
245
|
/**
|
|
192
246
|
* Returns all available countries.
|
|
193
247
|
* @returns {Country[]} Array of all countries
|
|
194
248
|
*/
|
|
195
249
|
static getAll() {
|
|
196
|
-
return
|
|
250
|
+
return CountryService.getInstance().getAll();
|
|
197
251
|
}
|
|
198
252
|
/**
|
|
199
253
|
* Returns countries from a specific continent.
|
|
@@ -201,7 +255,7 @@ class Country {
|
|
|
201
255
|
* @returns {Country[]} Array of countries in the specified continent
|
|
202
256
|
*/
|
|
203
257
|
static getByContinent(continent) {
|
|
204
|
-
return
|
|
258
|
+
return CountryService.getInstance().getByContinent(continent);
|
|
205
259
|
}
|
|
206
260
|
/**
|
|
207
261
|
* Returns countries from a specific region.
|
|
@@ -209,7 +263,7 @@ class Country {
|
|
|
209
263
|
* @returns {Country[]} Array of countries in the specified region
|
|
210
264
|
*/
|
|
211
265
|
static getByRegion(region) {
|
|
212
|
-
return
|
|
266
|
+
return CountryService.getInstance().getByRegion(region);
|
|
213
267
|
}
|
|
214
268
|
/**
|
|
215
269
|
* Returns a list of all available continents.
|
|
@@ -225,28 +279,6 @@ class Country {
|
|
|
225
279
|
static getRegions() {
|
|
226
280
|
return Object.values(SUB_REGION);
|
|
227
281
|
}
|
|
228
|
-
/**
|
|
229
|
-
* Validates if a given ISO-2 country code is valid
|
|
230
|
-
* @param code The country code to validate
|
|
231
|
-
* @returns True if the country code is valid
|
|
232
|
-
*/
|
|
233
|
-
static isValidCode(code) {
|
|
234
|
-
if (!code)
|
|
235
|
-
return false;
|
|
236
|
-
const country = Country.fromCode(code);
|
|
237
|
-
return !!country;
|
|
238
|
-
}
|
|
239
|
-
/**
|
|
240
|
-
* Validates if a given ISO-3 country code is valid
|
|
241
|
-
* @param iso3 The ISO-3 country code to validate
|
|
242
|
-
* @returns True if the ISO-3 country code is valid
|
|
243
|
-
*/
|
|
244
|
-
static isValidIso3(iso3) {
|
|
245
|
-
if (!iso3)
|
|
246
|
-
return false;
|
|
247
|
-
const country = Country.fromIso3(iso3);
|
|
248
|
-
return !!country;
|
|
249
|
-
}
|
|
250
282
|
/**
|
|
251
283
|
* Validates if a given country name is valid
|
|
252
284
|
* @param countryName The country name to validate
|
|
@@ -265,7 +297,7 @@ class Country {
|
|
|
265
297
|
validate() {
|
|
266
298
|
try {
|
|
267
299
|
return (Country.fromName(this._name) !== undefined &&
|
|
268
|
-
Country.fromCode(this.
|
|
300
|
+
Country.fromCode(this._iso2) !== undefined);
|
|
269
301
|
}
|
|
270
302
|
catch (_) {
|
|
271
303
|
return false;
|
|
@@ -281,9 +313,11 @@ class Country {
|
|
|
281
313
|
const country1 = Country.fromName(input);
|
|
282
314
|
if (country1)
|
|
283
315
|
return country1;
|
|
316
|
+
// deno-lint-ignore no-explicit-any
|
|
284
317
|
const country2 = Country.fromCode(input);
|
|
285
318
|
if (country2)
|
|
286
319
|
return country2;
|
|
320
|
+
// deno-lint-ignore no-explicit-any
|
|
287
321
|
const country3 = Country.fromIso3(input);
|
|
288
322
|
if (country3)
|
|
289
323
|
return country3;
|
|
@@ -301,8 +335,10 @@ class Country {
|
|
|
301
335
|
const text = input.trim();
|
|
302
336
|
if (text.length === 0)
|
|
303
337
|
return false;
|
|
304
|
-
const countryFromCode = Country.fromCode(text);
|
|
305
338
|
const countryFromName = Country.fromName(text);
|
|
339
|
+
// deno-lint-ignore no-explicit-any
|
|
340
|
+
const countryFromCode = Country.fromCode(text);
|
|
341
|
+
// deno-lint-ignore no-explicit-any
|
|
306
342
|
const countryFromIso3 = Country.fromIso3(text);
|
|
307
343
|
return countryFromCode !== undefined ||
|
|
308
344
|
countryFromName !== undefined ||
|
|
@@ -317,13 +353,14 @@ class Country {
|
|
|
317
353
|
if (!obj || typeof obj !== "object")
|
|
318
354
|
return false;
|
|
319
355
|
const maybeCountry = obj;
|
|
356
|
+
console.log(maybeCountry);
|
|
320
357
|
// Check private properties exist with correct types
|
|
321
358
|
if (typeof maybeCountry._name !== "string")
|
|
322
359
|
return false;
|
|
323
|
-
if (typeof maybeCountry.
|
|
360
|
+
if (typeof maybeCountry._iso2 !== "string")
|
|
324
361
|
return false;
|
|
325
362
|
// Validate against known countries
|
|
326
|
-
const countryFromCode = Country.from(maybeCountry.
|
|
363
|
+
const countryFromCode = Country.from(maybeCountry._iso2);
|
|
327
364
|
const countryFromName = Country.from(maybeCountry._name);
|
|
328
365
|
return Boolean(countryFromCode &&
|
|
329
366
|
countryFromName &&
|
|
@@ -331,11 +368,378 @@ class Country {
|
|
|
331
368
|
}
|
|
332
369
|
}
|
|
333
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;
|
|
334
733
|
// Initialize static properties by applying the references from CountryService
|
|
335
734
|
(function setupStaticReferences() {
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
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
|
+
}
|
|
341
745
|
})();
|
|
@@ -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"}
|
|
@@ -15,4 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./country.js"), exports);
|
|
18
|
-
__exportStar(require("./
|
|
18
|
+
__exportStar(require("./index.js"), exports);
|