Articulate/Sortation/Slug.pm

filter: Statementstmt 89.7% 35 / 39 Branchbran 75.0% 18 / 24 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 86.3% 63 / 73 CC 14 SCAR 24.5
CC
14
Cov
85%
cmp
15.4
BEGIN
1.0
BEGIN
1.0
CRAP 11.6
« Articulate/Serialisation/StatusSetter.pm ↑ Summary Articulate/Authentication.pm »
1 package Articulate::Sortation::Slug;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 with 'Articulate::Role::Sortation::AllYouNeedIsCmp';
7  
8 =head1 NAME
9  
10 Articulate::Sortation::Slug - natural slug sorting
11  
12 =head1 DESCRIPTION
13  
14 This implements the L<Articulate::Role::Sortation::AllYouNeedIsCmp>
15 role to provide a sorter object which will break up
16  
17 =head1 METHODS
18  
19 One method provided here, the rest are as in
20 L<Articulate::Role::Sortation::AllYouNeedIsCmp>.
21  
22 =head3 cmp
23  
24 $self->cmp('foo-1', 'bar'); # bar comes before foo
25 $self->cmp('foo-2', 'foo-11'); # 2 comes before 11
26 $self->cmp('foo-2', 'food'); # foo comes before food
27  
28 Splits into numerical and alphabetical. Sorts the former numerically
29 and the latter alphabetically. All characters other than a-z and 0-9
30 are treated as 'word breaks' and divide up components but are otherwise
31 ignored.
32  
33 =cut
34  
35 1 ▶sub _left { -1 }
Subroutine _left: called CC=1 SCAR=0.0
36  
37 4 ▶sub _right { 1 }
Subroutine _right: called CC=1 SCAR=0.0
38  
39 19 ▶sub cmp {
Subroutine cmp: called CC=14 SCAR=27.3
Pod documented
40 19 my $self = shift;
41 19 my $left = shift;
42 19 my $right = shift;
43 19 my $re_break = qr/(?: # Breaks between groups of characters
44 # (?<=[a-z])|(?=[a-z]) # aa
45 (?<=[a-z])(?![a-z]) # a0
46 | (?<![a-z])(?=[a-z]) # 0a
47 | (?<=[0-9])(?![0-9]) # 0_
48 | (?<![0-9])(?=[0-9]) # _0
49 )/ix;
50 19 my $la = [ grep { $_ ne '' } split( $re_break, $left ) ];
51 19 my $ra = [ grep { $_ ne '' } split( $re_break, $right ) ];
52  
53 #warn Dump {left => {$left, $la}, right => {$right,$ra}};
54 19 ▶ while ( scalar @$la && scalar @$ra ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$la and scalar @$ra) 39 4 43
Condition100%2/2
scalar @$la and scalar @$ra
!ll
142
MC/DC100%1/1
scalar @$la and scalar @$ra
scalar @$la
Truth table100%2/2
A
A= scalar @$la
Aresult
00
11
55 39 my $l = shift @$la;
56 39 my $r = shift @$ra;
57 39 ▶ if ( $l =~ /^[^a-z0-9]/i ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($l =~ /^[^a-z0-9]/i) { } 11 28 39
elsif ($r =~ /^[^a-z0-9]/i) { } 1 27 28
elsif ($l =~ /^[0-9]/) { } 11 16 27
58 11 ▶ if ( $r =~ /^[a-z0-9]/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r =~ /^[a-z0-9]/i) 0 11 11
59  
60 # left is dash and right is not - left wins
61 0 return _left;
62 }
63 11 next; # otherwise both are dash - continue
64 }
65 elsif ( $r =~ /^[^a-z0-9]/i ) {
66  
67 # right is dash and left is not - right wins
68 1 return _right;
69 }
70 elsif ( $l =~ /^[0-9]/ ) {
71 11 ▶ if ( $r =~ /^[0-9]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r =~ /^[0-9]/) { } 11 0 11
72  
73 # both are numbers
74 11 my $res = ( $l <=> $r );
75 11 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 9 2 11
76 }
77 else {
78 # left is number, right is alpha - left wins
79 0 return _left;
80 }
81 }
82 else {
83 # both are alphabetic
84 16 my $res = ( $l cmp $r );
85 16 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 5 11 16
86 }
87 }
88 4 ▶ return @$ra ? _left : 0 if ( !@$la );
Branch: 2 branches
BranchTrueFalseTotal
unless @$la 1 3 4
@$ra ? : 1 0 1
89 3 ▶ return _right if ( !@$ra );
Branch: 1 branch
BranchTrueFalseTotal
unless @$ra 3 0 3
90 0 ▶ die 'shouldn\'t be here' if $left ne $right;
Branch: 1 branch
BranchTrueFalseTotal
if $left ne $right 0 0 0
91 0 return $left cmp $right;
92 }
93  
94 =head1 SEE ALSO
95  
96 =over
97  
98 =item * L<Articulate::Sortation::String>
99  
100 =item * L<Articulate::Sortation::Numeric>
101  
102 =back
103  
104 =cut
105  
106 2 1;
« Articulate/Serialisation/StatusSetter.pm ↑ Summary Articulate/Authentication.pm »