From 24dbd3ded11f5c5b97c79a29950800d8a81eaf02 Mon Sep 17 00:00:00 2001 From: github-actions Date: Mon, 19 Jul 2021 13:59:42 -0700 Subject: [PATCH] fix #392 - Return array values properly for getJSONPathValue Closes #392 --- src/components/utils.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/components/utils.ts b/src/components/utils.ts index 1ea3298c..0a80a807 100644 --- a/src/components/utils.ts +++ b/src/components/utils.ts @@ -126,16 +126,27 @@ export const getDataTestAttributeProp = ( [TAG]: dataTag ? `${cmpName}-${dataTag}` : cmpName }) -export const getJSONPathValue = ( +export const getJSONPathValue = ( path: string, obj: Record ) => { - const value = JSONPath({ + const value = JSONPath({ 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 } /**