Skip to content

Commit

Permalink
feat(algolia): type highlight presets
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Sep 17, 2020
1 parent a6d858e commit 9f4b6bd
Show file tree
Hide file tree
Showing 20 changed files with 365 additions and 240 deletions.
47 changes: 32 additions & 15 deletions packages/autocomplete-js/src/__tests__/autocomplete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ describe('autocomplete-js', () => {
<div
class="aa-InputWrapper"
>
<input
aria-autocomplete="list"
aria-labelledby="autocomplete-label"
autocapitalize="off"
autocomplete="off"
autocorrect="off"
class="aa-Input"
id="autocomplete-input"
maxlength="512"
placeholder=""
spellcheck="false"
type="search"
/>
<label
class="aa-Label"
for="autocomplete-input"
Expand All @@ -70,24 +83,28 @@ describe('autocomplete-js', () => {
</svg>
</label>
<input
aria-autocomplete="list"
aria-labelledby="autocomplete-label"
autocapitalize="off"
autocomplete="off"
autocorrect="off"
class="aa-Input"
id="autocomplete-input"
maxlength="512"
placeholder=""
spellcheck="false"
type="search"
/>
<button
class="aa-Reset"
class="aa-ResetButton"
type="reset"
>
<svg
height="20"
viewBox="0 0 20 20"
width="20"
>
<path
d="M10 10l5.09-5.09L10 10l5.09 5.09L10 10zm0 0L4.91 4.91 10 10l-5.09 5.09L10 10z"
fill="none"
fill-rule="evenodd"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
/>
</svg>
</button>
</div>
</form>
Expand Down
131 changes: 0 additions & 131 deletions packages/autocomplete-preset-algolia/src/highlight.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type ParseAlgoliaHitParams<TItem> = {
hit: TItem;
attribute: keyof TItem;
ignoreEscape?: string[];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type ParsedAttribute = {
value: string;
isHighlighted: boolean;
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {
parseAlgoliaHitHighlight,
parseAlgoliaHitReverseHighlight,
parseAlgoliaHitSnippet,
parseAlgoliaHitReverseSnippet,
} from '../highlight';
import { parseAlgoliaHitHighlight } from '../parseAlgoliaHitHighlight';
import { parseAlgoliaHitReverseHighlight } from '../parseAlgoliaHitReverseHighlight';
import { parseAlgoliaHitReverseSnippet } from '../parseAlgoliaHitReverseSnippet';
import { parseAlgoliaHitSnippet } from '../parseAlgoliaHitSnippet';

describe('highlight', () => {
describe('parseAlgoliaHitHighlight', () => {
Expand All @@ -12,10 +10,15 @@ describe('highlight', () => {
parseAlgoliaHitHighlight({
attribute: 'title',
hit: {
objectID: '1',
title: 'Hello there',
_highlightResult: {
title: {
value:
'__aa-highlight__He__/aa-highlight__llo t__aa-highlight__he__/aa-highlight__re',
matchLevel: 'partial',
matchedWords: [],
fullyHighlighted: false,
},
},
},
Expand Down Expand Up @@ -47,6 +50,8 @@ describe('highlight', () => {
parseAlgoliaHitHighlight({
attribute: 'title',
hit: {
objectID: '1',
title: 'Hello there',
_highlightResult: {
title: {
value: `__aa-highlight__Food__/aa-highlight__ & <Drinks> 'n' "Music"`,
Expand All @@ -73,6 +78,8 @@ describe('highlight', () => {
parseAlgoliaHitHighlight({
attribute: 'title',
hit: {
objectID: '1',
title: 'Hello there',
_highlightResult: {
title: {
value: `__aa-highlight__Food__/aa-highlight__ & <Drinks> 'n' "Music"`,
Expand Down Expand Up @@ -102,6 +109,8 @@ describe('highlight', () => {
parseAlgoliaHitReverseHighlight({
attribute: 'title',
hit: {
objectID: '1',
title: 'Hello there',
_highlightResult: {
title: {
value:
Expand Down Expand Up @@ -136,7 +145,11 @@ describe('highlight', () => {
expect(
parseAlgoliaHitReverseHighlight({
attribute: 'title',
hit: { _highlightResult: { title: { value: 'Hello' } } },
hit: {
objectID: '1',
title: 'Hello there',
_highlightResult: { title: { value: 'Hello' } },
},
})
).toMatchInlineSnapshot(`
Array [
Expand All @@ -153,6 +166,8 @@ describe('highlight', () => {
parseAlgoliaHitReverseHighlight({
attribute: 'title',
hit: {
objectID: '1',
title: 'Hello there',
_highlightResult: {
title: {
value: `__aa-highlight__Food__/aa-highlight__ & <Drinks> 'n' "Music"`,
Expand All @@ -179,6 +194,8 @@ describe('highlight', () => {
parseAlgoliaHitReverseHighlight({
attribute: 'title',
hit: {
objectID: '1',
title: 'Hello there',
_highlightResult: {
title: {
value: `__aa-highlight__Food__/aa-highlight__ & <Drinks> 'n' "Music"`,
Expand Down Expand Up @@ -208,6 +225,8 @@ describe('highlight', () => {
parseAlgoliaHitReverseSnippet({
attribute: 'title',
hit: {
objectID: '1',
title: 'Hello there',
_snippetResult: {
title: {
value:
Expand Down Expand Up @@ -243,6 +262,8 @@ describe('highlight', () => {
parseAlgoliaHitReverseSnippet({
attribute: 'title',
hit: {
objectID: '1',
title: 'Hello there',
_snippetResult: {
title: {
value: `__aa-highlight__Food__/aa-highlight__ & <Drinks> 'n' "Music"`,
Expand All @@ -269,6 +290,8 @@ describe('highlight', () => {
parseAlgoliaHitReverseSnippet({
attribute: 'title',
hit: {
objectID: '1',
title: 'Hello there',
_snippetResult: {
title: {
value: `__aa-highlight__Food__/aa-highlight__ & <Drinks> 'n' "Music"`,
Expand Down Expand Up @@ -298,6 +321,8 @@ describe('highlight', () => {
parseAlgoliaHitSnippet({
attribute: 'title',
hit: {
objectID: '1',
title: 'Hello there',
_snippetResult: {
title: {
value:
Expand Down Expand Up @@ -334,6 +359,8 @@ describe('highlight', () => {
parseAlgoliaHitSnippet({
attribute: 'title',
hit: {
objectID: '1',
title: 'Hello there',
_snippetResult: {
title: {
value: `__aa-highlight__Food__/aa-highlight__ & <Drinks> 'n' "Music"`,
Expand All @@ -360,6 +387,8 @@ describe('highlight', () => {
parseAlgoliaHitSnippet({
attribute: 'title',
hit: {
objectID: '1',
title: 'Hello there',
_snippetResult: {
title: {
value: `__aa-highlight__Food__/aa-highlight__ & <Drinks> 'n' "Music"`,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function getAttributeValueByPath<THit>(hit: THit, path: string): string {
const parts = path.split('.');
const value = parts.reduce<string>(
(current, key) => current && current[key],
hit as any
);

if (typeof value !== 'string') {
throw new Error(
`The attribute ${JSON.stringify(path)} does not exist on the hit.`
);
}

return value;
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Hit } from '@algolia/client-search';

import { getAttributeValueByPath } from './getAttributeValueByPath';
import { ParseAlgoliaHitParams } from './ParseAlgoliaHitParams';
import { parseAttribute } from './parseAttribute';
import { ParsedAttribute } from './ParsedAttribute';

export function parseAlgoliaHitHighlight<THit extends Hit<{}>>({
hit,
attribute,
ignoreEscape,
}: ParseAlgoliaHitParams<THit>): ParsedAttribute[] {
const highlightedValue = getAttributeValueByPath(
hit,
`_highlightResult.${attribute}.value`
);

return parseAttribute({
highlightedValue,
ignoreEscape,
});
}

0 comments on commit 9f4b6bd

Please sign in to comment.