Skip to content

Commit

Permalink
fix #392 - Return array values properly for getJSONPathValue (#393)
Browse files Browse the repository at this point in the history
Closes #392
  • Loading branch information
nancy-dassana authored Jul 19, 2021
1 parent 0083a5a commit 27fee5e
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 45 deletions.
1 change: 1 addition & 0 deletions rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default {
'react-helmet',
'react-hook-form',
'react-jss',
'react-scroll',
'remark-gfm',
'react-markdown',
'typescript',
Expand Down
16 changes: 11 additions & 5 deletions src/components/Table/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ export function processData<TableData extends DataId>(
columns.forEach(col => {
const { dataIndex } = col

const value = getJSONPathValue(`$.${dataIndex}`, item)
const value = getJSONPathValue(
`$.${dataIndex}`,
item
) as ProcessedData<TableData>[keyof TableData]

if (value) {
partialData[dataIndex as keyof TableData] = value
Expand Down Expand Up @@ -186,9 +189,9 @@ const compareIcons =
? `$.${dataIndex}.${iconKey}`
: `$.${dataIndex}`

const compareValA = getJSONPathValue(jsonPath, a) || ''
const compareValA = getJSONPathValue(jsonPath, a)?.toString() || ''

const compareValB = getJSONPathValue(jsonPath, b) || ''
const compareValB = getJSONPathValue(jsonPath, b)?.toString() || ''

return compareValA.localeCompare(compareValB)
}
Expand Down Expand Up @@ -299,7 +302,7 @@ const getIconOrIconKey = (
jsonPath: string
): string | IconName | undefined => {
if (typeof record === 'object') {
const value = getJSONPathValue(jsonPath, record)
const value = getJSONPathValue(jsonPath, record)?.toString()

if (value) return value
} else return record
Expand Down Expand Up @@ -505,7 +508,10 @@ function applyRender<TableData extends DataId>(
const labelVal =
typeof record === 'string'
? labelKey
: getJSONPathValue(jsonPath, record)
: (getJSONPathValue(
jsonPath,
record
) as string) // eslint-disable-line no-mixed-spaces-and-tabs

return (
<IconCell
Expand Down
212 changes: 175 additions & 37 deletions src/components/assets/images/404_error_2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 14 additions & 3 deletions src/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,27 @@ export const getDataTestAttributeProp = (
[TAG]: dataTag ? `${cmpName}-${dataTag}` : cmpName
})

export const getJSONPathValue = (
export const getJSONPathValue = <T = string>(
path: string,
obj: Record<string, JSONValue>
) => {
const value = JSONPath({
const value = JSONPath<T>({
json: obj,
path
})

if (value && Array.isArray(value)) return value[0]
if (Array.isArray(value)) {
switch (value.length) {
case 0:
return undefined
case 1:
return value[0] as T
default:
return value as T[]
}
}

return value
}

/**
Expand Down

0 comments on commit 27fee5e

Please sign in to comment.