forked from Froelund/react-native-text-highlight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sectionString.js
35 lines (31 loc) · 819 Bytes
/
sectionString.js
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
export default (highlight, origStr, caseSensitive) => {
//Sanity check
if (!highlight || !origStr) {
return [{text: origStr}];
}
var indices = [];
var startIndex = 0;
var searchStrLen = highlight.length;
var index;
var str = origStr;
if (!caseSensitive) {
str = str.toLowerCase();
highlight = highlight.toLowerCase();
}
while ((index = str.indexOf(highlight, startIndex)) > -1) {
if (index > 0) {
indices.push({
text: origStr.substring(startIndex, index)
});
}
startIndex = index + searchStrLen;
indices.push({
highlight: true,
text: origStr.substring(index, startIndex)
});
}
if (startIndex < str.length) {
indices.push({text: origStr.substring(startIndex, str.length)});
}
return indices;
}