blib/lib/Hash/Squash.pm

filter: Statementstmt 100% 63 / 63 Branchbran 96.1% 25 / 26 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 99.0% 103 / 104 CC 16 SCAR 17.3
CC
16
Cov
99%
_squash_hash
9.0
_squash_array
7.0
_squash
2.0
CRAP 5.6
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Hash::Squash;
4 # ABSTRACT: Remove numbered keys from a nested hash/array
5  
6 1 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use List::Util qw/max/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use version; our $VERSION = version->declare("v0.0.4");
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our @EXPORT_OK = qw(squash unnumber);
12  
13 2 ▶sub squash { _squash(shift) }
Subroutine squash: called CC=1 SCAR=0.0
Pod documented
14 1 ▶sub unnumber { _squash(shift, { keep_empty => 1 }) }
Subroutine unnumber: called CC=1 SCAR=0.0
Pod documented
15  
16 124 ▶sub _squash {
Subroutine _squash: called CC=2 SCAR=6.9
17 124 my ($obj, $arg) = @_;
18 124 ▶ return $obj unless ref $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj 93 31 124
19  
20 31 $obj = _squash_hash($obj, $arg);
21 31 $obj = _squash_array($obj, $arg);
22  
23 31 return $obj;
24 }
25  
26 31 ▶sub _squash_hash {
Subroutine _squash_hash: called CC=9 SCAR=22.0
27 31 my ($obj, $arg) = @_;
28 31 ▶ return $obj if ref $obj ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $obj ne "HASH" 7 24 31
29  
30 EMPTY_HASH: {
31 24 ▶ last EMPTY_HASH if %{$obj};
Branch: 1 branch
BranchTrueFalseTotal
if %{$obj;} 22 2 24
32 2 ▶ return (undef) unless exists $arg->{keep_empty};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $arg->{"keep_empty"} 1 1 2
33 1 ▶ return (undef) unless $arg->{keep_empty};
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->{"keep_empty"} 0 1 1
34 1 return +{};
35 }
36  
37 22 my @keys = keys %{$obj};
38  
39 CONTAINS_NON_NUMERIC_KEYS: {
40 22 ▶ last CONTAINS_NON_NUMERIC_KEYS unless grep {/\D/} @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {/\D/;} @keys 12 10 22
41 10 my %hash = map { $_ => _squash($obj->{$_}, $arg) } @keys;
42 10 return \%hash;
43 }
44  
45 12 ▶ my $max = max(@keys) || 0;
Condition100%2/2
&max(@keys) || 0
l!l
75
MC/DC100%1/1
&max(@keys) || 0
&max(@keys)
Truth table100%2/2
A
A= &max(@keys)
Aresult
11
00
46  
47 12 my @ar;
48 12 for my $i (0 .. $max) {
49 #
50 # Some numbered keys might be partially discreated
51 #
52 44 ▶ push @ar, exists $obj->{$i} ? _squash($obj->{$i}, $arg) : (undef);
Branch: 1 branch
BranchTrueFalseTotal
exists $obj->{$i} ? : 41 3 44
53 }
54  
55 12 return \@ar;
56 }
57  
58 31 ▶sub _squash_array {
Subroutine _squash_array: called CC=7 SCAR=19.5
59 31 my ($obj, $arg) = @_;
60 31 ▶ return $obj if ref $obj ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $obj ne "ARRAY" 12 19 31
61  
62 19 ▶ my $keep_empty = exists $arg->{keep_empty} ? $arg->{keep_empty} : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $arg->{'keep_empty'} ? : 8 11 19
63  
64 EMPTY_ARRAY: {
65 19 ▶ last EMPTY_ARRAY if @{$obj} != 0;
Branch: 1 branch
BranchTrueFalseTotal
if @{$obj;} != 0 17 2 19
66 2 ▶ return (undef) unless $keep_empty;
Branch: 1 branch
BranchTrueFalseTotal
unless $keep_empty 1 1 2
67 1 return [];
68 }
69  
70 SINGLE_ELEMENT: {
71 17 ▶ last SINGLE_ELEMENT if @{$obj} != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$obj;} != 1 10 7 17
72 7 ▶ last SINGLE_ELEMENT if $keep_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $keep_empty 3 4 7
73 4 return _squash($obj->[0]);
74 }
75  
76 13 my @array = map { _squash($_, $arg) } @{$obj};
77  
78 13 return \@array;
79 }
80  
81 1 1;
82 __END__
↑ Summary