@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.
@@ -2,7 +2,7 @@ import { type ClassValue } from "clsx";
2
2
  export { createEnvConfig } from "./env-config";
3
3
  export { SupportedCountry, getRegionsByCountry, getRegionOptions, } from "./locations";
4
4
  export type {} from "./locations";
5
- export { COUNTRY_CODES, extractCountryCodeFromPhoneNumber, formatPhoneNumberWithCountryCode, getDefaultCountryCode, } from "./phone";
5
+ export { COUNTRY_CODES, extractCountryCodeFromPhoneNumber, formatPhoneNumberForDisplay, formatPhoneNumberWithCountryCode, getDefaultCountryCode, } from "./phone";
6
6
  export type { CountryCodeOption, CountryCodeSelectProps } from "./phone";
7
7
  export type { EnvConfig, EnvConfigOverrides, Environment, } from "./env-config";
8
8
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,UAAU,EAAE,MAAM,MAAM,CAAC;AAE7C,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EACL,gBAAgB,EAChB,mBAAmB,EACnB,gBAAgB,GACjB,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,EACL,aAAa,EACb,iCAAiC,EACjC,gCAAgC,EAChC,qBAAqB,GACtB,MAAM,SAAS,CAAC;AACjB,YAAY,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAC;AACzE,YAAY,EACV,SAAS,EACT,kBAAkB,EAClB,WAAW,GACZ,MAAM,cAAc,CAAC;AAEtB;;GAEG;AACH,eAAO,MAAM,QAAQ,QAAQ,CAAC;AAE9B;;;;GAIG;AACH,wBAAgB,EAAE,CAAC,GAAG,MAAM,EAAE,UAAU,EAAE,UAEzC;AAED;;;;;GAKG;AACH,wBAAgB,cAAc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AG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,IAAI,CAMlC;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,MAAM,SAAQ,GAAG,MAAM,CAEjD;AAED;;;;GAIG;AACH,eAAO,MAAM,aAAa,GAAI,OAAO,MAAM,KAAG,MAE7C,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAMzD;AAED;;GAEG;AACH,oBAAY,SAAS;IACnB,UAAU,OAAO;IACjB,UAAU,OAAO;IACjB,UAAU,OAAO;IACjB,UAAU,OAAO;IACjB,WAAW,QAAQ;IACnB,WAAW,QAAQ;IACnB,UAAU,OAAO;IACjB,UAAU,OAAO;IACjB,OAAO,YAAY;CACpB;AAED;;;GAGG;AACH,eAAO,MAAM,WAAW,EAAE,SAAS,EAUlC,CAAC"}
@@ -51,4 +51,18 @@ export declare function extractCountryCodeFromPhoneNumber(phoneNumber: string, c
51
51
  * // don't re-prepend the dial code)
52
52
  */
53
53
  export declare function formatPhoneNumberWithCountryCode(phoneNumber: string, countryCode?: string): string;
54
+ /**
55
+ * Formats a phone number for display only: the country code, then the
56
+ * national digits in their usual groups. Do not use it for values sent to the
57
+ * API, form values or tel: links; formatPhoneNumberWithCountryCode gives the
58
+ * unspaced value for those. A number from another country, or of an
59
+ * unexpected length, is returned as formatPhoneNumberWithCountryCode returns it.
60
+ *
61
+ * @example
62
+ * formatPhoneNumberForDisplay("221771234567") // "+221 77 123 45 67" (backend's storage format)
63
+ * formatPhoneNumberForDisplay("771234567") // "+221 77 123 45 67"
64
+ * formatPhoneNumberForDisplay("+23566123456") // "+235 66 12 34 56"
65
+ * formatPhoneNumberForDisplay("+22177123") // "+22177123" (unexpected length, unchanged)
66
+ */
67
+ export declare function formatPhoneNumberForDisplay(phoneNumber: string, countryCode?: string): string;
54
68
  //# sourceMappingURL=phone.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"phone.d.ts","sourceRoot":"","sources":["../../src/utils/phone.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC,OAAO,EAAE,iBAAiB,EAAE,CAAC;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;CAClC;AAED;;GAEG;AACH,eAAO,MAAM,aAAa,EAAE,iBAAiB,EAG5C,CAAC;AAEF;;;GAGG;AACH,wBAAgB,qBAAqB,CACnC,WAAW,CAAC,EAAE,MAAM,EACpB,OAAO,CAAC,EAAE,MAAM,GACf,MAAM,CAgBR;AAED;;;;;;;GAOG;AACH,wBAAgB,iCAAiC,CAC/C,WAAW,EAAE,MAAM,EACnB,OAAO,CAAC,EAAE,MAAM,GACf;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,kBAAkB,EAAE,MAAM,CAAA;CAAE,CA+BrD;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,gCAAgC,CAC9C,WAAW,EAAE,MAAM,EACnB,WAAW,GAAE,MAAe,GAC3B,MAAM,CA2BR"}
1
+ {"version":3,"file":"phone.d.ts","sourceRoot":"","sources":["../../src/utils/phone.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC,OAAO,EAAE,iBAAiB,EAAE,CAAC;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;CAClC;AAED;;GAEG;AACH,eAAO,MAAM,aAAa,EAAE,iBAAiB,EAG5C,CAAC;AAEF;;;GAGG;AACH,wBAAgB,qBAAqB,CACnC,WAAW,CAAC,EAAE,MAAM,EACpB,OAAO,CAAC,EAAE,MAAM,GACf,MAAM,CAgBR;AAED;;;;;;;GAOG;AACH,wBAAgB,iCAAiC,CAC/C,WAAW,EAAE,MAAM,EACnB,OAAO,CAAC,EAAE,MAAM,GACf;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,kBAAkB,EAAE,MAAM,CAAA;CAAE,CA+BrD;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,gCAAgC,CAC9C,WAAW,EAAE,MAAM,EACnB,WAAW,GAAE,MAAe,GAC3B,MAAM,CA2BR;AAYD;;;;;;;;;;;;GAYG;AACH,wBAAgB,2BAA2B,CACzC,WAAW,EAAE,MAAM,EACnB,WAAW,GAAE,MAAe,GAC3B,MAAM,CAiBR"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ews-admin/global-design-system",
3
- "version": "1.25.0",
3
+ "version": "1.26.0",
4
4
  "description": "EWS Global Design System - Reusable components for EWS applications",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",
@@ -50,6 +50,7 @@
50
50
  "eslint": "^9.36.0",
51
51
  "eslint-plugin-react": "^7.37.5",
52
52
  "eslint-plugin-react-hooks": "^5.2.0",
53
+ "jest": "29",
53
54
  "postcss": "^8.5.6",
54
55
  "rollup": "^4.6.1",
55
56
  "rollup-plugin-dts": "^6.1.0",
@@ -57,6 +58,7 @@
57
58
  "rollup-plugin-postcss": "^4.0.2",
58
59
  "storybook": "^7.5.0",
59
60
  "tailwindcss": "^3.4.0",
61
+ "ts-jest": "29",
60
62
  "typescript": "^5.2.2",
61
63
  "vite": "^4.5.0"
62
64
  },
package/src/index.ts CHANGED
@@ -73,6 +73,7 @@ export {
73
73
  formatDate,
74
74
  type FormatDateOptions,
75
75
  formatNumeric,
76
+ formatPhoneNumberForDisplay,
76
77
  formatPhoneNumberWithCountryCode,
77
78
  generateId,
78
79
  getDefaultCountryCode,
@@ -10,6 +10,7 @@ export type {} from "./locations";
10
10
  export {
11
11
  COUNTRY_CODES,
12
12
  extractCountryCodeFromPhoneNumber,
13
+ formatPhoneNumberForDisplay,
13
14
  formatPhoneNumberWithCountryCode,
14
15
  getDefaultCountryCode,
15
16
  } from "./phone";
@@ -139,3 +139,48 @@ export function formatPhoneNumberWithCountryCode(
139
139
 
140
140
  return `${countryCode}${cleaned}`;
141
141
  }
142
+
143
+ /**
144
+ * National digit groups used to display a phone number, per country code:
145
+ * Senegal numbers have 9 national digits (77 123 45 67, 33 823 45 67), Chad
146
+ * numbers 8 (66 12 34 56).
147
+ */
148
+ const DISPLAY_DIGIT_GROUPS: Record<string, number[]> = {
149
+ "+221": [2, 3, 2, 2],
150
+ "+235": [2, 2, 2, 2],
151
+ };
152
+
153
+ /**
154
+ * Formats a phone number for display only: the country code, then the
155
+ * national digits in their usual groups. Do not use it for values sent to the
156
+ * API, form values or tel: links; formatPhoneNumberWithCountryCode gives the
157
+ * unspaced value for those. A number from another country, or of an
158
+ * unexpected length, is returned as formatPhoneNumberWithCountryCode returns it.
159
+ *
160
+ * @example
161
+ * formatPhoneNumberForDisplay("221771234567") // "+221 77 123 45 67" (backend's storage format)
162
+ * formatPhoneNumberForDisplay("771234567") // "+221 77 123 45 67"
163
+ * formatPhoneNumberForDisplay("+23566123456") // "+235 66 12 34 56"
164
+ * formatPhoneNumberForDisplay("+22177123") // "+22177123" (unexpected length, unchanged)
165
+ */
166
+ export function formatPhoneNumberForDisplay(
167
+ phoneNumber: string,
168
+ countryCode: string = "+221"
169
+ ): string {
170
+ const canonical = formatPhoneNumberWithCountryCode(phoneNumber, countryCode);
171
+ const code = COUNTRY_CODES.find(({ code: c }) => canonical.startsWith(c))?.code;
172
+ const groups = code ? DISPLAY_DIGIT_GROUPS[code] : undefined;
173
+ if (!code || !groups) return canonical;
174
+
175
+ const national = canonical.slice(code.length).replace(/[\s-]/g, "");
176
+ const expectedLength = groups.reduce((sum, size) => sum + size, 0);
177
+ if (!/^\d+$/.test(national) || national.length !== expectedLength) return canonical;
178
+
179
+ const parts: string[] = [];
180
+ let start = 0;
181
+ for (const size of groups) {
182
+ parts.push(national.slice(start, start + size));
183
+ start += size;
184
+ }
185
+ return `${code} ${parts.join(" ")}`;
186
+ }