-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reverseHighlight/reverseSnippet): implement sibling strategy fro…
…m InstantSearch.js (#388) * Implement sibling strategy from InstantSearch.js * Increase bundle size * Rename `getHighlightFromSiblings` to `isPartHighlighted`
- Loading branch information
Showing
6 changed files
with
173 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/autocomplete-js/src/__tests__/reverseHighlightHit.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { reverseHighlightHit } from '../highlight'; | ||
|
||
describe('reverseHighlightHit', () => { | ||
test('returns a reversed partially highlighted hit', () => { | ||
expect( | ||
reverseHighlightHit({ | ||
hit: { | ||
objectID: 'amazon fire tablets', | ||
query: 'amazon fire tablets', | ||
_highlightResult: { | ||
query: { | ||
fullyHighlighted: false, | ||
matchLevel: 'full', | ||
matchedWords: ['fire', 'tablet'], | ||
value: | ||
'amazon __aa-highlight__fire__/aa-highlight__ __aa-highlight__tablet__/aa-highlight__s', | ||
}, | ||
}, | ||
}, | ||
attribute: 'query', | ||
}) | ||
).toMatchInlineSnapshot(`"<mark>amazon </mark>fire tablet<mark>s</mark>"`); | ||
}); | ||
|
||
test('returns a reversed fully highlighted hit', () => { | ||
expect( | ||
reverseHighlightHit({ | ||
hit: { | ||
objectID: 'amazon fire tablets', | ||
query: 'amazon fire tablets', | ||
_highlightResult: { | ||
query: { | ||
fullyHighlighted: true, | ||
matchLevel: 'full', | ||
matchedWords: ['amazon', 'fire', 'tablet'], | ||
value: | ||
'__aa-highlight__amazon__/aa-highlight__ __aa-highlight__fire__/aa-highlight__ __aa-highlight__tablets__/aa-highlight__', | ||
}, | ||
}, | ||
}, | ||
attribute: 'query', | ||
}) | ||
).toMatchInlineSnapshot(`"amazon fire tablets"`); | ||
}); | ||
|
||
test('returns a reversed empty highlighted query hit', () => { | ||
expect( | ||
reverseHighlightHit({ | ||
hit: { | ||
objectID: 'amazon fire tablets', | ||
query: 'amazon fire tablets', | ||
_highlightResult: { | ||
query: { | ||
fullyHighlighted: false, | ||
matchLevel: 'none', | ||
matchedWords: [], | ||
value: 'amazon fire tablets', | ||
}, | ||
}, | ||
}, | ||
attribute: 'query', | ||
}) | ||
).toMatchInlineSnapshot(`"amazon fire tablets"`); | ||
}); | ||
}); |
33 changes: 33 additions & 0 deletions
33
packages/autocomplete-preset-algolia/src/highlight/__tests__/isPartHighlighted.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { isPartHighlighted } from '../isPartHighlighted'; | ||
|
||
describe('isPartHighlighted', () => { | ||
test('returns the isHighlighted value with a missing sibling', () => { | ||
expect( | ||
isPartHighlighted( | ||
[ | ||
{ isHighlighted: true, value: 'Amazon' }, | ||
{ | ||
isHighlighted: false, | ||
value: ' - Fire HD8 - 8" - Tablet - 16GB - Wi-Fi - Black', | ||
}, | ||
], | ||
0 | ||
) | ||
).toEqual(true); | ||
}); | ||
|
||
test('returns the isHighlighted value with both siblings', () => { | ||
expect( | ||
isPartHighlighted( | ||
[ | ||
{ isHighlighted: true, value: 'Amazon' }, | ||
{ isHighlighted: false, value: ' - ' }, | ||
{ isHighlighted: true, value: 'Fire' }, | ||
{ isHighlighted: false, value: ' ' }, | ||
{ isHighlighted: true, value: 'TV' }, | ||
], | ||
1 | ||
) | ||
).toEqual(true); | ||
}); | ||
}); |
35 changes: 35 additions & 0 deletions
35
packages/autocomplete-preset-algolia/src/highlight/__tests__/reverseHighlightedParts.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { reverseHighlightedParts } from '../reverseHighlightedParts'; | ||
|
||
describe('reverseHighlightedParts', () => { | ||
test('returns a reversed partially highlighted parts array', () => { | ||
expect( | ||
reverseHighlightedParts([ | ||
{ isHighlighted: true, value: 'amazon ((fire' }, | ||
{ isHighlighted: false, value: 'tv)) tablet??' }, | ||
]) | ||
).toEqual([ | ||
{ isHighlighted: false, value: 'amazon ((fire' }, | ||
{ isHighlighted: true, value: 'tv)) tablet??' }, | ||
]); | ||
}); | ||
|
||
test('returns a reversed fully highlighted parts array', () => { | ||
expect( | ||
reverseHighlightedParts([ | ||
{ isHighlighted: true, value: 'amazon ((fire tv)) tablet??' }, | ||
]) | ||
).toEqual([{ isHighlighted: false, value: 'amazon ((fire tv)) tablet??' }]); | ||
}); | ||
|
||
test('returns a reversed highlighted parts array based on sibling highlighting', () => { | ||
expect( | ||
reverseHighlightedParts([ | ||
{ isHighlighted: true, value: 'amazon ((fire tv)) tablet' }, | ||
{ isHighlighted: false, value: '??' }, | ||
]) | ||
).toEqual([ | ||
{ isHighlighted: false, value: 'amazon ((fire tv)) tablet' }, | ||
{ isHighlighted: false, value: '??' }, | ||
]); | ||
}); | ||
}); |
33 changes: 33 additions & 0 deletions
33
packages/autocomplete-preset-algolia/src/highlight/isPartHighlighted.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ParsedAttribute } from './ParsedAttribute'; | ||
|
||
const htmlEscapes = { | ||
'&': '&', | ||
'<': '<', | ||
'>': '>', | ||
'"': '"', | ||
''': "'", | ||
}; | ||
const hasAlphanumeric = new RegExp(/\w/i); | ||
const regexEscapedHtml = /&(amp|quot|lt|gt|#39);/g; | ||
const regexHasEscapedHtml = RegExp(regexEscapedHtml.source); | ||
|
||
function unescape(value: string): string { | ||
return value && regexHasEscapedHtml.test(value) | ||
? value.replace(regexEscapedHtml, (character) => htmlEscapes[character]) | ||
: value; | ||
} | ||
|
||
export function isPartHighlighted(parts: ParsedAttribute[], i: number) { | ||
const current = parts[i]; | ||
const isNextHighlighted = parts[i + 1]?.isHighlighted || true; | ||
const isPreviousHighlighted = parts[i - 1]?.isHighlighted || true; | ||
|
||
if ( | ||
!hasAlphanumeric.test(unescape(current.value)) && | ||
isPreviousHighlighted === isNextHighlighted | ||
) { | ||
return isPreviousHighlighted; | ||
} | ||
|
||
return current.isHighlighted; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters