Stats API: do not convert arrays to objects#21053
Conversation
💔 Build Failed |
| days_of_the_week: ['monday', 'tuesday', 'wednesday'], | ||
| }); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
@tsullivan Could you write a test that uses an array of objects whose keys might need changing? So, for example:
const apiData = {
daysOfTheWeek: [
{
dayName: 'monday',
dayIndex: 1
},
{
dayName: 'tuesday',
dayIndex: 2
},
{
dayName: 'wednesday',
dayIndex: 3
}
]
}The expected result would be:
{
days_of_the_week: [
{
day_name: 'monday',
day_index: 1
},
{
day_name: 'tuesday',
day_index: 2
},
{
day_name: 'wednesday',
day_index: 3
}
]
}There was a problem hiding this comment.
Sure. That would not work with the code as it is. I stopped implementation on this before I started worrying about objects nested in arrays. But it is a fair ask.
| }; | ||
| }, {}); | ||
| } else { | ||
| return apiData; |
There was a problem hiding this comment.
Per the comments from @ycombinator about the test, it seems weird that we aren't doing any manipulation if it's an array since it seems reasonable it could be an array of objects that need to be manipulated?
| }, {}); | ||
| if (Array.isArray(apiData)) { | ||
| return apiData.map(getValueOrRecurse); | ||
| } else { |
There was a problem hiding this comment.
Since you are returning from the if above, you don't necessarily need the else here. Somewhat related: https://github.com/elastic/kibana/blob/master/style_guides/js_style_guide.md#returnthrow-early-from-functions.
ycombinator
left a comment
There was a problem hiding this comment.
One minor comment, otherwise LGTM.
💚 Build Succeeded |
* Stats API: do not convert arrays to objects * handle nested arrays in toApiFieldNames * add early return
This PR solves a problem where the
toApiFieldNamesmethod was converting arrays in the data to objects.For example:
would become
The changes in this PR handle arrays nested with object data as well.