-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathutils.js
136 lines (124 loc) · 3.49 KB
/
utils.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* Creates an array of chunk objects representing both higlightable and non highlightable pieces of text that match each search word.
* @return Array of "chunks" (where a Chunk is { start:number, end:number, highlight:boolean })
*/
export const findAll = ({
autoEscape,
caseSensitive = false,
sanitize,
searchWords,
textToHighlight
}) => (
fillInChunks({
chunksToHighlight: combineChunks({
chunks: findChunks({
autoEscape,
caseSensitive,
sanitize,
searchWords,
textToHighlight
})
}),
totalLength: textToHighlight.length
})
)
/**
* Takes an array of {start:number, end:number} objects and combines chunks that overlap into single chunks.
* @return {start:number, end:number}[]
*/
export const combineChunks = ({
chunks
}) => {
chunks = chunks
.sort((first, second) => first.start - second.start)
.reduce((processedChunks, nextChunk) => {
// First chunk just goes straight in the array...
if (processedChunks.length === 0) {
return [nextChunk]
} else {
// ... subsequent chunks get checked to see if they overlap...
const prevChunk = processedChunks.pop()
if (nextChunk.start <= prevChunk.end) {
// It may be the case that prevChunk completely surrounds nextChunk, so take the
// largest of the end indeces.
const endIndex = Math.max(prevChunk.end, nextChunk.end)
processedChunks.push({start: prevChunk.start, end: endIndex})
} else {
processedChunks.push(prevChunk, nextChunk)
}
return processedChunks
}
}, [])
return chunks
}
/**
* Examine text for any matches.
* If we find matches, add them to the returned array as a "chunk" object ({start:number, end:number}).
* @return {start:number, end:number}[]
*/
export const findChunks = ({
autoEscape,
caseSensitive,
sanitize = identity,
searchWords,
textToHighlight
}) => {
textToHighlight = sanitize(textToHighlight)
return searchWords
.filter(searchWord => searchWord) // Remove empty words
.reduce((chunks, searchWord) => {
searchWord = sanitize(searchWord)
if (autoEscape) {
searchWord = escapeRegExpFn(searchWord)
}
const regex = new RegExp(searchWord, caseSensitive ? 'g' : 'gi')
let match
while ((match = regex.exec(textToHighlight))) {
chunks.push({
start: match.index,
end: regex.lastIndex
})
}
return chunks
}, [])
}
/**
* Given a set of chunks to highlight, create an additional set of chunks
* to represent the bits of text between the highlighted text.
* @param chunksToHighlight {start:number, end:number}[]
* @param totalLength number
* @return {start:number, end:number, highlight:boolean}[]
*/
export const fillInChunks = ({
chunksToHighlight,
totalLength
}) => {
const allChunks = []
const append = (start, end, highlight) => {
if (end - start > 0) {
allChunks.push({
start,
end,
highlight
})
}
}
if (chunksToHighlight.length === 0) {
append(0, totalLength, false)
} else {
let lastIndex = 0
chunksToHighlight.forEach((chunk) => {
append(lastIndex, chunk.start, false)
append(chunk.start, chunk.end, true)
lastIndex = chunk.end
})
append(lastIndex, totalLength, false)
}
return allChunks
}
function identity (value) {
return value
}
function escapeRegExpFn (str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&')
}