Skip to content

Commit

Permalink
feat(js): type highlighting utils
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Sep 9, 2020
1 parent fa4b959 commit b178487
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
9 changes: 7 additions & 2 deletions examples/js/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const searchClient = algoliasearch(
'6be0576ff61c053d5f9a3225e2a90f76'
);

autocomplete<{ query: string }>({
type QuerySuggestionHit = { query: string };

autocomplete<QuerySuggestionHit>({
container: '#autocomplete',
debug: true,
// dropdownPlacement: 'start',
Expand All @@ -34,7 +36,10 @@ autocomplete<{ query: string }>({
},
templates: {
item({ item }) {
return reverseHighlightItem({ item, attribute: 'query' });
return reverseHighlightItem<QuerySuggestionHit>({
item,
attribute: 'query',
});
},
},
},
Expand Down
30 changes: 15 additions & 15 deletions packages/autocomplete-js/src/highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ function concatParts(
}, '');
}

type HighlightItemParams = {
item: any;
attribute: string;
type HighlightItemParams<TItem> = {
item: TItem;
attribute: keyof TItem;
highlightPreTag?: string;
highlightPostTag?: string;
ignoreEscape?: string[];
Expand All @@ -32,15 +32,15 @@ type HighlightItemParams = {
/**
* Highlights and escapes the matching parts of an Algolia hit.
*/
export function highlightItem({
export function highlightItem<TItem extends object>({
item,
attribute,
highlightPreTag = '<mark>',
highlightPostTag = '</mark>',
ignoreEscape,
}: HighlightItemParams) {
}: HighlightItemParams<TItem>) {
return concatParts(
parseAlgoliaHitHighlight({
parseAlgoliaHitHighlight<TItem>({
hit: item,
attribute,
ignoreEscape,
Expand All @@ -54,15 +54,15 @@ export function highlightItem({
*
* This is a common pattern for Query Suggestions.
*/
export function reverseHighlightItem({
export function reverseHighlightItem<TItem extends object>({
item,
attribute,
highlightPreTag = '<mark>',
highlightPostTag = '</mark>',
ignoreEscape,
}: HighlightItemParams) {
}: HighlightItemParams<TItem>) {
return concatParts(
parseAlgoliaHitReverseHighlight({
parseAlgoliaHitReverseHighlight<TItem>({
hit: item,
attribute,
ignoreEscape,
Expand All @@ -74,15 +74,15 @@ export function reverseHighlightItem({
/**
* Highlights and escapes the matching parts of an Algolia hit snippet.
*/
export function snippetItem({
export function snippetItem<TItem extends object>({
item,
attribute,
highlightPreTag = '<mark>',
highlightPostTag = '</mark>',
ignoreEscape,
}: HighlightItemParams) {
}: HighlightItemParams<TItem>) {
return concatParts(
parseAlgoliaHitSnippet({
parseAlgoliaHitSnippet<TItem>({
hit: item,
attribute,
ignoreEscape,
Expand All @@ -96,15 +96,15 @@ export function snippetItem({
*
* This is a common pattern for Query Suggestions.
*/
export function reverseSnippetItem({
export function reverseSnippetItem<TItem extends object>({
item,
attribute,
highlightPreTag = '<mark>',
highlightPostTag = '</mark>',
ignoreEscape,
}: HighlightItemParams) {
}: HighlightItemParams<TItem>) {
return concatParts(
parseAlgoliaHitReverseSnippet({
parseAlgoliaHitReverseSnippet<TItem>({
hit: item,
attribute,
ignoreEscape,
Expand Down

0 comments on commit b178487

Please sign in to comment.