{"version":3,"sources":["../src/utils/getInitials.ts"],"sourcesContent":["/**\n * Regular expressions matching characters to ignore when calculating the initials.\n */\n\n/**\n * Regular expression matching characters within various types of enclosures, including the enclosures themselves\n *  so for example, (xyz) [xyz] {xyz} all would be ignored\n */\nconst UNWANTED_ENCLOSURES_REGEX: RegExp = /[\\(\\[\\{][^\\)\\]\\}]*[\\)\\]\\}]/g;\n\n/**\n * Regular expression matching special ASCII characters except space, plus some unicode special characters.\n * Applies after unwanted enclosures have been removed.\n * Note: the range starts at \\uE000 (not \\uD800) to avoid matching surrogate code units, which would break\n * supplementary Unicode characters (encoded as surrogate pairs in UTF-16) such as GB18030-2022 extension characters.\n */\nconst UNWANTED_CHARS_REGEX: RegExp = /[\\0-\\u001F\\!-/:-@\\[-`\\{-\\u00BF\\u0250-\\u036F\\uE000-\\uFFFF]/g;\n\n/**\n * Regular expression matching phone numbers. Applied after chars matching UNWANTED_CHARS_REGEX have been removed\n * and number has been trimmed for whitespaces\n */\nconst PHONENUMBER_REGEX: RegExp = /^\\d+[\\d\\s]*(:?ext|x|)\\s*\\d+$/i;\n\n/** Regular expression matching one or more spaces. */\nconst MULTIPLE_WHITESPACES_REGEX: RegExp = /\\s+/g;\n\n/**\n * Regular expression matching languages for which we currently don't support initials.\n * Arabic:   Arabic, Arabic Supplement, Arabic Extended-A.\n * Korean:   Hangul Jamo, Hangul Compatibility Jamo, Hangul Jamo Extended-A, Hangul Syllables, Hangul Jamo Extended-B.\n * Japanese: Hiragana, Katakana.\n * CJK:      CJK Unified Ideographs Extension A, CJK Unified Ideographs, CJK Compatibility Ideographs.\n * Note: Supplementary CJK characters (GB18030-2022 extension characters in Ext B-I) are intentionally not listed\n * here so they can be rendered as initials.\n */\nconst UNSUPPORTED_TEXT_REGEX: RegExp =\n  /[\\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]/;\n\nfunction getFirstCodePoint(value: string): string {\n  if (!value) {\n    return '';\n  }\n\n  const codePoint = value.codePointAt(0);\n  return codePoint === undefined ? '' : String.fromCodePoint(codePoint);\n}\n\nfunction getInitialsLatin(displayName: string, isRtl: boolean, firstInitialOnly?: boolean): string {\n  let initials = '';\n\n  const splits: string[] = displayName.split(' ');\n  if (splits.length !== 0) {\n    // Use code point-aware helper to correctly handle supplementary characters (e.g. GB18030-2022 extension chars)\n    // that are encoded as surrogate pairs; charAt(0) would only return half of such a character.\n    initials += getFirstCodePoint(splits[0]).toUpperCase();\n  }\n\n  if (!firstInitialOnly) {\n    if (splits.length === 2) {\n      initials += getFirstCodePoint(splits[1]).toUpperCase();\n    } else if (splits.length === 3) {\n      initials += getFirstCodePoint(splits[2]).toUpperCase();\n    }\n  }\n\n  if (isRtl && [...initials].length > 1) {\n    const chars = [...initials];\n    return chars[1] + chars[0];\n  }\n\n  return initials;\n}\n\nfunction cleanupDisplayName(displayName: string): string {\n  displayName = displayName.replace(UNWANTED_ENCLOSURES_REGEX, '');\n  displayName = displayName.replace(UNWANTED_CHARS_REGEX, '');\n  displayName = displayName.replace(MULTIPLE_WHITESPACES_REGEX, ' ');\n  displayName = displayName.trim();\n\n  return displayName;\n}\n\n/**\n * Get (up to 2 characters) initials based on display name of the persona.\n *\n * @param displayName - The full name of the person or entity\n * @param isRtl - Whether the display is in RTL\n * @param options - Extra options to control the behavior of getInitials\n *\n * @returns The 1 or 2 character initials based on the name. Or an empty string if no initials\n * could be derived from the name.\n *\n * @internal\n */\nexport function getInitials(\n  displayName: string | undefined | null,\n  isRtl: boolean,\n  options?: {\n    /** Should initials be generated from phone numbers (default false) */\n    allowPhoneInitials?: boolean;\n\n    /** Returns only the first initial */\n    firstInitialOnly?: boolean;\n  },\n): string {\n  if (!displayName) {\n    return '';\n  }\n\n  displayName = cleanupDisplayName(displayName);\n\n  // Check only the first code point against UNSUPPORTED_TEXT_REGEX so that names starting with a supported\n  // character (e.g. GB18030-2022 extension characters) produce an initial even when the rest of the string\n  // contains BMP CJK characters that would otherwise trigger the regex.\n  const firstCodePoint = getFirstCodePoint(displayName);\n  if (\n    UNSUPPORTED_TEXT_REGEX.test(firstCodePoint) ||\n    (!options?.allowPhoneInitials && PHONENUMBER_REGEX.test(displayName))\n  ) {\n    return '';\n  }\n\n  return getInitialsLatin(displayName, isRtl, options?.firstInitialOnly);\n}\n"],"names":["UNWANTED_ENCLOSURES_REGEX","UNWANTED_CHARS_REGEX","PHONENUMBER_REGEX","MULTIPLE_WHITESPACES_REGEX","UNSUPPORTED_TEXT_REGEX","getFirstCodePoint","value","codePoint","codePointAt","undefined","String","fromCodePoint","getInitialsLatin","displayName","isRtl","firstInitialOnly","initials","splits","split","length","toUpperCase","chars","cleanupDisplayName","replace","trim","getInitials","options","firstCodePoint","test","allowPhoneInitials"],"mappings":"AAAA;;CAEC,GAED;;;CAGC;;;;;;;eAwFeyB;;;AAvFhB,MAAMzB,4BAAoC;AAE1C;;;;;CAKC,GACD,MAAMC,uBAA+B;AAErC;;;CAGC,GACD,MAAMC,oBAA4B;AAElC,oDAAoD,GACpD,MAAMC,6BAAqC;AAE3C;;;;;;;;CAQC,GACD,MAAMC,yBACJ;AAEF,SAASC,kBAAkBC,KAAa;IACtC,IAAI,CAACA,OAAO;QACV,OAAO;IACT;IAEA,MAAMC,YAAYD,MAAME,WAAW,CAAC;IACpC,OAAOD,cAAcE,YAAY,KAAKC,OAAOC,aAAa,CAACJ;AAC7D;AAEA,SAASK,iBAAiBC,WAAmB,EAAEC,KAAc,EAAEC,gBAA0B;IACvF,IAAIC,WAAW;IAEf,MAAMC,SAAmBJ,YAAYK,KAAK,CAAC;IAC3C,IAAID,OAAOE,MAAM,KAAK,GAAG;QACvB,+GAA+G;QAC/G,6FAA6F;QAC7FH,YAAYX,kBAAkBY,MAAM,CAAC,EAAE,EAAEG,W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