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

fix(core): allow number index for hit attribute #1261 #1262

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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)[];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative would be to add @algolia/autocomplete-shared as a dependency in this example project, and import the existing type, so this doesn't need to be duplicated

/**
* 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)[];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative, if preferred, could be to change this type to:

export type ParseAlgoliaHitParams<TItem> = Pick<HighlightHitParams<TItem>, 'hit' | 'attribute'>;

};
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