| 1 | package Catmandu::Fix::sort_field; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | ▶ | use Catmandu::Sane; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 1 | our $VERSION = '1.0201_02'; | |||||||||||||
| 6 | |||||||||||||||
| 7 | 1 | ▶ | use List::MoreUtils (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 1 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | ▶ | use namespace::clean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 1 | ▶ | use Catmandu::Fix::Has; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 1 | has path => (fix_arg => 1); | |||||||||||||
| 13 | 1 | has uniq => (fix_opt => 1); | |||||||||||||
| 14 | 1 | has reverse => (fix_opt => 1); | |||||||||||||
| 15 | 1 | has numeric => (fix_opt => 1); | |||||||||||||
| 16 | 6 | ▶ | has undef_position => (fix_opt => 1, default => sub {'last'}); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | 1 | with 'Catmandu::Fix::SimpleGetValue'; | |||||||||||||
| 19 | |||||||||||||||
| 20 | sub emit_value { | ||||||||||||||
| 21 | 12 | ▶ | my ($self, $var, $fixer) = @_; | ||||||||||||
|
Subroutine
emit_value: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||
| 22 | 12 | ▶ | my $comparer = $self->numeric ? "<=>" : "cmp"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 23 | |||||||||||||||
| 24 | 12 | my $perl = "if (is_array_ref(${var})) {"; | |||||||||||||
| 25 | |||||||||||||||
| 26 | #filter out undef | ||||||||||||||
| 27 | 12 | my $undef_values = $fixer->generate_var; | |||||||||||||
| 28 | 12 | $perl .= "my ${undef_values} = [ grep { !defined(\$_) } \@{${var}} ];"; | |||||||||||||
| 29 | 12 | $perl .= "${var} = [ grep { defined(\$_) } \@{${var}} ];"; | |||||||||||||
| 30 | |||||||||||||||
| 31 | #uniq | ||||||||||||||
| 32 | 12 | ▶ | if ($self->uniq) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 33 | 6 | $perl .= "${var} = [List::MoreUtils::uniq(\@{${var}})];"; | |||||||||||||
| 34 | } | ||||||||||||||
| 35 | |||||||||||||||
| 36 | #sort | ||||||||||||||
| 37 | 12 | ▶ | if ($self->reverse) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | 1 | $perl .= "${var} = [sort { \$b $comparer \$a } \@{${var}}];"; | |||||||||||||
| 39 | } | ||||||||||||||
| 40 | else { | ||||||||||||||
| 41 | 11 | $perl .= "${var} = [sort { \$a $comparer \$b } \@{${var}}];"; | |||||||||||||
| 42 | } | ||||||||||||||
| 43 | |||||||||||||||
| 44 | #insert undef at the end | ||||||||||||||
| 45 | 12 | ▶ | if ($self->undef_position eq "last") { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 46 | 8 | ▶ | if ($self->uniq) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 47 | 4 | $perl .= "push \@{${var}},undef if scalar(\@{${undef_values}});"; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | else { | ||||||||||||||
| 50 | 4 | $perl .= "push \@{${var}},\@{${undef_values}};"; | |||||||||||||
| 51 | } | ||||||||||||||
| 52 | } | ||||||||||||||
| 53 | |||||||||||||||
| 54 | #insert undef at the beginning | ||||||||||||||
| 55 | elsif ($self->undef_position eq "first") { | ||||||||||||||
| 56 | 2 | ▶ | if ($self->uniq) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | 1 | $perl | |||||||||||||
| 58 | .= "unshift \@{${var}},undef if scalar(\@{${undef_values}});"; | ||||||||||||||
| 59 | } | ||||||||||||||
| 60 | else { | ||||||||||||||
| 61 | 1 | $perl .= "unshift \@{${var}},\@{${undef_values}};"; | |||||||||||||
| 62 | } | ||||||||||||||
| 63 | } | ||||||||||||||
| 64 | |||||||||||||||
| 65 | #leave undef out of the list | ||||||||||||||
| 66 | |||||||||||||||
| 67 | 12 | $perl .= "}"; | |||||||||||||
| 68 | 12 | $perl; | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | 1 | 1; | |||||||||||||
| 72 | |||||||||||||||
| 73 | __END__ | ||||||||||||||
| 74 | |||||||||||||||
| 75 | =pod | ||||||||||||||
| 76 | |||||||||||||||
| 77 | =head1 NAME | ||||||||||||||
| 78 | |||||||||||||||
| 79 | Catmandu::Fix::sort_field - sort the values of an array | ||||||||||||||
| 80 | |||||||||||||||
| 81 | =head1 SYNOPSIS | ||||||||||||||
| 82 | |||||||||||||||
| 83 | # e.g. tags => ["foo", "bar","bar"] | ||||||||||||||
| 84 | sort_field(tags) # tags => ["bar","bar","foo"] | ||||||||||||||
| 85 | sort_field(tags, uniq: 1) # tags => ["bar","foo"] | ||||||||||||||
| 86 | sort_field(tags, uniq: 1, reverse: 1) # tags => ["foo","bar"] | ||||||||||||||
| 87 | # e.g. nums => [ 100, 1 , 10] | ||||||||||||||
| 88 | sort_field(nums, numeric: 1) # nums => [ 1, 10, 100] | ||||||||||||||
| 89 | |||||||||||||||
| 90 | #push undefined values to the end of the list (default) | ||||||||||||||
| 91 | #beware: reverse has no effect on this! | ||||||||||||||
| 92 | sort_field(tags,undef_position: last) | ||||||||||||||
| 93 | |||||||||||||||
| 94 | #push undefined values to the beginning of the list | ||||||||||||||
| 95 | #beware: reverse has no effect on this! | ||||||||||||||
| 96 | sort_field(tags,undef_position: first) | ||||||||||||||
| 97 | |||||||||||||||
| 98 | #remove undefined values from the list | ||||||||||||||
| 99 | sort_field(tags,undef_position: delete) | ||||||||||||||
| 100 | |||||||||||||||
| 101 | =head1 SEE ALSO | ||||||||||||||
| 102 | |||||||||||||||
| 103 | L<Catmandu::Fix> | ||||||||||||||
| 104 | |||||||||||||||
| 105 | =cut | ||||||||||||||