-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathGetStyledTextArray.ts
35 lines (31 loc) · 1.21 KB
/
GetStyledTextArray.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import Str from 'expensify-common/lib/str';
type StyledText = {
text: string;
isColored: boolean;
};
const getStyledTextArray = (name: string, prefix: string): StyledText[] => {
const texts = [];
const prefixLowercase = prefix.toLowerCase();
const prefixLocation = name.toLowerCase().search(Str.escapeForRegExp(prefixLowercase));
if (prefixLocation === 0 && prefix.length === name.length) {
texts.push({text: name, isColored: true});
} else if (prefixLocation === 0 && prefix.length !== name.length) {
texts.push({text: name.slice(0, prefix.length), isColored: true}, {text: name.slice(prefix.length), isColored: false});
} else if (prefixLocation > 0 && prefix.length !== name.length) {
texts.push(
{text: name.slice(0, prefixLocation), isColored: false},
{
text: name.slice(prefixLocation, prefixLocation + prefix.length),
isColored: true,
},
{
text: name.slice(prefixLocation + prefix.length),
isColored: false,
},
);
} else {
texts.push({text: name, isColored: false});
}
return texts;
};
export default getStyledTextArray;