line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Fix::sort_field; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
106357
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
7
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.2020'; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
661
|
use List::MoreUtils qw(uniq); |
|
1
|
|
|
|
|
9419
|
|
|
1
|
|
|
|
|
10
|
|
8
|
1
|
|
|
1
|
|
1601
|
use Catmandu::Util::Path qw(as_path); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
59
|
|
9
|
1
|
|
|
1
|
|
6
|
use Moo; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
14
|
|
10
|
1
|
|
|
1
|
|
473
|
use namespace::clean; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
11
|
1
|
|
|
1
|
|
761
|
use Catmandu::Fix::Has; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
6
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
with 'Catmandu::Fix::Builder'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has path => (fix_arg => 1); |
16
|
|
|
|
|
|
|
has uniq => (fix_opt => 1); |
17
|
|
|
|
|
|
|
has reverse => (fix_opt => 1); |
18
|
|
|
|
|
|
|
has numeric => (fix_opt => 1); |
19
|
|
|
|
|
|
|
has undef_position => (fix_opt => 1, default => sub {'last'}); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub _build_fixer { |
22
|
12
|
|
|
12
|
|
100
|
my ($self) = @_; |
23
|
12
|
|
|
|
|
30
|
my $uniq = $self->uniq; |
24
|
12
|
|
|
|
|
23
|
my $reverse = $self->reverse; |
25
|
12
|
|
|
|
|
26
|
my $numeric = $self->numeric; |
26
|
12
|
|
|
|
|
22
|
my $undef_position = $self->undef_position; |
27
|
|
|
|
|
|
|
as_path($self->path)->updater( |
28
|
|
|
|
|
|
|
if_array_ref => sub { |
29
|
12
|
|
|
12
|
|
25
|
my $val = $_[0]; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
#filter out undef |
32
|
12
|
|
|
|
|
24
|
my $undefs = [grep {!defined($_)} @$val]; |
|
60
|
|
|
|
|
150
|
|
33
|
12
|
|
|
|
|
26
|
$val = [grep {defined($_)} @$val]; |
|
60
|
|
|
|
|
108
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
#uniq |
36
|
12
|
100
|
|
|
|
31
|
if ($uniq) { |
37
|
6
|
|
|
|
|
60
|
$val = [uniq(@$val)]; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
#sort |
41
|
12
|
50
|
66
|
|
|
80
|
if ($reverse && $numeric) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
42
|
0
|
|
|
|
|
0
|
$val = [sort {$b <=> $a} @$val]; |
|
0
|
|
|
|
|
0
|
|
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
elsif ($numeric) { |
45
|
1
|
|
|
|
|
3
|
$val = [sort {$a <=> $b} @$val]; |
|
3
|
|
|
|
|
7
|
|
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
elsif ($reverse) { |
48
|
1
|
|
|
|
|
4
|
$val = [sort {$b cmp $a} @$val]; |
|
1
|
|
|
|
|
4
|
|
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
else { |
51
|
10
|
|
|
|
|
38
|
$val = [sort {$a cmp $b} @$val]; |
|
32
|
|
|
|
|
72
|
|
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
#insert undef at the end |
55
|
12
|
100
|
|
|
|
72
|
if ($undef_position eq 'first') { |
|
|
100
|
|
|
|
|
|
56
|
2
|
100
|
|
|
|
4
|
if ($uniq) { |
57
|
1
|
50
|
|
|
|
6
|
unshift @$val, undef if @$undefs; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
else { |
60
|
1
|
|
|
|
|
4
|
unshift @$val, @$undefs; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
elsif ($undef_position eq 'last') { |
64
|
8
|
100
|
|
|
|
17
|
if ($uniq) { |
65
|
4
|
100
|
|
|
|
12
|
push @$val, undef if @$undefs; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
else { |
68
|
4
|
|
|
|
|
10
|
push @$val, @$undefs; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
12
|
|
|
|
|
270
|
$val; |
73
|
|
|
|
|
|
|
} |
74
|
12
|
|
|
|
|
43
|
); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
1; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
__END__ |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=pod |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head1 NAME |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Catmandu::Fix::sort_field - sort the values of an array |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 SYNOPSIS |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# e.g. tags => ["foo", "bar","bar"] |
90
|
|
|
|
|
|
|
sort_field(tags) # tags => ["bar","bar","foo"] |
91
|
|
|
|
|
|
|
sort_field(tags, uniq: 1) # tags => ["bar","foo"] |
92
|
|
|
|
|
|
|
sort_field(tags, uniq: 1, reverse: 1) # tags => ["foo","bar"] |
93
|
|
|
|
|
|
|
# e.g. nums => [ 100, 1 , 10] |
94
|
|
|
|
|
|
|
sort_field(nums, numeric: 1) # nums => [ 1, 10, 100] |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
#push undefined values to the end of the list (default) |
97
|
|
|
|
|
|
|
#beware: reverse has no effect on this! |
98
|
|
|
|
|
|
|
sort_field(tags,undef_position: last) |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
#push undefined values to the beginning of the list |
101
|
|
|
|
|
|
|
#beware: reverse has no effect on this! |
102
|
|
|
|
|
|
|
sort_field(tags,undef_position: first) |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
#remove undefined values from the list |
105
|
|
|
|
|
|
|
sort_field(tags,undef_position: delete) |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 SEE ALSO |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
L<Catmandu::Fix> |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=cut |