@temboplus/frontend-core 0.2.1 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +126 -25
- package/esm/deps/jsr.io/@std/internal/1.0.5/assertion_state.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/testing/1.0.9/_test_suite.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/testing/1.0.9/bdd.d.ts.map +1 -0
- package/esm/mod.d.ts +2 -0
- package/esm/mod.d.ts.map +1 -1
- package/esm/mod.js +2 -0
- package/esm/src/config/config_service.d.ts +11 -0
- package/esm/src/config/config_service.d.ts.map +1 -0
- package/esm/src/config/config_service.js +19 -0
- package/esm/src/config/index.d.ts +2 -0
- package/esm/src/config/index.d.ts.map +1 -0
- package/esm/src/config/index.js +1 -0
- package/esm/src/{models/bank/banks.d.ts → data/banks_tz.d.ts} +1 -1
- package/esm/src/data/banks_tz.d.ts.map +1 -0
- package/esm/src/data/countries.d.ts +6 -0
- package/esm/src/data/countries.d.ts.map +1 -0
- package/esm/src/data/countries.js +974 -0
- package/esm/src/data/currencies.d.ts +1901 -0
- package/esm/src/data/currencies.d.ts.map +1 -0
- package/esm/src/data/currencies.js +1073 -0
- package/esm/src/data/phone_patterns.d.ts +3809 -0
- package/esm/src/data/phone_patterns.d.ts.map +1 -0
- package/esm/src/data/phone_patterns.js +2325 -0
- package/esm/src/models/amount/amount.d.ts +10 -12
- package/esm/src/models/amount/amount.d.ts.map +1 -1
- package/esm/src/models/amount/amount.js +58 -96
- package/esm/src/models/bank/bank.d.ts +58 -53
- package/esm/src/models/bank/bank.d.ts.map +1 -1
- package/esm/src/models/bank/bank.js +49 -84
- package/esm/src/models/bank/index.d.ts +1 -1
- package/esm/src/models/bank/index.d.ts.map +1 -1
- package/esm/src/models/bank/index.js +1 -1
- package/esm/src/models/bank/service.d.ts +106 -0
- package/esm/src/models/bank/service.d.ts.map +1 -0
- package/esm/src/models/bank/service.js +240 -0
- package/esm/src/models/country/country.d.ts +568 -0
- package/esm/src/models/country/country.d.ts.map +1 -0
- package/esm/src/models/country/country.js +165 -0
- package/esm/src/models/country/country.test.d.ts.map +1 -0
- package/esm/src/models/country/index.d.ts +3 -0
- package/esm/src/models/country/index.d.ts.map +1 -0
- package/esm/src/models/country/index.js +2 -0
- package/esm/src/models/country/service.d.ts +75 -0
- package/esm/src/models/country/service.d.ts.map +1 -0
- package/esm/src/models/country/service.js +267 -0
- package/esm/src/models/currency/currency.d.ts +341 -0
- package/esm/src/models/currency/currency.d.ts.map +1 -0
- package/esm/src/models/currency/currency.js +225 -0
- package/esm/src/models/currency/currency.test.d.ts.map +1 -0
- package/esm/src/models/currency/index.d.ts +3 -0
- package/esm/src/models/currency/index.d.ts.map +1 -0
- package/esm/src/models/currency/index.js +2 -0
- package/esm/src/models/currency/service.d.ts +96 -0
- package/esm/src/models/currency/service.d.ts.map +1 -0
- package/esm/src/models/currency/service.js +194 -0
- package/esm/src/models/index.d.ts +2 -0
- package/esm/src/models/index.d.ts.map +1 -1
- package/esm/src/models/index.js +2 -0
- package/esm/src/models/phone_number/format.d.ts +14 -0
- package/esm/src/models/phone_number/format.d.ts.map +1 -0
- package/esm/src/models/phone_number/format.js +14 -0
- package/esm/src/models/phone_number/global/phone_number.d.ts +160 -0
- package/esm/src/models/phone_number/global/phone_number.d.ts.map +1 -0
- package/esm/src/models/phone_number/global/phone_number.js +453 -0
- package/esm/src/models/phone_number/global/phone_number.test.d.ts.map +1 -0
- package/esm/src/models/phone_number/global/service.d.ts +260 -0
- package/esm/src/models/phone_number/global/service.d.ts.map +1 -0
- package/esm/src/models/phone_number/global/service.js +477 -0
- package/esm/src/models/phone_number/global/service.test.d.ts.map +1 -0
- package/esm/src/models/phone_number/index.d.ts +5 -3
- package/esm/src/models/phone_number/index.d.ts.map +1 -1
- package/esm/src/models/phone_number/index.js +5 -3
- package/esm/src/models/phone_number/{antd_validator.d.ts → tz/antd_validator.d.ts} +2 -2
- package/esm/src/models/phone_number/tz/antd_validator.d.ts.map +1 -0
- package/esm/src/models/phone_number/{antd_validator.js → tz/antd_validator.js} +2 -2
- package/esm/src/models/phone_number/tz/antdvalidator.test.d.ts.map +1 -0
- package/esm/src/models/phone_number/tz/network_operator.d.ts.map +1 -0
- package/{script/src/models/phone_number → esm/src/models/phone_number/tz}/phone_number.d.ts +46 -29
- package/esm/src/models/phone_number/tz/phone_number.d.ts.map +1 -0
- package/esm/src/models/phone_number/{phone_number.js → tz/phone_number.js} +87 -41
- package/esm/src/models/phone_number/tz/phone_number.test.d.ts.map +1 -0
- package/esm/src/reports/index.d.ts +2 -0
- package/esm/src/reports/index.d.ts.map +1 -0
- package/esm/src/reports/index.js +1 -0
- package/esm/src/reports/report_manager.d.ts +144 -0
- package/esm/src/reports/report_manager.d.ts.map +1 -0
- package/esm/src/reports/report_manager.js +315 -0
- package/package.json +4 -3
- package/script/deps/jsr.io/@std/internal/1.0.5/assertion_state.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/testing/1.0.9/_test_suite.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/testing/1.0.9/bdd.d.ts.map +1 -0
- package/script/mod.d.ts +2 -0
- package/script/mod.d.ts.map +1 -1
- package/script/mod.js +2 -0
- package/script/src/config/config_service.d.ts +11 -0
- package/script/src/config/config_service.d.ts.map +1 -0
- package/script/src/config/config_service.js +23 -0
- package/script/src/config/index.d.ts +2 -0
- package/script/src/config/index.d.ts.map +1 -0
- package/script/src/config/index.js +17 -0
- package/script/src/{models/bank/banks.d.ts → data/banks_tz.d.ts} +1 -1
- package/script/src/data/banks_tz.d.ts.map +1 -0
- package/script/src/data/countries.d.ts +6 -0
- package/script/src/data/countries.d.ts.map +1 -0
- package/script/src/data/countries.js +976 -0
- package/script/src/data/currencies.d.ts +1901 -0
- package/script/src/data/currencies.d.ts.map +1 -0
- package/script/src/data/currencies.js +1075 -0
- package/script/src/data/phone_patterns.d.ts +3809 -0
- package/script/src/data/phone_patterns.d.ts.map +1 -0
- package/script/src/data/phone_patterns.js +2327 -0
- package/script/src/models/amount/amount.d.ts +10 -12
- package/script/src/models/amount/amount.d.ts.map +1 -1
- package/script/src/models/amount/amount.js +60 -98
- package/script/src/models/bank/bank.d.ts +58 -53
- package/script/src/models/bank/bank.d.ts.map +1 -1
- package/script/src/models/bank/bank.js +49 -87
- package/script/src/models/bank/index.d.ts +1 -1
- package/script/src/models/bank/index.d.ts.map +1 -1
- package/script/src/models/bank/index.js +1 -1
- package/script/src/models/bank/service.d.ts +106 -0
- package/script/src/models/bank/service.d.ts.map +1 -0
- package/script/src/models/bank/service.js +247 -0
- package/script/src/models/country/country.d.ts +568 -0
- package/script/src/models/country/country.d.ts.map +1 -0
- package/script/src/models/country/country.js +169 -0
- package/script/src/models/country/country.test.d.ts.map +1 -0
- package/script/src/models/country/index.d.ts +3 -0
- package/script/src/models/country/index.d.ts.map +1 -0
- package/script/src/models/country/index.js +18 -0
- package/script/src/models/country/service.d.ts +75 -0
- package/script/src/models/country/service.d.ts.map +1 -0
- package/script/src/models/country/service.js +274 -0
- package/script/src/models/currency/currency.d.ts +341 -0
- package/script/src/models/currency/currency.d.ts.map +1 -0
- package/script/src/models/currency/currency.js +229 -0
- package/script/src/models/currency/currency.test.d.ts.map +1 -0
- package/script/src/models/currency/index.d.ts +3 -0
- package/script/src/models/currency/index.d.ts.map +1 -0
- package/script/src/models/currency/index.js +18 -0
- package/script/src/models/currency/service.d.ts +96 -0
- package/script/src/models/currency/service.d.ts.map +1 -0
- package/script/src/models/currency/service.js +201 -0
- package/script/src/models/index.d.ts +2 -0
- package/script/src/models/index.d.ts.map +1 -1
- package/script/src/models/index.js +2 -0
- package/script/src/models/phone_number/format.d.ts +14 -0
- package/script/src/models/phone_number/format.d.ts.map +1 -0
- package/script/src/models/phone_number/format.js +17 -0
- package/script/src/models/phone_number/global/phone_number.d.ts +160 -0
- package/script/src/models/phone_number/global/phone_number.d.ts.map +1 -0
- package/script/src/models/phone_number/global/phone_number.js +457 -0
- package/script/src/models/phone_number/global/phone_number.test.d.ts.map +1 -0
- package/script/src/models/phone_number/global/service.d.ts +260 -0
- package/script/src/models/phone_number/global/service.d.ts.map +1 -0
- package/script/src/models/phone_number/global/service.js +485 -0
- package/script/src/models/phone_number/global/service.test.d.ts.map +1 -0
- package/script/src/models/phone_number/index.d.ts +5 -3
- package/script/src/models/phone_number/index.d.ts.map +1 -1
- package/script/src/models/phone_number/index.js +5 -3
- package/script/src/models/phone_number/{antd_validator.d.ts → tz/antd_validator.d.ts} +2 -2
- package/script/src/models/phone_number/tz/antd_validator.d.ts.map +1 -0
- package/script/src/models/phone_number/{antd_validator.js → tz/antd_validator.js} +1 -1
- package/script/src/models/phone_number/tz/antdvalidator.test.d.ts.map +1 -0
- package/script/src/models/phone_number/tz/network_operator.d.ts.map +1 -0
- package/{esm/src/models/phone_number → script/src/models/phone_number/tz}/phone_number.d.ts +46 -29
- package/script/src/models/phone_number/tz/phone_number.d.ts.map +1 -0
- package/script/src/models/phone_number/{phone_number.js → tz/phone_number.js} +89 -43
- package/script/src/models/phone_number/tz/phone_number.test.d.ts.map +1 -0
- package/script/src/reports/index.d.ts +2 -0
- package/script/src/reports/index.d.ts.map +1 -0
- package/script/src/reports/index.js +17 -0
- package/script/src/reports/report_manager.d.ts +144 -0
- package/script/src/reports/report_manager.d.ts.map +1 -0
- package/script/src/reports/report_manager.js +325 -0
- package/esm/src/models/bank/banks.d.ts.map +0 -1
- package/esm/src/models/bank/utils.d.ts +0 -25
- package/esm/src/models/bank/utils.d.ts.map +0 -1
- package/esm/src/models/bank/utils.js +0 -35
- package/esm/src/models/phone_number/antd_validator.d.ts.map +0 -1
- package/esm/src/models/phone_number/antdvalidator.test.d.ts.map +0 -1
- package/esm/src/models/phone_number/network_operator.d.ts.map +0 -1
- package/esm/src/models/phone_number/phone_number.d.ts.map +0 -1
- package/esm/src/models/phone_number/phone_number.test.d.ts.map +0 -1
- package/script/src/models/bank/banks.d.ts.map +0 -1
- package/script/src/models/bank/utils.d.ts +0 -25
- package/script/src/models/bank/utils.d.ts.map +0 -1
- package/script/src/models/bank/utils.js +0 -41
- package/script/src/models/phone_number/antd_validator.d.ts.map +0 -1
- package/script/src/models/phone_number/antdvalidator.test.d.ts.map +0 -1
- package/script/src/models/phone_number/network_operator.d.ts.map +0 -1
- package/script/src/models/phone_number/phone_number.d.ts.map +0 -1
- package/script/src/models/phone_number/phone_number.test.d.ts.map +0 -1
- /package/esm/src/{models/bank/banks.js → data/banks_tz.js} +0 -0
- /package/esm/src/models/phone_number/{network_operator.d.ts → tz/network_operator.d.ts} +0 -0
- /package/esm/src/models/phone_number/{network_operator.js → tz/network_operator.js} +0 -0
- /package/script/src/{models/bank/banks.js → data/banks_tz.js} +0 -0
- /package/script/src/models/phone_number/{network_operator.d.ts → tz/network_operator.d.ts} +0 -0
- /package/script/src/models/phone_number/{network_operator.js → tz/network_operator.js} +0 -0
|
@@ -0,0 +1,485 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview
|
|
4
|
+
*
|
|
5
|
+
* # Global Phone Number Service
|
|
6
|
+
*
|
|
7
|
+
* ## Features
|
|
8
|
+
* 1. Loads and maintains phone number metadata from patterns database
|
|
9
|
+
* 2. Validates phone numbers against country-specific patterns
|
|
10
|
+
* 3. Provides utility functions for phone number operations
|
|
11
|
+
* 4. Follows singleton pattern for efficient resource usage
|
|
12
|
+
*/
|
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.GlobalPhoneNumberService = exports.SharedDialCodeError = exports.PhoneNumberType = void 0;
|
|
18
|
+
const country_js_1 = require("../../country/country.js");
|
|
19
|
+
const phone_number_js_1 = require("./phone_number.js");
|
|
20
|
+
const phone_patterns_js_1 = __importDefault(require("../../../data/phone_patterns.js"));
|
|
21
|
+
const format_js_1 = require("../format.js");
|
|
22
|
+
/**
|
|
23
|
+
* Phone number pattern type
|
|
24
|
+
*/
|
|
25
|
+
var PhoneNumberType;
|
|
26
|
+
(function (PhoneNumberType) {
|
|
27
|
+
PhoneNumberType["LANDLINE"] = "landline";
|
|
28
|
+
PhoneNumberType["MOBILE"] = "mobile";
|
|
29
|
+
PhoneNumberType["TOLL_FREE"] = "toll_free";
|
|
30
|
+
PhoneNumberType["PREMIUM_RATE"] = "premium_rate";
|
|
31
|
+
PhoneNumberType["SHARED_COST"] = "shared_cost";
|
|
32
|
+
PhoneNumberType["EMERGENCY"] = "emergency";
|
|
33
|
+
PhoneNumberType["SPECIAL_SERVICES"] = "special_services";
|
|
34
|
+
PhoneNumberType["VOIP"] = "voip";
|
|
35
|
+
PhoneNumberType["PERSONAL"] = "personal";
|
|
36
|
+
PhoneNumberType["UNKNOWN"] = "unknown";
|
|
37
|
+
})(PhoneNumberType || (exports.PhoneNumberType = PhoneNumberType = {}));
|
|
38
|
+
/**
|
|
39
|
+
* Error thrown when a phone number has an ambiguous country due to shared dial code
|
|
40
|
+
*/
|
|
41
|
+
class SharedDialCodeError extends Error {
|
|
42
|
+
/**
|
|
43
|
+
* Creates a new SharedDialCodeError
|
|
44
|
+
*
|
|
45
|
+
* @param dialCode - The shared dial code
|
|
46
|
+
* @param countries - Countries that share this dial code
|
|
47
|
+
*/
|
|
48
|
+
constructor(dialCode, countries) {
|
|
49
|
+
const message = `Dial code +${dialCode} is shared by multiple countries: ${countries.join(", ")}. Please use 'fromWithCountry' with a specific country.`;
|
|
50
|
+
super(message);
|
|
51
|
+
/** The dial code that's shared */
|
|
52
|
+
Object.defineProperty(this, "dialCode", {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
configurable: true,
|
|
55
|
+
writable: true,
|
|
56
|
+
value: void 0
|
|
57
|
+
});
|
|
58
|
+
/** List of country codes that share this dial code */
|
|
59
|
+
Object.defineProperty(this, "countries", {
|
|
60
|
+
enumerable: true,
|
|
61
|
+
configurable: true,
|
|
62
|
+
writable: true,
|
|
63
|
+
value: void 0
|
|
64
|
+
});
|
|
65
|
+
this.name = "SharedDialCodeError";
|
|
66
|
+
this.dialCode = dialCode;
|
|
67
|
+
this.countries = countries;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.SharedDialCodeError = SharedDialCodeError;
|
|
71
|
+
/**
|
|
72
|
+
* Service for managing global phone number operations
|
|
73
|
+
*/
|
|
74
|
+
class GlobalPhoneNumberService {
|
|
75
|
+
/**
|
|
76
|
+
* Private constructor to enforce singleton pattern
|
|
77
|
+
*/
|
|
78
|
+
constructor() {
|
|
79
|
+
Object.defineProperty(this, "countryMetadata", {
|
|
80
|
+
enumerable: true,
|
|
81
|
+
configurable: true,
|
|
82
|
+
writable: true,
|
|
83
|
+
value: {}
|
|
84
|
+
});
|
|
85
|
+
Object.defineProperty(this, "sharedCountryCodes", {
|
|
86
|
+
enumerable: true,
|
|
87
|
+
configurable: true,
|
|
88
|
+
writable: true,
|
|
89
|
+
value: {}
|
|
90
|
+
});
|
|
91
|
+
Object.defineProperty(this, "initialized", {
|
|
92
|
+
enumerable: true,
|
|
93
|
+
configurable: true,
|
|
94
|
+
writable: true,
|
|
95
|
+
value: false
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Gets the singleton instance of GlobalPhoneNumberService
|
|
100
|
+
* Creates the instance if it doesn't exist
|
|
101
|
+
*
|
|
102
|
+
* @returns {GlobalPhoneNumberService} The singleton instance
|
|
103
|
+
*/
|
|
104
|
+
static getInstance() {
|
|
105
|
+
if (!GlobalPhoneNumberService.instance) {
|
|
106
|
+
GlobalPhoneNumberService.instance = new GlobalPhoneNumberService();
|
|
107
|
+
GlobalPhoneNumberService.instance.initialize();
|
|
108
|
+
}
|
|
109
|
+
return GlobalPhoneNumberService.instance;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Initializes the service with phone patterns data
|
|
113
|
+
* Should be called once when the service is first used
|
|
114
|
+
*/
|
|
115
|
+
initialize() {
|
|
116
|
+
try {
|
|
117
|
+
if (this.initialized)
|
|
118
|
+
return;
|
|
119
|
+
// Parse the phone patterns data
|
|
120
|
+
const data = JSON.parse(JSON.stringify(phone_patterns_js_1.default));
|
|
121
|
+
// Extract shared country codes
|
|
122
|
+
this.sharedCountryCodes = data._shared_country_codes || {};
|
|
123
|
+
const countries = Object.entries(data)
|
|
124
|
+
.filter(([key]) => !key.startsWith("_"))
|
|
125
|
+
.reduce((result, [key, value]) => {
|
|
126
|
+
result[key] = value;
|
|
127
|
+
return result;
|
|
128
|
+
}, {});
|
|
129
|
+
this.countryMetadata = countries;
|
|
130
|
+
this.initialized = true;
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
console.error("Failed to initialize GlobalPhoneNumberService:", error);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Gets the metadata for a specific country
|
|
138
|
+
*
|
|
139
|
+
* @param {string} countryCode - The ISO country code
|
|
140
|
+
* @returns {CountryMetadata | undefined} The country metadata or undefined if not found
|
|
141
|
+
*/
|
|
142
|
+
getCountryMetadata(countryCode) {
|
|
143
|
+
return this.countryMetadata[countryCode];
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Gets all country metadata
|
|
147
|
+
*
|
|
148
|
+
* @returns {CountryMetadataMap} All country metadata
|
|
149
|
+
*/
|
|
150
|
+
getAllCountryMetadata() {
|
|
151
|
+
return this.countryMetadata;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Gets all the countries that share a specific dial code
|
|
155
|
+
*
|
|
156
|
+
* @param {string | number} dialCode - The dial code to check
|
|
157
|
+
* @returns {string[]} Array of ISO country codes that share this dial code
|
|
158
|
+
*/
|
|
159
|
+
getCountriesWithDialCode(dialCode) {
|
|
160
|
+
const dialCodeStr = dialCode.toString();
|
|
161
|
+
return this.sharedCountryCodes[dialCodeStr] || [];
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Checks if a dial code is shared between multiple countries
|
|
165
|
+
*
|
|
166
|
+
* @param {string | number} dialCode - The dial code to check
|
|
167
|
+
* @returns {boolean} True if the dial code is shared, false otherwise
|
|
168
|
+
*/
|
|
169
|
+
isSharedDialCode(dialCode) {
|
|
170
|
+
const dialCodeStr = dialCode.toString();
|
|
171
|
+
const countries = this.sharedCountryCodes[dialCodeStr];
|
|
172
|
+
return countries !== undefined && countries.length > 1;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Finds the country code associated with a given dial code
|
|
176
|
+
*
|
|
177
|
+
* @param dialCode - The dial code to look up
|
|
178
|
+
* @returns The country code or undefined if not found
|
|
179
|
+
*/
|
|
180
|
+
getCountryForDialCode(dialCode) {
|
|
181
|
+
for (const [countryCode, metadata] of Object.entries(this.countryMetadata)) {
|
|
182
|
+
if (metadata.code.toString() === dialCode) {
|
|
183
|
+
return countryCode;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return undefined;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Extracts dial code information from a phone number
|
|
190
|
+
*
|
|
191
|
+
* @param phoneNumber - The phone number in international format (with +)
|
|
192
|
+
* @returns Dial code information or undefined if not found
|
|
193
|
+
*/
|
|
194
|
+
extractDialCode(phoneNumber) {
|
|
195
|
+
const cleaned = this.cleanPhoneNumber(phoneNumber);
|
|
196
|
+
if (!cleaned.startsWith("+"))
|
|
197
|
+
return undefined;
|
|
198
|
+
const numberWithoutPlus = cleaned.substring(1);
|
|
199
|
+
// Try to find the dial code by checking prefixes of increasing length
|
|
200
|
+
for (let i = 3; i >= 1; i--) {
|
|
201
|
+
if (numberWithoutPlus.length <= i)
|
|
202
|
+
continue;
|
|
203
|
+
const dialCode = numberWithoutPlus.substring(0, i);
|
|
204
|
+
// Check if any country has this dial code
|
|
205
|
+
let countryFound = false;
|
|
206
|
+
for (const [_, metadata] of Object.entries(this.countryMetadata)) {
|
|
207
|
+
if (metadata.code.toString() === dialCode) {
|
|
208
|
+
countryFound = true;
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (countryFound) {
|
|
213
|
+
const nationalNumber = numberWithoutPlus.substring(dialCode.length);
|
|
214
|
+
const isShared = this.isSharedDialCode(dialCode);
|
|
215
|
+
const possibleCountries = isShared
|
|
216
|
+
? this.sharedCountryCodes[dialCode]
|
|
217
|
+
: [this.getCountryForDialCode(dialCode) ?? ""];
|
|
218
|
+
return {
|
|
219
|
+
dialCode,
|
|
220
|
+
isShared,
|
|
221
|
+
nationalNumber,
|
|
222
|
+
possibleCountries,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return undefined;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Gets the countries that might be associated with a phone number
|
|
230
|
+
*
|
|
231
|
+
* @param {string} phoneNumber - The phone number in international format
|
|
232
|
+
* @returns {Country[]} Array of possible countries for this phone number
|
|
233
|
+
*/
|
|
234
|
+
getPossibleCountries(phoneNumber) {
|
|
235
|
+
const cleanedNumber = this.cleanPhoneNumber(phoneNumber);
|
|
236
|
+
if (!cleanedNumber.startsWith("+"))
|
|
237
|
+
return [];
|
|
238
|
+
const dialCodeInfo = this.extractDialCode(cleanedNumber);
|
|
239
|
+
if (!dialCodeInfo)
|
|
240
|
+
return [];
|
|
241
|
+
const possibleCountries = [];
|
|
242
|
+
for (const countryCode of dialCodeInfo.possibleCountries) {
|
|
243
|
+
const country = country_js_1.Country.fromCode(countryCode);
|
|
244
|
+
if (country)
|
|
245
|
+
possibleCountries.push(country);
|
|
246
|
+
}
|
|
247
|
+
return possibleCountries;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Determines the most likely type of a phone number
|
|
251
|
+
*
|
|
252
|
+
* @param {string} countryCode - ISO country code
|
|
253
|
+
* @param {string} nationalNumber - The national number to check
|
|
254
|
+
* @returns {PhoneNumberType} The type of the phone number
|
|
255
|
+
*/
|
|
256
|
+
getNumberType(countryCode, nationalNumber) {
|
|
257
|
+
if (!countryCode || !nationalNumber) {
|
|
258
|
+
return PhoneNumberType.UNKNOWN;
|
|
259
|
+
}
|
|
260
|
+
const metadata = this.countryMetadata[countryCode];
|
|
261
|
+
if (!metadata || !metadata.patterns) {
|
|
262
|
+
return PhoneNumberType.UNKNOWN;
|
|
263
|
+
}
|
|
264
|
+
// Verify that required patterns exist
|
|
265
|
+
if (!metadata.patterns.landline || !metadata.patterns.mobile) {
|
|
266
|
+
console.error(`Country ${countryCode} is missing required patterns for landline or mobile`);
|
|
267
|
+
return PhoneNumberType.UNKNOWN;
|
|
268
|
+
}
|
|
269
|
+
// Check each type of pattern
|
|
270
|
+
for (const [type, pattern] of Object.entries(metadata.patterns)) {
|
|
271
|
+
if (!pattern)
|
|
272
|
+
continue;
|
|
273
|
+
try {
|
|
274
|
+
const regex = new RegExp(pattern);
|
|
275
|
+
if (regex.test(nationalNumber)) {
|
|
276
|
+
return type;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
catch (e) {
|
|
280
|
+
console.error(`Invalid regex pattern for ${countryCode}.${type}:`, e);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
return PhoneNumberType.UNKNOWN;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Validates a phone number against country-specific patterns
|
|
287
|
+
*
|
|
288
|
+
* @param {string} countryCode - ISO country code
|
|
289
|
+
* @param {string} nationalNumber - The national number to validate
|
|
290
|
+
* @returns {boolean} True if the phone number is valid, false otherwise
|
|
291
|
+
*/
|
|
292
|
+
validatePattern(countryCode, nationalNumber) {
|
|
293
|
+
if (!countryCode || !nationalNumber) {
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
const metadata = this.countryMetadata[countryCode];
|
|
297
|
+
if (!metadata || !metadata.patterns) {
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
// Verify that required patterns exist
|
|
301
|
+
if (!metadata.patterns.landline || !metadata.patterns.mobile) {
|
|
302
|
+
console.error(`Country ${countryCode} is missing required patterns for landline or mobile`);
|
|
303
|
+
return false;
|
|
304
|
+
}
|
|
305
|
+
// Check if the number matches any of the patterns for this country
|
|
306
|
+
for (const [type, pattern] of Object.entries(metadata.patterns)) {
|
|
307
|
+
if (!pattern)
|
|
308
|
+
continue;
|
|
309
|
+
try {
|
|
310
|
+
const regex = new RegExp(pattern);
|
|
311
|
+
if (regex.test(nationalNumber)) {
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
catch (e) {
|
|
316
|
+
console.error(`Invalid regex pattern for ${countryCode}.${type}:`, e);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Formats a phone number according to the country's formatting rules
|
|
323
|
+
*
|
|
324
|
+
* @param {string} phoneNumber - The phone number to format
|
|
325
|
+
* @param {PhoneNumberFormat} format - The desired format
|
|
326
|
+
* @returns {string} The formatted phone number
|
|
327
|
+
*/
|
|
328
|
+
formatNumber(phoneNumber, format = format_js_1.PhoneNumberFormat.INTERNATIONAL) {
|
|
329
|
+
const phone = phone_number_js_1.PhoneNumber.from(phoneNumber);
|
|
330
|
+
if (!phone)
|
|
331
|
+
return phoneNumber;
|
|
332
|
+
return phone.getWithFormat(format);
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Creates a PhoneNumber from an international format string
|
|
336
|
+
*
|
|
337
|
+
* @param {string} phoneNumber - The phone number in international format
|
|
338
|
+
* @returns {PhoneNumber | undefined} The parsed phone number or undefined if invalid
|
|
339
|
+
*/
|
|
340
|
+
parsePhoneNumber(phoneNumber) {
|
|
341
|
+
return phone_number_js_1.PhoneNumber.from(phoneNumber);
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Creates a PhoneNumber from a phone number with explicit country
|
|
345
|
+
*
|
|
346
|
+
* @param {string} phoneNumber - The phone number in any format
|
|
347
|
+
* @param {Country | string} country - The country or country code
|
|
348
|
+
* @returns {PhoneNumber | undefined} The parsed phone number or undefined if invalid
|
|
349
|
+
*/
|
|
350
|
+
parsePhoneNumberWithCountry(phoneNumber, country) {
|
|
351
|
+
const countryObj = typeof country === "string"
|
|
352
|
+
? country_js_1.Country.fromCode(country)
|
|
353
|
+
: country;
|
|
354
|
+
if (!countryObj)
|
|
355
|
+
return undefined;
|
|
356
|
+
return phone_number_js_1.PhoneNumber.fromWithCountry(phoneNumber, countryObj);
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Extracts the information needed for dialing from one country to another
|
|
360
|
+
*
|
|
361
|
+
* @param {string} fromCountry - ISO code of the country dialing from
|
|
362
|
+
* @param {PhoneNumber} phoneNumber - The phone number to dial
|
|
363
|
+
* @returns {string} The appropriately formatted dialing string
|
|
364
|
+
*/
|
|
365
|
+
getDialingString(fromCountry, phoneNumber) {
|
|
366
|
+
if (!fromCountry || !phoneNumber || !phone_number_js_1.PhoneNumber.is(phoneNumber)) {
|
|
367
|
+
return "";
|
|
368
|
+
}
|
|
369
|
+
// Simple implementation that always uses international format
|
|
370
|
+
// A production version would handle local dialing formats
|
|
371
|
+
return phoneNumber.getWithFormat(format_js_1.PhoneNumberFormat.INTERNATIONAL);
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Cleans a phone number by removing all non-digit characters except the leading plus
|
|
375
|
+
*
|
|
376
|
+
* @param {string} phoneNumber - The phone number to clean
|
|
377
|
+
* @returns {string} The cleaned phone number
|
|
378
|
+
*/
|
|
379
|
+
cleanPhoneNumber(phoneNumber) {
|
|
380
|
+
if (!phoneNumber)
|
|
381
|
+
return "";
|
|
382
|
+
// Preserve the leading +
|
|
383
|
+
const hasPlus = phoneNumber.trim().startsWith("+");
|
|
384
|
+
// Remove all non-digit characters
|
|
385
|
+
const digitsOnly = phoneNumber.replace(/\D/g, "");
|
|
386
|
+
// Add the plus back if it was present
|
|
387
|
+
return hasPlus ? `+${digitsOnly}` : digitsOnly;
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Extracts the country and national number from an international phone number
|
|
391
|
+
*
|
|
392
|
+
* @param {string} phoneNumber - The phone number in international format
|
|
393
|
+
* @returns {[Country | undefined, string]} The country and national number
|
|
394
|
+
*/
|
|
395
|
+
extractParts(phoneNumber) {
|
|
396
|
+
const cleaned = this.cleanPhoneNumber(phoneNumber);
|
|
397
|
+
if (!cleaned.startsWith("+"))
|
|
398
|
+
return [undefined, cleaned];
|
|
399
|
+
const dialCodeInfo = this.extractDialCode(cleaned);
|
|
400
|
+
if (!dialCodeInfo)
|
|
401
|
+
return [undefined, cleaned.substring(1)];
|
|
402
|
+
if (dialCodeInfo.isShared) {
|
|
403
|
+
// For shared dial codes, we return undefined for the country
|
|
404
|
+
return [undefined, dialCodeInfo.nationalNumber];
|
|
405
|
+
}
|
|
406
|
+
else {
|
|
407
|
+
// For non-shared dial codes, we return the country
|
|
408
|
+
const countryCode = dialCodeInfo.possibleCountries[0];
|
|
409
|
+
const country = countryCode ? country_js_1.Country.fromCode(countryCode) : undefined;
|
|
410
|
+
return [country, dialCodeInfo.nationalNumber];
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Gets a list of commonly used examples of valid phone numbers for a country
|
|
415
|
+
*
|
|
416
|
+
* @param {string} countryCode - The ISO country code
|
|
417
|
+
* @returns {string[]} Array of example phone numbers
|
|
418
|
+
*/
|
|
419
|
+
getExampleNumbers(countryCode) {
|
|
420
|
+
const metadata = this.countryMetadata[countryCode];
|
|
421
|
+
if (!metadata)
|
|
422
|
+
return [];
|
|
423
|
+
const dialCode = metadata.code;
|
|
424
|
+
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)}`);
|
|
430
|
+
return examples;
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Generates an example phone number that would match a given pattern
|
|
434
|
+
* This is a simple implementation that handles basic patterns
|
|
435
|
+
*
|
|
436
|
+
* @param pattern - The regex pattern
|
|
437
|
+
* @returns A string that would match the pattern
|
|
438
|
+
*/
|
|
439
|
+
generateExampleFromPattern(pattern) {
|
|
440
|
+
// Very basic implementation - a real one would be more sophisticated
|
|
441
|
+
// but this handles simple patterns like "^[2-8]\\d{7}$"
|
|
442
|
+
// Find basic digit patterns like \d{7}
|
|
443
|
+
const digitMatch = pattern.match(/\\d\{(\d+)\}/);
|
|
444
|
+
const digitCount = digitMatch ? parseInt(digitMatch[1]) : 8;
|
|
445
|
+
// Find range patterns like [2-8]
|
|
446
|
+
const rangeMatch = pattern.match(/\[(\d)-(\d)\]/);
|
|
447
|
+
const firstDigit = rangeMatch ? rangeMatch[1] : "5";
|
|
448
|
+
// Combine them
|
|
449
|
+
return `${firstDigit}${"0".repeat(digitCount - 1)}`;
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Checks if a phone number is valid for a specific country
|
|
453
|
+
*
|
|
454
|
+
* @param {string} phoneNumber - The phone number to validate
|
|
455
|
+
* @param {string} countryCode - The ISO country code
|
|
456
|
+
* @returns {boolean} True if the phone number is valid for the country, false otherwise
|
|
457
|
+
*/
|
|
458
|
+
isValidForCountry(phoneNumber, countryCode) {
|
|
459
|
+
const country = country_js_1.Country.fromCode(countryCode);
|
|
460
|
+
if (!country)
|
|
461
|
+
return false;
|
|
462
|
+
const phone = phone_number_js_1.PhoneNumber.fromWithCountry(phoneNumber, country);
|
|
463
|
+
return phone !== undefined;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Checks if a given dial code exists in our database
|
|
467
|
+
*
|
|
468
|
+
* @param dialCode - The dial code to check
|
|
469
|
+
* @returns True if the dial code exists, false otherwise
|
|
470
|
+
*/
|
|
471
|
+
hasDialCode(dialCode) {
|
|
472
|
+
// Check if it's a shared dial code
|
|
473
|
+
if (this.sharedCountryCodes[dialCode]?.length > 0) {
|
|
474
|
+
return true;
|
|
475
|
+
}
|
|
476
|
+
// Check if any country has this dial code
|
|
477
|
+
for (const metadata of Object.values(this.countryMetadata)) {
|
|
478
|
+
if (metadata.code.toString() === dialCode) {
|
|
479
|
+
return true;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
return false;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
exports.GlobalPhoneNumberService = GlobalPhoneNumberService;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.test.d.ts","sourceRoot":"","sources":["../../../../../src/src/models/phone_number/global/service.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export * from "./antd_validator.js";
|
|
2
|
-
export * from "./phone_number.js";
|
|
3
|
-
export * from "./network_operator.js";
|
|
1
|
+
export * from "./tz/antd_validator.js";
|
|
2
|
+
export * from "./tz/phone_number.js";
|
|
3
|
+
export * from "./tz/network_operator.js";
|
|
4
|
+
export * from "./global/phone_number.js";
|
|
5
|
+
export * from "./format.js";
|
|
4
6
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/src/models/phone_number/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/src/models/phone_number/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,0BAA0B,CAAC;AACzC,cAAc,0BAA0B,CAAC;AACzC,cAAc,aAAa,CAAA"}
|
|
@@ -14,6 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./antd_validator.js"), exports);
|
|
18
|
-
__exportStar(require("./phone_number.js"), exports);
|
|
19
|
-
__exportStar(require("./network_operator.js"), exports);
|
|
17
|
+
__exportStar(require("./tz/antd_validator.js"), exports);
|
|
18
|
+
__exportStar(require("./tz/phone_number.js"), exports);
|
|
19
|
+
__exportStar(require("./tz/network_operator.js"), exports);
|
|
20
|
+
__exportStar(require("./global/phone_number.js"), exports);
|
|
21
|
+
__exportStar(require("./format.js"), exports);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { RuleObject } from "antd/es/form";
|
|
2
|
-
import {
|
|
2
|
+
import { TZPhoneNumber } from "./phone_number.js";
|
|
3
3
|
/**
|
|
4
4
|
* Validates a Tanzanian phone number according to specified format rules.
|
|
5
5
|
* This validator is designed for Ant Design Form components and performs the following validations:
|
|
@@ -73,5 +73,5 @@ import { PhoneNumber } from "./phone_number.js";
|
|
|
73
73
|
* $ -> End of string
|
|
74
74
|
*/
|
|
75
75
|
export declare const TZ_PHONE_NUMBER_REGEX: RegExp;
|
|
76
|
-
export declare const PHONENUMBER_VALIDATOR: (rule: RuleObject, value: string | null | undefined) => Promise<
|
|
76
|
+
export declare const PHONENUMBER_VALIDATOR: (rule: RuleObject, value: string | null | undefined) => Promise<TZPhoneNumber | undefined>;
|
|
77
77
|
//# sourceMappingURL=antd_validator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"antd_validator.d.ts","sourceRoot":"","sources":["../../../../../src/src/models/phone_number/tz/antd_validator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuEG;AACH,eAAO,MAAM,qBAAqB,QACkF,CAAC;AAErH,eAAO,MAAM,qBAAqB,SAC1B,UAAU,SACT,MAAM,GAAG,IAAI,GAAG,SAAS,KAC/B,OAAO,CAAC,aAAa,GAAG,SAAS,CAiBnC,CAAC"}
|
|
@@ -86,7 +86,7 @@ const PHONENUMBER_VALIDATOR = (rule, value) => {
|
|
|
86
86
|
// If field is not required and empty, validation passes
|
|
87
87
|
return Promise.resolve(undefined);
|
|
88
88
|
}
|
|
89
|
-
const phone = phone_number_js_1.
|
|
89
|
+
const phone = phone_number_js_1.TZPhoneNumber.from(phoneString);
|
|
90
90
|
if (phone)
|
|
91
91
|
return Promise.resolve(phone);
|
|
92
92
|
return Promise.reject(new Error("Invalid phone number format."));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"antdvalidator.test.d.ts","sourceRoot":"","sources":["../../../../../src/src/models/phone_number/tz/antdvalidator.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"network_operator.d.ts","sourceRoot":"","sources":["../../../../../src/src/models/phone_number/tz/network_operator.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,oBAAY,eAAe;IACzB,OAAO,YAAY;IACnB,MAAM,WAAW;IACjB,IAAI,SAAS;IACb,OAAO,YAAY;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,iDAAiD;IACjD,EAAE,EAAE,eAAe,CAAC;IAEpB,+DAA+D;IAC/D,oBAAoB,EAAE,MAAM,EAAE,CAAC;IAE/B,2CAA2C;IAC3C,WAAW,EAAE,MAAM,CAAC;IAEpB,wDAAwD;IACxD,kBAAkB,EAAE,MAAM,CAAC;IAE3B,+CAA+C;IAC/C,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,eAAO,MAAM,uBAAuB,EAAE,MAAM,CAC1C,eAAe,EACf,mBAAmB,CA8BpB,CAAC"}
|