@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 CHANGED
@@ -695,13 +695,19 @@ declare function extractCountryCodeFromPhoneNumber(phoneNumber: string, country?
695
695
  };
696
696
  /**
697
697
  * Formats a phone number by prepending the country code if not already present.
698
- * If the number already starts with a known country code it is returned as-is.
699
- * Leading zeros, spaces and dashes are stripped from the local part before formatting.
698
+ * If the number already starts with a known country code (with or without the
699
+ * leading "+") it is returned unchanged except for the "+" being added when
700
+ * missing. Leading zeros, spaces and dashes are stripped from the local part
701
+ * before formatting.
700
702
  *
701
703
  * @example
702
- * formatPhoneNumberWithCountryCode("77123456", "+221") // "+22177123456"
704
+ * formatPhoneNumberWithCountryCode("77123456", "+221") // "+22177123456"
703
705
  * formatPhoneNumberWithCountryCode("+22177123456", "+221") // "+22177123456" (unchanged)
704
- * formatPhoneNumberWithCountryCode("077123456", "+221") // "+22177123456" (leading 0 stripped)
706
+ * formatPhoneNumberWithCountryCode("077123456", "+221") // "+22177123456" (leading 0 stripped)
707
+ * formatPhoneNumberWithCountryCode("22177123456") // "+22177123456" (backend's
708
+ * // canonical digits-only
709
+ * // storage format — just add "+",
710
+ * // don't re-prepend the dial code)
705
711
  */
706
712
  declare function formatPhoneNumberWithCountryCode(phoneNumber: string, countryCode?: string): string;
707
713
 
package/dist/index.esm.js CHANGED
@@ -152,6 +152,17 @@ function extractCountryCodeFromPhoneNumber(phoneNumber, country) {
152
152
  .trim();
153
153
  return { countryCode: matched.code, cleanedPhoneNumber: cleaned };
154
154
  }
155
+ // Same dial code, but without the leading "+" — e.g. the backend's canonical
156
+ // digits-only storage format ("221770000010" instead of "+221770000010").
157
+ // Without this check the caller below would treat the whole string (dial
158
+ // code digits included) as the local part.
159
+ const bareMatch = COUNTRY_CODES.find(({ code }) => phoneNumber.startsWith(code.slice(1)));
160
+ if (bareMatch) {
161
+ const cleaned = phoneNumber
162
+ .replace(new RegExp(`^${bareMatch.code.slice(1)}\\s*`), "")
163
+ .trim();
164
+ return { countryCode: bareMatch.code, cleanedPhoneNumber: cleaned };
165
+ }
155
166
  return {
156
167
  countryCode: getDefaultCountryCode(phoneNumber, country),
157
168
  cleanedPhoneNumber: phoneNumber.trim(),
@@ -159,13 +170,19 @@ function extractCountryCodeFromPhoneNumber(phoneNumber, country) {
159
170
  }
160
171
  /**
161
172
  * Formats a phone number by prepending the country code if not already present.
162
- * If the number already starts with a known country code it is returned as-is.
163
- * Leading zeros, spaces and dashes are stripped from the local part before formatting.
173
+ * If the number already starts with a known country code (with or without the
174
+ * leading "+") it is returned unchanged except for the "+" being added when
175
+ * missing. Leading zeros, spaces and dashes are stripped from the local part
176
+ * before formatting.
164
177
  *
165
178
  * @example
166
- * formatPhoneNumberWithCountryCode("77123456", "+221") // "+22177123456"
179
+ * formatPhoneNumberWithCountryCode("77123456", "+221") // "+22177123456"
167
180
  * formatPhoneNumberWithCountryCode("+22177123456", "+221") // "+22177123456" (unchanged)
168
- * formatPhoneNumberWithCountryCode("077123456", "+221") // "+22177123456" (leading 0 stripped)
181
+ * formatPhoneNumberWithCountryCode("077123456", "+221") // "+22177123456" (leading 0 stripped)
182
+ * formatPhoneNumberWithCountryCode("22177123456") // "+22177123456" (backend's
183
+ * // canonical digits-only
184
+ * // storage format — just add "+",
185
+ * // don't re-prepend the dial code)
169
186
  */
170
187
  function formatPhoneNumberWithCountryCode(phoneNumber, countryCode = "+221") {
171
188
  const trimmed = phoneNumber?.trim() || "";
@@ -175,6 +192,14 @@ function formatPhoneNumberWithCountryCode(phoneNumber, countryCode = "+221") {
175
192
  if (COUNTRY_CODES.some(({ code }) => trimmed.startsWith(code))) {
176
193
  return trimmed;
177
194
  }
195
+ // Same dial code, but without the leading "+" (e.g. "221770000010", the
196
+ // backend's canonical digits-only storage format). Just add the "+" —
197
+ // re-prepending countryCode here would double the dial-code digits
198
+ // ("+221221770000010").
199
+ const bareMatch = COUNTRY_CODES.find(({ code }) => trimmed.startsWith(code.slice(1)));
200
+ if (bareMatch) {
201
+ return `+${trimmed}`;
202
+ }
178
203
  // Strip leading zeros, spaces, dashes and + signs from the local part
179
204
  const cleaned = trimmed.replace(/^[\s+0-]*/, "").trim();
180
205
  if (!cleaned)