Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(reverseHighlight/reverseSnippet): Implements reverseHighlight and reverseSnippet #4592

Merged
merged 22 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
7baa09d
feat(highlight): accept array for attribute
Haroenv Nov 23, 2020
1a66d30
allow number in type
Haroenv Nov 24, 2020
31f6c64
Implement reverseHighlight helper
shortcuts Nov 24, 2020
c741b44
Implement reverseHighlight helper
shortcuts Nov 24, 2020
94fd93d
Implement reverseSnippet, rebase from `feat/highlight-array`, clearne…
shortcuts Nov 24, 2020
c479f7f
Implement reverseSnippet, rebase from `feat/highlight-array`, clearne…
shortcuts Nov 24, 2020
a8b8e74
Moved `getReverseHighlight` function to utils
shortcuts Nov 24, 2020
d6a59b1
Add tests for `getReversedHighlight` and `unescape`
shortcuts Nov 24, 2020
1ce4538
Add `@MAJOR` comments from feat/highlight-array
shortcuts Nov 26, 2020
fc9b985
Merge branch 'master' into feat/reverseHighlight
shortcuts Nov 26, 2020
ca9ab81
Resolve conflicts on newest files
shortcuts Nov 26, 2020
f842b61
Remove `getReversedHighlight`, add `getHighlightedParts` and `reverse…
shortcuts Dec 3, 2020
1b53d3f
Merge branch 'master' into feat/reverseHighlight
shortcuts Dec 3, 2020
00f664a
Remove tmp classes to prevent duplicate with `instantsearch-specs`, r…
shortcuts Dec 8, 2020
5294e82
Merge branch 'master' of github.com:algolia/instantsearch.js into fea…
shortcuts Dec 8, 2020
1e9f3aa
Add tests for `reverseSnippet` and `reverseHighlight` based on the si…
shortcuts Dec 10, 2020
3167129
Merge branch 'master' into feat/reverseHighlight
shortcuts Dec 10, 2020
0a47dd2
Increase bundle size limit from 65.25KB to 65.5KB
shortcuts Dec 11, 2020
51d39fd
Merge branch 'master' into feat/reverseHighlight
shortcuts Dec 21, 2020
05d4a33
Inlined function export and test snapshots, moved `isAlphanumeric` re…
shortcuts Dec 21, 2020
2c851c5
fix regex naming from `isAlphanumeric` to `hasAlphanumeric`
shortcuts Dec 22, 2020
a88b614
Merge branch 'master' into feat/reverseHighlight
shortcuts Dec 22, 2020
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
40 changes: 21 additions & 19 deletions src/lib/utils/__tests__/concatHighlightedParts-test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
import { HighlightedParts } from '../../../types';
import concatHighlightedParts from '../concatHighlightedParts';

const oneMatch: HighlightedParts[] = [
{ isHighlighted: false, value: 'Amazon' },
{
isHighlighted: true,
value: ' - Fire HD8 - 8" - Tablet - 16GB - Wi-Fi - Black',
},
];

const multipleMatches: HighlightedParts[] = [
{ isHighlighted: false, value: 'Amazon' },
{ isHighlighted: true, value: ' - Fire HD8 - 8" - Tablet - 16GB - Wi-' },
{ isHighlighted: false, value: 'Fi' },
{ isHighlighted: false, value: ' - ' },
{ isHighlighted: false, value: 'Black' },
];

describe('concatHighlightedParts', () => {
test('returns a concatenated string from HighlightedParts with a single match', () => {
expect(concatHighlightedParts(oneMatch)).toMatchInlineSnapshot(
expect(
concatHighlightedParts([
{ isHighlighted: false, value: 'Amazon' },
{
isHighlighted: true,
value: ' - Fire HD8 - 8" - Tablet - 16GB - Wi-Fi - Black',
},
])
).toMatchInlineSnapshot(
`"Amazon<mark> - Fire HD8 - 8&quot; - Tablet - 16GB - Wi-Fi - Black</mark>"`
);
});

test('returns a concatenated string from HighlightedParts with multiple matches', () => {
expect(concatHighlightedParts(multipleMatches)).toMatchInlineSnapshot(
expect(
concatHighlightedParts([
{ isHighlighted: false, value: 'Amazon' },
{
isHighlighted: true,
value: ' - Fire HD8 - 8&quot; - Tablet - 16GB - Wi-',
},
{ isHighlighted: false, value: 'Fi' },
{ isHighlighted: false, value: ' - ' },
{ isHighlighted: false, value: 'Black' },
])
).toMatchInlineSnapshot(
`"Amazon<mark> - Fire HD8 - 8&quot; - Tablet - 16GB - Wi-</mark>Fi - Black"`
);
});
Expand Down
18 changes: 10 additions & 8 deletions src/lib/utils/__tests__/getHighlightedParts-test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import getHighlightedParts from '../getHighlightedParts';

const oneMatch =
'<mark>Amazon</mark> - Fire HD8 - 8&quot; - Tablet - 16GB - Wi-Fi - Black';

const multipleMatches =
'<mark>Amazon</mark> - Fire HD8 - 8&quot; - <mark>Tablet</mark> - 16GB - Wi-Fi - Black';

describe('getHighlightedParts', () => {
test('returns an HighlightParts array of object from a string with a single match', () => {
expect(getHighlightedParts(oneMatch)).toEqual([
expect(
getHighlightedParts(
'<mark>Amazon</mark> - Fire HD8 - 8&quot; - Tablet - 16GB - Wi-Fi - Black'
)
).toEqual([
{ isHighlighted: true, value: 'Amazon' },
{
isHighlighted: false,
Expand All @@ -18,7 +16,11 @@ describe('getHighlightedParts', () => {
});

test('returns an HighlightedParts array of object from a string with multiple matches', () => {
expect(getHighlightedParts(multipleMatches)).toEqual([
expect(
getHighlightedParts(
'<mark>Amazon</mark> - Fire HD8 - 8&quot; - <mark>Tablet</mark> - 16GB - Wi-Fi - Black'
)
).toEqual([
{ isHighlighted: true, value: 'Amazon' },
{
isHighlighted: false,
Expand Down
40 changes: 21 additions & 19 deletions src/lib/utils/__tests__/reverseHighlightedParts-test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
import { HighlightedParts } from '../../../types';
import reverseHighlightedParts from '../reverseHighlightedParts';

const oneMatch: HighlightedParts[] = [
{ isHighlighted: false, value: 'Amazon' },
{
isHighlighted: true,
value: ' - Fire HD8 - 8&quot; - Tablet - 16GB - Wi-Fi - Black',
},
];

const multipleMatches: HighlightedParts[] = [
{ isHighlighted: false, value: 'Amazon' },
{ isHighlighted: true, value: ' - Fire HD8 - 8&quot; - Tablet - 16GB - Wi-' },
{ isHighlighted: false, value: 'Fi' },
{ isHighlighted: false, value: ' - ' },
{ isHighlighted: false, value: 'Black' },
];

describe('reverseHighlightedParts', () => {
test('returns reversed HighlightedParts with a single match', () => {
expect(reverseHighlightedParts(oneMatch)).toEqual([
expect(
reverseHighlightedParts([
{ isHighlighted: false, value: 'Amazon' },
{
isHighlighted: true,
value: ' - Fire HD8 - 8&quot; - Tablet - 16GB - Wi-Fi - Black',
},
])
).toEqual([
{ isHighlighted: true, value: 'Amazon' },
{
isHighlighted: false,
Expand All @@ -29,7 +20,18 @@ describe('reverseHighlightedParts', () => {
});

test('with reversed HighlightedParts with multiple matches', () => {
expect(reverseHighlightedParts(multipleMatches)).toEqual([
expect(
reverseHighlightedParts([
{ isHighlighted: false, value: 'Amazon' },
{
isHighlighted: true,
value: ' - Fire HD8 - 8&quot; - Tablet - 16GB - Wi-',
},
{ isHighlighted: false, value: 'Fi' },
{ isHighlighted: false, value: ' - ' },
{ isHighlighted: false, value: 'Black' },
])
).toEqual([
{ isHighlighted: true, value: 'Amazon' },
{
isHighlighted: false,
Expand Down
6 changes: 2 additions & 4 deletions src/lib/utils/concatHighlightedParts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HighlightedParts } from '../../types';
import { TAG_REPLACEMENT } from '../escape-highlight';

const concatHighlightedParts = (parts: HighlightedParts[]) => {
export default function concatHighlightedParts(parts: HighlightedParts[]) {
const { highlightPreTag, highlightPostTag } = TAG_REPLACEMENT;

return parts
Expand All @@ -11,6 +11,4 @@ const concatHighlightedParts = (parts: HighlightedParts[]) => {
: part.value
)
.join('');
};

export default concatHighlightedParts;
}
12 changes: 7 additions & 5 deletions src/lib/utils/getHighlightFromSiblings.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import unescape from './unescape';
import { HighlightedParts } from '../../types';

const getHighlightFromSiblings = (parts: HighlightedParts[], i: number) => {
const isAlphanumeric = new RegExp(/\w/i);
shortcuts marked this conversation as resolved.
Show resolved Hide resolved

export default function getHighlightFromSiblings(
parts: HighlightedParts[],
i: number
) {
const current = parts[i];
const isAlphanumeric = new RegExp(/\w/gi);
const isNextHighlighted = parts[i + 1]?.isHighlighted || true;
const isPreviousHighlighted = parts[i - 1]?.isHighlighted || true;

Expand All @@ -15,6 +19,4 @@ const getHighlightFromSiblings = (parts: HighlightedParts[], i: number) => {
}

return current.isHighlighted;
};

export default getHighlightFromSiblings;
}
6 changes: 2 additions & 4 deletions src/lib/utils/getHighlightedParts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TAG_REPLACEMENT } from '../../lib/escape-highlight';

const getHighlightedParts = (highlightedValue: string) => {
export default function getHighlightedParts(highlightedValue: string) {
const { highlightPostTag, highlightPreTag } = TAG_REPLACEMENT;

const splitByPreTag = highlightedValue.split(highlightPreTag);
Expand All @@ -26,6 +26,4 @@ const getHighlightedParts = (highlightedValue: string) => {
});

return elements;
};

export default getHighlightedParts;
}
6 changes: 2 additions & 4 deletions src/lib/utils/reverseHighlightedParts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HighlightedParts } from '../../types';
shortcuts marked this conversation as resolved.
Show resolved Hide resolved
import getHighlightFromSiblings from './getHighlightFromSiblings';

const reverseHighlightedParts = (parts: HighlightedParts[]) => {
export default function reverseHighlightedParts(parts: HighlightedParts[]) {
if (!parts.some(part => part.isHighlighted)) {
return parts.map(part => ({ ...part, isHighlighted: false }));
}
Expand All @@ -10,6 +10,4 @@ const reverseHighlightedParts = (parts: HighlightedParts[]) => {
...part,
isHighlighted: !getHighlightFromSiblings(parts, i),
}));
};

export default reverseHighlightedParts;
}
4 changes: 1 addition & 3 deletions src/lib/utils/unescape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ const regexHasEscapedHtml = RegExp(regexEscapedHtml.source);
* Converts the HTML entities "&", "<", ">", '"', and "'" in `string` to their
* characters.
*/
function unescape(value: string): string {
export default function unescape(value: string): string {
return value && regexHasEscapedHtml.test(value)
? value.replace(regexEscapedHtml, character => htmlEscapes[character])
: value;
}

export default unescape;