lib/JQ/Lite/Value.pm

filter: Statementstmt 96.8% 61 / 63 Branchbran 79.5% 35 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 3 totaltotal 88.1% 104 / 118 CC 22 SCAR 23.1
CC
22
Cov
90%
compare
14.0
type_of
9.9
BEGIN
1.0
CRAP 10.0
« bin/jq-lite ↑ Summary blib/lib/JQ/Lite/Util.pm »
1 package JQ::Lite::Value;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # Older B releases do not expose the public SV flag constants. The flag
11 # values themselves are part of Perl's long-standing SV layout, so retain
12 # their historical values when running with one of those releases.
13 1 ▶my $SVf_IOK = B->can('SVf_IOK') ? B::SVf_IOK() : 0x00000100;
Branch: 1 branch
BranchTrueFalseTotal
'B'->can('SVf_IOK') ? : 1 0 1
14 1 ▶my $SVf_NOK = B->can('SVf_NOK') ? B::SVf_NOK() : 0x00000200;
Branch: 1 branch
BranchTrueFalseTotal
'B'->can('SVf_NOK') ? : 1 0 1
15 1 ▶my $SVf_POK = B->can('SVf_POK') ? B::SVf_POK() : 0x00000400;
Branch: 1 branch
BranchTrueFalseTotal
'B'->can('SVf_POK') ? : 1 0 1
16  
17 # This module is deliberately internal. It provides the jq-style value
18 # semantics needed by the 3.0 evaluator without changing the compatibility
19 # behaviour of the 2.x filters that still live in JQ::Lite::Util.
20  
21 54 ▶sub type_of {
Subroutine type_of: called CC=9 SCAR=22.9
Pod undocumented
22 54 my ($value) = @_;
23  
24 54 ▶ return 'null' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 53 54
25 53 ▶ return 'boolean' if JSON::PP::is_bool($value);
Branch: 1 branch
BranchTrueFalseTotal
if JSON::PP::is_bool($value) 4 49 53
26 49 ▶ return 'array' if ref($value) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 13 36 49
27 36 ▶ return 'object' if ref($value) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 7 29 36
28 29 ▶ if (!ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value) 29 0 29
29 29 my $flags = B::svref_2object(\$value)->FLAGS;
30 # Test the public numeric flags first. Older Perl releases may promote
31 # the cached PV to SVf_POK during interpolation even though IOK/NOK is
32 # still present. Numeric identity therefore wins for dual-valued
33 # scalars regardless of how the bundled JSON::PP treats dualvars.
34 29 ▶ return 'number' if $flags & ($SVf_IOK | $SVf_NOK);
Branch: 1 branch
BranchTrueFalseTotal
if $flags & ($SVf_IOK | $SVf_NOK) 21 8 29
35 8 ▶ return 'string' if $flags & $SVf_POK;
Branch: 1 branch
BranchTrueFalseTotal
if $flags & $SVf_POK 8 0 8
36 0 ▶ return 'number' if looks_like_number($value);
Branch: 1 branch
BranchTrueFalseTotal
if &looks_like_number($value) 0 0 0
37 }
38 0 return 'string';
39 }
40  
41 23 ▶sub compare {
Subroutine compare: called CC=14 SCAR=26.4
Pod undocumented
42 23 my ($left, $right) = @_;
43  
44 23 my %rank = (
45 null => 0,
46 boolean => 1,
47 number => 2,
48 string => 3,
49 array => 4,
50 object => 5,
51 );
52  
53 23 my $left_type = type_of($left);
54 23 my $right_type = type_of($right);
55 23 ▶ return $rank{$left_type} <=> $rank{$right_type}
Branch: 1 branch
BranchTrueFalseTotal
if $left_type ne $right_type 2 21 23
56 if $left_type ne $right_type;
57  
58 21 ▶ return 0 if $left_type eq 'null';
Branch: 1 branch
BranchTrueFalseTotal
if $left_type eq "null" 0 21 21
59 21 ▶ return (!!$left) <=> (!!$right) if $left_type eq 'boolean';
Branch: 1 branch
BranchTrueFalseTotal
if $left_type eq "boolean" 1 20 21
60 20 ▶ return $left <=> $right if $left_type eq 'number';
Branch: 1 branch
BranchTrueFalseTotal
if $left_type eq "number" 8 12 20
61 12 ▶ return $left cmp $right if $left_type eq 'string';
Branch: 1 branch
BranchTrueFalseTotal
if $left_type eq "string" 3 9 12
62  
63 9 ▶ if ($left_type eq 'array') {
Branch: 1 branch
BranchTrueFalseTotal
if ($left_type eq "array") 6 3 9
64 6 ▶ my $limit = @{$left} < @{$right} ? @{$left} : @{$right};
Branch: 1 branch
BranchTrueFalseTotal
@{$left;} < @{$right;} ? : 1 5 6
65 6 ▶ for my $index ($limit ? (0 .. $limit - 1) : ()) {
Branch: 1 branch
BranchTrueFalseTotal
$limit ? : 6 0 6
66 9 my $ordering = compare($left->[$index], $right->[$index]);
67 9 ▶ return $ordering if $ordering;
Branch: 1 branch
BranchTrueFalseTotal
if $ordering 2 7 9
68 }
69 4 return @{$left} <=> @{$right};
70 }
71  
72 3 my @left_keys = sort keys %{$left};
73 3 my @right_keys = sort keys %{$right};
74 3 my $key_ordering = compare(\@left_keys, \@right_keys);
75 3 ▶ return $key_ordering if $key_ordering;
Branch: 1 branch
BranchTrueFalseTotal
if $key_ordering 1 2 3
76 2 for my $key (@left_keys) {
77 2 my $ordering = compare($left->{$key}, $right->{$key});
78 2 ▶ return $ordering if $ordering;
Branch: 1 branch
BranchTrueFalseTotal
if $ordering 1 1 2
79 }
80 1 return 0;
81 }
82  
83 4 ▶sub equal {
Subroutine equal: called CC=1 SCAR=0.0
Pod undocumented
84 4 my ($left, $right) = @_;
85 4 return compare($left, $right) == 0;
86 }
87  
88 1 1;
« bin/jq-lite ↑ Summary blib/lib/JQ/Lite/Util.pm »