File Coverage

blib/lib/Sort/Sub/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::date_in_text;
2              
3             our $DATE = '2016-12-17'; # DATE
4             our $VERSION = '0.005'; # VERSION
5              
6 1     1   1695 use 5.010001;
  1         3  
7 1     1   4 use strict;
  1         1  
  1         21  
8 1     1   3 use warnings;
  1         1  
  1         25  
9              
10 1     1   727 use DateTime;
  1         314202  
  1         181  
11              
12             our $DATE_EXTRACT_MODULE = $ENV{PERL_DATE_EXTRACT_MODULE} // "Date::Extract";
13              
14             sub gen_sorter {
15 3     3 0 1850 my ($is_reverse, $is_ci) = @_;
16              
17 3         12 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       14 $module = "Date::Extract::$module" unless $module =~ /::/;
25 3 50       16 die "Invalid module '$module'" unless $module =~ /\A\w+(::\w+)*\z/;
26 1 50   1   497 eval "use $module"; die if $@;
  1     1   38203  
  1     1   21  
  1         7  
  1         1  
  1         15  
  1         7  
  1         1  
  1         32  
  3         247  
  3         12  
27 3         14 my $parser = $module->new;
28              
29             sub {
30 1     1   7 no strict 'refs';
  1         1  
  1         213  
31              
32 21     21   203 my $caller = caller();
33 21 50       54 my $a = @_ ? $_[0] : ${"$caller\::a"};
  21         102  
34 21 50       41 my $b = @_ ? $_[1] : ${"$caller\::b"};
  21         58  
35              
36 21         21 my $cmp;
37              
38             # XXX cache
39              
40 21         59 my $dt_a = $parser->extract($a);
41 21 50 33     191994 warn "Found date $dt_a in $a\n" if $ENV{DEBUG} && $dt_a;
42 21         65 my $dt_b = $parser->extract($b);
43 21 50 33     228809 warn "Found date $dt_b in $b\n" if $ENV{DEBUG} && $dt_b;
44              
45             {
46 21 100 100     24 if ($dt_a && $dt_b) {
  21 100 66     119  
    50 33        
47 12         819 $cmp = DateTime->compare($dt_a, $dt_b);
48 12 100       761 last if $cmp;
49             } elsif ($dt_a && !$dt_b) {
50 2         124 $cmp = -1;
51 2         4 last;
52             } elsif (!$dt_a && $dt_b) {
53 7         320 $cmp = 1;
54 7         12 last;
55             }
56              
57 3 100       13 if ($is_ci) {
58 2         7 $cmp = lc($a) cmp lc($b);
59             } else {
60 1         4 $cmp = $a cmp $b;
61             }
62             }
63              
64 21 100       214 $is_reverse ? -1*$cmp : $cmp;
65 3         68 };
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::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.005 of Sort::Sub::date_in_text (from Perl distribution Sort-Sub-date_in_text), released on 2016-12-17.
84              
85             =head1 SYNOPSIS
86              
87             Generate sorter (accessed as variable) via L<Sort::Sub> import:
88              
89             use Sort::Sub '$date_in_text'; # use '$date_in_text<i>' for case-insensitive sorting, '$date_in_text<r>' for reverse sorting
90             my @sorted = sort $date_in_text ('item', ...);
91              
92             Generate sorter (accessed as subroutine):
93              
94             use Sort::Sub 'date_in_text<ir>';
95             my @sorted = sort {date_in_text} ('item', ...);
96              
97             Generate directly without Sort::Sub:
98              
99             use Sort::Sub::date_in_text;
100             my $sorter = Sort::Sub::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 date_in_text
109             % some-cmd | sortsub 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-date_in_text>.
132              
133             =head1 SOURCE
134              
135             Source repository is at L<https://github.com/perlancar/perl-Sort-Sub-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-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