Skip to content

Commit

Permalink
feat: add option to return multiple matches for JSON capture (#3311)
Browse files Browse the repository at this point in the history
  • Loading branch information
Archangelza1 authored Oct 15, 2024
1 parent 032408b commit de79a42
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/commons/engine_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,7 @@ function templateObjectOrArray(o, context) {
debug(
`path = ${path} ; value = ${JSON.stringify(
value
)} (${typeof value}) ; (subj type: ${
subj.length ? 'list' : 'hash'
)} (${typeof value}) ; (subj type: ${subj.length ? 'list' : 'hash'
}) ; newValue = ${JSON.stringify(newValue)} ; newPath = ${newPath}`
);

Expand Down Expand Up @@ -572,7 +571,7 @@ function dummyParser(body, callback) {
}

// doc is a JSON object
function extractJSONPath(doc, expr) {
function extractJSONPath(doc, expr, opts) {
// typeof null is 'object' hence the explicit check here
if (typeof doc !== 'object' || doc === null) {
return '';
Expand All @@ -581,7 +580,7 @@ function extractJSONPath(doc, expr) {
let results;

try {
results = jsonpath(expr, doc);
results = jsonpath({ path: expr, json: doc, wrap: opts.multiple ?? true });
} catch (queryErr) {
debug(queryErr);
}
Expand All @@ -590,6 +589,10 @@ function extractJSONPath(doc, expr) {
return '';
}

if (opts.multiple === false) {
return results;
}

if (results.length > 1) {
return results[randomInt(0, results.length - 1)];
} else {
Expand Down

0 comments on commit de79a42

Please sign in to comment.