Data/Unixish/bool.pm

filter: Statementstmt 83.3% 50 / 60 Branchbran 61.3% 27 / 44 Conditioncond 87.5% 14 / 16 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 76.6% 108 / 141 CC 28 SCAR 34.3
CC
28
Cov
76%
_is_true
54.2
_bool_begin
6.2
_bool_item
3.0
CRAP 30.8
« Data/Unixish/rand.pm ↑ Summary Data/Unixish/pick.pm »
1 ## no critic: Subroutines::ProhibitExplicitReturnUndef
2 package Data::Unixish::bool;
3  
4 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use syntax 'each_on_array'; # to support perl < 5.12
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 #use Log::Any '$log';
10  
11 2 ▶use Data::Unixish::Util qw(%common_args);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
14 2 our $DATE = '2025-02-24'; # DATE
15 2 our $DIST = 'Data-Unixish'; # DIST
16 2 our $VERSION = '1.574'; # VERSION
17  
18 2 our %SPEC;
19  
20 38 ▶sub _is_true {
Subroutine _is_true: called CC=20 SCAR=39.9
21 38 my ($val, $notion) = @_;
22  
23 38 ▶ if ($notion =~ /^n1/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($notion =~ /^n1/) { } 18 20 38
24 18 ▶ return undef unless defined($val);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 2 16 18
25 16 ▶ return 0 if ref($val) eq 'ARRAY' && !@$val;
Branch: 1 branch
BranchTrueFalseTotal
if ref $val eq "ARRAY" and not @$val 2 14 16
Condition100%3/3
ref $val eq "ARRAY" and not @$val
!ll&&!rl&&r
1222
MC/DC100%2/2
ref $val eq "ARRAY" and not @$val
ref $val eq "ARRAY" not @$val
Truth table100%3/3
A and not(B)
A= ref $val eq "ARRAY"B= not @$val
ABresult
0X0
100
111
26 14 ▶ return 0 if ref($val) eq 'HASH' && !keys(%$val);
Branch: 1 branch
BranchTrueFalseTotal
if ref $val eq "HASH" and not keys %$val 2 12 14
Condition100%3/3
ref $val eq "HASH" and not keys %$val
!ll&&!rl&&r
1022
MC/DC100%2/2
ref $val eq "HASH" and not keys %$val
ref $val eq "HASH" not keys %$val
Truth table100%3/3
A and not(B)
A= ref $val eq "HASH"B= not keys %$val
ABresult
0X0
100
111
27 12 ▶ if ($notion eq 'n1+en_id') {
Branch: 4 branches
BranchTrueFalseTotal
if ($notion eq 'n1+en_id') { } 0 12 12
elsif ($notion eq 'n1+en') { } 0 12 12
elsif ($notion eq 'n1+en_id') { } 0 12 12
elsif ($notion eq 'n1') { } 12 0 12
28 0 ▶ return undef if $val =~ /^(undef|undefined|null|)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^(undef|undefined|null|)$/i 0 0 0
29 0 ▶ return 0 if $val =~ /^(no|n|false|f|tidak|tdk|t|salah|s|bukan)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^(no|n|false|f|tidak|tdk|t|salah|s|bukan)$/i 0 0 0
30 0 return 1;
31 } elsif ($notion eq 'n1+en') {
32 0 ▶ return undef if $val =~ /^(undef|undefined|null|)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^(undef|undefined|null|)$/i 0 0 0
33 0 ▶ return 0 if $val =~ /^(no|n|false|f)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^(no|n|false|f)$/i 0 0 0
34 0 return 1;
35 } elsif ($notion eq 'n1+en_id') {
36 0 ▶ return undef if $val =~ /^(undef|undefined|null|kosong|)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^(undef|undefined|null|kosong|)$/i 0 0 0
37 0 ▶ return 0 if $val =~ /^(tidak|tdk|t|salah|s|bukan)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^(tidak|tdk|t|salah|s|bukan)$/i 0 0 0
38 0 return 1;
39 } elsif ($notion eq 'n1') {
40 12 ▶ return $val ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 8 4 12
41 } else {
42 0 die "Unknown notion '$notion'";
43 }
44 } else {
45 # perl
46 20 ▶ return undef unless defined($val);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 4 16 20
47 16 ▶ return $val ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 10 6 16
48 }
49 }
50  
51 2 my %styles = (
52 one_zero => ['1', '0'],
53 t_f => ['t', 'f'],
54 true_false => ['true', 'false'],
55 y_n => ['y', 'n'],
56 Y_N => ['Y', 'N'],
57 yes_no => ['yes', 'no'],
58 v_X => ['v', 'X'],
59 check => ['✓', ' ', 'uses Unicode'],
60 check_cross => ['✓', '✕', 'uses Unicode'],
61 heavy_check_cross => ['✔', '✘', 'uses Unicode'],
62 dot => ['●', ' ', 'uses Unicode'],
63 dot_cross => ['●', '✘', 'uses Unicode'],
64  
65 );
66  
67 $SPEC{bool} = {
68 v => 1.1,
69 summary => 'Format boolean',
70 description => <<'MARKDOWN',
71  
72 MARKDOWN
73 args => {
74 %common_args,
75 style => {
76 schema=>[str => in=>[keys %styles], default=>'one_zero'],
77 description => "Available styles:\n\n".
78 2 ▶ join("", map {" * $_" . ($styles{$_}[2] ? " ($styles{$_}[2])":"").": $styles{$_}[1] $styles{$_}[0]\n"} sort keys %styles),
Branch: 1 branch
BranchTrueFalseTotal
$styles{$_}[2] ? : 10 14 24
79 cmdline_aliases => { s=>{} },
80 },
81 true_char => {
82 summary => 'Instead of style, you can also specify character for true value',
83 schema=>['str*'],
84 cmdline_aliases => { t => {} },
85 },
86 false_char => {
87 summary => 'Instead of style, you can also specify character for true value',
88 schema=>['str*'],
89 cmdline_aliases => { f => {} },
90 },
91 notion => {
92 summary => 'What notion to use to determine true/false',
93 schema => [str => in=>[qw/perl n1 n1+en n1+id n1+en_id/], default => 'perl'],
94 description => <<'MARKDOWN',
95  
96 `perl` uses Perl notion.
97  
98 `n1` (for lack of better name) is just like Perl notion, but empty array and
99 empty hash is considered false.
100  
101 `n1+en` is like `n1` but also handle 'Yes', 'No', 'true', 'false', etc
102 (English).
103  
104 `n1+id` is like `n1` but also handle 'ya', 'tidak', etc (Indonesian).
105  
106 `n1+en_id` is like `n1` but also handle 'Yes', 'No', 'true', 'false', etc
107 (English) and 'ya', 'tidak', etc (Indonesian).
108  
109 TODO: add Ruby, Python, PHP, JavaScript, etc notion.
110  
111 MARKDOWN
112 },
113 # XXX: flag to ignore references
114 },
115 tags => [qw/datatype:bool itemfunc formatting/],
116 };
117 3 ▶sub bool {
Subroutine bool: called CC=2 SCAR=6.9
Pod documented
118 3 my %args = @_;
119 3 my ($in, $out) = ($args{in}, $args{out});
120  
121 3 _bool_begin(\%args);
122 3 ▶ while (my ($index, $item) = each @$in) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @$in) 19 3 22
123 19 push @$out, _bool_item($item, \%args);
124 }
125  
126 3 [200, "OK"];
127 }
128  
129 6 ▶sub _bool_begin {
Subroutine _bool_begin: called CC=6 SCAR=18.2
130 6 my $args = shift;
131  
132 6 ▶ $args->{notion} //= 'perl';
Condition100%2/2
$args->{"notion"} //= "perl"
l!l
24
MC/DC100%1/1
$args->{"notion"} //= "perl"
$args->{"notion"}
Truth table100%2/2
A
A= $args->{"notion"}
Aresult
11
00
133 6 ▶ $args->{style} //= 'one_zero';
Condition100%2/2
$args->{"style"} //= "one_zero"
l!l
24
MC/DC100%1/1
$args->{"style"} //= "one_zero"
$args->{"style"}
Truth table100%2/2
A
A= $args->{"style"}
Aresult
11
00
134 6 ▶ $args->{style} = 'one_zero' if !$styles{$args->{style}};
Branch: 1 branch
BranchTrueFalseTotal
unless $styles{$args->{"style"}} 0 6 6
135  
136 6 ▶ $args->{true_char} //= $styles{$args->{style}}[0];
Condition66%2/3
$args->{"true_char"} //= $styles{$args->{"style"}}[0]
l!l&&r!l&&!r
240
MC/DC0%0/2
$args->{"true_char"} //= $styles{$args->{"style"}}[0]
$args->{"true_char"} $styles{$args->{"style"}}[0]
Truth table66%2/3
A //= B
A= $args->{"true_char"}B= $styles{$args->{"style"}}[0]
ABresult
1X1
011
000
137 6 ▶ $args->{false_char} //= $styles{$args->{style}}[1];
Condition66%2/3
$args->{"false_char"} //= $styles{$args->{"style"}}[1]
l!l&&r!l&&!r
240
MC/DC0%0/2
$args->{"false_char"} //= $styles{$args->{"style"}}[1]
$args->{"false_char"} $styles{$args->{"style"}}[1]
Truth table66%2/3
A //= B
A= $args->{"false_char"}B= $styles{$args->{"style"}}[1]
ABresult
1X1
011
000
138 }
139  
140 38 ▶sub _bool_item {
Subroutine _bool_item: called CC=3 SCAR=11.0
141 38 my ($item, $args) = @_;
142  
143 38 my $t = _is_true($item, $args->{notion});
144 38 ▶ $t ? $args->{true_char} : defined($t) ? $args->{false_char} : undef;
Branch: 2 branches
BranchTrueFalseTotal
$t ? : 18 20 38
defined $t ? : 14 6 20
145 }
146  
147 2 1;
148 # ABSTRACT: Format boolean
149  
150 __END__
« Data/Unixish/rand.pm ↑ Summary Data/Unixish/pick.pm »