@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
@@ -76,12 +76,13 @@ class GlobalPhoneNumberService {
76
76
  * Private constructor to enforce singleton pattern
77
77
  */
78
78
  constructor() {
79
+ // Use Map for countryMetadata
79
80
  Object.defineProperty(this, "countryMetadata", {
80
81
  enumerable: true,
81
82
  configurable: true,
82
83
  writable: true,
83
- value: {}
84
- });
84
+ value: new Map()
85
+ }); // <--- CHANGED
85
86
  Object.defineProperty(this, "sharedCountryCodes", {
86
87
  enumerable: true,
87
88
  configurable: true,
@@ -120,13 +121,14 @@ class GlobalPhoneNumberService {
120
121
  const data = JSON.parse(JSON.stringify(phone_patterns_js_1.default));
121
122
  // Extract shared country codes
122
123
  this.sharedCountryCodes = data._shared_country_codes || {};
123
- const countries = Object.entries(data)
124
+ // Populate the countryMetadata Map
125
+ const countriesMap = new Map(); // <--- CHANGED
126
+ Object.entries(data)
124
127
  .filter(([key]) => !key.startsWith("_"))
125
- .reduce((result, [key, value]) => {
126
- result[key] = value;
127
- return result;
128
- }, {});
129
- this.countryMetadata = countries;
128
+ .forEach(([key, value]) => {
129
+ countriesMap.set(key, value); // <--- CHANGED (using .set())
130
+ });
131
+ this.countryMetadata = countriesMap; // <--- CHANGED
130
132
  this.initialized = true;
131
133
  }
132
134
  catch (error) {
@@ -136,16 +138,16 @@ class GlobalPhoneNumberService {
136
138
  /**
137
139
  * Gets the metadata for a specific country
138
140
  *
139
- * @param {string} countryCode - The ISO country code
141
+ * @param {ISO2CountryCode} countryCode - The ISO country code
140
142
  * @returns {CountryMetadata | undefined} The country metadata or undefined if not found
141
143
  */
142
144
  getCountryMetadata(countryCode) {
143
- return this.countryMetadata[countryCode];
145
+ return this.countryMetadata.get(countryCode); // <--- CHANGED (using .get())
144
146
  }
145
147
  /**
146
148
  * Gets all country metadata
147
149
  *
148
- * @returns {CountryMetadataMap} All country metadata
150
+ * @returns {CountryMetadataMap} All country metadata (as a Map)
149
151
  */
150
152
  getAllCountryMetadata() {
151
153
  return this.countryMetadata;
@@ -158,6 +160,7 @@ class GlobalPhoneNumberService {
158
160
  */
159
161
  getCountriesWithDialCode(dialCode) {
160
162
  const dialCodeStr = dialCode.toString();
163
+ // sharedCountryCodes remains an object, access is unchanged
161
164
  return this.sharedCountryCodes[dialCodeStr] || [];
162
165
  }
163
166
  /**
@@ -168,6 +171,7 @@ class GlobalPhoneNumberService {
168
171
  */
169
172
  isSharedDialCode(dialCode) {
170
173
  const dialCodeStr = dialCode.toString();
174
+ // sharedCountryCodes remains an object, access is unchanged
171
175
  const countries = this.sharedCountryCodes[dialCodeStr];
172
176
  return countries !== undefined && countries.length > 1;
173
177
  }
@@ -178,9 +182,10 @@ class GlobalPhoneNumberService {
178
182
  * @returns The country code or undefined if not found
179
183
  */
180
184
  getCountryForDialCode(dialCode) {
181
- for (const [countryCode, metadata] of Object.entries(this.countryMetadata)) {
185
+ // Iterate over Map entries
186
+ for (const [countryCode, metadata] of this.countryMetadata.entries()) { // <--- CHANGED (using .entries())
182
187
  if (metadata.code.toString() === dialCode) {
183
- return countryCode;
188
+ return countryCode; // No need for type assertion here
184
189
  }
185
190
  }
186
191
  return undefined;
@@ -200,23 +205,24 @@ class GlobalPhoneNumberService {
200
205
  for (let i = 3; i >= 1; i--) {
201
206
  if (numberWithoutPlus.length <= i)
202
207
  continue;
203
- const dialCode = numberWithoutPlus.substring(0, i);
204
- // Check if any country has this dial code
208
+ const potentialDialCode = numberWithoutPlus.substring(0, i); // Renamed for clarity
209
+ // Check if any country has this dial code by iterating Map values
205
210
  let countryFound = false;
206
- for (const [_, metadata] of Object.entries(this.countryMetadata)) {
207
- if (metadata.code.toString() === dialCode) {
211
+ for (const metadata of this.countryMetadata.values()) { // <--- CHANGED (using .values())
212
+ if (metadata.code.toString() === potentialDialCode) {
208
213
  countryFound = true;
209
214
  break;
210
215
  }
211
216
  }
212
217
  if (countryFound) {
213
- const nationalNumber = numberWithoutPlus.substring(dialCode.length);
214
- const isShared = this.isSharedDialCode(dialCode);
218
+ const nationalNumber = numberWithoutPlus.substring(potentialDialCode.length);
219
+ const isShared = this.isSharedDialCode(potentialDialCode);
220
+ // Use the existing methods which now work with the Map internally
215
221
  const possibleCountries = isShared
216
- ? this.sharedCountryCodes[dialCode]
217
- : [this.getCountryForDialCode(dialCode) ?? ""];
222
+ ? this.sharedCountryCodes[potentialDialCode] // Access shared codes directly
223
+ : this.getCountryForDialCode(potentialDialCode) ? [this.getCountryForDialCode(potentialDialCode)] : [];
218
224
  return {
219
- dialCode,
225
+ dialCode: potentialDialCode,
220
226
  isShared,
221
227
  nationalNumber,
222
228
  possibleCountries,
@@ -249,7 +255,7 @@ class GlobalPhoneNumberService {
249
255
  /**
250
256
  * Determines the most likely type of a phone number
251
257
  *
252
- * @param {string} countryCode - ISO country code
258
+ * @param {ISO2CountryCode} countryCode - ISO country code
253
259
  * @param {string} nationalNumber - The national number to check
254
260
  * @returns {PhoneNumberType} The type of the phone number
255
261
  */
@@ -257,8 +263,10 @@ class GlobalPhoneNumberService {
257
263
  if (!countryCode || !nationalNumber) {
258
264
  return PhoneNumberType.UNKNOWN;
259
265
  }
260
- const metadata = this.countryMetadata[countryCode];
261
- if (!metadata || !metadata.patterns) {
266
+ // Use .get() to retrieve metadata
267
+ const metadata = this.countryMetadata.get(countryCode); // <--- CHANGED (using .get())
268
+ // Check if metadata exists after .get()
269
+ if (!metadata || !metadata.patterns) { // <--- ADDED Check for undefined
262
270
  return PhoneNumberType.UNKNOWN;
263
271
  }
264
272
  // Verify that required patterns exist
@@ -285,7 +293,7 @@ class GlobalPhoneNumberService {
285
293
  /**
286
294
  * Validates a phone number against country-specific patterns
287
295
  *
288
- * @param {string} countryCode - ISO country code
296
+ * @param {ISO2CountryCode} countryCode - ISO country code
289
297
  * @param {string} nationalNumber - The national number to validate
290
298
  * @returns {boolean} True if the phone number is valid, false otherwise
291
299
  */
@@ -293,8 +301,10 @@ class GlobalPhoneNumberService {
293
301
  if (!countryCode || !nationalNumber) {
294
302
  return false;
295
303
  }
296
- const metadata = this.countryMetadata[countryCode];
297
- if (!metadata || !metadata.patterns) {
304
+ // Use .get() to retrieve metadata
305
+ const metadata = this.countryMetadata.get(countryCode); // <--- CHANGED (using .get())
306
+ // Check if metadata exists after .get()
307
+ if (!metadata || !metadata.patterns) { // <--- ADDED Check for undefined
298
308
  return false;
299
309
  }
300
310
  // Verify that required patterns exist
@@ -320,6 +330,7 @@ class GlobalPhoneNumberService {
320
330
  }
321
331
  /**
322
332
  * Formats a phone number according to the country's formatting rules
333
+ * (No changes needed in this method's logic)
323
334
  *
324
335
  * @param {string} phoneNumber - The phone number to format
325
336
  * @param {PhoneNumberFormat} format - The desired format
@@ -333,6 +344,7 @@ class GlobalPhoneNumberService {
333
344
  }
334
345
  /**
335
346
  * Creates a PhoneNumber from an international format string
347
+ * (No changes needed in this method's logic)
336
348
  *
337
349
  * @param {string} phoneNumber - The phone number in international format
338
350
  * @returns {PhoneNumber | undefined} The parsed phone number or undefined if invalid
@@ -342,6 +354,7 @@ class GlobalPhoneNumberService {
342
354
  }
343
355
  /**
344
356
  * Creates a PhoneNumber from a phone number with explicit country
357
+ * (No changes needed in this method's logic)
345
358
  *
346
359
  * @param {string} phoneNumber - The phone number in any format
347
360
  * @param {Country | string} country - The country or country code
@@ -357,6 +370,7 @@ class GlobalPhoneNumberService {
357
370
  }
358
371
  /**
359
372
  * Extracts the information needed for dialing from one country to another
373
+ * (No changes needed in this method's logic)
360
374
  *
361
375
  * @param {string} fromCountry - ISO code of the country dialing from
362
376
  * @param {PhoneNumber} phoneNumber - The phone number to dial
@@ -372,6 +386,7 @@ class GlobalPhoneNumberService {
372
386
  }
373
387
  /**
374
388
  * Cleans a phone number by removing all non-digit characters except the leading plus
389
+ * (No changes needed in this method's logic)
375
390
  *
376
391
  * @param {string} phoneNumber - The phone number to clean
377
392
  * @returns {string} The cleaned phone number
@@ -388,6 +403,7 @@ class GlobalPhoneNumberService {
388
403
  }
389
404
  /**
390
405
  * Extracts the country and national number from an international phone number
406
+ * (No changes needed in this method's logic - relies on extractDialCode which was updated)
391
407
  *
392
408
  * @param {string} phoneNumber - The phone number in international format
393
409
  * @returns {[Country | undefined, string]} The country and national number
@@ -413,25 +429,29 @@ class GlobalPhoneNumberService {
413
429
  /**
414
430
  * Gets a list of commonly used examples of valid phone numbers for a country
415
431
  *
416
- * @param {string} countryCode - The ISO country code
432
+ * @param {ISO2CountryCode} countryCode - The ISO country code
417
433
  * @returns {string[]} Array of example phone numbers
418
434
  */
419
435
  getExampleNumbers(countryCode) {
420
- const metadata = this.countryMetadata[countryCode];
436
+ // Use .get() to retrieve metadata
437
+ const metadata = this.countryMetadata.get(countryCode); // <--- CHANGED (using .get())
421
438
  if (!metadata)
422
- return [];
439
+ return []; // <--- ADDED Check for undefined
423
440
  const dialCode = metadata.code;
424
441
  const examples = [];
425
- // Since landline and mobile patterns are required, we can always generate examples
426
- // Landline example
427
- examples.push(`+${dialCode}${this.generateExampleFromPattern(metadata.patterns.landline)}`);
428
- // Mobile example
429
- examples.push(`+${dialCode}${this.generateExampleFromPattern(metadata.patterns.mobile)}`);
442
+ // Ensure patterns exist before trying to generate examples
443
+ if (metadata.patterns.landline) {
444
+ examples.push(`+${dialCode}${this.generateExampleFromPattern(metadata.patterns.landline)}`);
445
+ }
446
+ if (metadata.patterns.mobile) {
447
+ examples.push(`+${dialCode}${this.generateExampleFromPattern(metadata.patterns.mobile)}`);
448
+ }
430
449
  return examples;
431
450
  }
432
451
  /**
433
452
  * Generates an example phone number that would match a given pattern
434
453
  * This is a simple implementation that handles basic patterns
454
+ * (No changes needed in this method's logic)
435
455
  *
436
456
  * @param pattern - The regex pattern
437
457
  * @returns A string that would match the pattern
@@ -450,9 +470,10 @@ class GlobalPhoneNumberService {
450
470
  }
451
471
  /**
452
472
  * Checks if a phone number is valid for a specific country
473
+ * (No changes needed in this method's logic)
453
474
  *
454
475
  * @param {string} phoneNumber - The phone number to validate
455
- * @param {string} countryCode - The ISO country code
476
+ * @param {ISO2CountryCode} countryCode - The ISO country code
456
477
  * @returns {boolean} True if the phone number is valid for the country, false otherwise
457
478
  */
458
479
  isValidForCountry(phoneNumber, countryCode) {
@@ -469,12 +490,12 @@ class GlobalPhoneNumberService {
469
490
  * @returns True if the dial code exists, false otherwise
470
491
  */
471
492
  hasDialCode(dialCode) {
472
- // Check if it's a shared dial code
493
+ // Check if it's a shared dial code (access unchanged)
473
494
  if (this.sharedCountryCodes[dialCode]?.length > 0) {
474
495
  return true;
475
496
  }
476
- // Check if any country has this dial code
477
- for (const metadata of Object.values(this.countryMetadata)) {
497
+ // Check if any country has this dial code by iterating Map values
498
+ for (const metadata of this.countryMetadata.values()) { // <--- CHANGED (using .values())
478
499
  if (metadata.code.toString() === dialCode) {
479
500
  return true;
480
501
  }
@@ -1,106 +0,0 @@
1
- import { Bank } from "./bank.js";
2
- /**
3
- * Regex pattern to validate SWIFT codes.
4
- * The SWIFT code must follow the format: XXXX XX XX XXX (optional last part for branches).
5
- * @constant {RegExp}
6
- */
7
- export declare const SWIFT_CODE_REGEX: RegExp;
8
- /**
9
- * Service for managing bank data.
10
- * @class BankService
11
- */
12
- export declare class BankService {
13
- private static instance;
14
- private bankList;
15
- private bankRecord;
16
- private banksByName;
17
- private banksByShortName;
18
- private staticReferences;
19
- private constructor();
20
- /**
21
- * Gets the singleton instance of BankService.
22
- * Creates the instance if it doesn't exist.
23
- * @static
24
- * @returns {BankService} The singleton instance
25
- */
26
- static getInstance(): BankService;
27
- /**
28
- * Initializes the service with bank data.
29
- * Should be called once when your application starts.
30
- */
31
- private initialize;
32
- /**
33
- * Gets all banks.
34
- * @returns {Bank[]} Array of all banks
35
- */
36
- getAll(): Bank[];
37
- /**
38
- * Gets all banks as a record.
39
- * @returns {Record<string, Bank>} Record of bank SWIFT codes and bank objects
40
- */
41
- getAllAsRecord(): Record<string, Bank>;
42
- /**
43
- * Gets static bank references to be used by the Bank class.
44
- * @returns {Map<string, Bank>} Map of static references
45
- */
46
- getStaticReferences(): Map<string, Bank>;
47
- /**
48
- * Retrieves a bank by its SWIFT code.
49
- * @param {string} swiftCode The SWIFT code of the bank.
50
- * @returns {Bank | undefined} The bank corresponding to the SWIFT code or `undefined` if not found.
51
- */
52
- fromSWIFTCode(swiftCode: string): Bank | undefined;
53
- /**
54
- * Retrieves a bank by its name.
55
- * @param {string} bankName The name of the bank.
56
- * @returns {Bank | undefined} The bank corresponding to the name or `undefined` if not found.
57
- */
58
- fromBankName(bankName: string): Bank | undefined;
59
- /**
60
- * Validates if a given SWIFT/BIC (Bank Identifier Code) is valid.
61
- * @param {string | null | undefined} swiftCode - The SWIFT/BIC code to validate.
62
- * @returns {boolean} Returns true if the SWIFT code is valid, false otherwise.
63
- */
64
- isValidSwiftCode(swiftCode?: string | null): boolean;
65
- /**
66
- * Validates if a given bank name is valid.
67
- * @param {string | null | undefined} bankName - The bank name to validate.
68
- * @returns {boolean} Returns true if the bank name is valid, false otherwise.
69
- */
70
- isValidBankName(bankName?: string | null): boolean;
71
- /**
72
- * Searches for banks that match the given search term.
73
- * @param {string} searchTerm - The partial name or SWIFT code to search for.
74
- * @param {number} [limit=10] - Maximum number of results to return.
75
- * @returns {Bank[]} Array of matching banks, limited to specified count.
76
- */
77
- search(searchTerm: string, limit?: number): Bank[];
78
- /**
79
- * Compares two Bank instances for equality by checking their full name, short name, and SWIFT code
80
- *
81
- * @param {Bank} bank1 - First bank to compare
82
- * @param {Bank} bank2 - Second bank to compare
83
- * @returns {boolean} True if banks are equal, false otherwise
84
- * @private
85
- */
86
- compare(bank1: Bank, bank2: Bank): boolean;
87
- /**
88
- * Retrieves all SWIFT codes from the TZ_BANKS list.
89
- * @returns {string[]} A list of all SWIFT codes in uppercase.
90
- */
91
- getAllSwiftCodes(): string[];
92
- /**
93
- * Checks whether a given SWIFT code is in the correct format.
94
- * @param {string} swiftCode The SWIFT code to check.
95
- * @returns {boolean} `true` if the SWIFT code matches the expected format; `false` otherwise.
96
- */
97
- isValidSwiftCodeFormat(swiftCode: string): boolean;
98
- /**
99
- * Validates whether a given SWIFT code is correct.
100
- * It checks both the SWIFT code format and if the SWIFT code exists in the list of valid codes.
101
- * @param {string} [swiftCode] The SWIFT code to validate.
102
- * @returns {boolean} `true` if valid, otherwise `false`.
103
- */
104
- validateSWIFTCode: (swiftCode?: string) => boolean;
105
- }
106
- //# sourceMappingURL=service.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../../../src/src/models/bank/service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAGjC;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,QAAgD,CAAC;AAE9E;;;GAGG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAc;IACrC,OAAO,CAAC,QAAQ,CAAc;IAC9B,OAAO,CAAC,UAAU,CAA4B;IAC9C,OAAO,CAAC,WAAW,CAA4B;IAC/C,OAAO,CAAC,gBAAgB,CAA4B;IAGpD,OAAO,CAAC,gBAAgB,CAAgC;IAExD,OAAO;IAEP;;;;;OAKG;IACH,MAAM,CAAC,WAAW,IAAI,WAAW;IAQjC;;;OAGG;IACH,OAAO,CAAC,UAAU;IA0ClB;;;OAGG;IACH,MAAM,IAAI,IAAI,EAAE;IAIhB;;;OAGG;IACH,cAAc,IAAI,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC;IAItC;;;OAGG;IACH,mBAAmB,IAAI,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC;IAIxC;;;;OAIG;IACH,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS;IAKlD;;;;OAIG;IACH,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS;IAqChD;;;;OAIG;IACH,gBAAgB,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO;IAKpD;;;;OAIG;IACH,eAAe,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO;IAKlD;;;;;OAKG;IACH,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,KAAK,GAAE,MAAW,GAAG,IAAI,EAAE;IAetD;;;;;;;OAOG;IACH,OAAO,CAAC,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,GAAG,OAAO;IAQ1C;;;OAGG;IACH,gBAAgB,IAAI,MAAM,EAAE;IAI5B;;;;OAIG;IACH,sBAAsB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO;IAIlD;;;;;OAKG;IACH,iBAAiB,eAAgB,MAAM,KAAG,OAAO,CAQ/C;CACH"}
@@ -1,240 +0,0 @@
1
- import { Bank } from "./bank.js";
2
- import file from "../../data/banks_tz.js";
3
- /**
4
- * Regex pattern to validate SWIFT codes.
5
- * The SWIFT code must follow the format: XXXX XX XX XXX (optional last part for branches).
6
- * @constant {RegExp}
7
- */
8
- export const SWIFT_CODE_REGEX = /^[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}([A-Z0-9]{3})?$/;
9
- /**
10
- * Service for managing bank data.
11
- * @class BankService
12
- */
13
- export class BankService {
14
- constructor() {
15
- Object.defineProperty(this, "bankList", {
16
- enumerable: true,
17
- configurable: true,
18
- writable: true,
19
- value: []
20
- });
21
- Object.defineProperty(this, "bankRecord", {
22
- enumerable: true,
23
- configurable: true,
24
- writable: true,
25
- value: {}
26
- });
27
- Object.defineProperty(this, "banksByName", {
28
- enumerable: true,
29
- configurable: true,
30
- writable: true,
31
- value: {}
32
- });
33
- Object.defineProperty(this, "banksByShortName", {
34
- enumerable: true,
35
- configurable: true,
36
- writable: true,
37
- value: {}
38
- });
39
- // Static references for direct access through Bank class
40
- Object.defineProperty(this, "staticReferences", {
41
- enumerable: true,
42
- configurable: true,
43
- writable: true,
44
- value: new Map()
45
- });
46
- /**
47
- * Validates whether a given SWIFT code is correct.
48
- * It checks both the SWIFT code format and if the SWIFT code exists in the list of valid codes.
49
- * @param {string} [swiftCode] The SWIFT code to validate.
50
- * @returns {boolean} `true` if valid, otherwise `false`.
51
- */
52
- Object.defineProperty(this, "validateSWIFTCode", {
53
- enumerable: true,
54
- configurable: true,
55
- writable: true,
56
- value: (swiftCode) => {
57
- if (!swiftCode)
58
- return false;
59
- const normalizedCode = swiftCode.trim().toUpperCase();
60
- return (this.isValidSwiftCodeFormat(normalizedCode) &&
61
- this.getAllSwiftCodes().includes(normalizedCode));
62
- }
63
- });
64
- }
65
- /**
66
- * Gets the singleton instance of BankService.
67
- * Creates the instance if it doesn't exist.
68
- * @static
69
- * @returns {BankService} The singleton instance
70
- */
71
- static getInstance() {
72
- if (!BankService.instance) {
73
- BankService.instance = new BankService();
74
- BankService.instance.initialize();
75
- }
76
- return BankService.instance;
77
- }
78
- /**
79
- * Initializes the service with bank data.
80
- * Should be called once when your application starts.
81
- */
82
- initialize() {
83
- try {
84
- const data = JSON.parse(JSON.stringify(file));
85
- const banks = Object.values(data).map((b) => new Bank(b.fullName, b.shortName, b.swiftCode));
86
- const swiftCodeRecord = {};
87
- const nameRecord = {};
88
- const shortNameRecord = {};
89
- banks.forEach((bank) => {
90
- // Populate records
91
- swiftCodeRecord[bank.swiftCode.toUpperCase()] = bank;
92
- nameRecord[bank.fullName.toUpperCase()] = bank;
93
- shortNameRecord[bank.shortName.toUpperCase()] = bank;
94
- // Add to static references with uppercase short name
95
- const shortName = bank.shortName.toUpperCase();
96
- this.staticReferences.set(shortName, bank);
97
- // Handle bank names with spaces (like "GT BANK")
98
- if (shortName.includes(" ")) {
99
- const noSpaceName = shortName.replace(/\s+/g, "_");
100
- this.staticReferences.set(noSpaceName, bank);
101
- }
102
- });
103
- this.bankRecord = swiftCodeRecord;
104
- this.banksByName = nameRecord;
105
- this.banksByShortName = shortNameRecord;
106
- this.bankList = banks;
107
- }
108
- catch (error) {
109
- console.error("Failed to initialize BankService:", error);
110
- }
111
- }
112
- /**
113
- * Gets all banks.
114
- * @returns {Bank[]} Array of all banks
115
- */
116
- getAll() {
117
- return this.bankList;
118
- }
119
- /**
120
- * Gets all banks as a record.
121
- * @returns {Record<string, Bank>} Record of bank SWIFT codes and bank objects
122
- */
123
- getAllAsRecord() {
124
- return this.bankRecord;
125
- }
126
- /**
127
- * Gets static bank references to be used by the Bank class.
128
- * @returns {Map<string, Bank>} Map of static references
129
- */
130
- getStaticReferences() {
131
- return this.staticReferences;
132
- }
133
- /**
134
- * Retrieves a bank by its SWIFT code.
135
- * @param {string} swiftCode The SWIFT code of the bank.
136
- * @returns {Bank | undefined} The bank corresponding to the SWIFT code or `undefined` if not found.
137
- */
138
- fromSWIFTCode(swiftCode) {
139
- if (!swiftCode || typeof swiftCode !== "string")
140
- return;
141
- return this.bankRecord[swiftCode.toUpperCase()];
142
- }
143
- /**
144
- * Retrieves a bank by its name.
145
- * @param {string} bankName The name of the bank.
146
- * @returns {Bank | undefined} The bank corresponding to the name or `undefined` if not found.
147
- */
148
- fromBankName(bankName) {
149
- if (!bankName || typeof bankName !== "string")
150
- return;
151
- // First try shortname exact match
152
- const bankByShortName = this.banksByShortName[bankName.toUpperCase()];
153
- if (bankByShortName)
154
- return bankByShortName;
155
- // Next try fullname exact match
156
- const bankByName = this.banksByName[bankName.toUpperCase()];
157
- if (bankByName)
158
- return bankByName;
159
- // If not found, try more lenient matching on full name
160
- for (const [name, bankObj] of Object.entries(this.banksByName)) {
161
- if (bankName.toUpperCase() === name.toUpperCase()) {
162
- return bankObj;
163
- }
164
- }
165
- // Try lenient matching on short name
166
- for (const [shortName, bankObj] of Object.entries(this.banksByShortName)) {
167
- if (bankName.toUpperCase() === shortName.toUpperCase()) {
168
- return bankObj;
169
- }
170
- }
171
- // Legacy fallback - case insensitive exact match
172
- return this.bankList.find((bank) => bank.fullName.toLowerCase() === bankName.toLowerCase() ||
173
- bankName.toLowerCase() === bank.shortName.toLowerCase());
174
- }
175
- /**
176
- * Validates if a given SWIFT/BIC (Bank Identifier Code) is valid.
177
- * @param {string | null | undefined} swiftCode - The SWIFT/BIC code to validate.
178
- * @returns {boolean} Returns true if the SWIFT code is valid, false otherwise.
179
- */
180
- isValidSwiftCode(swiftCode) {
181
- if (!swiftCode)
182
- return false;
183
- return !!this.fromSWIFTCode(swiftCode);
184
- }
185
- /**
186
- * Validates if a given bank name is valid.
187
- * @param {string | null | undefined} bankName - The bank name to validate.
188
- * @returns {boolean} Returns true if the bank name is valid, false otherwise.
189
- */
190
- isValidBankName(bankName) {
191
- if (!bankName)
192
- return false;
193
- return !!this.fromBankName(bankName);
194
- }
195
- /**
196
- * Searches for banks that match the given search term.
197
- * @param {string} searchTerm - The partial name or SWIFT code to search for.
198
- * @param {number} [limit=10] - Maximum number of results to return.
199
- * @returns {Bank[]} Array of matching banks, limited to specified count.
200
- */
201
- search(searchTerm, limit = 10) {
202
- if (!searchTerm || typeof searchTerm !== "string")
203
- return [];
204
- const term = searchTerm.toLowerCase().trim();
205
- if (term.length === 0)
206
- return [];
207
- const results = this.bankList.filter((bank) => bank.fullName.toLowerCase().includes(term) ||
208
- bank.shortName.toLowerCase().includes(term) ||
209
- bank.swiftCode.toLowerCase().includes(term));
210
- return results.slice(0, limit);
211
- }
212
- /**
213
- * Compares two Bank instances for equality by checking their full name, short name, and SWIFT code
214
- *
215
- * @param {Bank} bank1 - First bank to compare
216
- * @param {Bank} bank2 - Second bank to compare
217
- * @returns {boolean} True if banks are equal, false otherwise
218
- * @private
219
- */
220
- compare(bank1, bank2) {
221
- return (bank1.fullName === bank2.fullName &&
222
- bank1.shortName === bank2.shortName &&
223
- bank1.swiftCode === bank2.swiftCode);
224
- }
225
- /**
226
- * Retrieves all SWIFT codes from the TZ_BANKS list.
227
- * @returns {string[]} A list of all SWIFT codes in uppercase.
228
- */
229
- getAllSwiftCodes() {
230
- return this.bankList.map((bank) => bank.swiftCode.toUpperCase());
231
- }
232
- /**
233
- * Checks whether a given SWIFT code is in the correct format.
234
- * @param {string} swiftCode The SWIFT code to check.
235
- * @returns {boolean} `true` if the SWIFT code matches the expected format; `false` otherwise.
236
- */
237
- isValidSwiftCodeFormat(swiftCode) {
238
- return SWIFT_CODE_REGEX.test(swiftCode);
239
- }
240
- }