@ews-admin/global-design-system 1.23.0 → 1.25.0
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/dist/index.d.ts +10 -4
- package/dist/index.esm.js +29 -4
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +29 -4
- package/dist/index.js.map +1 -1
- package/dist/utils/phone.d.ts +10 -4
- package/dist/utils/phone.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/utils/phone.ts +36 -4
package/dist/index.js
CHANGED
|
@@ -172,6 +172,17 @@ function extractCountryCodeFromPhoneNumber(phoneNumber, country) {
|
|
|
172
172
|
.trim();
|
|
173
173
|
return { countryCode: matched.code, cleanedPhoneNumber: cleaned };
|
|
174
174
|
}
|
|
175
|
+
// Same dial code, but without the leading "+" — e.g. the backend's canonical
|
|
176
|
+
// digits-only storage format ("221770000010" instead of "+221770000010").
|
|
177
|
+
// Without this check the caller below would treat the whole string (dial
|
|
178
|
+
// code digits included) as the local part.
|
|
179
|
+
const bareMatch = COUNTRY_CODES.find(({ code }) => phoneNumber.startsWith(code.slice(1)));
|
|
180
|
+
if (bareMatch) {
|
|
181
|
+
const cleaned = phoneNumber
|
|
182
|
+
.replace(new RegExp(`^${bareMatch.code.slice(1)}\\s*`), "")
|
|
183
|
+
.trim();
|
|
184
|
+
return { countryCode: bareMatch.code, cleanedPhoneNumber: cleaned };
|
|
185
|
+
}
|
|
175
186
|
return {
|
|
176
187
|
countryCode: getDefaultCountryCode(phoneNumber, country),
|
|
177
188
|
cleanedPhoneNumber: phoneNumber.trim(),
|
|
@@ -179,13 +190,19 @@ function extractCountryCodeFromPhoneNumber(phoneNumber, country) {
|
|
|
179
190
|
}
|
|
180
191
|
/**
|
|
181
192
|
* Formats a phone number by prepending the country code if not already present.
|
|
182
|
-
* If the number already starts with a known country code
|
|
183
|
-
*
|
|
193
|
+
* If the number already starts with a known country code (with or without the
|
|
194
|
+
* leading "+") it is returned unchanged except for the "+" being added when
|
|
195
|
+
* missing. Leading zeros, spaces and dashes are stripped from the local part
|
|
196
|
+
* before formatting.
|
|
184
197
|
*
|
|
185
198
|
* @example
|
|
186
|
-
* formatPhoneNumberWithCountryCode("77123456", "+221")
|
|
199
|
+
* formatPhoneNumberWithCountryCode("77123456", "+221") // "+22177123456"
|
|
187
200
|
* formatPhoneNumberWithCountryCode("+22177123456", "+221") // "+22177123456" (unchanged)
|
|
188
|
-
* formatPhoneNumberWithCountryCode("077123456", "+221")
|
|
201
|
+
* formatPhoneNumberWithCountryCode("077123456", "+221") // "+22177123456" (leading 0 stripped)
|
|
202
|
+
* formatPhoneNumberWithCountryCode("22177123456") // "+22177123456" (backend's
|
|
203
|
+
* // canonical digits-only
|
|
204
|
+
* // storage format — just add "+",
|
|
205
|
+
* // don't re-prepend the dial code)
|
|
189
206
|
*/
|
|
190
207
|
function formatPhoneNumberWithCountryCode(phoneNumber, countryCode = "+221") {
|
|
191
208
|
const trimmed = phoneNumber?.trim() || "";
|
|
@@ -195,6 +212,14 @@ function formatPhoneNumberWithCountryCode(phoneNumber, countryCode = "+221") {
|
|
|
195
212
|
if (COUNTRY_CODES.some(({ code }) => trimmed.startsWith(code))) {
|
|
196
213
|
return trimmed;
|
|
197
214
|
}
|
|
215
|
+
// Same dial code, but without the leading "+" (e.g. "221770000010", the
|
|
216
|
+
// backend's canonical digits-only storage format). Just add the "+" —
|
|
217
|
+
// re-prepending countryCode here would double the dial-code digits
|
|
218
|
+
// ("+221221770000010").
|
|
219
|
+
const bareMatch = COUNTRY_CODES.find(({ code }) => trimmed.startsWith(code.slice(1)));
|
|
220
|
+
if (bareMatch) {
|
|
221
|
+
return `+${trimmed}`;
|
|
222
|
+
}
|
|
198
223
|
// Strip leading zeros, spaces, dashes and + signs from the local part
|
|
199
224
|
const cleaned = trimmed.replace(/^[\s+0-]*/, "").trim();
|
|
200
225
|
if (!cleaned)
|