UNPKG

@uifabric/utilities

Version:

Fluent UI React utilities for building components.

76 lines 3.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Regular expression matching characters to ignore when calculating the initials. */ /** * Regular expression matching characters within various types of enclosures, including the enclosures themselves * so for example, (xyz) [xyz] {xyz} all would be ignored */ var UNWANTED_ENCLOSURES_REGEX = /[\(\[\{][^\)\]\}]*[\)\]\}]/g; /** * Regular expression matching special ASCII characters except space, plus some unicode special characters. * Applies after unwanted enclosures have been removed */ var UNWANTED_CHARS_REGEX = /[\0-\u001F\!-/:-@\[-`\{-\u00BF\u0250-\u036F\uD800-\uFFFF]/g; /** * Regular expression matching phone numbers. Applied after chars matching UNWANTED_CHARS_REGEX have been removed * and number has been trimmed for whitespaces */ var PHONENUMBER_REGEX = /^\d+[\d\s]*(:?ext|x|)\s*\d+$/i; /** Regular expression matching one or more spaces. */ var MULTIPLE_WHITESPACES_REGEX = /\s+/g; /** * Regular expression matching languages for which we currently don't support initials. * Arabic: Arabic, Arabic Supplement, Arabic Extended-A. * Korean: Hangul Jamo, Hangul Compatibility Jamo, Hangul Jamo Extended-A, Hangul Syllables, Hangul Jamo Extended-B. * Japanese: Hiragana, Katakana. * CJK: CJK Unified Ideographs Extension A, CJK Unified Ideographs, CJK Compatibility Ideographs, * CJK Unified Ideographs Extension B */ // eslint-disable-next-line @fluentui/max-len var 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]/; function getInitialsLatin(displayName, isRtl) { var initials = ''; var splits = displayName.split(' '); if (splits.length === 2) { initials += splits[0].charAt(0).toUpperCase(); initials += splits[1].charAt(0).toUpperCase(); } else if (splits.length === 3) { initials += splits[0].charAt(0).toUpperCase(); initials += splits[2].charAt(0).toUpperCase(); } else if (splits.length !== 0) { initials += splits[0].charAt(0).toUpperCase(); } if (isRtl && initials.length > 1) { return initials.charAt(1) + initials.charAt(0); } return initials; } function cleanupDisplayName(displayName) { displayName = displayName.replace(UNWANTED_ENCLOSURES_REGEX, ''); displayName = displayName.replace(UNWANTED_CHARS_REGEX, ''); displayName = displayName.replace(MULTIPLE_WHITESPACES_REGEX, ' '); displayName = displayName.trim(); return displayName; } /** * Get (up to 2 characters) initials based on display name of the persona. * * @public */ function getInitials(displayName, isRtl, allowPhoneInitials) { if (!displayName) { return ''; } displayName = cleanupDisplayName(displayName); // For names containing CJK characters, and phone numbers, we don't display initials if (UNSUPPORTED_TEXT_REGEX.test(displayName) || (!allowPhoneInitials && PHONENUMBER_REGEX.test(displayName))) { return ''; } return getInitialsLatin(displayName, isRtl); } exports.getInitials = getInitials; //# sourceMappingURL=initials.js.map