diff --git a/package.json b/package.json index fcb789f..307f141 100644 --- a/package.json +++ b/package.json @@ -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 ", "license": "MIT", "main": "dist/index.js", diff --git a/src/utils.js b/src/utils.js index 8ef0530..91de36c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -11,6 +11,7 @@ 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, @@ -18,6 +19,7 @@ export const findAll = ({ searchWords, textToHighlight }: { + activeIndex?: number, autoEscape?: boolean, caseSensitive?: boolean, findChunks?: typeof defaultFindChunks, @@ -27,6 +29,7 @@ export const findAll = ({ }): Array => ( fillInChunks({ chunksToHighlight: combineChunks({ + activeIndex, chunks: findChunks({ autoEscape, caseSensitive, @@ -44,8 +47,10 @@ export const findAll = ({ * @return {start:number, end:number}[] */ export const combineChunks = ({ + activeIndex = -1, chunks }: { + activeIndex?: number, chunks: Array, }): Array => { chunks = chunks @@ -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) diff --git a/src/utils.test.js b/src/utils.test.js index e7b52a8..26bd5f4 100644 --- a/src/utils.test.js +++ b/src/utils.test.js @@ -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'],