1 | import emojiRegexFactory from 'emoji-regex';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | export const splitAfterSymbols = (symbols, string) => {
|
22 | const textParts = [];
|
23 | let textPartStartIndex = 0;
|
24 |
|
25 | if (string.length === 0) {
|
26 | return [string];
|
27 | }
|
28 |
|
29 | for (let i = 0; i < string.length; ) {
|
30 | let symbolFound = false;
|
31 |
|
32 | for (let j = 0; j < symbols.length; j += 1) {
|
33 | const symbol = symbols[j];
|
34 |
|
35 | if (!symbol) {
|
36 |
|
37 | continue;
|
38 | }
|
39 |
|
40 | symbolFound = string.slice(i, i + symbol.length) === symbol;
|
41 |
|
42 | if (symbolFound) {
|
43 | const textPartEndIndex = i + symbol.length;
|
44 | const textPart = string.slice(textPartStartIndex, textPartEndIndex);
|
45 | textParts.push(textPart);
|
46 | textPartStartIndex = textPartEndIndex;
|
47 | i = textPartStartIndex;
|
48 | break;
|
49 | }
|
50 | }
|
51 |
|
52 | if (!symbolFound) {
|
53 | i += 1;
|
54 | }
|
55 | }
|
56 |
|
57 | const final = string.slice(textPartStartIndex);
|
58 | if (final) {
|
59 | textParts.push(final);
|
60 | }
|
61 |
|
62 | return textParts;
|
63 | };
|
64 |
|
65 | const startsWithEmojiRegex = `^(${emojiRegexFactory().source})`;
|
66 |
|
67 | export const getAvatarChar = (name) => {
|
68 | if (name) {
|
69 |
|
70 | const match = name.match(startsWithEmojiRegex);
|
71 | if (match) {
|
72 |
|
73 | return match[0];
|
74 | }
|
75 | return name.charAt(0).toUpperCase();
|
76 | }
|
77 |
|
78 | return '';
|
79 | };
|