From 1e34ef31693a3785a7578596f3b4d5caa0a767d0 Mon Sep 17 00:00:00 2001 From: Dzmitry Ramaniuk Date: Thu, 9 May 2019 23:22:03 +0300 Subject: [PATCH 1/4] Adjacent active chunks are handled as a single chunk --- src/utils.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index 8ef0530..d6472bb 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, 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) From fa98328620efbccf507d180d98959110eab4413b Mon Sep 17 00:00:00 2001 From: Dzmitry Ramaniuk Date: Sat, 11 May 2019 14:34:01 +0300 Subject: [PATCH 2/4] Adjacent active chunks are handled as a single chunk --- src/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index d6472bb..91de36c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -47,7 +47,7 @@ export const findAll = ({ * @return {start:number, end:number}[] */ export const combineChunks = ({ - activeIndex, + activeIndex = -1, chunks }: { activeIndex?: number, From 674c9cd4bcdca6462e6d6c34bd43d681971458a0 Mon Sep 17 00:00:00 2001 From: Dzmitry Ramaniuk Date: Sat, 11 May 2019 15:10:48 +0300 Subject: [PATCH 3/4] Adjacent active chunks are handled as a single chunk --- src/utils.test.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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'], From 590bb50b21ac4dbc3b05c2c7fba4139214371761 Mon Sep 17 00:00:00 2001 From: Dzmitry Ramaniuk Date: Sat, 11 May 2019 15:12:20 +0300 Subject: [PATCH 4/4] Adjacent active chunks are handled as a single chunk --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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",