@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,75 +0,0 @@
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
@@ -1 +0,0 @@
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"}
@@ -1,267 +0,0 @@
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
- }
@@ -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
- }