| 1 | package Zwoelf::Hash::Union; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 2 | our $VERSION = 0.1; | |||||||||||||
| 4 | |||||||||||||||
| 5 | 2 | require Exporter; | |||||||||||||
| 6 | 2 | @ISA = qw(Exporter); | |||||||||||||
| 7 | 2 | @EXPORT_OK = qw( merge_hash unique_array ); | |||||||||||||
| 8 | |||||||||||||||
| 9 | 2 | ▶ | use Test::Deep::NoTest; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 2 | ▶ | use List::Util qw(reduce none); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 2 | ▶ | use Hash::Merge; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | =head1 NAME | ||||||||||||||
| 14 | |||||||||||||||
| 15 | Zwoelf::Hash::Union | ||||||||||||||
| 16 | |||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||
| 18 | |||||||||||||||
| 19 | Work in progress: Menat to merge to hashes together like Hash::Merge BUT | ||||||||||||||
| 20 | will produce unions of any arrays that occur under the same key | ||||||||||||||
| 21 | |||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||
| 23 | |||||||||||||||
| 24 | use Zwoelf::Hash::Union qw( merge_hash unique_array ); | ||||||||||||||
| 25 | |||||||||||||||
| 26 | unique_array([ {a=>1}, {b=>2}, {c=>3}, {b=>2} ]) | ||||||||||||||
| 27 | # yields | ||||||||||||||
| 28 | [{ a => 1 }, { b => 2 }, { c => 3 }]; | ||||||||||||||
| 29 | |||||||||||||||
| 30 | merge_hash( | ||||||||||||||
| 31 | { a => 1, b => [{ b => 2 }, { c => 3 }] }, | ||||||||||||||
| 32 | { a => 2, b => [{ b => 2 }, { d => 4 }] }, | ||||||||||||||
| 33 | ); | ||||||||||||||
| 34 | # yields | ||||||||||||||
| 35 | { a => 2, b => [{ b => 2}, { c => 3 }, { d => 4 }] }, | ||||||||||||||
| 36 | |||||||||||||||
| 37 | merge_hash( | ||||||||||||||
| 38 | { a => 1, b => [{ b => 2 }, { c => 3 }] }, | ||||||||||||||
| 39 | { a => 2, b => [{ b => 2 }, { d => 4 }] }, | ||||||||||||||
| 40 | 'LEFT_PRECEDENT' | ||||||||||||||
| 41 | ); | ||||||||||||||
| 42 | # yields | ||||||||||||||
| 43 | { a => 1, b => [{ b => 2}, { c => 3 }, { d => 4 }] }, | ||||||||||||||
| 44 | |||||||||||||||
| 45 | =cut | ||||||||||||||
| 46 | |||||||||||||||
| 47 | 10 | ▶ | sub unique_array { | ||||||||||||
|
Subroutine
unique_array: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 48 | 10 | my ( $array ) = @_; | |||||||||||||
| 49 | return | ||||||||||||||
| 50 | 50 | ▶ | reduce { push @$a, $b if none { eq_deeply( $_, $b) } @$a ; $a } | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||
| 51 | 10 | ( [], @$array ) | |||||||||||||
| 52 | ; | ||||||||||||||
| 53 | } | ||||||||||||||
| 54 | |||||||||||||||
| 55 | 6 | ▶ | sub merge_hash { | ||||||||||||
|
Subroutine
merge_hash: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 56 | 6 | my ( $a, $b, $precedence ) = @_; | |||||||||||||
| 57 | 6 | ▶ | my $merge = Hash::Merge->new( $precedence || 'RIGHT_PRECEDENT' ); | ||||||||||||
|
Condition100%2/2
$precedence || 'RIGHT_PRECEDENT'
MC/DC100%1/1
$precedence || 'RIGHT_PRECEDENT'
$precedence
Truth table100%2/2
A
A= $precedence
| |||||||||||||||
| 58 | 6 | my $merged = $merge->merge( $a, $b ); | |||||||||||||
| 59 | 6 | return unify_hash( $merged ); | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | |||||||||||||||
| 62 | 26 | ▶ | sub unify_hash { | ||||||||||||
|
Subroutine
unify_hash: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 63 | 26 | my ( $hash ) = @_; | |||||||||||||
| 64 | 26 | for my $key ( keys %$hash ){ | |||||||||||||
| 65 | 36 | $hash->{$key} = unify_value( $hash->{$key} ); | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | 26 | return $hash; | |||||||||||||
| 68 | } | ||||||||||||||
| 69 | |||||||||||||||
| 70 | 8 | ▶ | sub unify_array { | ||||||||||||
|
Subroutine
unify_array: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 71 | 8 | my ( $array ) = @_; | |||||||||||||
| 72 | 8 | for my $index ( 0..$#$array ){ | |||||||||||||
| 73 | 28 | $array->[ $index ] = unify_value( $array->[ $index ] ) | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | 8 | return $array; | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | |||||||||||||||
| 78 | 64 | ▶ | sub unify_value { | ||||||||||||
|
Subroutine
unify_value: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 79 | 64 | my ( $value ) = @_; | |||||||||||||
| 80 | return | ||||||||||||||
| 81 | 64 | ▶ | ref $value eq 'ARRAY' ? unify_array( unique_array( $value ) ) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 82 | : ref $value eq 'HASH' ? unify_hash( $value ) | ||||||||||||||
| 83 | : $value | ||||||||||||||
| 84 | ; | ||||||||||||||
| 85 | } | ||||||||||||||
| 86 | 2 | 1; | |||||||||||||