|
| 1 | +export type Chunk = { |
| 2 | + highlight: boolean |
| 3 | + start: number |
| 4 | + end: number |
| 5 | +} |
| 6 | + |
| 7 | +/** |
| 8 | + * Creates an array of chunk objects representing both higlightable and non highlightable pieces of text that match each search word. |
| 9 | + * @return Array of "chunks" (where a Chunk is { start:number, end:number, highlight:boolean }) |
| 10 | + */ |
| 11 | +export const findAll = ({ |
| 12 | + autoEscape, |
| 13 | + caseSensitive = false, |
| 14 | + findChunks = defaultFindChunks, |
| 15 | + sanitize, |
| 16 | + searchWords, |
| 17 | + textToHighlight, |
| 18 | +}: { |
| 19 | + autoEscape?: boolean |
| 20 | + caseSensitive?: boolean |
| 21 | + findChunks?: typeof defaultFindChunks |
| 22 | + sanitize?: typeof defaultSanitize |
| 23 | + searchWords: Array<string> |
| 24 | + textToHighlight: string |
| 25 | +}): Array<Chunk> => |
| 26 | + fillInChunks({ |
| 27 | + chunksToHighlight: combineChunks({ |
| 28 | + chunks: findChunks({ |
| 29 | + autoEscape, |
| 30 | + caseSensitive, |
| 31 | + sanitize, |
| 32 | + searchWords, |
| 33 | + textToHighlight, |
| 34 | + }), |
| 35 | + }), |
| 36 | + totalLength: textToHighlight ? textToHighlight.length : 0, |
| 37 | + }) |
| 38 | + |
| 39 | +/** |
| 40 | + * Takes an array of {start:number, end:number} objects and combines chunks that overlap into single chunks. |
| 41 | + * @return {start:number, end:number}[] |
| 42 | + */ |
| 43 | +export const combineChunks = ({chunks}: {chunks: Array<Chunk>}): Array<Chunk> => { |
| 44 | + const res: Array<Chunk> = chunks |
| 45 | + .sort((first, second) => first.start - second.start) |
| 46 | + .reduce((processedChunks: Chunk[], nextChunk) => { |
| 47 | + // First chunk just goes straight in the array... |
| 48 | + if (processedChunks.length === 0) { |
| 49 | + return [nextChunk] |
| 50 | + } else { |
| 51 | + // ... subsequent chunks get checked to see if they overlap... |
| 52 | + const prevChunk = processedChunks.pop() |
| 53 | + if (prevChunk && nextChunk.start <= prevChunk.end) { |
| 54 | + // It may be the case that prevChunk completely surrounds nextChunk, so take the |
| 55 | + // largest of the end indeces. |
| 56 | + const endIndex = Math.max(prevChunk.end, nextChunk.end) |
| 57 | + processedChunks.push({highlight: false, start: prevChunk.start, end: endIndex}) |
| 58 | + } else { |
| 59 | + if (prevChunk) { |
| 60 | + processedChunks.push(prevChunk, nextChunk) |
| 61 | + } |
| 62 | + } |
| 63 | + return processedChunks |
| 64 | + } |
| 65 | + }, []) |
| 66 | + |
| 67 | + return res |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Examine text for any matches. |
| 72 | + * If we find matches, add them to the returned array as a "chunk" object ({start:number, end:number}). |
| 73 | + * @return {start:number, end:number}[] |
| 74 | + */ |
| 75 | +const defaultFindChunks = ({ |
| 76 | + autoEscape, |
| 77 | + caseSensitive, |
| 78 | + sanitize = defaultSanitize, |
| 79 | + searchWords, |
| 80 | + textToHighlight, |
| 81 | +}: { |
| 82 | + autoEscape?: boolean |
| 83 | + caseSensitive?: boolean |
| 84 | + sanitize?: typeof defaultSanitize |
| 85 | + searchWords: Array<string> |
| 86 | + textToHighlight: string |
| 87 | +}): Array<Chunk> => { |
| 88 | + textToHighlight = sanitize(textToHighlight) |
| 89 | + |
| 90 | + return searchWords |
| 91 | + .filter(searchWord => searchWord) // Remove empty words |
| 92 | + .reduce((chunks: Array<Chunk>, searchWord) => { |
| 93 | + searchWord = sanitize(searchWord) |
| 94 | + |
| 95 | + if (autoEscape) { |
| 96 | + searchWord = escapeRegExpFn(searchWord) |
| 97 | + } |
| 98 | + |
| 99 | + const regex = new RegExp(searchWord, caseSensitive ? 'g' : 'gi') |
| 100 | + |
| 101 | + let match |
| 102 | + while ((match = regex.exec(textToHighlight))) { |
| 103 | + const start = match.index |
| 104 | + const end = regex.lastIndex |
| 105 | + // We do not return zero-length matches |
| 106 | + if (end > start) { |
| 107 | + chunks.push({highlight: false, start, end}) |
| 108 | + } |
| 109 | + |
| 110 | + // Prevent browsers like Firefox from getting stuck in an infinite loop |
| 111 | + // See http://www.regexguru.com/2008/04/watch-out-for-zero-length-matches/ |
| 112 | + if (match.index === regex.lastIndex) { |
| 113 | + regex.lastIndex++ |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return chunks |
| 118 | + }, []) |
| 119 | +} |
| 120 | +// Allow the findChunks to be overridden in findAll, |
| 121 | +// but for backwards compatibility we export as the old name |
| 122 | +export {defaultFindChunks as findChunks} |
| 123 | + |
| 124 | +/** |
| 125 | + * Given a set of chunks to highlight, create an additional set of chunks |
| 126 | + * to represent the bits of text between the highlighted text. |
| 127 | + * @param chunksToHighlight {start:number, end:number}[] |
| 128 | + * @param totalLength number |
| 129 | + * @return {start:number, end:number, highlight:boolean}[] |
| 130 | + */ |
| 131 | +export const fillInChunks = ({ |
| 132 | + chunksToHighlight, |
| 133 | + totalLength, |
| 134 | +}: { |
| 135 | + chunksToHighlight: Array<Chunk> |
| 136 | + totalLength: number |
| 137 | +}): Array<Chunk> => { |
| 138 | + const allChunks: Array<Chunk> = [] |
| 139 | + const append = (start: number, end: number, highlight: boolean) => { |
| 140 | + if (end - start > 0) { |
| 141 | + allChunks.push({ |
| 142 | + start, |
| 143 | + end, |
| 144 | + highlight, |
| 145 | + }) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + if (chunksToHighlight.length === 0) { |
| 150 | + append(0, totalLength, false) |
| 151 | + } else { |
| 152 | + let lastIndex = 0 |
| 153 | + chunksToHighlight.forEach(chunk => { |
| 154 | + append(lastIndex, chunk.start, false) |
| 155 | + append(chunk.start, chunk.end, true) |
| 156 | + lastIndex = chunk.end |
| 157 | + }) |
| 158 | + append(lastIndex, totalLength, false) |
| 159 | + } |
| 160 | + return allChunks |
| 161 | +} |
| 162 | + |
| 163 | +function defaultSanitize(string: string): string { |
| 164 | + return string |
| 165 | +} |
| 166 | + |
| 167 | +function escapeRegExpFn(string: string): string { |
| 168 | + return string.replace(/[-[\]{}()*+?.,\\^$|]/g, '\\$&') |
| 169 | +} |
0 commit comments