Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "highlight-words-core",
"description": "Utility functions shared by react-highlight-words and react-native-highlight-words",
"version": "1.2.2",
"version": "1.2.3",
"author": "Brian Vaughn <brian.david.vaughn@gmail.com>",
"license": "MIT",
"main": "dist/index.js",
Expand Down
7 changes: 6 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ export type Chunk = {|
* @return Array of "chunks" (where a Chunk is { start:number, end:number, highlight:boolean })
*/
export const findAll = ({
activeIndex,
autoEscape,
caseSensitive = false,
findChunks = defaultFindChunks,
sanitize,
searchWords,
textToHighlight
}: {
activeIndex?: number,
autoEscape?: boolean,
caseSensitive?: boolean,
findChunks?: typeof defaultFindChunks,
Expand All @@ -27,6 +29,7 @@ export const findAll = ({
}): Array<Chunk> => (
fillInChunks({
chunksToHighlight: combineChunks({
activeIndex,
chunks: findChunks({
autoEscape,
caseSensitive,
Expand All @@ -44,8 +47,10 @@ export const findAll = ({
* @return {start:number, end:number}[]
*/
export const combineChunks = ({
activeIndex = -1,
chunks
}: {
activeIndex?: number,
chunks: Array<Chunk>,
}): Array<Chunk> => {
chunks = chunks
Expand All @@ -57,7 +62,7 @@ export const combineChunks = ({
} else {
// ... subsequent chunks get checked to see if they overlap...
const prevChunk = processedChunks.pop()
if (nextChunk.start <= prevChunk.end) {
if (nextChunk.start <= prevChunk.end && activeIndex < 0) {
// 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)
Expand Down
15 changes: 15 additions & 0 deletions src/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ describe('utils', () => {
])
})

it('shouldn\'t highlight words that partially overlap if passed activeIndex', () => {
const combinedChunks = Chunks.combineChunks({
activeIndex: 1,
chunks: Chunks.findChunks({
searchWords: ['thi', 'is'],
textToHighlight: TEXT
})
})
expect(combinedChunks).to.eql([
{start: 0, end: 3, highlight: false},
{start: 2, end: 4, highlight: false},
{start: 5, end: 7, highlight: false}
])
})

it('should combine into the minimum number of marked and unmarked chunks', () => {
const filledInChunks = Chunks.findAll({
searchWords: ['thi', 'is'],
Expand Down