@temboplus/frontend-core 0.2.4 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/README.md +74 -20
  2. package/esm/src/data/countries.d.ts +36 -3
  3. package/esm/src/data/countries.js +2475 -974
  4. package/esm/src/models/amount/amount.d.ts.map +1 -1
  5. package/esm/src/models/amount/amount.js +1 -1
  6. package/esm/src/models/bank/bank.d.ts +127 -0
  7. package/esm/src/models/bank/bank.d.ts.map +1 -1
  8. package/esm/src/models/bank/bank.js +264 -7
  9. package/esm/src/models/bank/index.d.ts +0 -1
  10. package/esm/src/models/bank/index.d.ts.map +1 -1
  11. package/esm/src/models/bank/index.js +0 -1
  12. package/esm/src/models/country/country.d.ts +278 -24
  13. package/esm/src/models/country/country.d.ts.map +1 -1
  14. package/esm/src/models/country/country.js +603 -31
  15. package/esm/src/models/country/index.d.ts +1 -1
  16. package/esm/src/models/country/index.d.ts.map +1 -1
  17. package/esm/src/models/country/index.js +1 -1
  18. package/esm/src/models/country/types.d.ts +53 -0
  19. package/esm/src/models/country/types.d.ts.map +1 -0
  20. package/esm/src/models/country/types.js +43 -0
  21. package/esm/src/models/currency/currency.d.ts +127 -4
  22. package/esm/src/models/currency/currency.d.ts.map +1 -1
  23. package/esm/src/models/currency/currency.js +229 -11
  24. package/esm/src/models/currency/index.d.ts +1 -1
  25. package/esm/src/models/currency/index.d.ts.map +1 -1
  26. package/esm/src/models/currency/index.js +1 -1
  27. package/esm/src/models/currency/types.d.ts +20 -0
  28. package/esm/src/models/currency/types.d.ts.map +1 -0
  29. package/esm/src/models/currency/types.js +13 -0
  30. package/esm/src/models/phone_number/global/phone_number.d.ts +3 -2
  31. package/esm/src/models/phone_number/global/phone_number.d.ts.map +1 -1
  32. package/esm/src/models/phone_number/global/service.d.ts +27 -20
  33. package/esm/src/models/phone_number/global/service.d.ts.map +1 -1
  34. package/esm/src/models/phone_number/global/service.js +61 -40
  35. package/package.json +1 -1
  36. package/script/src/data/countries.d.ts +36 -3
  37. package/script/src/data/countries.js +2475 -974
  38. package/script/src/models/amount/amount.d.ts.map +1 -1
  39. package/script/src/models/amount/amount.js +4 -4
  40. package/script/src/models/bank/bank.d.ts +127 -0
  41. package/script/src/models/bank/bank.d.ts.map +1 -1
  42. package/script/src/models/bank/bank.js +273 -12
  43. package/script/src/models/bank/index.d.ts +0 -1
  44. package/script/src/models/bank/index.d.ts.map +1 -1
  45. package/script/src/models/bank/index.js +0 -1
  46. package/script/src/models/country/country.d.ts +278 -24
  47. package/script/src/models/country/country.d.ts.map +1 -1
  48. package/script/src/models/country/country.js +611 -35
  49. package/script/src/models/country/index.d.ts +1 -1
  50. package/script/src/models/country/index.d.ts.map +1 -1
  51. package/script/src/models/country/index.js +1 -1
  52. package/script/src/models/country/types.d.ts +53 -0
  53. package/script/src/models/country/types.d.ts.map +1 -0
  54. package/script/src/models/country/types.js +46 -0
  55. package/script/src/models/currency/currency.d.ts +127 -4
  56. package/script/src/models/currency/currency.d.ts.map +1 -1
  57. package/script/src/models/currency/currency.js +237 -15
  58. package/script/src/models/currency/index.d.ts +1 -1
  59. package/script/src/models/currency/index.d.ts.map +1 -1
  60. package/script/src/models/currency/index.js +1 -1
  61. package/script/src/models/currency/types.d.ts +20 -0
  62. package/script/src/models/currency/types.d.ts.map +1 -0
  63. package/script/src/models/currency/types.js +14 -0
  64. package/script/src/models/phone_number/global/phone_number.d.ts +3 -2
  65. package/script/src/models/phone_number/global/phone_number.d.ts.map +1 -1
  66. package/script/src/models/phone_number/global/service.d.ts +27 -20
  67. package/script/src/models/phone_number/global/service.d.ts.map +1 -1
  68. package/script/src/models/phone_number/global/service.js +61 -40
  69. package/esm/src/models/bank/service.d.ts +0 -106
  70. package/esm/src/models/bank/service.d.ts.map +0 -1
  71. package/esm/src/models/bank/service.js +0 -240
  72. package/esm/src/models/country/service.d.ts +0 -75
  73. package/esm/src/models/country/service.d.ts.map +0 -1
  74. package/esm/src/models/country/service.js +0 -267
  75. package/esm/src/models/currency/service.d.ts +0 -96
  76. package/esm/src/models/currency/service.d.ts.map +0 -1
  77. package/esm/src/models/currency/service.js +0 -194
  78. package/script/src/models/bank/service.d.ts +0 -106
  79. package/script/src/models/bank/service.d.ts.map +0 -1
  80. package/script/src/models/bank/service.js +0 -247
  81. package/script/src/models/country/service.d.ts +0 -75
  82. package/script/src/models/country/service.d.ts.map +0 -1
  83. package/script/src/models/country/service.js +0 -274
  84. package/script/src/models/currency/service.d.ts +0 -96
  85. package/script/src/models/currency/service.d.ts.map +0 -1
  86. package/script/src/models/currency/service.js +0 -201
@@ -1,274 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.CountryService = void 0;
7
- const country_js_1 = require("./country.js");
8
- const countries_js_1 = __importDefault(require("../../data/countries.js"));
9
- /**
10
- * Service for managing country data.
11
- * @class CountryService
12
- */
13
- class CountryService {
14
- constructor() {
15
- Object.defineProperty(this, "countryList", {
16
- enumerable: true,
17
- configurable: true,
18
- writable: true,
19
- value: []
20
- });
21
- Object.defineProperty(this, "codeRecord", {
22
- enumerable: true,
23
- configurable: true,
24
- writable: true,
25
- value: {}
26
- });
27
- Object.defineProperty(this, "nameRecord", {
28
- enumerable: true,
29
- configurable: true,
30
- writable: true,
31
- value: {}
32
- });
33
- Object.defineProperty(this, "fullNameRecord", {
34
- enumerable: true,
35
- configurable: true,
36
- writable: true,
37
- value: {}
38
- });
39
- // Static references for direct access through Country class
40
- Object.defineProperty(this, "staticReferences", {
41
- enumerable: true,
42
- configurable: true,
43
- writable: true,
44
- value: new Map()
45
- });
46
- }
47
- /**
48
- * Gets the singleton instance of CountryService.
49
- * Creates the instance if it doesn't exist.
50
- * @static
51
- * @returns {CountryService} The singleton instance
52
- */
53
- static getInstance() {
54
- if (!CountryService.instance) {
55
- CountryService.instance = new CountryService();
56
- CountryService.instance.initialize();
57
- }
58
- return CountryService.instance;
59
- }
60
- /**
61
- * Initializes the service with country data.
62
- * Should be called once when your application starts.
63
- */
64
- initialize() {
65
- try {
66
- const data = JSON.parse(JSON.stringify(countries_js_1.default));
67
- const countries = Object.values(data).map((c) => new country_js_1.Country(c.name, c.code));
68
- const code_record = {};
69
- const name_record = {};
70
- const fullname_record = {};
71
- countries.forEach((country) => {
72
- // Populate code record
73
- code_record[country.code] = country;
74
- // Add to record by name
75
- name_record[country.name.toUpperCase()] = country;
76
- // Generate uppercase full name with underscores
77
- const fullNameKey = country.name
78
- .toUpperCase()
79
- .replace(/\s+/g, "_")
80
- .replace(/[-(),.']/g, "")
81
- .replace(/&/g, "AND");
82
- fullname_record[fullNameKey] = country;
83
- // Save references for static access
84
- this.staticReferences.set(country.code.toUpperCase(), country);
85
- this.staticReferences.set(fullNameKey, country);
86
- });
87
- // Ensure specific mappings are available
88
- if (code_record["US"]) {
89
- fullname_record["UNITED_STATES"] = code_record["US"];
90
- this.staticReferences.set("UNITED_STATES", code_record["US"]);
91
- }
92
- // Add specific country mappings
93
- // Cocos Islands
94
- if (code_record["CC"]) {
95
- fullname_record["COCOS_ISLANDS"] = code_record["CC"];
96
- this.staticReferences.set("COCOS_ISLANDS", code_record["CC"]);
97
- }
98
- // Democratic Republic of Congo
99
- if (code_record["CD"]) {
100
- fullname_record["DEMOCRATIC_REPUBLIC_OF_CONGO"] = code_record["CD"];
101
- this.staticReferences.set("DEMOCRATIC_REPUBLIC_OF_CONGO", code_record["CD"]);
102
- }
103
- // Cote d'Ivoire
104
- if (code_record["CI"]) {
105
- fullname_record["COTE_DIVOIRE"] = code_record["CI"];
106
- this.staticReferences.set("COTE_DIVOIRE", code_record["CI"]);
107
- }
108
- // Falkland Islands
109
- if (code_record["FK"]) {
110
- fullname_record["FALKLAND_ISLANDS"] = code_record["FK"];
111
- this.staticReferences.set("FALKLAND_ISLANDS", code_record["FK"]);
112
- }
113
- // Holy See (Vatican)
114
- if (code_record["VA"]) {
115
- fullname_record["HOLY_SEE"] = code_record["VA"];
116
- this.staticReferences.set("HOLY_SEE", code_record["VA"]);
117
- }
118
- // Iran
119
- if (code_record["IR"]) {
120
- fullname_record["IRAN"] = code_record["IR"];
121
- this.staticReferences.set("IRAN", code_record["IR"]);
122
- }
123
- // North Korea
124
- if (code_record["KP"]) {
125
- fullname_record["NORTH_KOREA"] = code_record["KP"];
126
- this.staticReferences.set("NORTH_KOREA", code_record["KP"]);
127
- }
128
- // South Korea
129
- if (code_record["KR"]) {
130
- fullname_record["SOUTH_KOREA"] = code_record["KR"];
131
- this.staticReferences.set("SOUTH_KOREA", code_record["KR"]);
132
- }
133
- // Lao People's Democratic Republic
134
- if (code_record["LA"]) {
135
- fullname_record["LAO"] = code_record["LA"];
136
- this.staticReferences.set("LAO", code_record["LA"]);
137
- }
138
- // Palestine
139
- if (code_record["PS"]) {
140
- fullname_record["PALESTINE"] = code_record["PS"];
141
- this.staticReferences.set("PALESTINE", code_record["PS"]);
142
- }
143
- // Macedonia
144
- if (code_record["MK"]) {
145
- fullname_record["MACEDONIA"] = code_record["MK"];
146
- this.staticReferences.set("MACEDONIA", code_record["MK"]);
147
- }
148
- // Micronesia
149
- if (code_record["FM"]) {
150
- fullname_record["MICRONESIA"] = code_record["FM"];
151
- this.staticReferences.set("MICRONESIA", code_record["FM"]);
152
- }
153
- // Moldova
154
- if (code_record["MD"]) {
155
- fullname_record["MOLDOVA"] = code_record["MD"];
156
- this.staticReferences.set("MOLDOVA", code_record["MD"]);
157
- }
158
- // Taiwan
159
- if (code_record["TW"]) {
160
- fullname_record["TAIWAN"] = code_record["TW"];
161
- this.staticReferences.set("TAIWAN", code_record["TW"]);
162
- }
163
- // Tanzania
164
- if (code_record["TZ"]) {
165
- fullname_record["TANZANIA"] = code_record["TZ"];
166
- this.staticReferences.set("TANZANIA", code_record["TZ"]);
167
- }
168
- // US Virgin Islands
169
- if (code_record["VI"]) {
170
- fullname_record["VIRGIN_ISLANDS_US"] = code_record["VI"];
171
- this.staticReferences.set("VIRGIN_ISLANDS_US", code_record["VI"]);
172
- }
173
- // British Virgin Islands
174
- if (code_record["VG"]) {
175
- fullname_record["VIRGIN_ISLANDS_BRITISH"] = code_record["VG"];
176
- this.staticReferences.set("VIRGIN_ISLANDS_BRITISH", code_record["VG"]);
177
- }
178
- this.codeRecord = code_record;
179
- this.nameRecord = name_record;
180
- this.fullNameRecord = fullname_record;
181
- this.countryList = countries;
182
- }
183
- catch (error) {
184
- console.error("Failed to initialize CountryService:", error);
185
- }
186
- }
187
- /**
188
- * Gets all countries.
189
- * @returns {Country[]} Array of all countries
190
- */
191
- getAll() {
192
- return this.countryList;
193
- }
194
- /**
195
- * Gets all countries as a record.
196
- * @returns {Record<string, Country>} Record of country codes and country objects
197
- */
198
- getAllAsRecord() {
199
- return this.codeRecord;
200
- }
201
- /**
202
- * Gets static country references to be used by the Country class.
203
- * @returns {Map<string, Country>} Map of static references
204
- */
205
- getStaticReferences() {
206
- return this.staticReferences;
207
- }
208
- /**
209
- * Gets the full name record mapping.
210
- * @returns {Record<string, Country>} Record of uppercase full name keys to country objects
211
- */
212
- getFullNameRecord() {
213
- return this.fullNameRecord;
214
- }
215
- /**
216
- * Retrieves a country by its ISO code.
217
- * @param {string} code The ISO code of the country.
218
- * @returns {Country | undefined} The country corresponding to the ISO code or `undefined` if not found.
219
- */
220
- fromCode(code) {
221
- return this.codeRecord[code.toUpperCase()];
222
- }
223
- /**
224
- * Retrieves a country by its name.
225
- * @param {string} countryName The name of the country.
226
- * @returns {Country | undefined} The country corresponding to the name or `undefined` if not found.
227
- */
228
- fromName(countryName) {
229
- // First try direct lookup in name record
230
- const directMatch = this.nameRecord[countryName.toUpperCase()];
231
- if (directMatch)
232
- return directMatch;
233
- // Then try full name record
234
- const fullNameKey = countryName
235
- .toUpperCase()
236
- .replace(/\s+/g, "_")
237
- .replace(/[-(),.']/g, "")
238
- .replace(/&/g, "AND");
239
- const fullNameMatch = this.fullNameRecord[fullNameKey];
240
- if (fullNameMatch)
241
- return fullNameMatch;
242
- // If not found, try more lenient matching
243
- return this.countryList.find((country) => country.name.toLowerCase() === countryName.toLowerCase());
244
- }
245
- /**
246
- * Searches for countries that match the given search term.
247
- * @param {string} searchTerm - The partial name or code to search for.
248
- * @param {number} [limit=10] - Maximum number of results to return.
249
- * @returns {Country[]} Array of matching countries, limited to specified count.
250
- */
251
- search(searchTerm, limit = 10) {
252
- if (!searchTerm || typeof searchTerm !== "string")
253
- return [];
254
- const term = searchTerm.toLowerCase().trim();
255
- if (term.length === 0)
256
- return [];
257
- const results = this.countryList.filter((country) => country.name.toLowerCase().includes(term) ||
258
- country.code.toLowerCase().includes(term));
259
- return results.slice(0, limit);
260
- }
261
- /**
262
- * Compares two Country instances for equality by checking their name and code
263
- *
264
- * @param {Country} country1 - First country to compare
265
- * @param {Country} country2 - Second country to compare
266
- * @returns {boolean} True if countries are equal, false otherwise
267
- * @private
268
- */
269
- compare(country1, country2) {
270
- return (country1.name === country2.name &&
271
- country1.code === country2.code);
272
- }
273
- }
274
- exports.CountryService = CountryService;
@@ -1,96 +0,0 @@
1
- import { Currency } from "./currency.js";
2
- /**
3
- * Service for managing currency data.
4
- * @class CurrencyService
5
- */
6
- export declare class CurrencyService {
7
- private static instance;
8
- private currencyList;
9
- private currencyRecord;
10
- private nameRecord;
11
- private staticReferences;
12
- private constructor();
13
- /**
14
- * Gets the singleton instance of CurrencyService.
15
- * Creates the instance if it doesn't exist.
16
- * @static
17
- * @returns {CurrencyService} The singleton instance
18
- */
19
- static getInstance(): CurrencyService;
20
- /**
21
- * Initializes the service with currency data.
22
- * Should be called once when your application starts.
23
- */
24
- private initialize;
25
- /**
26
- * Gets all currencies.
27
- * @returns {Currency[]} Array of all currencies
28
- */
29
- getAll(): Currency[];
30
- /**
31
- * Gets all currencies as a record.
32
- * @returns {Record<string, Currency>} Record of currency codes and currency objects
33
- */
34
- getAllAsRecord(): Record<string, Currency>;
35
- /**
36
- * Gets static currency references to be used by the Currency class.
37
- * @returns {Map<string, Currency>} Map of static references
38
- */
39
- getStaticReferences(): Map<string, Currency>;
40
- /**
41
- * Retrieves a currency by its ISO code.
42
- * @param {string} code The ISO code of the currency.
43
- * @returns {Currency | undefined} The currency corresponding to the ISO code or `undefined` if not found.
44
- */
45
- fromCode(code: string): Currency | undefined;
46
- /**
47
- * Retrieves a currency by its name.
48
- * @param {string} currencyName The name of the currency.
49
- * @returns {Currency | undefined} The currency corresponding to the name or `undefined` if not found.
50
- */
51
- fromName(currencyName: string): Currency | undefined;
52
- /**
53
- * Validates if a given ISO currency code is valid
54
- *
55
- * @param {string | null | undefined} code - The currency code to validate.
56
- * Should be a three-letter ISO currency code (e.g., 'USD', 'EUR').
57
- *
58
- * @returns {boolean} Returns true if:
59
- * - The currency code is not null/undefined
60
- * - The currency code successfully resolves to a valid Currency instance
61
- * Returns false otherwise.
62
- */
63
- isValidCode(code?: string | null): boolean;
64
- /**
65
- * Validates if a given currency name is valid
66
- *
67
- * @param {string | null | undefined} currencyName - The currency name to validate.
68
- *
69
- * @returns {boolean} Returns true if:
70
- * - The currency name is not null/undefined
71
- * - The currency name successfully resolves to a valid Currency instance
72
- * Returns false otherwise.
73
- */
74
- isValidName(currencyName?: string | null): boolean;
75
- /**
76
- * Gets a regex-ready pattern matching all currency symbols
77
- * Pattern is memoized for performance
78
- * @returns {string} Pipe-separated pattern of escaped currency symbols
79
- *
80
- * @example
81
- * const pattern = currencyService.getCurrencySymbolPattern();
82
- * // Returns something like: "\$|€|\£|¥"
83
- */
84
- getCurrencySymbolPattern(): string;
85
- /**
86
- * Escapes special characters in a string for use in regular expressions
87
- * @private
88
- * @param {string} string - The string to escape
89
- * @returns {string} The escaped string
90
- *
91
- * @example
92
- * private escapeRegExp("$") // Returns "\$"
93
- */
94
- private escapeRegExp;
95
- }
96
- //# sourceMappingURL=service.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../../../src/src/models/currency/service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAiBzC;;;GAGG;AACH,qBAAa,eAAe;IAC1B,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAkB;IACzC,OAAO,CAAC,YAAY,CAAkB;IACtC,OAAO,CAAC,cAAc,CAAgC;IACtD,OAAO,CAAC,UAAU,CAAgC;IAGlD,OAAO,CAAC,gBAAgB,CAAoC;IAE5D,OAAO;IAEP;;;;;OAKG;IACH,MAAM,CAAC,WAAW,IAAI,eAAe;IAQrC;;;OAGG;IACH,OAAO,CAAC,UAAU;IAmDlB;;;OAGG;IACH,MAAM,IAAI,QAAQ,EAAE;IAIpB;;;OAGG;IACH,cAAc,IAAI,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC;IAI1C;;;OAGG;IACH,mBAAmB,IAAI,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC;IAI5C;;;;OAIG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,GAAG,SAAS;IAI5C;;;;OAIG;IACH,QAAQ,CAAC,YAAY,EAAE,MAAM,GAAG,QAAQ,GAAG,SAAS;IAmBpD;;;;;;;;;;OAUG;IACH,WAAW,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO;IAM1C;;;;;;;;;OASG;IACH,WAAW,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO;IAMlD;;;;;;;;OAQG;IACH,wBAAwB,IAAI,MAAM;IAUlC;;;;;;;;OAQG;IACH,OAAO,CAAC,YAAY;CAGrB"}
@@ -1,201 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.CurrencyService = void 0;
7
- const currency_js_1 = require("./currency.js");
8
- const currencies_js_1 = __importDefault(require("../../data/currencies.js"));
9
- /**
10
- * Service for managing currency data.
11
- * @class CurrencyService
12
- */
13
- class CurrencyService {
14
- constructor() {
15
- Object.defineProperty(this, "currencyList", {
16
- enumerable: true,
17
- configurable: true,
18
- writable: true,
19
- value: []
20
- });
21
- Object.defineProperty(this, "currencyRecord", {
22
- enumerable: true,
23
- configurable: true,
24
- writable: true,
25
- value: {}
26
- });
27
- Object.defineProperty(this, "nameRecord", {
28
- enumerable: true,
29
- configurable: true,
30
- writable: true,
31
- value: {}
32
- });
33
- // Static references for direct access through Currency 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 CurrencyService.
43
- * Creates the instance if it doesn't exist.
44
- * @static
45
- * @returns {CurrencyService} The singleton instance
46
- */
47
- static getInstance() {
48
- if (!CurrencyService.instance) {
49
- CurrencyService.instance = new CurrencyService();
50
- CurrencyService.instance.initialize();
51
- }
52
- return CurrencyService.instance;
53
- }
54
- /**
55
- * Initializes the service with currency data.
56
- * Should be called once when your application starts.
57
- */
58
- initialize() {
59
- try {
60
- const data = JSON.parse(JSON.stringify(currencies_js_1.default));
61
- const currencies = Object.values(data).map((c) => new currency_js_1.Currency(c.symbol, c.name, c.symbol_native, c.decimal_digits, c.rounding, c.code, c.name_plural));
62
- const codeRecord = {};
63
- const nameRecord = {};
64
- currencies.forEach((currency) => {
65
- // Populate code record
66
- codeRecord[currency.code] = currency;
67
- // Add to record by name
68
- nameRecord[currency.name.toUpperCase()] = currency;
69
- const upperCode = currency.code.toUpperCase();
70
- // Add to static references for uppercase code
71
- this.staticReferences.set(upperCode, currency);
72
- // Add formatted full name static reference based on name property
73
- // Transform from "US Dollar" to "US_DOLLAR"
74
- const nameKey = currency.name
75
- .toUpperCase()
76
- .replace(/\s+/g, "_")
77
- .replace(/[-(),.']/g, "")
78
- .replace(/&/g, "AND");
79
- this.staticReferences.set(nameKey, currency);
80
- });
81
- this.currencyRecord = codeRecord;
82
- this.nameRecord = nameRecord;
83
- this.currencyList = currencies;
84
- }
85
- catch (error) {
86
- console.error("Failed to initialize CurrencyService:", error);
87
- }
88
- }
89
- /**
90
- * Gets all currencies.
91
- * @returns {Currency[]} Array of all currencies
92
- */
93
- getAll() {
94
- return this.currencyList;
95
- }
96
- /**
97
- * Gets all currencies as a record.
98
- * @returns {Record<string, Currency>} Record of currency codes and currency objects
99
- */
100
- getAllAsRecord() {
101
- return this.currencyRecord;
102
- }
103
- /**
104
- * Gets static currency references to be used by the Currency class.
105
- * @returns {Map<string, Currency>} Map of static references
106
- */
107
- getStaticReferences() {
108
- return this.staticReferences;
109
- }
110
- /**
111
- * Retrieves a currency by its ISO code.
112
- * @param {string} code The ISO code of the currency.
113
- * @returns {Currency | undefined} The currency corresponding to the ISO code or `undefined` if not found.
114
- */
115
- fromCode(code) {
116
- return this.currencyRecord[code.trim().toUpperCase()];
117
- }
118
- /**
119
- * Retrieves a currency by its name.
120
- * @param {string} currencyName The name of the currency.
121
- * @returns {Currency | undefined} The currency corresponding to the name or `undefined` if not found.
122
- */
123
- fromName(currencyName) {
124
- const input = currencyName.trim().toUpperCase();
125
- // First try direct lookup in name record
126
- const directMatch = this.nameRecord[input];
127
- if (directMatch)
128
- return directMatch;
129
- // If not found, try more lenient matching
130
- for (const [name, currObj] of Object.entries(this.nameRecord)) {
131
- if (name.toUpperCase() === input) {
132
- return currObj;
133
- }
134
- }
135
- // Finally, try case-insensitive exact match
136
- return this.currencyList.find((currency) => currency.name.toUpperCase() === input);
137
- }
138
- /**
139
- * Validates if a given ISO currency code is valid
140
- *
141
- * @param {string | null | undefined} code - The currency code to validate.
142
- * Should be a three-letter ISO currency code (e.g., 'USD', 'EUR').
143
- *
144
- * @returns {boolean} Returns true if:
145
- * - The currency code is not null/undefined
146
- * - The currency code successfully resolves to a valid Currency instance
147
- * Returns false otherwise.
148
- */
149
- isValidCode(code) {
150
- if (!code)
151
- return false;
152
- const currency = this.fromCode(code);
153
- return !!currency;
154
- }
155
- /**
156
- * Validates if a given currency name is valid
157
- *
158
- * @param {string | null | undefined} currencyName - The currency name to validate.
159
- *
160
- * @returns {boolean} Returns true if:
161
- * - The currency name is not null/undefined
162
- * - The currency name successfully resolves to a valid Currency instance
163
- * Returns false otherwise.
164
- */
165
- isValidName(currencyName) {
166
- if (!currencyName)
167
- return false;
168
- const currency = this.fromName(currencyName);
169
- return !!currency;
170
- }
171
- /**
172
- * Gets a regex-ready pattern matching all currency symbols
173
- * Pattern is memoized for performance
174
- * @returns {string} Pipe-separated pattern of escaped currency symbols
175
- *
176
- * @example
177
- * const pattern = currencyService.getCurrencySymbolPattern();
178
- * // Returns something like: "\$|€|\£|¥"
179
- */
180
- getCurrencySymbolPattern() {
181
- const symbols = new Set();
182
- Object.values(this.currencyList).forEach((currency) => {
183
- symbols.add(this.escapeRegExp(currency.symbol));
184
- symbols.add(this.escapeRegExp(currency.symbolNative));
185
- });
186
- return Array.from(symbols).join("|");
187
- }
188
- /**
189
- * Escapes special characters in a string for use in regular expressions
190
- * @private
191
- * @param {string} string - The string to escape
192
- * @returns {string} The escaped string
193
- *
194
- * @example
195
- * private escapeRegExp("$") // Returns "\$"
196
- */
197
- escapeRegExp(string) {
198
- return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
199
- }
200
- }
201
- exports.CurrencyService = CurrencyService;