Coerce/js/To_bool/From_str/common_words.pm

filter: Statementstmt 100% 22 / 22 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 2 totaltotal 93.1% 27 / 29 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Coerce/js/To_duration/From_float/seconds.pm ↑ Summary Coerce/js/To_duration/From_str/iso8601.pm »
1 package Data::Sah::Coerce::js::To_bool::From_str::common_words;
2  
3 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8 2 our $DATE = '2023-10-24'; # DATE
9 2 our $DIST = 'Data-Sah-Coerce'; # DIST
10 2 our $VERSION = '0.054'; # VERSION
11  
12 1 ▶sub meta {
Subroutine meta: called CC=1 SCAR=0.0
Pod undocumented
13 +{
14 1 v => 4,
15 summary => 'Coerce from common true/false words (e.g. "true","yes","on" for true, and "false","no","off" to false)',
16 prio => 50,
17 };
18 }
19  
20 1 ▶sub coerce {
Subroutine coerce: called CC=1 SCAR=0.0
Pod undocumented
21 1 my %args = @_;
22  
23 1 my $dt = $args{data_term};
24  
25 1 my $res = {};
26  
27 1 my $re = '/^(yes|no|true|false|on|off|1|0)$/i';
28 1 my $re_true = '/^(yes|true|on|1)$/i';
29  
30 1 $res->{expr_match} = join(
31 " && ",
32 "typeof($dt)=='string'",
33 "$dt.match($re)",
34 );
35  
36 # XXX how to avoid matching twice? even three times now
37  
38 1 $res->{expr_coerce} = "(function(_m) { _m = $dt.match($re); return _m[1].match($re_true) ? true : false })()";
39  
40 1 $res;
41 }
42  
43 2 1;
44 # ABSTRACT: Coerce from common true/false words (e.g. "true","yes","on" for true, and "false","no","off" to false)
45  
46 __END__
« Coerce/js/To_duration/From_float/seconds.pm ↑ Summary Coerce/js/To_duration/From_str/iso8601.pm »