Skip to content

Commit

Permalink
only loop over array if not nested
Browse files Browse the repository at this point in the history
  • Loading branch information
adlerfaulkner committed Jul 7, 2022
1 parent 5686a84 commit b339992
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/input-parser/JSONParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ class JsonParser {
getData(index, selector) {
const sel = selector.replace(/^PATH~/, '');
const splitter = sel.startsWith('[') ? '' : '.';
return JSONPath({
const arr = JSONPath({
path: `${this.paths[index]}${splitter}${sel}`,
json: this.json,
resultType: selector.startsWith('PATH~') ? 'pointer' : 'value',
})
.filter((e) => e !== null && e !== undefined) // null values are ignored (undefined shouldn't happens since input is json)
.map((e) => {
if (Array.isArray(e)) {
return e.map((eItem) => eItem.toString());
}
return e.toString();
}); // return only strings
.filter((e) => e !== null && e !== undefined); // null values are ignored (undefined shouldn't happens since input is json)

if (arr.length === 1 && Array.isArray(arr[0])) {
return arr[0].map((e) => e.toString());
}
return arr.map((e) => e.toString());
}
}

Expand Down

0 comments on commit b339992

Please sign in to comment.