Skip to content

Commit

Permalink
fix(core): allow number index for hit attribute #1261 (#1262)
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenquimby authored Jul 8, 2024
1 parent 9da66b7 commit 0ae0c5c
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dist/

# IDE
.vscode/
.idea/

# Environment files
.env
Expand Down
2 changes: 1 addition & 1 deletion examples/react-17/src/Highlight.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type HighlightHitParams<THit> = {
*
* You can use the array syntax to reference nested attributes.
*/
attribute: keyof THit | string[];
attribute: keyof THit | (string | number)[];
/**
* The tag name to use for highlighted parts.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ function AccountItem({ hit }: AccountItemProps) {

type HighlightParams<THit> = {
hit: THit;
attribute: keyof THit | string[];
attribute: keyof THit | (string | number)[];
};

function Highlight<THit>({ hit, attribute }: HighlightParams<THit>) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type ParseAlgoliaHitParams<TItem> = {
hit: TItem;
attribute: keyof TItem | string[];
attribute: keyof TItem | (string | number)[];
};
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,35 @@ describe('parseAlgoliaHitHighlight', () => {
]);
});

test('returns the highlighted parts of the hit with an array result', () => {
expect(
parseAlgoliaHitHighlight({
attribute: ['titles', 1],
hit: {
objectID: '1',
titles: ['Hello', 'world'],
_highlightResult: {
titles: [{
value: 'Hello',
matchLevel: 'none',
matchedWords: [],
}, {
value: '__aa-highlight__world__/aa-highlight__',
matchLevel: 'full',
matchedWords: ['world'],
fullyHighlighted: true,
}]
},
},
})
).toEqual([
{
isHighlighted: true,
value: 'world',
},
]);
});

test('returns the attribute value if the attribute cannot be highlighted', () => {
expect(
parseAlgoliaHitHighlight({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('parseAlgoliaHitReverseHighlight', () => {
]);
});

test('returns the highlighted parts of the hit with a nested array attribute containing a dot', () => {
test('returns the reverse-highlighted parts of the hit with a nested array attribute containing a dot', () => {
expect(
parseAlgoliaHitReverseHighlight({
attribute: ['hierarchy', 'lvl0.inside'],
Expand Down Expand Up @@ -164,6 +164,35 @@ describe('parseAlgoliaHitReverseHighlight', () => {
]);
});

test('returns the reverse-highlighted parts of the hit with an array result', () => {
expect(
parseAlgoliaHitReverseHighlight({
attribute: ['titles', 1],
hit: {
objectID: '1',
titles: ['Hello', 'world'],
_highlightResult: {
titles: [{
value: 'Hello',
matchLevel: 'none',
matchedWords: [],
}, {
value: '__aa-highlight__world__/aa-highlight__',
matchLevel: 'full',
matchedWords: ['world'],
fullyHighlighted: true,
}]
},
},
})
).toEqual([
{
isHighlighted: false,
value: 'world',
},
]);
});

test('returns the non-highlighted parts when every part matches', () => {
expect(
parseAlgoliaHitReverseHighlight({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('parseAlgoliaHitReverseSnippet', () => {
]);
});

test('returns the highlighted snippet parts of the hit with a nested array attribute containing a dot', () => {
test('returns the reverse-highlighted snippet parts of the hit with a nested array attribute containing a dot', () => {
expect(
parseAlgoliaHitReverseSnippet({
attribute: ['hierarchy', 'lvl0.inside'],
Expand Down Expand Up @@ -155,6 +155,32 @@ describe('parseAlgoliaHitReverseSnippet', () => {
]);
});

test('returns the reverse-highlighted snippet parts of the hit with an array result', () => {
expect(
parseAlgoliaHitReverseSnippet({
attribute: ['titles', 1],
hit: {
objectID: '1',
titles: ['Hello', 'world'],
_snippetResult: {
titles: [{
value: 'Hello',
matchLevel: 'none',
}, {
value: '__aa-highlight__world__/aa-highlight__',
matchLevel: 'full',
}]
},
},
})
).toEqual([
{
isHighlighted: false,
value: 'world',
},
]);
});

test('returns the non-highlighted parts when every part matches', () => {
expect(
parseAlgoliaHitReverseSnippet({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,32 @@ describe('parseAlgoliaHitSnippet', () => {
]);
});

test('returns the highlighted snippet parts of the hit with an array result', () => {
expect(
parseAlgoliaHitSnippet({
attribute: ['titles', 1],
hit: {
objectID: '1',
titles: ['Hello', 'world'],
_snippetResult: {
titles: [{
value: 'Hello',
matchLevel: 'none',
}, {
value: '__aa-highlight__world__/aa-highlight__',
matchLevel: 'full',
}]
},
},
})
).toEqual([
{
isHighlighted: true,
value: 'world',
},
]);
});

test('returns the attribute value if the attribute cannot be snippeted', () => {
expect(
parseAlgoliaHitSnippet({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export function getAttributeValueByPath<TRecord>(
record: TRecord,
path: string[]
path: (string | number)[]
): any {
return path.reduce((current, key) => current && current[key], record);
}
2 changes: 1 addition & 1 deletion packages/autocomplete-shared/src/js/HighlightHitParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type HighlightHitParams<THit> = {
*
* You can use the array syntax to reference nested attributes.
*/
attribute: keyof THit | string[];
attribute: keyof THit | (string | number)[];
/**
* The tag name to use for highlighted parts.
*
Expand Down

0 comments on commit 0ae0c5c

Please sign in to comment.