Skip to content

Commit 0ae0c5c

Browse files
authored
fix(core): allow number index for hit attribute #1261 (#1262)
1 parent 9da66b7 commit 0ae0c5c

File tree

10 files changed

+118
-7
lines changed

10 files changed

+118
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ dist/
1212

1313
# IDE
1414
.vscode/
15+
.idea/
1516

1617
# Environment files
1718
.env

examples/react-17/src/Highlight.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type HighlightHitParams<THit> = {
1111
*
1212
* You can use the array syntax to reference nested attributes.
1313
*/
14-
attribute: keyof THit | string[];
14+
attribute: keyof THit | (string | number)[];
1515
/**
1616
* The tag name to use for highlighted parts.
1717
*

examples/twitter-compose-with-typeahead/src/Autocomplete.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ function AccountItem({ hit }: AccountItemProps) {
235235

236236
type HighlightParams<THit> = {
237237
hit: THit;
238-
attribute: keyof THit | string[];
238+
attribute: keyof THit | (string | number)[];
239239
};
240240

241241
function Highlight<THit>({ hit, attribute }: HighlightParams<THit>) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export type ParseAlgoliaHitParams<TItem> = {
22
hit: TItem;
3-
attribute: keyof TItem | string[];
3+
attribute: keyof TItem | (string | number)[];
44
};

packages/autocomplete-preset-algolia/src/highlight/__tests__/parseAlgoliaHitHighlight.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,35 @@ describe('parseAlgoliaHitHighlight', () => {
167167
]);
168168
});
169169

170+
test('returns the highlighted parts of the hit with an array result', () => {
171+
expect(
172+
parseAlgoliaHitHighlight({
173+
attribute: ['titles', 1],
174+
hit: {
175+
objectID: '1',
176+
titles: ['Hello', 'world'],
177+
_highlightResult: {
178+
titles: [{
179+
value: 'Hello',
180+
matchLevel: 'none',
181+
matchedWords: [],
182+
}, {
183+
value: '__aa-highlight__world__/aa-highlight__',
184+
matchLevel: 'full',
185+
matchedWords: ['world'],
186+
fullyHighlighted: true,
187+
}]
188+
},
189+
},
190+
})
191+
).toEqual([
192+
{
193+
isHighlighted: true,
194+
value: 'world',
195+
},
196+
]);
197+
});
198+
170199
test('returns the attribute value if the attribute cannot be highlighted', () => {
171200
expect(
172201
parseAlgoliaHitHighlight({

packages/autocomplete-preset-algolia/src/highlight/__tests__/parseAlgoliaHitReverseHighlight.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ describe('parseAlgoliaHitReverseHighlight', () => {
122122
]);
123123
});
124124

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

167+
test('returns the reverse-highlighted parts of the hit with an array result', () => {
168+
expect(
169+
parseAlgoliaHitReverseHighlight({
170+
attribute: ['titles', 1],
171+
hit: {
172+
objectID: '1',
173+
titles: ['Hello', 'world'],
174+
_highlightResult: {
175+
titles: [{
176+
value: 'Hello',
177+
matchLevel: 'none',
178+
matchedWords: [],
179+
}, {
180+
value: '__aa-highlight__world__/aa-highlight__',
181+
matchLevel: 'full',
182+
matchedWords: ['world'],
183+
fullyHighlighted: true,
184+
}]
185+
},
186+
},
187+
})
188+
).toEqual([
189+
{
190+
isHighlighted: false,
191+
value: 'world',
192+
},
193+
]);
194+
});
195+
167196
test('returns the non-highlighted parts when every part matches', () => {
168197
expect(
169198
parseAlgoliaHitReverseHighlight({

packages/autocomplete-preset-algolia/src/highlight/__tests__/parseAlgoliaHitReverseSnippet.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ describe('parseAlgoliaHitReverseSnippet', () => {
116116
]);
117117
});
118118

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

158+
test('returns the reverse-highlighted snippet parts of the hit with an array result', () => {
159+
expect(
160+
parseAlgoliaHitReverseSnippet({
161+
attribute: ['titles', 1],
162+
hit: {
163+
objectID: '1',
164+
titles: ['Hello', 'world'],
165+
_snippetResult: {
166+
titles: [{
167+
value: 'Hello',
168+
matchLevel: 'none',
169+
}, {
170+
value: '__aa-highlight__world__/aa-highlight__',
171+
matchLevel: 'full',
172+
}]
173+
},
174+
},
175+
})
176+
).toEqual([
177+
{
178+
isHighlighted: false,
179+
value: 'world',
180+
},
181+
]);
182+
});
183+
158184
test('returns the non-highlighted parts when every part matches', () => {
159185
expect(
160186
parseAlgoliaHitReverseSnippet({

packages/autocomplete-preset-algolia/src/highlight/__tests__/parseAlgoliaHitSnippet.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,32 @@ describe('parseAlgoliaHitSnippet', () => {
155155
]);
156156
});
157157

158+
test('returns the highlighted snippet parts of the hit with an array result', () => {
159+
expect(
160+
parseAlgoliaHitSnippet({
161+
attribute: ['titles', 1],
162+
hit: {
163+
objectID: '1',
164+
titles: ['Hello', 'world'],
165+
_snippetResult: {
166+
titles: [{
167+
value: 'Hello',
168+
matchLevel: 'none',
169+
}, {
170+
value: '__aa-highlight__world__/aa-highlight__',
171+
matchLevel: 'full',
172+
}]
173+
},
174+
},
175+
})
176+
).toEqual([
177+
{
178+
isHighlighted: true,
179+
value: 'world',
180+
},
181+
]);
182+
});
183+
158184
test('returns the attribute value if the attribute cannot be snippeted', () => {
159185
expect(
160186
parseAlgoliaHitSnippet({
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export function getAttributeValueByPath<TRecord>(
22
record: TRecord,
3-
path: string[]
3+
path: (string | number)[]
44
): any {
55
return path.reduce((current, key) => current && current[key], record);
66
}

packages/autocomplete-shared/src/js/HighlightHitParams.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type HighlightHitParams<THit> = {
88
*
99
* You can use the array syntax to reference nested attributes.
1010
*/
11-
attribute: keyof THit | string[];
11+
attribute: keyof THit | (string | number)[];
1212
/**
1313
* The tag name to use for highlighted parts.
1414
*

0 commit comments

Comments
 (0)