Skip to content

Commit

Permalink
fix #392 - Return array values properly for getJSONPathValue
Browse files Browse the repository at this point in the history
Closes #392
  • Loading branch information
nancy-dassana committed Jul 19, 2021
1 parent 5769738 commit 24dbd3d
Showing 1 changed file with 14 additions and 3 deletions.
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 24dbd3d

Please sign in to comment.