| 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
| |||||||||||||
| 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
| |||||||||||||
| 29 | |||||||||||||
| 30 | EMPTY_HASH: { | ||||||||||||
| 31 | 24 | ▶ | last EMPTY_HASH if %{$obj}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 32 | 2 | ▶ | return (undef) unless exists $arg->{keep_empty}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 33 | 1 | ▶ | return (undef) unless $arg->{keep_empty}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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
MC/DC100%1/1
&max(@keys) || 0
&max(@keys)
Truth table100%2/2
A
A= &max(@keys)
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 61 | |||||||||||||
| 62 | 19 | ▶ | my $keep_empty = exists $arg->{keep_empty} ? $arg->{keep_empty} : (); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | |||||||||||||
| 64 | EMPTY_ARRAY: { | ||||||||||||
| 65 | 19 | ▶ | last EMPTY_ARRAY if @{$obj} != 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | 2 | ▶ | return (undef) unless $keep_empty; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | 1 | return []; | |||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | SINGLE_ELEMENT: { | ||||||||||||
| 71 | 17 | ▶ | last SINGLE_ELEMENT if @{$obj} != 1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | 7 | ▶ | last SINGLE_ELEMENT if $keep_empty; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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__ | ||||||||||||