@temboplus/frontend-core 0.2.1 → 0.2.3
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 +126 -25
- package/esm/deps/jsr.io/@std/internal/1.0.5/assertion_state.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/testing/1.0.9/_test_suite.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/testing/1.0.9/bdd.d.ts.map +1 -0
- package/esm/mod.d.ts +2 -0
- package/esm/mod.d.ts.map +1 -1
- package/esm/mod.js +2 -0
- package/esm/src/config/config_service.d.ts +11 -0
- package/esm/src/config/config_service.d.ts.map +1 -0
- package/esm/src/config/config_service.js +19 -0
- package/esm/src/config/index.d.ts +2 -0
- package/esm/src/config/index.d.ts.map +1 -0
- package/esm/src/config/index.js +1 -0
- package/esm/src/{models/bank/banks.d.ts → data/banks_tz.d.ts} +1 -1
- package/esm/src/data/banks_tz.d.ts.map +1 -0
- package/esm/src/data/countries.d.ts +6 -0
- package/esm/src/data/countries.d.ts.map +1 -0
- package/esm/src/data/countries.js +974 -0
- package/esm/src/data/currencies.d.ts +1901 -0
- package/esm/src/data/currencies.d.ts.map +1 -0
- package/esm/src/data/currencies.js +1073 -0
- package/esm/src/data/phone_patterns.d.ts +3809 -0
- package/esm/src/data/phone_patterns.d.ts.map +1 -0
- package/esm/src/data/phone_patterns.js +2325 -0
- package/esm/src/models/amount/amount.d.ts +10 -12
- package/esm/src/models/amount/amount.d.ts.map +1 -1
- package/esm/src/models/amount/amount.js +58 -96
- package/esm/src/models/bank/bank.d.ts +58 -53
- package/esm/src/models/bank/bank.d.ts.map +1 -1
- package/esm/src/models/bank/bank.js +49 -84
- package/esm/src/models/bank/index.d.ts +1 -1
- package/esm/src/models/bank/index.d.ts.map +1 -1
- package/esm/src/models/bank/index.js +1 -1
- package/esm/src/models/bank/service.d.ts +106 -0
- package/esm/src/models/bank/service.d.ts.map +1 -0
- package/esm/src/models/bank/service.js +240 -0
- package/esm/src/models/country/country.d.ts +568 -0
- package/esm/src/models/country/country.d.ts.map +1 -0
- package/esm/src/models/country/country.js +165 -0
- package/esm/src/models/country/country.test.d.ts.map +1 -0
- package/esm/src/models/country/index.d.ts +3 -0
- package/esm/src/models/country/index.d.ts.map +1 -0
- package/esm/src/models/country/index.js +2 -0
- package/esm/src/models/country/service.d.ts +75 -0
- package/esm/src/models/country/service.d.ts.map +1 -0
- package/esm/src/models/country/service.js +267 -0
- package/esm/src/models/currency/currency.d.ts +341 -0
- package/esm/src/models/currency/currency.d.ts.map +1 -0
- package/esm/src/models/currency/currency.js +225 -0
- package/esm/src/models/currency/currency.test.d.ts.map +1 -0
- package/esm/src/models/currency/index.d.ts +3 -0
- package/esm/src/models/currency/index.d.ts.map +1 -0
- package/esm/src/models/currency/index.js +2 -0
- package/esm/src/models/currency/service.d.ts +96 -0
- package/esm/src/models/currency/service.d.ts.map +1 -0
- package/esm/src/models/currency/service.js +194 -0
- package/esm/src/models/index.d.ts +2 -0
- package/esm/src/models/index.d.ts.map +1 -1
- package/esm/src/models/index.js +2 -0
- package/esm/src/models/phone_number/format.d.ts +14 -0
- package/esm/src/models/phone_number/format.d.ts.map +1 -0
- package/esm/src/models/phone_number/format.js +14 -0
- package/esm/src/models/phone_number/global/phone_number.d.ts +160 -0
- package/esm/src/models/phone_number/global/phone_number.d.ts.map +1 -0
- package/esm/src/models/phone_number/global/phone_number.js +453 -0
- package/esm/src/models/phone_number/global/phone_number.test.d.ts.map +1 -0
- package/esm/src/models/phone_number/global/service.d.ts +260 -0
- package/esm/src/models/phone_number/global/service.d.ts.map +1 -0
- package/esm/src/models/phone_number/global/service.js +477 -0
- package/esm/src/models/phone_number/global/service.test.d.ts.map +1 -0
- package/esm/src/models/phone_number/index.d.ts +5 -3
- package/esm/src/models/phone_number/index.d.ts.map +1 -1
- package/esm/src/models/phone_number/index.js +5 -3
- package/esm/src/models/phone_number/{antd_validator.d.ts → tz/antd_validator.d.ts} +2 -2
- package/esm/src/models/phone_number/tz/antd_validator.d.ts.map +1 -0
- package/esm/src/models/phone_number/{antd_validator.js → tz/antd_validator.js} +2 -2
- package/esm/src/models/phone_number/tz/antdvalidator.test.d.ts.map +1 -0
- package/esm/src/models/phone_number/tz/network_operator.d.ts.map +1 -0
- package/{script/src/models/phone_number → esm/src/models/phone_number/tz}/phone_number.d.ts +46 -29
- package/esm/src/models/phone_number/tz/phone_number.d.ts.map +1 -0
- package/esm/src/models/phone_number/{phone_number.js → tz/phone_number.js} +87 -41
- package/esm/src/models/phone_number/tz/phone_number.test.d.ts.map +1 -0
- package/esm/src/reports/index.d.ts +2 -0
- package/esm/src/reports/index.d.ts.map +1 -0
- package/esm/src/reports/index.js +1 -0
- package/esm/src/reports/report_manager.d.ts +144 -0
- package/esm/src/reports/report_manager.d.ts.map +1 -0
- package/esm/src/reports/report_manager.js +315 -0
- package/package.json +4 -3
- package/script/deps/jsr.io/@std/internal/1.0.5/assertion_state.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/testing/1.0.9/_test_suite.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/testing/1.0.9/bdd.d.ts.map +1 -0
- package/script/mod.d.ts +2 -0
- package/script/mod.d.ts.map +1 -1
- package/script/mod.js +2 -0
- package/script/src/config/config_service.d.ts +11 -0
- package/script/src/config/config_service.d.ts.map +1 -0
- package/script/src/config/config_service.js +23 -0
- package/script/src/config/index.d.ts +2 -0
- package/script/src/config/index.d.ts.map +1 -0
- package/script/src/config/index.js +17 -0
- package/script/src/{models/bank/banks.d.ts → data/banks_tz.d.ts} +1 -1
- package/script/src/data/banks_tz.d.ts.map +1 -0
- package/script/src/data/countries.d.ts +6 -0
- package/script/src/data/countries.d.ts.map +1 -0
- package/script/src/data/countries.js +976 -0
- package/script/src/data/currencies.d.ts +1901 -0
- package/script/src/data/currencies.d.ts.map +1 -0
- package/script/src/data/currencies.js +1075 -0
- package/script/src/data/phone_patterns.d.ts +3809 -0
- package/script/src/data/phone_patterns.d.ts.map +1 -0
- package/script/src/data/phone_patterns.js +2327 -0
- package/script/src/models/amount/amount.d.ts +10 -12
- package/script/src/models/amount/amount.d.ts.map +1 -1
- package/script/src/models/amount/amount.js +60 -98
- package/script/src/models/bank/bank.d.ts +58 -53
- package/script/src/models/bank/bank.d.ts.map +1 -1
- package/script/src/models/bank/bank.js +49 -87
- package/script/src/models/bank/index.d.ts +1 -1
- package/script/src/models/bank/index.d.ts.map +1 -1
- package/script/src/models/bank/index.js +1 -1
- package/script/src/models/bank/service.d.ts +106 -0
- package/script/src/models/bank/service.d.ts.map +1 -0
- package/script/src/models/bank/service.js +247 -0
- package/script/src/models/country/country.d.ts +568 -0
- package/script/src/models/country/country.d.ts.map +1 -0
- package/script/src/models/country/country.js +169 -0
- package/script/src/models/country/country.test.d.ts.map +1 -0
- package/script/src/models/country/index.d.ts +3 -0
- package/script/src/models/country/index.d.ts.map +1 -0
- package/script/src/models/country/index.js +18 -0
- package/script/src/models/country/service.d.ts +75 -0
- package/script/src/models/country/service.d.ts.map +1 -0
- package/script/src/models/country/service.js +274 -0
- package/script/src/models/currency/currency.d.ts +341 -0
- package/script/src/models/currency/currency.d.ts.map +1 -0
- package/script/src/models/currency/currency.js +229 -0
- package/script/src/models/currency/currency.test.d.ts.map +1 -0
- package/script/src/models/currency/index.d.ts +3 -0
- package/script/src/models/currency/index.d.ts.map +1 -0
- package/script/src/models/currency/index.js +18 -0
- package/script/src/models/currency/service.d.ts +96 -0
- package/script/src/models/currency/service.d.ts.map +1 -0
- package/script/src/models/currency/service.js +201 -0
- package/script/src/models/index.d.ts +2 -0
- package/script/src/models/index.d.ts.map +1 -1
- package/script/src/models/index.js +2 -0
- package/script/src/models/phone_number/format.d.ts +14 -0
- package/script/src/models/phone_number/format.d.ts.map +1 -0
- package/script/src/models/phone_number/format.js +17 -0
- package/script/src/models/phone_number/global/phone_number.d.ts +160 -0
- package/script/src/models/phone_number/global/phone_number.d.ts.map +1 -0
- package/script/src/models/phone_number/global/phone_number.js +457 -0
- package/script/src/models/phone_number/global/phone_number.test.d.ts.map +1 -0
- package/script/src/models/phone_number/global/service.d.ts +260 -0
- package/script/src/models/phone_number/global/service.d.ts.map +1 -0
- package/script/src/models/phone_number/global/service.js +485 -0
- package/script/src/models/phone_number/global/service.test.d.ts.map +1 -0
- package/script/src/models/phone_number/index.d.ts +5 -3
- package/script/src/models/phone_number/index.d.ts.map +1 -1
- package/script/src/models/phone_number/index.js +5 -3
- package/script/src/models/phone_number/{antd_validator.d.ts → tz/antd_validator.d.ts} +2 -2
- package/script/src/models/phone_number/tz/antd_validator.d.ts.map +1 -0
- package/script/src/models/phone_number/{antd_validator.js → tz/antd_validator.js} +1 -1
- package/script/src/models/phone_number/tz/antdvalidator.test.d.ts.map +1 -0
- package/script/src/models/phone_number/tz/network_operator.d.ts.map +1 -0
- package/{esm/src/models/phone_number → script/src/models/phone_number/tz}/phone_number.d.ts +46 -29
- package/script/src/models/phone_number/tz/phone_number.d.ts.map +1 -0
- package/script/src/models/phone_number/{phone_number.js → tz/phone_number.js} +89 -43
- package/script/src/models/phone_number/tz/phone_number.test.d.ts.map +1 -0
- package/script/src/reports/index.d.ts +2 -0
- package/script/src/reports/index.d.ts.map +1 -0
- package/script/src/reports/index.js +17 -0
- package/script/src/reports/report_manager.d.ts +144 -0
- package/script/src/reports/report_manager.d.ts.map +1 -0
- package/script/src/reports/report_manager.js +325 -0
- package/esm/src/models/bank/banks.d.ts.map +0 -1
- package/esm/src/models/bank/utils.d.ts +0 -25
- package/esm/src/models/bank/utils.d.ts.map +0 -1
- package/esm/src/models/bank/utils.js +0 -35
- package/esm/src/models/phone_number/antd_validator.d.ts.map +0 -1
- package/esm/src/models/phone_number/antdvalidator.test.d.ts.map +0 -1
- package/esm/src/models/phone_number/network_operator.d.ts.map +0 -1
- package/esm/src/models/phone_number/phone_number.d.ts.map +0 -1
- package/esm/src/models/phone_number/phone_number.test.d.ts.map +0 -1
- package/script/src/models/bank/banks.d.ts.map +0 -1
- package/script/src/models/bank/utils.d.ts +0 -25
- package/script/src/models/bank/utils.d.ts.map +0 -1
- package/script/src/models/bank/utils.js +0 -41
- package/script/src/models/phone_number/antd_validator.d.ts.map +0 -1
- package/script/src/models/phone_number/antdvalidator.test.d.ts.map +0 -1
- package/script/src/models/phone_number/network_operator.d.ts.map +0 -1
- package/script/src/models/phone_number/phone_number.d.ts.map +0 -1
- package/script/src/models/phone_number/phone_number.test.d.ts.map +0 -1
- /package/esm/src/{models/bank/banks.js → data/banks_tz.js} +0 -0
- /package/esm/src/models/phone_number/{network_operator.d.ts → tz/network_operator.d.ts} +0 -0
- /package/esm/src/models/phone_number/{network_operator.js → tz/network_operator.js} +0 -0
- /package/script/src/{models/bank/banks.js → data/banks_tz.js} +0 -0
- /package/script/src/models/phone_number/{network_operator.d.ts → tz/network_operator.d.ts} +0 -0
- /package/script/src/models/phone_number/{network_operator.js → tz/network_operator.js} +0 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { CountryService } from "./service.js";
|
|
2
|
+
/**
|
|
3
|
+
* Represents a country with essential details.
|
|
4
|
+
* @class Country
|
|
5
|
+
*/
|
|
6
|
+
export class Country {
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new Country instance.
|
|
9
|
+
* @param {string} _name - The full name of the country
|
|
10
|
+
* @param {string} _code - The ISO country code
|
|
11
|
+
*/
|
|
12
|
+
constructor(_name, _code) {
|
|
13
|
+
Object.defineProperty(this, "_name", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
configurable: true,
|
|
16
|
+
writable: true,
|
|
17
|
+
value: _name
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(this, "_code", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
configurable: true,
|
|
22
|
+
writable: true,
|
|
23
|
+
value: _code
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Gets the full name of the country.
|
|
28
|
+
* @returns {string} The full name of the country
|
|
29
|
+
*/
|
|
30
|
+
get name() {
|
|
31
|
+
return this._name;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Gets the ISO code of the country.
|
|
35
|
+
* @returns {string} The ISO code of the country
|
|
36
|
+
*/
|
|
37
|
+
get code() {
|
|
38
|
+
return this._code;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Creates a string representation of the country.
|
|
42
|
+
* @returns {string} String representation of the country
|
|
43
|
+
*/
|
|
44
|
+
toString() {
|
|
45
|
+
return `${this.name} (${this.code})`;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Retrieves a country by its ISO code.
|
|
49
|
+
* @param {string} code The ISO code of the country.
|
|
50
|
+
* @returns {Country | undefined} The country corresponding to the ISO code or `undefined` if not found.
|
|
51
|
+
*/
|
|
52
|
+
static fromCode(code) {
|
|
53
|
+
return CountryService.getInstance().fromCode(code);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Retrieves a country by its name.
|
|
57
|
+
* @param {string} countryName The name of the country.
|
|
58
|
+
* @returns {Country | undefined} The country corresponding to the name or `undefined` if not found.
|
|
59
|
+
*/
|
|
60
|
+
static fromName(countryName) {
|
|
61
|
+
return CountryService.getInstance().fromName(countryName);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Returns all available countries.
|
|
65
|
+
* @returns {Country[]} Array of all countries
|
|
66
|
+
*/
|
|
67
|
+
static getAll() {
|
|
68
|
+
return CountryService.getInstance().getAll();
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Validates if a given ISO country code is valid
|
|
72
|
+
* @param code The country code to validate
|
|
73
|
+
* @returns True if the country code is valid
|
|
74
|
+
*/
|
|
75
|
+
static isValidCode(code) {
|
|
76
|
+
if (!code)
|
|
77
|
+
return false;
|
|
78
|
+
const country = Country.fromCode(code);
|
|
79
|
+
return !!country;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Validates if a given country name is valid
|
|
83
|
+
* @param countryName The country name to validate
|
|
84
|
+
* @returns True if the country name is valid
|
|
85
|
+
*/
|
|
86
|
+
static isValidName(countryName) {
|
|
87
|
+
if (!countryName)
|
|
88
|
+
return false;
|
|
89
|
+
const country = Country.fromName(countryName);
|
|
90
|
+
return !!country;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Checks the validity of the country data
|
|
94
|
+
* @returns true if the country information is available and valid
|
|
95
|
+
*/
|
|
96
|
+
validate() {
|
|
97
|
+
try {
|
|
98
|
+
return (Country.fromName(this._name) !== undefined &&
|
|
99
|
+
Country.fromCode(this._code) !== undefined);
|
|
100
|
+
}
|
|
101
|
+
catch (_) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Attempts to create a Country instance from a country name or ISO code
|
|
107
|
+
* @param input The country name or ISO code
|
|
108
|
+
* @returns A Country instance if valid input, undefined otherwise
|
|
109
|
+
*/
|
|
110
|
+
static from(input) {
|
|
111
|
+
if (Country.canConstruct(input)) {
|
|
112
|
+
const country1 = Country.fromName(input);
|
|
113
|
+
if (country1)
|
|
114
|
+
return country1;
|
|
115
|
+
const country2 = Country.fromCode(input);
|
|
116
|
+
if (country2)
|
|
117
|
+
return country2;
|
|
118
|
+
}
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Validates if the input can be used to construct a valid Country instance
|
|
123
|
+
* @param input The country name or ISO code to validate
|
|
124
|
+
* @returns True if input can construct a valid country, false otherwise
|
|
125
|
+
*/
|
|
126
|
+
static canConstruct(input) {
|
|
127
|
+
if (!input || typeof input !== "string")
|
|
128
|
+
return false;
|
|
129
|
+
const text = input.trim();
|
|
130
|
+
if (text.length === 0)
|
|
131
|
+
return false;
|
|
132
|
+
const countryFromCode = Country.fromCode(text);
|
|
133
|
+
const countryFromName = Country.fromName(text);
|
|
134
|
+
return countryFromCode !== undefined || countryFromName !== undefined;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Checks if an unknown value is a Country instance
|
|
138
|
+
* @param obj The value to validate
|
|
139
|
+
* @returns Type predicate indicating if the value is a valid Country
|
|
140
|
+
*/
|
|
141
|
+
static is(obj) {
|
|
142
|
+
if (!obj || typeof obj !== "object")
|
|
143
|
+
return false;
|
|
144
|
+
const maybeCountry = obj;
|
|
145
|
+
// Check private properties exist with correct types
|
|
146
|
+
if (typeof maybeCountry._name !== "string")
|
|
147
|
+
return false;
|
|
148
|
+
if (typeof maybeCountry._code !== "string")
|
|
149
|
+
return false;
|
|
150
|
+
// Validate against known countries
|
|
151
|
+
const countryFromCode = Country.from(maybeCountry._code);
|
|
152
|
+
const countryFromName = Country.from(maybeCountry._name);
|
|
153
|
+
return Boolean(countryFromCode &&
|
|
154
|
+
countryFromName &&
|
|
155
|
+
countryFromCode.code === countryFromName.code);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
// Initialize static properties by applying the references from CountryService
|
|
159
|
+
(function setupStaticReferences() {
|
|
160
|
+
const staticRefs = CountryService.getInstance().getStaticReferences();
|
|
161
|
+
staticRefs.forEach((country, key) => {
|
|
162
|
+
// deno-lint-ignore no-explicit-any
|
|
163
|
+
Country[key] = country;
|
|
164
|
+
});
|
|
165
|
+
})();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"country.test.d.ts","sourceRoot":"","sources":["../../../../src/src/models/country/country.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/src/models/country/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,cAAc,CAAA"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Country } from "./country.js";
|
|
2
|
+
/**
|
|
3
|
+
* Service for managing country data.
|
|
4
|
+
* @class CountryService
|
|
5
|
+
*/
|
|
6
|
+
export declare class CountryService {
|
|
7
|
+
private static instance;
|
|
8
|
+
private countryList;
|
|
9
|
+
private codeRecord;
|
|
10
|
+
private nameRecord;
|
|
11
|
+
private fullNameRecord;
|
|
12
|
+
private staticReferences;
|
|
13
|
+
private constructor();
|
|
14
|
+
/**
|
|
15
|
+
* Gets the singleton instance of CountryService.
|
|
16
|
+
* Creates the instance if it doesn't exist.
|
|
17
|
+
* @static
|
|
18
|
+
* @returns {CountryService} The singleton instance
|
|
19
|
+
*/
|
|
20
|
+
static getInstance(): CountryService;
|
|
21
|
+
/**
|
|
22
|
+
* Initializes the service with country data.
|
|
23
|
+
* Should be called once when your application starts.
|
|
24
|
+
*/
|
|
25
|
+
private initialize;
|
|
26
|
+
/**
|
|
27
|
+
* Gets all countries.
|
|
28
|
+
* @returns {Country[]} Array of all countries
|
|
29
|
+
*/
|
|
30
|
+
getAll(): Country[];
|
|
31
|
+
/**
|
|
32
|
+
* Gets all countries as a record.
|
|
33
|
+
* @returns {Record<string, Country>} Record of country codes and country objects
|
|
34
|
+
*/
|
|
35
|
+
getAllAsRecord(): Record<string, Country>;
|
|
36
|
+
/**
|
|
37
|
+
* Gets static country references to be used by the Country class.
|
|
38
|
+
* @returns {Map<string, Country>} Map of static references
|
|
39
|
+
*/
|
|
40
|
+
getStaticReferences(): Map<string, Country>;
|
|
41
|
+
/**
|
|
42
|
+
* Gets the full name record mapping.
|
|
43
|
+
* @returns {Record<string, Country>} Record of uppercase full name keys to country objects
|
|
44
|
+
*/
|
|
45
|
+
getFullNameRecord(): Record<string, Country>;
|
|
46
|
+
/**
|
|
47
|
+
* Retrieves a country by its ISO code.
|
|
48
|
+
* @param {string} code The ISO code of the country.
|
|
49
|
+
* @returns {Country | undefined} The country corresponding to the ISO code or `undefined` if not found.
|
|
50
|
+
*/
|
|
51
|
+
fromCode(code: string): Country | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* Retrieves a country by its name.
|
|
54
|
+
* @param {string} countryName The name of the country.
|
|
55
|
+
* @returns {Country | undefined} The country corresponding to the name or `undefined` if not found.
|
|
56
|
+
*/
|
|
57
|
+
fromName(countryName: string): Country | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* Searches for countries that match the given search term.
|
|
60
|
+
* @param {string} searchTerm - The partial name or code to search for.
|
|
61
|
+
* @param {number} [limit=10] - Maximum number of results to return.
|
|
62
|
+
* @returns {Country[]} Array of matching countries, limited to specified count.
|
|
63
|
+
*/
|
|
64
|
+
search(searchTerm: string, limit?: number): Country[];
|
|
65
|
+
/**
|
|
66
|
+
* Compares two Country instances for equality by checking their name and code
|
|
67
|
+
*
|
|
68
|
+
* @param {Country} country1 - First country to compare
|
|
69
|
+
* @param {Country} country2 - Second country to compare
|
|
70
|
+
* @returns {boolean} True if countries are equal, false otherwise
|
|
71
|
+
* @private
|
|
72
|
+
*/
|
|
73
|
+
compare(country1: Country, country2: Country): boolean;
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../../../src/src/models/country/service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAGvC;;;GAGG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAiB;IACxC,OAAO,CAAC,WAAW,CAAiB;IACpC,OAAO,CAAC,UAAU,CAA+B;IACjD,OAAO,CAAC,UAAU,CAA+B;IACjD,OAAO,CAAC,cAAc,CAA+B;IAGrD,OAAO,CAAC,gBAAgB,CAAmC;IAE3D,OAAO;IAEP;;;;;OAKG;IACH,MAAM,CAAC,WAAW,IAAI,cAAc;IAQpC;;;OAGG;IACH,OAAO,CAAC,UAAU;IA2JlB;;;OAGG;IACH,MAAM,IAAI,OAAO,EAAE;IAInB;;;OAGG;IACH,cAAc,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAIzC;;;OAGG;IACH,mBAAmB,IAAI,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC;IAI3C;;;OAGG;IACH,iBAAiB,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAI5C;;;;OAIG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,GAAG,SAAS;IAI3C;;;;OAIG;IACH,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,GAAG,SAAS;IAqBlD;;;;;OAKG;IACH,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,KAAK,GAAE,MAAW,GAAG,OAAO,EAAE;IAczD;;;;;;;OAOG;IACH,OAAO,CAAC,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,GAAG,OAAO;CAMvD"}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { Country } from "./country.js";
|
|
2
|
+
import file from "../../data/countries.js";
|
|
3
|
+
/**
|
|
4
|
+
* Service for managing country data.
|
|
5
|
+
* @class CountryService
|
|
6
|
+
*/
|
|
7
|
+
export class CountryService {
|
|
8
|
+
constructor() {
|
|
9
|
+
Object.defineProperty(this, "countryList", {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
writable: true,
|
|
13
|
+
value: []
|
|
14
|
+
});
|
|
15
|
+
Object.defineProperty(this, "codeRecord", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
writable: true,
|
|
19
|
+
value: {}
|
|
20
|
+
});
|
|
21
|
+
Object.defineProperty(this, "nameRecord", {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
writable: true,
|
|
25
|
+
value: {}
|
|
26
|
+
});
|
|
27
|
+
Object.defineProperty(this, "fullNameRecord", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
configurable: true,
|
|
30
|
+
writable: true,
|
|
31
|
+
value: {}
|
|
32
|
+
});
|
|
33
|
+
// Static references for direct access through Country class
|
|
34
|
+
Object.defineProperty(this, "staticReferences", {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
configurable: true,
|
|
37
|
+
writable: true,
|
|
38
|
+
value: new Map()
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Gets the singleton instance of CountryService.
|
|
43
|
+
* Creates the instance if it doesn't exist.
|
|
44
|
+
* @static
|
|
45
|
+
* @returns {CountryService} The singleton instance
|
|
46
|
+
*/
|
|
47
|
+
static getInstance() {
|
|
48
|
+
if (!CountryService.instance) {
|
|
49
|
+
CountryService.instance = new CountryService();
|
|
50
|
+
CountryService.instance.initialize();
|
|
51
|
+
}
|
|
52
|
+
return CountryService.instance;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Initializes the service with country data.
|
|
56
|
+
* Should be called once when your application starts.
|
|
57
|
+
*/
|
|
58
|
+
initialize() {
|
|
59
|
+
try {
|
|
60
|
+
const data = JSON.parse(JSON.stringify(file));
|
|
61
|
+
const countries = Object.values(data).map((c) => new Country(c.name, c.code));
|
|
62
|
+
const code_record = {};
|
|
63
|
+
const name_record = {};
|
|
64
|
+
const fullname_record = {};
|
|
65
|
+
countries.forEach((country) => {
|
|
66
|
+
// Populate code record
|
|
67
|
+
code_record[country.code] = country;
|
|
68
|
+
// Add to record by name
|
|
69
|
+
name_record[country.name.toUpperCase()] = country;
|
|
70
|
+
// Generate uppercase full name with underscores
|
|
71
|
+
const fullNameKey = country.name
|
|
72
|
+
.toUpperCase()
|
|
73
|
+
.replace(/\s+/g, "_")
|
|
74
|
+
.replace(/[-(),.']/g, "")
|
|
75
|
+
.replace(/&/g, "AND");
|
|
76
|
+
fullname_record[fullNameKey] = country;
|
|
77
|
+
// Save references for static access
|
|
78
|
+
this.staticReferences.set(country.code.toUpperCase(), country);
|
|
79
|
+
this.staticReferences.set(fullNameKey, country);
|
|
80
|
+
});
|
|
81
|
+
// Ensure specific mappings are available
|
|
82
|
+
if (code_record["US"]) {
|
|
83
|
+
fullname_record["UNITED_STATES"] = code_record["US"];
|
|
84
|
+
this.staticReferences.set("UNITED_STATES", code_record["US"]);
|
|
85
|
+
}
|
|
86
|
+
// Add specific country mappings
|
|
87
|
+
// Cocos Islands
|
|
88
|
+
if (code_record["CC"]) {
|
|
89
|
+
fullname_record["COCOS_ISLANDS"] = code_record["CC"];
|
|
90
|
+
this.staticReferences.set("COCOS_ISLANDS", code_record["CC"]);
|
|
91
|
+
}
|
|
92
|
+
// Democratic Republic of Congo
|
|
93
|
+
if (code_record["CD"]) {
|
|
94
|
+
fullname_record["DEMOCRATIC_REPUBLIC_OF_CONGO"] = code_record["CD"];
|
|
95
|
+
this.staticReferences.set("DEMOCRATIC_REPUBLIC_OF_CONGO", code_record["CD"]);
|
|
96
|
+
}
|
|
97
|
+
// Cote d'Ivoire
|
|
98
|
+
if (code_record["CI"]) {
|
|
99
|
+
fullname_record["COTE_DIVOIRE"] = code_record["CI"];
|
|
100
|
+
this.staticReferences.set("COTE_DIVOIRE", code_record["CI"]);
|
|
101
|
+
}
|
|
102
|
+
// Falkland Islands
|
|
103
|
+
if (code_record["FK"]) {
|
|
104
|
+
fullname_record["FALKLAND_ISLANDS"] = code_record["FK"];
|
|
105
|
+
this.staticReferences.set("FALKLAND_ISLANDS", code_record["FK"]);
|
|
106
|
+
}
|
|
107
|
+
// Holy See (Vatican)
|
|
108
|
+
if (code_record["VA"]) {
|
|
109
|
+
fullname_record["HOLY_SEE"] = code_record["VA"];
|
|
110
|
+
this.staticReferences.set("HOLY_SEE", code_record["VA"]);
|
|
111
|
+
}
|
|
112
|
+
// Iran
|
|
113
|
+
if (code_record["IR"]) {
|
|
114
|
+
fullname_record["IRAN"] = code_record["IR"];
|
|
115
|
+
this.staticReferences.set("IRAN", code_record["IR"]);
|
|
116
|
+
}
|
|
117
|
+
// North Korea
|
|
118
|
+
if (code_record["KP"]) {
|
|
119
|
+
fullname_record["NORTH_KOREA"] = code_record["KP"];
|
|
120
|
+
this.staticReferences.set("NORTH_KOREA", code_record["KP"]);
|
|
121
|
+
}
|
|
122
|
+
// South Korea
|
|
123
|
+
if (code_record["KR"]) {
|
|
124
|
+
fullname_record["SOUTH_KOREA"] = code_record["KR"];
|
|
125
|
+
this.staticReferences.set("SOUTH_KOREA", code_record["KR"]);
|
|
126
|
+
}
|
|
127
|
+
// Lao People's Democratic Republic
|
|
128
|
+
if (code_record["LA"]) {
|
|
129
|
+
fullname_record["LAO"] = code_record["LA"];
|
|
130
|
+
this.staticReferences.set("LAO", code_record["LA"]);
|
|
131
|
+
}
|
|
132
|
+
// Palestine
|
|
133
|
+
if (code_record["PS"]) {
|
|
134
|
+
fullname_record["PALESTINE"] = code_record["PS"];
|
|
135
|
+
this.staticReferences.set("PALESTINE", code_record["PS"]);
|
|
136
|
+
}
|
|
137
|
+
// Macedonia
|
|
138
|
+
if (code_record["MK"]) {
|
|
139
|
+
fullname_record["MACEDONIA"] = code_record["MK"];
|
|
140
|
+
this.staticReferences.set("MACEDONIA", code_record["MK"]);
|
|
141
|
+
}
|
|
142
|
+
// Micronesia
|
|
143
|
+
if (code_record["FM"]) {
|
|
144
|
+
fullname_record["MICRONESIA"] = code_record["FM"];
|
|
145
|
+
this.staticReferences.set("MICRONESIA", code_record["FM"]);
|
|
146
|
+
}
|
|
147
|
+
// Moldova
|
|
148
|
+
if (code_record["MD"]) {
|
|
149
|
+
fullname_record["MOLDOVA"] = code_record["MD"];
|
|
150
|
+
this.staticReferences.set("MOLDOVA", code_record["MD"]);
|
|
151
|
+
}
|
|
152
|
+
// Taiwan
|
|
153
|
+
if (code_record["TW"]) {
|
|
154
|
+
fullname_record["TAIWAN"] = code_record["TW"];
|
|
155
|
+
this.staticReferences.set("TAIWAN", code_record["TW"]);
|
|
156
|
+
}
|
|
157
|
+
// Tanzania
|
|
158
|
+
if (code_record["TZ"]) {
|
|
159
|
+
fullname_record["TANZANIA"] = code_record["TZ"];
|
|
160
|
+
this.staticReferences.set("TANZANIA", code_record["TZ"]);
|
|
161
|
+
}
|
|
162
|
+
// US Virgin Islands
|
|
163
|
+
if (code_record["VI"]) {
|
|
164
|
+
fullname_record["VIRGIN_ISLANDS_US"] = code_record["VI"];
|
|
165
|
+
this.staticReferences.set("VIRGIN_ISLANDS_US", code_record["VI"]);
|
|
166
|
+
}
|
|
167
|
+
// British Virgin Islands
|
|
168
|
+
if (code_record["VG"]) {
|
|
169
|
+
fullname_record["VIRGIN_ISLANDS_BRITISH"] = code_record["VG"];
|
|
170
|
+
this.staticReferences.set("VIRGIN_ISLANDS_BRITISH", code_record["VG"]);
|
|
171
|
+
}
|
|
172
|
+
this.codeRecord = code_record;
|
|
173
|
+
this.nameRecord = name_record;
|
|
174
|
+
this.fullNameRecord = fullname_record;
|
|
175
|
+
this.countryList = countries;
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
console.error("Failed to initialize CountryService:", error);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Gets all countries.
|
|
183
|
+
* @returns {Country[]} Array of all countries
|
|
184
|
+
*/
|
|
185
|
+
getAll() {
|
|
186
|
+
return this.countryList;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Gets all countries as a record.
|
|
190
|
+
* @returns {Record<string, Country>} Record of country codes and country objects
|
|
191
|
+
*/
|
|
192
|
+
getAllAsRecord() {
|
|
193
|
+
return this.codeRecord;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Gets static country references to be used by the Country class.
|
|
197
|
+
* @returns {Map<string, Country>} Map of static references
|
|
198
|
+
*/
|
|
199
|
+
getStaticReferences() {
|
|
200
|
+
return this.staticReferences;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Gets the full name record mapping.
|
|
204
|
+
* @returns {Record<string, Country>} Record of uppercase full name keys to country objects
|
|
205
|
+
*/
|
|
206
|
+
getFullNameRecord() {
|
|
207
|
+
return this.fullNameRecord;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Retrieves a country by its ISO code.
|
|
211
|
+
* @param {string} code The ISO code of the country.
|
|
212
|
+
* @returns {Country | undefined} The country corresponding to the ISO code or `undefined` if not found.
|
|
213
|
+
*/
|
|
214
|
+
fromCode(code) {
|
|
215
|
+
return this.codeRecord[code.toUpperCase()];
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Retrieves a country by its name.
|
|
219
|
+
* @param {string} countryName The name of the country.
|
|
220
|
+
* @returns {Country | undefined} The country corresponding to the name or `undefined` if not found.
|
|
221
|
+
*/
|
|
222
|
+
fromName(countryName) {
|
|
223
|
+
// First try direct lookup in name record
|
|
224
|
+
const directMatch = this.nameRecord[countryName.toUpperCase()];
|
|
225
|
+
if (directMatch)
|
|
226
|
+
return directMatch;
|
|
227
|
+
// Then try full name record
|
|
228
|
+
const fullNameKey = countryName
|
|
229
|
+
.toUpperCase()
|
|
230
|
+
.replace(/\s+/g, "_")
|
|
231
|
+
.replace(/[-(),.']/g, "")
|
|
232
|
+
.replace(/&/g, "AND");
|
|
233
|
+
const fullNameMatch = this.fullNameRecord[fullNameKey];
|
|
234
|
+
if (fullNameMatch)
|
|
235
|
+
return fullNameMatch;
|
|
236
|
+
// If not found, try more lenient matching
|
|
237
|
+
return this.countryList.find((country) => country.name.toLowerCase() === countryName.toLowerCase());
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Searches for countries that match the given search term.
|
|
241
|
+
* @param {string} searchTerm - The partial name or code to search for.
|
|
242
|
+
* @param {number} [limit=10] - Maximum number of results to return.
|
|
243
|
+
* @returns {Country[]} Array of matching countries, limited to specified count.
|
|
244
|
+
*/
|
|
245
|
+
search(searchTerm, limit = 10) {
|
|
246
|
+
if (!searchTerm || typeof searchTerm !== "string")
|
|
247
|
+
return [];
|
|
248
|
+
const term = searchTerm.toLowerCase().trim();
|
|
249
|
+
if (term.length === 0)
|
|
250
|
+
return [];
|
|
251
|
+
const results = this.countryList.filter((country) => country.name.toLowerCase().includes(term) ||
|
|
252
|
+
country.code.toLowerCase().includes(term));
|
|
253
|
+
return results.slice(0, limit);
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Compares two Country instances for equality by checking their name and code
|
|
257
|
+
*
|
|
258
|
+
* @param {Country} country1 - First country to compare
|
|
259
|
+
* @param {Country} country2 - Second country to compare
|
|
260
|
+
* @returns {boolean} True if countries are equal, false otherwise
|
|
261
|
+
* @private
|
|
262
|
+
*/
|
|
263
|
+
compare(country1, country2) {
|
|
264
|
+
return (country1.name === country2.name &&
|
|
265
|
+
country1.code === country2.code);
|
|
266
|
+
}
|
|
267
|
+
}
|