File Coverage

blib/lib/Sort/Sub/by_num_in_text.pm
Criterion Covered Total %
statement 29 35 82.8
branch 13 20 65.0
condition 6 9 66.6
subroutine 6 7 85.7
pod 0 2 0.0
total 54 73 73.9


line stmt bran cond sub pod time code
1             package Sort::Sub::by_num_in_text;
2              
3             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4             our $DATE = '2020-05-25'; # DATE
5             our $DIST = 'Sort-Sub'; # DIST
6             our $VERSION = '0.120'; # VERSION
7              
8 2     2   31 use 5.010001;
  2         6  
9 2     2   8 use strict;
  2         3  
  2         32  
10 2     2   8 use warnings;
  2         3  
  2         147  
11              
12             sub meta {
13             return {
14 0     0 0 0 v => 1,
15             summary => 'Sort by first number found in text or (if no number is found) ascibetically',
16             };
17             }
18             sub gen_sorter {
19 2     2 0 5 my ($is_reverse, $is_ci) = @_;
20              
21             sub {
22 2     2   12 no strict 'refs';
  2         10  
  2         445  
23              
24 18     18   25 my $caller = caller();
25 18 50       27 my $a = @_ ? $_[0] : ${"$caller\::a"};
  0         0  
26 18 50       22 my $b = @_ ? $_[1] : ${"$caller\::b"};
  0         0  
27              
28 18         19 my $cmp;
29              
30 18 100       48 my $num_a; $num_a = $1 if $a =~ /(\d+)/;
31 18 100       21 my $num_b; $num_b = $1 if $b =~ /(\d+)/;
  18         42  
32              
33             {
34 18 100 100     19 if (defined $num_a && defined $num_b) {
  18 100 66     58  
    50 33        
35 12         21 $cmp = $num_a <=> $num_b;
36 12 50       16 last if $cmp;
37             } elsif (defined $num_a && !defined $num_b) {
38 2         4 $cmp = -1;
39 2         3 last;
40             } elsif (!defined $num_a && defined $num_b) {
41 4         6 $cmp = 1;
42 4         4 last;
43             }
44              
45 0 0       0 if ($is_ci) {
46 0         0 $cmp = lc($a) cmp lc($b);
47             } else {
48 0         0 $cmp = $a cmp $b;
49             }
50             }
51              
52 18 50       42 $is_reverse ? -1*$cmp : $cmp;
53 2         10 };
54             }
55              
56             1;
57             # ABSTRACT: Sort by first number found in text or (if no number is found) ascibetically
58              
59             __END__
60              
61             =pod
62              
63             =encoding UTF-8
64              
65             =head1 NAME
66              
67             Sort::Sub::by_num_in_text - Sort by first number found in text or (if no number is found) ascibetically
68              
69             =head1 VERSION
70              
71             This document describes version 0.120 of Sort::Sub::by_num_in_text (from Perl distribution Sort-Sub), released on 2020-05-25.
72              
73             =head1 SYNOPSIS
74              
75             Generate sorter (accessed as variable) via L<Sort::Sub> import:
76              
77             use Sort::Sub '$by_num_in_text'; # use '$by_num_in_text<i>' for case-insensitive sorting, '$by_num_in_text<r>' for reverse sorting
78             my @sorted = sort $by_num_in_text ('item', ...);
79              
80             Generate sorter (accessed as subroutine):
81              
82             use Sort::Sub 'by_num_in_text<ir>';
83             my @sorted = sort {by_num_in_text} ('item', ...);
84              
85             Generate directly without Sort::Sub:
86              
87             use Sort::Sub::by_num_in_text;
88             my $sorter = Sort::Sub::by_num_in_text::gen_sorter(
89             ci => 1, # default 0, set 1 to sort case-insensitively
90             reverse => 1, # default 0, set 1 to sort in reverse order
91             );
92             my @sorted = sort $sorter ('item', ...);
93              
94             Use in shell/CLI with L<sortsub> (from L<App::sortsub>):
95              
96             % some-cmd | sortsub by_num_in_text
97             % some-cmd | sortsub by_num_in_text --ignore-case -r
98              
99             =head1 DESCRIPTION
100              
101             The generated sort routine will sort by first number (sequence of [0-9]) found
102             in text or (f no number is found in text) ascibetically. Items that have a
103             number will sort before items that do not.
104              
105             =for Pod::Coverage ^(gen_sorter|meta)$
106              
107             =head1 HOMEPAGE
108              
109             Please visit the project's homepage at L<https://metacpan.org/release/Sort-Sub>.
110              
111             =head1 SOURCE
112              
113             Source repository is at L<https://github.com/perlancar/perl-Sort-Sub>.
114              
115             =head1 BUGS
116              
117             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Sort-Sub>
118              
119             When submitting a bug or request, please include a test-file or a
120             patch to an existing test-file that illustrates the bug or desired
121             feature.
122              
123             =head1 SEE ALSO
124              
125             L<Sort::Sub>
126              
127             L<Sort::Sub::by_last_num_in_text>
128              
129             =head1 AUTHOR
130              
131             perlancar <perlancar@cpan.org>
132              
133             =head1 COPYRIGHT AND LICENSE
134              
135             This software is copyright (c) 2020, 2019, 2018, 2016, 2015 by perlancar@cpan.org.
136              
137             This is free software; you can redistribute it and/or modify it under
138             the same terms as the Perl 5 programming language system itself.
139              
140             =cut