JQ-Lite
view release on metacpan or search on metacpan
lib/JQ/Lite.pm view on Meta::CPAN
=item * C<.array | map(.field) | join(", ")> to transform and format array values.
=back
=head2 Conditionals
=over 4
=item * C<if CONDITION then FILTER [elif CONDITION then FILTER ...] [else FILTER] end> for jq-style branching.
Evaluates conditions as filters against the current input. The first truthy branch emits its result; optional C<elif> clauses cascade additional tests, and the optional C<else> filter only runs when no prior branch matches. Without an C<else> clause ...
Example:
if .score >= 90 then "A"
elif .score >= 80 then "B"
else "C"
end
=back
t/if_then_else.t view on Meta::CPAN
is_deeply(\@passed, ['Alice'], 'if/then/else emits the true branch when condition is truthy');
my @failed = $jq->run_query($student, 'if .score > 100 then .score else "retry" end');
is_deeply(\@failed, ['retry'], 'if/then/else falls back to else when condition is falsey');
my @no_else = $jq->run_query($student, 'if .score > 100 then .score end');
ok(!@no_else, 'if/then end without else produces no results when condition is false');
my $second = q({"name":"Bob","score":55});
my @graded = $jq->run_query($second, 'if .score >= 70 then "A" elif .score >= 60 then "B" elif .score >= 50 then "C" else "D" end');
is_deeply(\@graded, ['C'], 'elif clauses cascade until one succeeds');
my @nested = $jq->run_query($student, 'if .score >= 70 then if .score > 90 then "A" else "B" end else "C" end');
is_deeply(\@nested, ['B'], 'nested if expressions are parsed recursively');
done_testing;
( run in 0.652 second using v1.01-cache-2.11-cpan-9581c071862 )