File Coverage

blib/lib/Sort/Sub/by_date_in_text.pm
Criterion Covered Total %
statement 58 78 74.3
branch 18 32 56.2
condition 8 15 53.3
subroutine 11 15 73.3
pod 0 2 0.0
total 95 142 66.9


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