UNPKG

3.29 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * Regular expression matching characters to ignore when calculating the initials.
5 */
6/**
7 * Regular expression matching characters within various types of enclosures, including the enclosures themselves
8 * so for example, (xyz) [xyz] {xyz} all would be ignored
9 */
10var UNWANTED_ENCLOSURES_REGEX = /[\(\[\{][^\)\]\}]*[\)\]\}]/g;
11/**
12 * Regular expression matching special ASCII characters except space, plus some unicode special characters.
13 * Applies after unwanted enclosures have been removed
14 */
15var UNWANTED_CHARS_REGEX = /[\0-\u001F\!-/:-@\[-`\{-\u00BF\u0250-\u036F\uD800-\uFFFF]/g;
16/**
17 * Regular expression matching phone numbers. Applied after chars matching UNWANTED_CHARS_REGEX have been removed
18 * and number has been trimmed for whitespaces
19 */
20var PHONENUMBER_REGEX = /^\d+[\d\s]*(:?ext|x|)\s*\d+$/i;
21/** Regular expression matching one or more spaces. */
22var MULTIPLE_WHITESPACES_REGEX = /\s+/g;
23/**
24 * Regular expression matching languages for which we currently don't support initials.
25 * Arabic: Arabic, Arabic Supplement, Arabic Extended-A.
26 * Korean: Hangul Jamo, Hangul Compatibility Jamo, Hangul Jamo Extended-A, Hangul Syllables, Hangul Jamo Extended-B.
27 * Japanese: Hiragana, Katakana.
28 * CJK: CJK Unified Ideographs Extension A, CJK Unified Ideographs, CJK Compatibility Ideographs,
29 * CJK Unified Ideographs Extension B
30 */
31// eslint-disable-next-line @fluentui/max-len
32var UNSUPPORTED_TEXT_REGEX = /[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\u1100-\u11FF\u3130-\u318F\uA960-\uA97F\uAC00-\uD7AF\uD7B0-\uD7FF\u3040-\u309F\u30A0-\u30FF\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD869][\uDC00-\uDED6]/;
33function getInitialsLatin(displayName, isRtl) {
34 var initials = '';
35 var splits = displayName.split(' ');
36 if (splits.length === 2) {
37 initials += splits[0].charAt(0).toUpperCase();
38 initials += splits[1].charAt(0).toUpperCase();
39 }
40 else if (splits.length === 3) {
41 initials += splits[0].charAt(0).toUpperCase();
42 initials += splits[2].charAt(0).toUpperCase();
43 }
44 else if (splits.length !== 0) {
45 initials += splits[0].charAt(0).toUpperCase();
46 }
47 if (isRtl && initials.length > 1) {
48 return initials.charAt(1) + initials.charAt(0);
49 }
50 return initials;
51}
52function cleanupDisplayName(displayName) {
53 displayName = displayName.replace(UNWANTED_ENCLOSURES_REGEX, '');
54 displayName = displayName.replace(UNWANTED_CHARS_REGEX, '');
55 displayName = displayName.replace(MULTIPLE_WHITESPACES_REGEX, ' ');
56 displayName = displayName.trim();
57 return displayName;
58}
59/**
60 * Get (up to 2 characters) initials based on display name of the persona.
61 *
62 * @public
63 */
64function getInitials(displayName, isRtl, allowPhoneInitials) {
65 if (!displayName) {
66 return '';
67 }
68 displayName = cleanupDisplayName(displayName);
69 // For names containing CJK characters, and phone numbers, we don't display initials
70 if (UNSUPPORTED_TEXT_REGEX.test(displayName) || (!allowPhoneInitials && PHONENUMBER_REGEX.test(displayName))) {
71 return '';
72 }
73 return getInitialsLatin(displayName, isRtl);
74}
75exports.getInitials = getInitials;
76//# sourceMappingURL=initials.js.map
\No newline at end of file