@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.
Files changed (86) hide show
  1. package/README.md +63 -14
  2. package/esm/src/data/countries.d.ts +12 -2
  3. package/esm/src/data/countries.js +496 -248
  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 +162 -24
  13. package/esm/src/models/country/country.d.ts.map +1 -1
  14. package/esm/src/models/country/country.js +442 -42
  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 +12 -2
  37. package/script/src/data/countries.js +496 -248
  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 +162 -24
  47. package/script/src/models/country/country.d.ts.map +1 -1
  48. package/script/src/models/country/country.js +453 -49
  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 -111
  73. package/esm/src/models/country/service.d.ts.map +0 -1
  74. package/esm/src/models/country/service.js +0 -366
  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 -111
  82. package/script/src/models/country/service.d.ts.map +0 -1
  83. package/script/src/models/country/service.js +0 -373
  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,366 +0,0 @@
1
- import { CONTINENT, Country, SUB_REGION } 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, "iso3Record", {
22
- enumerable: true,
23
- configurable: true,
24
- writable: true,
25
- value: {}
26
- });
27
- // private nameRecord: Record<string, Country> = {};
28
- Object.defineProperty(this, "nameRecord", {
29
- enumerable: true,
30
- configurable: true,
31
- writable: true,
32
- value: {}
33
- });
34
- Object.defineProperty(this, "continentRecord", {
35
- enumerable: true,
36
- configurable: true,
37
- writable: true,
38
- value: {}
39
- });
40
- Object.defineProperty(this, "regionRecord", {
41
- enumerable: true,
42
- configurable: true,
43
- writable: true,
44
- value: {}
45
- });
46
- // Static references for direct access through Country class
47
- Object.defineProperty(this, "staticReferences", {
48
- enumerable: true,
49
- configurable: true,
50
- writable: true,
51
- value: new Map()
52
- });
53
- }
54
- /**
55
- * Gets the singleton instance of CountryService.
56
- * Creates the instance if it doesn't exist.
57
- * @static
58
- * @returns {CountryService} The singleton instance
59
- */
60
- static getInstance() {
61
- if (!CountryService.instance) {
62
- CountryService.instance = new CountryService();
63
- CountryService.instance.initialize();
64
- }
65
- return CountryService.instance;
66
- }
67
- /**
68
- * Maps a string continent name to the CONTINENT enum
69
- * @param continentName String continent name from JSON
70
- * @returns The corresponding CONTINENT enum value
71
- */
72
- mapContinent(continentName) {
73
- switch (continentName) {
74
- case "Africa":
75
- return CONTINENT.AFRICA;
76
- case "Antarctica":
77
- return CONTINENT.ANTARCTICA;
78
- case "Asia":
79
- return CONTINENT.ASIA;
80
- case "Europe":
81
- return CONTINENT.EUROPE;
82
- case "North America":
83
- return CONTINENT.NORTH_AMERICA;
84
- case "Oceania":
85
- return CONTINENT.OCEANIA;
86
- case "South America":
87
- return CONTINENT.SOUTH_AMERICA;
88
- default:
89
- return CONTINENT.EUROPE; // Default value
90
- }
91
- }
92
- /**
93
- * Maps a string region name to the SUB_REGION enum
94
- * @param regionName String region name from JSON
95
- * @returns The corresponding SUB_REGION enum value
96
- */
97
- mapRegion(regionName) {
98
- switch (regionName) {
99
- case "Australia and New Zealand":
100
- return SUB_REGION.AUSTRALIA_AND_NEW_ZEALAND;
101
- case "Caribbean":
102
- return SUB_REGION.CARIBBEAN;
103
- case "Central America":
104
- return SUB_REGION.CENTRAL_AMERICA;
105
- case "Central Asia":
106
- return SUB_REGION.CENTRAL_ASIA;
107
- case "Eastern Africa":
108
- return SUB_REGION.EASTERN_AFRICA;
109
- case "Eastern Asia":
110
- return SUB_REGION.EASTERN_ASIA;
111
- case "Eastern Europe":
112
- return SUB_REGION.EASTERN_EUROPE;
113
- case "Melanesia":
114
- return SUB_REGION.MELANESIA;
115
- case "Micronesia":
116
- return SUB_REGION.MICRONESIA;
117
- case "Middle Africa":
118
- return SUB_REGION.MIDDLE_AFRICA;
119
- case "Northern Africa":
120
- return SUB_REGION.NORTHERN_AFRICA;
121
- case "Northern America":
122
- return SUB_REGION.NORTHERN_AMERICA;
123
- case "Northern Europe":
124
- return SUB_REGION.NORTHERN_EUROPE;
125
- case "Polynesia":
126
- return SUB_REGION.POLYNESIA;
127
- case "South-eastern Asia":
128
- return SUB_REGION.SOUTH_EASTERN_ASIA;
129
- case "Southern Africa":
130
- return SUB_REGION.SOUTHERN_AFRICA;
131
- case "Southern Asia":
132
- return SUB_REGION.SOUTHERN_ASIA;
133
- case "Southern Europe":
134
- return SUB_REGION.SOUTHERN_EUROPE;
135
- case "Western Africa":
136
- return SUB_REGION.WESTERN_AFRICA;
137
- case "Western Asia":
138
- return SUB_REGION.WESTERN_ASIA;
139
- case "Western Europe":
140
- return SUB_REGION.WESTERN_EUROPE;
141
- default:
142
- return SUB_REGION.NORTHERN_EUROPE; // Default value
143
- }
144
- }
145
- /**
146
- * Initializes the service with country data.
147
- * Should be called once when your application starts.
148
- */
149
- initialize() {
150
- try {
151
- // Parse the JSON data
152
- const data = JSON.parse(JSON.stringify(file));
153
- // deno-lint-ignore no-explicit-any
154
- const countriesData = data.countries || [];
155
- // Initialize continent and region records
156
- Object.values(CONTINENT).forEach((continent) => {
157
- this.continentRecord[continent] = [];
158
- });
159
- Object.values(SUB_REGION).forEach((region) => {
160
- this.regionRecord[region] = [];
161
- });
162
- // Create Country instances from the data
163
- const countries = countriesData.map((c) => {
164
- const continent = this.mapContinent(c.continent);
165
- const region = this.mapRegion(c.region);
166
- return new Country(c.name, c.iso_2, c.name_official, c.iso_3, c.flag_emoji, continent, region);
167
- });
168
- const code_record = {};
169
- const iso3_record = {};
170
- // const name_record: Record<string, Country> = {};
171
- const name_record = {};
172
- countries.forEach((country) => {
173
- // Populate code records
174
- code_record[country.code] = country;
175
- iso3_record[country.iso3] = country;
176
- // Add to record by name
177
- // name_record[country.name.toUpperCase()] = country;
178
- // Generate uppercase full name with underscores
179
- const nameKey = country.name
180
- .toUpperCase()
181
- .replace(/\s+/g, "_")
182
- .replace(/[-(),.']/g, "")
183
- .replace(/&/g, "AND");
184
- name_record[nameKey] = country;
185
- // Group countries by continent
186
- this.continentRecord[country.continent].push(country);
187
- // Group countries by region
188
- this.regionRecord[country.region].push(country);
189
- this.staticReferences.set(country.code, country);
190
- this.staticReferences.set(nameKey, country);
191
- });
192
- // Add specific country mappings for special cases
193
- // Cocos Islands
194
- if (code_record["CC"]) {
195
- name_record["COCOS_ISLANDS"] = code_record["CC"];
196
- this.staticReferences.set("COCOS_ISLANDS", code_record["CC"]);
197
- }
198
- // Cote d'Ivoire
199
- if (code_record["CI"]) {
200
- name_record["COTE_DIVOIRE"] = code_record["CI"];
201
- this.staticReferences.set("COTE_DIVOIRE", code_record["CI"]);
202
- }
203
- // Macedonia (North Macedonia)
204
- if (code_record["MK"]) {
205
- name_record["MACEDONIA"] = code_record["MK"];
206
- this.staticReferences.set("MACEDONIA", code_record["MK"]);
207
- }
208
- // US Virgin Islands
209
- if (code_record["VI"]) {
210
- name_record["VIRGIN_ISLANDS_US"] = code_record["VI"];
211
- this.staticReferences.set("VIRGIN_ISLANDS_US", code_record["VI"]);
212
- }
213
- // British Virgin Islands
214
- if (code_record["VG"]) {
215
- name_record["VIRGIN_ISLANDS_BRITISH"] = code_record["VG"];
216
- this.staticReferences.set("VIRGIN_ISLANDS_BRITISH", code_record["VG"]);
217
- }
218
- // Democratic Republic of the Congo
219
- if (code_record["CD"]) {
220
- name_record["DEMOCRATIC_REPUBLIC_OF_CONGO"] = code_record["CD"];
221
- this.staticReferences.set("DEMOCRATIC_REPUBLIC_OF_CONGO", code_record["CD"]);
222
- }
223
- // Falkland Islands (Malvinas)
224
- if (code_record["FK"]) {
225
- name_record["FALKLAND_ISLANDS"] = code_record["FK"];
226
- this.staticReferences.set("FALKLAND_ISLANDS", code_record["FK"]);
227
- }
228
- // Lao
229
- if (code_record["LA"]) {
230
- name_record["LAO"] = code_record["LA"];
231
- this.staticReferences.set("LAO", code_record["LA"]);
232
- }
233
- this.codeRecord = code_record;
234
- this.iso3Record = iso3_record;
235
- this.nameRecord = name_record;
236
- this.countryList = countries;
237
- // Initialize static properties on Country class
238
- this.initializeCountryStatics();
239
- }
240
- catch (error) {
241
- console.error("Failed to initialize CountryService:", error);
242
- }
243
- }
244
- /**
245
- * Initialize the static properties on the Country class
246
- */
247
- initializeCountryStatics() {
248
- // Initialize ISO-2 code properties
249
- Object.entries(this.codeRecord).forEach(([code, country]) => {
250
- // deno-lint-ignore no-explicit-any
251
- Country[code.toUpperCase()] = country;
252
- });
253
- // Initialize full name properties
254
- Object.entries(this.nameRecord).forEach(([fullName, country]) => {
255
- // deno-lint-ignore no-explicit-any
256
- Country[fullName] = country;
257
- });
258
- }
259
- /**
260
- * Gets all countries.
261
- * @returns {Country[]} Array of all countries
262
- */
263
- getAll() {
264
- return this.countryList;
265
- }
266
- /**
267
- * Gets static country references to be used by the Country class.
268
- * @returns {Map<string, Country>} Map of static references
269
- */
270
- getStaticReferences() {
271
- return this.staticReferences;
272
- }
273
- /**
274
- * Gets all countries as a record.
275
- * @returns {Record<string, Country>} Record of country codes and country objects
276
- */
277
- getAllAsRecord() {
278
- return this.codeRecord;
279
- }
280
- /**
281
- * Gets all countries from a specific continent.
282
- * @param {CONTINENT} continent The continent enum value
283
- * @returns {Country[]} Array of countries in the specified continent
284
- */
285
- getByContinent(continent) {
286
- return this.continentRecord[continent] || [];
287
- }
288
- /**
289
- * Gets all countries from a specific region.
290
- * @param {SUB_REGION} region The region enum value
291
- * @returns {Country[]} Array of countries in the specified region
292
- */
293
- getByRegion(region) {
294
- return this.regionRecord[region] || [];
295
- }
296
- /**
297
- * Gets the full name record mapping.
298
- * @returns {Record<string, Country>} Record of uppercase full name keys to country objects
299
- */
300
- getFullNameRecord() {
301
- return this.nameRecord;
302
- }
303
- /**
304
- * Retrieves a country by its ISO-2 code.
305
- * @param {string} code The ISO-2 code of the country.
306
- * @returns {Country | undefined} The country corresponding to the ISO code or `undefined` if not found.
307
- */
308
- fromCode(code) {
309
- return this.codeRecord[code.toUpperCase()];
310
- }
311
- /**
312
- * Retrieves a country by its ISO-3 code.
313
- * @param {string} iso3 The ISO-3 code of the country.
314
- * @returns {Country | undefined} The country corresponding to the ISO-3 code or `undefined` if not found.
315
- */
316
- fromIso3(iso3) {
317
- return this.iso3Record[iso3.toUpperCase()];
318
- }
319
- /**
320
- * Retrieves a country by its name.
321
- * @param {string} countryName The name of the country.
322
- * @returns {Country | undefined} The country corresponding to the name or `undefined` if not found.
323
- */
324
- fromName(countryName) {
325
- const fullNameKey = countryName
326
- .toUpperCase()
327
- .replace(/\s+/g, "_")
328
- .replace(/[-(),.']/g, "")
329
- .replace(/&/g, "AND");
330
- const fullNameMatch = this.nameRecord[fullNameKey];
331
- if (fullNameMatch)
332
- return fullNameMatch;
333
- // If not found, try more lenient matching
334
- return this.countryList.find((country) => country.name.toLowerCase() === countryName.toLowerCase());
335
- }
336
- /**
337
- * Searches for countries that match the given search term.
338
- * @param {string} searchTerm - The partial name or code to search for.
339
- * @param {number} [limit=10] - Maximum number of results to return.
340
- * @returns {Country[]} Array of matching countries, limited to specified count.
341
- */
342
- search(searchTerm, limit = 10) {
343
- if (!searchTerm || typeof searchTerm !== "string")
344
- return [];
345
- const term = searchTerm.toLowerCase().trim();
346
- if (term.length === 0)
347
- return [];
348
- const results = this.countryList.filter((country) => country.name.toLowerCase().includes(term) ||
349
- country.nameOfficial.toLowerCase().includes(term) ||
350
- country.code.toLowerCase().includes(term) ||
351
- country.iso3.toLowerCase().includes(term));
352
- return results.slice(0, limit);
353
- }
354
- /**
355
- * Compares two Country instances for equality by checking their name and code
356
- *
357
- * @param {Country} country1 - First country to compare
358
- * @param {Country} country2 - Second country to compare
359
- * @returns {boolean} True if countries are equal, false otherwise
360
- * @private
361
- */
362
- compare(country1, country2) {
363
- return (country1.name === country2.name &&
364
- country1.code === country2.code);
365
- }
366
- }
@@ -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,194 +0,0 @@
1
- import { Currency } from "./currency.js";
2
- import file from "../../data/currencies.js";
3
- /**
4
- * Service for managing currency data.
5
- * @class CurrencyService
6
- */
7
- export class CurrencyService {
8
- constructor() {
9
- Object.defineProperty(this, "currencyList", {
10
- enumerable: true,
11
- configurable: true,
12
- writable: true,
13
- value: []
14
- });
15
- Object.defineProperty(this, "currencyRecord", {
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
- // Static references for direct access through Currency class
28
- Object.defineProperty(this, "staticReferences", {
29
- enumerable: true,
30
- configurable: true,
31
- writable: true,
32
- value: new Map()
33
- });
34
- }
35
- /**
36
- * Gets the singleton instance of CurrencyService.
37
- * Creates the instance if it doesn't exist.
38
- * @static
39
- * @returns {CurrencyService} The singleton instance
40
- */
41
- static getInstance() {
42
- if (!CurrencyService.instance) {
43
- CurrencyService.instance = new CurrencyService();
44
- CurrencyService.instance.initialize();
45
- }
46
- return CurrencyService.instance;
47
- }
48
- /**
49
- * Initializes the service with currency data.
50
- * Should be called once when your application starts.
51
- */
52
- initialize() {
53
- try {
54
- const data = JSON.parse(JSON.stringify(file));
55
- const currencies = Object.values(data).map((c) => new Currency(c.symbol, c.name, c.symbol_native, c.decimal_digits, c.rounding, c.code, c.name_plural));
56
- const codeRecord = {};
57
- const nameRecord = {};
58
- currencies.forEach((currency) => {
59
- // Populate code record
60
- codeRecord[currency.code] = currency;
61
- // Add to record by name
62
- nameRecord[currency.name.toUpperCase()] = currency;
63
- const upperCode = currency.code.toUpperCase();
64
- // Add to static references for uppercase code
65
- this.staticReferences.set(upperCode, currency);
66
- // Add formatted full name static reference based on name property
67
- // Transform from "US Dollar" to "US_DOLLAR"
68
- const nameKey = currency.name
69
- .toUpperCase()
70
- .replace(/\s+/g, "_")
71
- .replace(/[-(),.']/g, "")
72
- .replace(/&/g, "AND");
73
- this.staticReferences.set(nameKey, currency);
74
- });
75
- this.currencyRecord = codeRecord;
76
- this.nameRecord = nameRecord;
77
- this.currencyList = currencies;
78
- }
79
- catch (error) {
80
- console.error("Failed to initialize CurrencyService:", error);
81
- }
82
- }
83
- /**
84
- * Gets all currencies.
85
- * @returns {Currency[]} Array of all currencies
86
- */
87
- getAll() {
88
- return this.currencyList;
89
- }
90
- /**
91
- * Gets all currencies as a record.
92
- * @returns {Record<string, Currency>} Record of currency codes and currency objects
93
- */
94
- getAllAsRecord() {
95
- return this.currencyRecord;
96
- }
97
- /**
98
- * Gets static currency references to be used by the Currency class.
99
- * @returns {Map<string, Currency>} Map of static references
100
- */
101
- getStaticReferences() {
102
- return this.staticReferences;
103
- }
104
- /**
105
- * Retrieves a currency by its ISO code.
106
- * @param {string} code The ISO code of the currency.
107
- * @returns {Currency | undefined} The currency corresponding to the ISO code or `undefined` if not found.
108
- */
109
- fromCode(code) {
110
- return this.currencyRecord[code.trim().toUpperCase()];
111
- }
112
- /**
113
- * Retrieves a currency by its name.
114
- * @param {string} currencyName The name of the currency.
115
- * @returns {Currency | undefined} The currency corresponding to the name or `undefined` if not found.
116
- */
117
- fromName(currencyName) {
118
- const input = currencyName.trim().toUpperCase();
119
- // First try direct lookup in name record
120
- const directMatch = this.nameRecord[input];
121
- if (directMatch)
122
- return directMatch;
123
- // If not found, try more lenient matching
124
- for (const [name, currObj] of Object.entries(this.nameRecord)) {
125
- if (name.toUpperCase() === input) {
126
- return currObj;
127
- }
128
- }
129
- // Finally, try case-insensitive exact match
130
- return this.currencyList.find((currency) => currency.name.toUpperCase() === input);
131
- }
132
- /**
133
- * Validates if a given ISO currency code is valid
134
- *
135
- * @param {string | null | undefined} code - The currency code to validate.
136
- * Should be a three-letter ISO currency code (e.g., 'USD', 'EUR').
137
- *
138
- * @returns {boolean} Returns true if:
139
- * - The currency code is not null/undefined
140
- * - The currency code successfully resolves to a valid Currency instance
141
- * Returns false otherwise.
142
- */
143
- isValidCode(code) {
144
- if (!code)
145
- return false;
146
- const currency = this.fromCode(code);
147
- return !!currency;
148
- }
149
- /**
150
- * Validates if a given currency name is valid
151
- *
152
- * @param {string | null | undefined} currencyName - The currency name to validate.
153
- *
154
- * @returns {boolean} Returns true if:
155
- * - The currency name is not null/undefined
156
- * - The currency name successfully resolves to a valid Currency instance
157
- * Returns false otherwise.
158
- */
159
- isValidName(currencyName) {
160
- if (!currencyName)
161
- return false;
162
- const currency = this.fromName(currencyName);
163
- return !!currency;
164
- }
165
- /**
166
- * Gets a regex-ready pattern matching all currency symbols
167
- * Pattern is memoized for performance
168
- * @returns {string} Pipe-separated pattern of escaped currency symbols
169
- *
170
- * @example
171
- * const pattern = currencyService.getCurrencySymbolPattern();
172
- * // Returns something like: "\$|€|\£|¥"
173
- */
174
- getCurrencySymbolPattern() {
175
- const symbols = new Set();
176
- Object.values(this.currencyList).forEach((currency) => {
177
- symbols.add(this.escapeRegExp(currency.symbol));
178
- symbols.add(this.escapeRegExp(currency.symbolNative));
179
- });
180
- return Array.from(symbols).join("|");
181
- }
182
- /**
183
- * Escapes special characters in a string for use in regular expressions
184
- * @private
185
- * @param {string} string - The string to escape
186
- * @returns {string} The escaped string
187
- *
188
- * @example
189
- * private escapeRegExp("$") // Returns "\$"
190
- */
191
- escapeRegExp(string) {
192
- return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
193
- }
194
- }