@ews-admin/global-design-system 1.25.0 → 1.26.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.js CHANGED
@@ -226,6 +226,46 @@ function formatPhoneNumberWithCountryCode(phoneNumber, countryCode = "+221") {
226
226
  return "";
227
227
  return `${countryCode}${cleaned}`;
228
228
  }
229
+ /**
230
+ * National digit groups used to display a phone number, per country code:
231
+ * Senegal numbers have 9 national digits (77 123 45 67, 33 823 45 67), Chad
232
+ * numbers 8 (66 12 34 56).
233
+ */
234
+ const DISPLAY_DIGIT_GROUPS = {
235
+ "+221": [2, 3, 2, 2],
236
+ "+235": [2, 2, 2, 2],
237
+ };
238
+ /**
239
+ * Formats a phone number for display only: the country code, then the
240
+ * national digits in their usual groups. Do not use it for values sent to the
241
+ * API, form values or tel: links; formatPhoneNumberWithCountryCode gives the
242
+ * unspaced value for those. A number from another country, or of an
243
+ * unexpected length, is returned as formatPhoneNumberWithCountryCode returns it.
244
+ *
245
+ * @example
246
+ * formatPhoneNumberForDisplay("221771234567") // "+221 77 123 45 67" (backend's storage format)
247
+ * formatPhoneNumberForDisplay("771234567") // "+221 77 123 45 67"
248
+ * formatPhoneNumberForDisplay("+23566123456") // "+235 66 12 34 56"
249
+ * formatPhoneNumberForDisplay("+22177123") // "+22177123" (unexpected length, unchanged)
250
+ */
251
+ function formatPhoneNumberForDisplay(phoneNumber, countryCode = "+221") {
252
+ const canonical = formatPhoneNumberWithCountryCode(phoneNumber, countryCode);
253
+ const code = COUNTRY_CODES.find(({ code: c }) => canonical.startsWith(c))?.code;
254
+ const groups = code ? DISPLAY_DIGIT_GROUPS[code] : undefined;
255
+ if (!code || !groups)
256
+ return canonical;
257
+ const national = canonical.slice(code.length).replace(/[\s-]/g, "");
258
+ const expectedLength = groups.reduce((sum, size) => sum + size, 0);
259
+ if (!/^\d+$/.test(national) || national.length !== expectedLength)
260
+ return canonical;
261
+ const parts = [];
262
+ let start = 0;
263
+ for (const size of groups) {
264
+ parts.push(national.slice(start, start + size));
265
+ start += size;
266
+ }
267
+ return `${code} ${parts.join(" ")}`;
268
+ }
229
269
 
230
270
  /**
231
271
  * Default currency for price formatting
@@ -40865,6 +40905,7 @@ exports.extractCountryCodeFromPhoneNumber = extractCountryCodeFromPhoneNumber;
40865
40905
  exports.formatCurrency = formatCurrency;
40866
40906
  exports.formatDate = formatDate;
40867
40907
  exports.formatNumeric = formatNumeric;
40908
+ exports.formatPhoneNumberForDisplay = formatPhoneNumberForDisplay;
40868
40909
  exports.formatPhoneNumberWithCountryCode = formatPhoneNumberWithCountryCode;
40869
40910
  exports.generateId = generateId;
40870
40911
  exports.getDefaultCountryCode = getDefaultCountryCode;