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-16'; # DATE
4             our $VERSION = '0.003'; # VERSION
5              
6 1     1   1729 use 5.010001;
  1         2  
7 1     1   4 use strict;
  1         2  
  1         24  
8 1     1   5 use warnings;
  1         1  
  1         27  
9              
10 1     1   892 use DateTime;
  1         400453  
  1         283  
11              
12             our $DATE_EXTRACT_MODULE = $ENV{PERL_DATE_EXTRACT_MODULE} // "Date::Extract";
13              
14             sub gen_sorter {
15 3     3 0 1615 my ($is_reverse, $is_ci) = @_;
16              
17 3         14 my $re_is_num = qr/\A
18             [+-]?
19             (?:\d+|\d*(?:\.\d*)?)
20             (?:[Ee][+-]?\d+)?
21             \z/x;
22              
23 3         6 my $module = $DATE_EXTRACT_MODULE;
24 3 50       15 $module = "Date::Extract::$module" unless $module =~ /::/;
25 3 50       23 die "Invalid module '$module'" unless $module =~ /\A\w+(::\w+)*\z/;
26 1 50   1   660 eval "use $module"; die if $@;
  1     1   47939  
  1     1   23  
  1         8  
  1         2  
  1         17  
  1         7  
  1         2  
  1         38  
  3         324  
  3         14  
27 3         15 my $parser = $module->new;
28              
29             sub {
30 1     1   9 no strict 'refs';
  1         2  
  1         265  
31              
32 21     21   236 my $caller = caller();
33 21 50       66 my $a = @_ ? $_[0] : ${"$caller\::a"};
  21         115  
34 21 50       65 my $b = @_ ? $_[1] : ${"$caller\::b"};
  21         67  
35              
36 21         34 my $cmp;
37              
38             # XXX cache
39              
40 21         74 my $dt_a = $parser->extract($a);
41 21 50 33     219273 warn "Found date $dt_a in $a\n" if $ENV{DEBUG} && $dt_a;
42 21         74 my $dt_b = $parser->extract($b);
43 21 50 33     255455 warn "Found date $dt_b in $b\n" if $ENV{DEBUG} && $dt_b;
44              
45             {
46 21 100 100     42 if ($dt_a && $dt_b) {
  21 100 66     133  
    50 33        
47 12         983 $cmp = DateTime->compare($dt_a, $dt_b);
48 12 100       881 last if $cmp;
49             } elsif ($dt_a && !$dt_b) {
50 2         141 $cmp = -1;
51 2         5 last;
52             } elsif (!$dt_a && $dt_b) {
53 7         402 $cmp = 1;
54 7         14 last;
55             }
56              
57 3 100       15 if ($is_ci) {
58 2         8 $cmp = lc($a) cmp lc($b);
59             } else {
60 1         3 $cmp = $a cmp $b;
61             }
62             }
63              
64 21 100       304 $is_reverse ? -1*$cmp : $cmp;
65 3         86 };
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.003 of Sort::Sub::date_in_text (from Perl distribution Sort-Sub-date_in_text), released on 2016-12-16.
84              
85             =head1 DESCRIPTION
86              
87             The generated sort routine will sort by date found in text (extracted using
88             L<Date::Extract>) or (f no date is found in text) ascibetically. Items that have
89             a date will sort before items that do not.
90              
91             =for Pod::Coverage ^(gen_sorter)$
92              
93             =head1 ENVIRONMENT
94              
95             =head2 DEBUG => bool
96              
97             If set to true, will print stuffs to stderr.
98              
99             =head2 PERL_DATE_EXTRACT_MODULE => str
100              
101             Set Date::Extract module to use (the default is L<Date::Extract>).
102              
103             =head1 HOMEPAGE
104              
105             Please visit the project's homepage at L<https://metacpan.org/release/Sort-Sub-date_in_text>.
106              
107             =head1 SOURCE
108              
109             Source repository is at L<https://github.com/perlancar/perl-Sort-Sub-date_in_text>.
110              
111             =head1 BUGS
112              
113             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>
114              
115             When submitting a bug or request, please include a test-file or a
116             patch to an existing test-file that illustrates the bug or desired
117             feature.
118              
119             =head1 SEE ALSO
120              
121             =head1 AUTHOR
122              
123             perlancar <perlancar@cpan.org>
124              
125             =head1 COPYRIGHT AND LICENSE
126              
127             This software is copyright (c) 2016 by perlancar@cpan.org.
128              
129             This is free software; you can redistribute it and/or modify it under
130             the same terms as the Perl 5 programming language system itself.
131              
132             =cut