File Coverage

blib/lib/Sort/Sub/by_date_in_text.pm
Criterion Covered Total %
statement 54 54 100.0
branch 18 26 69.2
condition 8 15 53.3
subroutine 10 10 100.0
pod 0 1 0.0
total 90 106 84.9


line stmt bran cond sub pod time code
1             package Sort::Sub::by_date_in_text;
2              
3             our $DATE = '2016-12-18'; # DATE
4             our $VERSION = '0.007'; # VERSION
5              
6 1     1   1182 use 5.010001;
  1         2  
7 1     1   3 use strict;
  1         1  
  1         15  
8 1     1   3 use warnings;
  1         1  
  1         19  
9              
10 1     1   776 use DateTime;
  1         303463  
  1         180  
11              
12             our $DATE_EXTRACT_MODULE = $ENV{PERL_DATE_EXTRACT_MODULE} // "Date::Extract";
13              
14             sub gen_sorter {
15 3     3 0 1425 my ($is_reverse, $is_ci) = @_;
16              
17 3         11 my $re_is_num = qr/\A
18             [+-]?
19             (?:\d+|\d*(?:\.\d*)?)
20             (?:[Ee][+-]?\d+)?
21             \z/x;
22              
23 3         5 my $module = $DATE_EXTRACT_MODULE;
24 3 50       11 $module = "Date::Extract::$module" unless $module =~ /::/;
25 3 50       19 die "Invalid module '$module'" unless $module =~ /\A\w+(::\w+)*\z/;
26 1 50   1   484 eval "use $module"; die if $@;
  1     1   39487  
  1     1   20  
  1         7  
  1         2  
  1         13  
  1         6  
  1         3  
  1         28  
  3         234  
  3         12  
27 3         12 my $parser = $module->new;
28              
29             sub {
30 1     1   9 no strict 'refs';
  1         1  
  1         225  
31              
32 21     21   157 my $caller = caller();
33 21 50       33 my $a = @_ ? $_[0] : ${"$caller\::a"};
  21         60  
34 21 50       30 my $b = @_ ? $_[1] : ${"$caller\::b"};
  21         38  
35              
36 21         15 my $cmp;
37              
38             # XXX cache
39              
40 21         43 my $dt_a = $parser->extract($a);
41 21 50 33     123349 warn "Found date $dt_a in $a\n" if $ENV{DEBUG} && $dt_a;
42 21         46 my $dt_b = $parser->extract($b);
43 21 50 33     134575 warn "Found date $dt_b in $b\n" if $ENV{DEBUG} && $dt_b;
44              
45             {
46 21 100 100     22 if ($dt_a && $dt_b) {
  21 100 66     85  
    50 33        
47 12         734 $cmp = DateTime->compare($dt_a, $dt_b);
48 12 100       554 last if $cmp;
49             } elsif ($dt_a && !$dt_b) {
50 2         97 $cmp = -1;
51 2         3 last;
52             } elsif (!$dt_a && $dt_b) {
53 7         265 $cmp = 1;
54 7         10 last;
55             }
56              
57 3 100       11 if ($is_ci) {
58 2         6 $cmp = lc($a) cmp lc($b);
59             } else {
60 1         2 $cmp = $a cmp $b;
61             }
62             }
63              
64 21 100       149 $is_reverse ? -1*$cmp : $cmp;
65 3         66 };
66             }
67              
68             1;
69             # ABSTRACT: Sort by date found in text or (if no date is found) ascibetically
70              
71             __END__
72              
73             =pod
74              
75             =encoding UTF-8
76              
77             =head1 NAME
78              
79             Sort::Sub::by_date_in_text - Sort by date found in text or (if no date is found) ascibetically
80              
81             =head1 VERSION
82              
83             This document describes version 0.007 of Sort::Sub::by_date_in_text (from Perl distribution Sort-Sub-by_date_in_text), released on 2016-12-18.
84              
85             =head1 SYNOPSIS
86              
87             Generate sorter (accessed as variable) via L<Sort::Sub> import:
88              
89             use Sort::Sub '$by_date_in_text'; # use '$by_date_in_text<i>' for case-insensitive sorting, '$by_date_in_text<r>' for reverse sorting
90             my @sorted = sort $by_date_in_text ('item', ...);
91              
92             Generate sorter (accessed as subroutine):
93              
94             use Sort::Sub 'by_date_in_text<ir>';
95             my @sorted = sort {by_date_in_text} ('item', ...);
96              
97             Generate directly without Sort::Sub:
98              
99             use Sort::Sub::by_date_in_text;
100             my $sorter = Sort::Sub::by_date_in_text::gen_sorter(
101             ci => 1, # default 0, set 1 to sort case-insensitively
102             reverse => 1, # default 0, set 1 to sort in reverse order
103             );
104             my @sorted = sort $sorter ('item', ...);
105              
106             Use in shell/CLI with L<sortsub> (from L<App::sortsub>):
107              
108             % some-cmd | sortsub by_date_in_text
109             % some-cmd | sortsub by_date_in_text --ignore-case -r
110              
111             =head1 DESCRIPTION
112              
113             The generated sort routine will sort by date found in text (extracted using
114             L<Date::Extract>) or (f no date is found in text) ascibetically. Items that have
115             a date will sort before items that do not.
116              
117             =for Pod::Coverage ^(gen_sorter)$
118              
119             =head1 ENVIRONMENT
120              
121             =head2 DEBUG => bool
122              
123             If set to true, will print stuffs to stderr.
124              
125             =head2 PERL_DATE_EXTRACT_MODULE => str
126              
127             Set Date::Extract module to use (the default is L<Date::Extract>).
128              
129             =head1 HOMEPAGE
130              
131             Please visit the project's homepage at L<https://metacpan.org/release/Sort-Sub-by_date_in_text>.
132              
133             =head1 SOURCE
134              
135             Source repository is at L<https://github.com/perlancar/perl-Sort-Sub-by_date_in_text>.
136              
137             =head1 BUGS
138              
139             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Sort-Sub-by_date_in_text>
140              
141             When submitting a bug or request, please include a test-file or a
142             patch to an existing test-file that illustrates the bug or desired
143             feature.
144              
145             =head1 SEE ALSO
146              
147             L<Sort::Sub>
148              
149             =head1 AUTHOR
150              
151             perlancar <perlancar@cpan.org>
152              
153             =head1 COPYRIGHT AND LICENSE
154              
155             This software is copyright (c) 2016 by perlancar@cpan.org.
156              
157             This is free software; you can redistribute it and/or modify it under
158             the same terms as the Perl 5 programming language system itself.
159              
160             =cut