File Coverage

blib/lib/Data/Sah/Coerce/perl/To_int/From_str/convert_en_dow_name_to_num.pm
Criterion Covered Total %
statement 16 16 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod 0 2 0.0
total 21 23 91.3


line stmt bran cond sub pod time code
1             package Data::Sah::Coerce::perl::To_int::From_str::convert_en_dow_name_to_num;
2              
3             # AUTHOR
4             our $DATE = '2021-08-04'; # DATE
5             our $DIST = 'Sah-Schemas-Date'; # DIST
6             our $VERSION = '0.016'; # VERSION
7              
8 1     1   2339 use 5.010001;
  1         4  
9 1     1   6 use strict;
  1         2  
  1         23  
10 1     1   6 use warnings;
  1         2  
  1         186  
11              
12             sub meta {
13             +{
14 1     1 0 14 v => 4,
15             summary => 'Convert English day-of-week name (e.g. su, MON, Tuesday) to number (1-7, 1=Monday)',
16             prio => 50,
17             };
18             }
19              
20             sub coerce {
21 1     1 0 19 my %args = @_;
22              
23 1         2 my $dt = $args{data_term};
24              
25 1         3 my $res = {};
26 1         3 my $pkg = __PACKAGE__;
27              
28 1         2 $res->{expr_match} = "!ref($dt)";
29 1         7 $res->{expr_coerce} = join(
30             "",
31             "do { ",
32              
33             # since this is a small translation table we put it inline, but for
34             # larger translation table we should move it to a separate perl module
35             "\$$pkg\::dow_nums ||= {",
36             " mo=>1, mon=>1, monday=>1, ",
37             " tu=>2, tue=>2, tuesday=>2, ",
38             " we=>3, wed=>3, wednesday=>3, ",
39             " th=>4, thu=>4, thursday=>4, ",
40             " fr=>5, fri=>5, friday=>5, ",
41             " sa=>6, sat=>6, saturday=>6, ",
42             " su=>7, sun=>7, sunday=>7, ",
43             "}; ",
44             "\$$pkg\::dow_nums->{lc $dt} || $dt; ",
45             "}",
46             );
47              
48 1         4 $res;
49             }
50              
51             1;
52             # ABSTRACT: Convert English day-of-week name (e.g. su, MON, Tuesday) to number (1-7, 1=Monday)
53              
54             __END__
55              
56             =pod
57              
58             =encoding UTF-8
59              
60             =head1 NAME
61              
62             Data::Sah::Coerce::perl::To_int::From_str::convert_en_dow_name_to_num - Convert English day-of-week name (e.g. su, MON, Tuesday) to number (1-7, 1=Monday)
63              
64             =head1 VERSION
65              
66             This document describes version 0.016 of Data::Sah::Coerce::perl::To_int::From_str::convert_en_dow_name_to_num (from Perl distribution Sah-Schemas-Date), released on 2021-08-04.
67              
68             =head1 SYNOPSIS
69              
70             To use in a Sah schema:
71              
72             ["int",{"x.perl.coerce_rules"=>["From_str::convert_en_dow_name_to_num"]}]
73              
74             =head1 DESCRIPTION
75              
76             This rule can convert English day-of-week names like:
77              
78             su
79             MON
80             Tuesday
81              
82             to corresponding day-of-week numbers (i.e. 7, 1, 2 in the examples above).
83             Unrecognized strings will just be passed as-is.
84              
85             =for Pod::Coverage ^(meta|coerce)$
86              
87             =head1 HOMEPAGE
88              
89             Please visit the project's homepage at L<https://metacpan.org/release/Sah-Schemas-Date>.
90              
91             =head1 SOURCE
92              
93             Source repository is at L<https://github.com/perlancar/perl-Sah-Schemas-Date>.
94              
95             =head1 BUGS
96              
97             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Sah-Schemas-Date>
98              
99             When submitting a bug or request, please include a test-file or a
100             patch to an existing test-file that illustrates the bug or desired
101             feature.
102              
103             =head1 AUTHOR
104              
105             perlancar <perlancar@cpan.org>
106              
107             =head1 COPYRIGHT AND LICENSE
108              
109             This software is copyright (c) 2021, 2020, 2019 by perlancar@cpan.org.
110              
111             This is free software; you can redistribute it and/or modify it under
112             the same terms as the Perl 5 programming language system itself.
113              
114             =cut