JQ-Lite
view release on metacpan or search on metacpan
lib/JQ/Lite/Util/Transform.pm view on Meta::CPAN
if (JSON::PP::is_bool($value)) {
$value = $value ? 1 : 0;
}
return undef if ref $value;
return undef unless looks_like_number($value);
my $fraction = 0 + $value;
return undef if $fraction != $fraction; # NaN
return undef if ($fraction * 0) != ($fraction * 0); # infinity
if ($fraction > 1) {
$fraction /= 100 if $fraction <= 100;
}
$fraction = 0 if $fraction < 0;
$fraction = 1 if $fraction > 1;
return $fraction;
t/percentile.t view on Meta::CPAN
my $json_mixed = q(["foo", 10, "20", null]);
my ($p_mixed) = $jq->run_query($json_mixed, 'percentile(50)');
is($p_mixed, 15, 'percentile ignores non-numeric values but keeps numeric strings');
my $json_invalid = q(["a", "b"]);
my ($p_invalid_arg) = $jq->run_query($json_invalid, 'percentile("oops")');
ok(!defined $p_invalid_arg, 'percentile returns undef when the argument is not numeric');
my ($p_nan_arg) = $jq->run_query($json_ordered, 'percentile("NaN")');
ok(!defined $p_nan_arg, 'percentile returns undef when the argument is NaN');
my ($p_inf_arg) = $jq->run_query($json_ordered, 'percentile("Infinity")');
ok(!defined $p_inf_arg, 'percentile returns undef when the argument is infinite');
my ($p_inf_negative) = $jq->run_query($json_ordered, 'percentile("-Infinity")');
ok(!defined $p_inf_negative, 'percentile returns undef when the argument is negative infinity');
my ($p_no_numeric) = $jq->run_query($json_invalid, 'percentile(10)');
ok(!defined $p_no_numeric, 'percentile returns undef when no numeric values are present');
( run in 2.552 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )