From d3f62a12312b131afb968271e90e84e6540cadf5 Mon Sep 17 00:00:00 2001 From: Alan Gutierrez Date: Sat, 28 Jul 2018 19:22:55 -0500 Subject: [PATCH] Predicate functions return cadidate if true. Instead of returning true or false, the predicate funcitons now return an array that contains the candidate if it matches. This will allow us to use the same function call for transforms. --- inquiry.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/inquiry.js b/inquiry.js index cf1913e..4874bf3 100644 --- a/inquiry.js +++ b/inquiry.js @@ -48,7 +48,8 @@ args.push('return ' + source + ')'); struct.push((function (predicate) { return function (candidate, vargs) { - return predicate.apply(this, [ candidate._, candidate.o, candidate.i ].concat(vargs)); + return predicate.apply(this, [ candidate._, candidate.o, candidate.i ].concat(vargs)) + ? [ candidate ] : [] } })(Function.apply(Function, args)), []); break; @@ -57,8 +58,8 @@ case "[": struct.push((function (negate, subquery) { return function (candidate, args) { - var length = subquery.call(this, candidate, [ candidate.o, candidate.i ].concat(args)).length - return negate ? ! length : length; + var length = subquery.call(this, candidate, [ candidate.o, candidate.i ].concat(args)).length; + return (negate ? ! length : length) ? [ candidate ] : []; } })($[3] == '!', ($ = parse(rest, fixup, nesting + 1, "]"))[0])); rest = $[1].slice(1); @@ -104,8 +105,8 @@ for (j = object.length - 1; j > -1; --j) { candidates.unshift({ o: object[j], _: [ j, expression[i][0] ].concat(_), p: [ object ].concat(path), i: j }); } - } else if (expression[i][1].call(this, candidate, vargs)) { - stack.push(candidate); + } else { + stack.push.apply(stack, expression[i][1].call(this, candidate, vargs)) } } } else {