blib/lib/Number/Textify.pm

filter: Statementstmt 100% 60 / 60 Branchbran 97.7% 43 / 44 Conditioncond 93.3% 28 / 30 MC/DCmcdc 85.7% 12 / 14 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 96.8% 153 / 158 CC 35 SCAR 24.6
CC
35
Cov
98%
new
19.0
textify
9.0
_range_value
5.0
CRAP 11.7
↑ Summary
1 package Number::Textify;
2 3 $Number::Textify::VERSION = '20200512';
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =encoding utf8
9  
10 =head1 NAME
11  
12 Number::Textify - turn number into some string.
13  
14 =head1 VERSION
15  
16 version 20200512
17  
18 =head1 SYNOPSIS
19  
20 use Number::Textify ();
21  
22 my $time_converter = Number::Textify
23 -> new( [ [ 60 * 60, 'hour' ],
24 [ 60, 'minute' ],
25 [ 0, 'second' ],
26 ],
27  
28 skip_zeroes => 1,
29 );
30  
31 print $time_converter -> textify( 7274 ); # 2 hours 1 minute 14 seconds
32  
33  
34 my $time_converter_digital_neat = Number::Textify
35 -> new( [ [ 24 * 60 * 60, '%dd ' ],
36 [ 60 * 60, '%02d:' ],
37 [ 60, '%02d:' ],
38 [ 0, '%02d' ],
39 ],
40  
41 joiner => '',
42 formatter => sub { my $format = $_[ 1 ] // '%02d';
43 sprintf $format,
44 $_[ 0 ];
45 },
46 post_process => sub {
47 $_[ 0 ] =~ s/^0+//;
48 $_[ 0 ];
49 },
50 );
51  
52 print $time_converter_digital_neat -> textify( 10_000_000 ); # 115d 17:46:40
53  
54  
55 my $size_converter = Number::Textify
56 -> new( [ [ 1_024 * 1_024 * 1_024, '%.2f GiB' ],
57 [ 1_024 * 1_024, '%.2f MiB' ],
58 [ 1_024, '%.2f KiB' ],
59 [ 0, '%d B' ],
60 ],
61  
62 rounding => 1,
63 formatter => sub { sprintf $_[ 1 ],
64 $_[ 0 ];
65 },
66 );
67  
68 print $size_converter -> textify( 10_000_000 ); # 9.54 MiB
69  
70 =head1 DESCRIPTION
71  
72 Number::Textify helps you to build and use converter of number to some text representation of it.
73  
74 For example 10_000_000 can be represented as '115 days 17 hours 46 minutes 40 seconds', '115.7 days', '115d 17:46:40', '9.54 MiB' or '10.00 MB'. You can see some examples in t/02-examples.t
75  
76 This module uses object oriented approach and doesn't export anything.
77  
78 =head1 OBJECT CREATION
79  
80 =head2 new
81  
82 Expects one required parameter and a hash of optional ones. If some incorrectness detected, it dies.
83  
84 First (and only required) parameter is an arrayref to arrayrefs. First element in the nested arrayref is a positive number, which is a threshold for the range. The rest of elements are passed to the formatter. Arrayrefs should be sorted by the first element in descending order.
85  
86 Range representation figured using the threshold is passed to the formatter along with the rest of elements in the nested arrayref.
87  
88 Default formatter joins the range representation with the first of the rest of elements in the arrayref. Unless range representation equals 1, adds 's' to the result.
89  
90 If you need something else instead of that, you can pass a pair:
91  
92 formatter => sub { my ( $range_representation, @tail_of_nested_arrayref ) = @_; .. },
93  
94 Then those formatted range representations are joined with the default joiner, which is ' ' (a space). If you want to use another joiner, you can provide it as:
95  
96 joiner => ':',
97  
98 Then the joined result is passed through the post_process sub, which by default doesn't change anything. If you want to do some processing though, you can replace it:
99  
100 post_process => sub { my $result = shift; .. },
101  
102 If you prefer to avoid zero values in the middle of result ( like '2 hours 0 minutes 14 seconds' ), you can use the option:
103  
104 skip_zeroes => 1,
105  
106 If you don't want the exact representation, but only some rounding, there's an option for that:
107  
108 rounding => 1,
109  
110 though in this case it usually has sense to provide a custom formatter too.
111  
112 =cut
113  
114 21 ▶sub new {
Subroutine new: called CC=19 SCAR=29.5
Pod documented
115 21 ▶ ref( my $class = shift )
Branch: 1 branch
BranchTrueFalseTotal
ref(my $class = shift()) and die "I'm only a class method!" 1 20 21
116 and die "I'm only a class method!";
117  
118 20 ▶ die 'Constructor expects at least one argument'
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 0 1 19 20
119 unless @_ > 0;
120  
121 19 my $ranges = shift;
122  
123 19 ▶ die 'Incorrect number of additional parameters (hash expected)'
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 18 19
124 if @_ % 2;
125  
126 18 my %arg = @_;
127  
128 # let's check that ranges are in the way we expect (as structure)
129 18 ▶ die 'Ranges should be defined as array of arrays'
Branch: 1 branch
BranchTrueFalseTotal
unless $ranges and "ARRAY" eq ref $ranges and @$ranges and not grep((!($_ && "ARRAY" eq ref $_ && @$_ >= 1 && $_->[0] >= 0)), @$ranges) 8 10 18
Condition100%18/18
$ranges and "ARRAY" eq ref $ranges and @$ranges and not grep((!($_ && "ARRAY" eq ref $_ && @$_ >= 1 && $_->[0] >= 0)), @$ranges)
!ll&&!rl&&r
3510
$ranges and "ARRAY" eq ref $ranges and @$ranges
!ll&&!rl&&r
2115
$ranges and "ARRAY" eq ref $ranges
!ll&&!rl&&r
1116
$_ && 'ARRAY' eq ref $_ && @$_ >= 1 && $_->[0] >= 0
!ll&&!rl&&r
4134
$_ and "ARRAY" eq ref $_ and @$_ >= 1
!ll&&!rl&&r
3135
$_ and "ARRAY" eq ref $_
!ll&&!rl&&r
1236
MC/DC100%4/4
$ranges and "ARRAY" eq ref $ranges and @$ranges and not grep((!($_ && "ARRAY" eq ref $_ && @$_ >= 1 && $_->[0] >= 0)), @$ranges)
$ranges "ARRAY" eq ref $ranges @$ranges not grep((!($_ && "ARRAY" eq ref $_ && @$_ >= 1 && $_->[0] >= 0)), @$ranges)
MC/DC100%4/4
$_ && 'ARRAY' eq ref $_ && @$_ >= 1 && $_->[0] >= 0
$_ "ARRAY" eq ref $_ @$_ >= 1 $_->[0] >= 0
Truth table100%5/5
A and B and C and not(D)
A= $rangesB= "ARRAY" eq ref $rangesC= @$rangesD= not grep((!($_ && "ARRAY" eq ref $_ && @$_ >= 1 && $_->[0] >= 0)), @$ranges)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
Truth table100%5/5
A and B and C && D
A= $_B= "ARRAY" eq ref $_C= @$_ >= 1D= $_->[0] >= 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
130 unless $ranges
131 && 'ARRAY' eq ref $ranges
132 && @$ranges
133 && ! grep( ! ( $_
134 && 'ARRAY' eq ref $_
135 && @$_ >= 1 # range cutoff, additional parameters (e. g. description string) for formatter
136 && $_ -> [ 0 ] >= 0 # range cutoff should be positive
137 ),
138 @$ranges
139 )
140 ;
141  
142 # now let's check that ranges are in descending order
143 10 my $prev_range = $ranges -> [ 0 ][ 0 ];
144 10 for my $range
145 ( @$ranges[ 1 .. $#$ranges ]
146 ) {
147 24 ▶ die 'Ranges should be defined in descending order'
Branch: 1 branch
BranchTrueFalseTotal
unless $prev_range > $range->[0] 2 22 24
148 unless $prev_range > $range -> [ 0 ];
149  
150 22 $prev_range = $range -> [ 0 ];
151 }
152  
153 my %self =
154 ( ranges => $ranges,
155 joiner => exists $arg{joiner} ? $arg{joiner} : ' ',
156 skip_zeroes => exists $arg{skip_zeroes} ? !! $arg{skip_zeroes} : '',
157 rounding => exists $arg{rounding} ? !! $arg{rounding} : '',
158  
159 map {
160 8 ▶ exists $arg{ $_ }
Branch: 3 branches
BranchTrueFalseTotal
exists $arg{'joiner'} ? : 2 6 8
exists $arg{'skip_zeroes'} ? : 1 7 8
exists $arg{'rounding'} ? : 3 5 8
161 && $arg{ $_ }
162 && 'CODE' eq ref $arg{ $_ } ?
163 16 ▶ ( $_ => $arg{ $_ } )
Branch: 1 branch
BranchTrueFalseTotal
exists $arg{$_} && $arg{$_} && 'CODE' eq ref $arg{$_} ? : 6 10 16
Condition66%4/6
exists $arg{$_} and $arg{$_} and "CODE" eq ref $arg{$_}
!ll&&!rl&&r
1006
exists $arg{$_} and $arg{$_}
!ll&&!rl&&r
1006
MC/DC33%1/3
exists $arg{$_} and $arg{$_} and "CODE" eq ref $arg{$_}
exists $arg{$_} $arg{$_} "CODE" eq ref $arg{$_}
Truth table50%2/4
A and B and C
A= exists $arg{$_}B= $arg{$_}C= "CODE" eq ref $arg{$_}
ABCresult
0XX0
10X0
1100
1111
164 : ();
165 }
166 qw/ formatter
167 post_process
168 /,
169 );
170  
171 8 bless \ %self, $class;
172 }
173  
174 =head1 OBJECT METHODS
175  
176 =head2 textify
177  
178 Returns text presentation of the only one passed numeric parameter.
179  
180 =cut
181  
182 59 ▶sub textify {
Subroutine textify: called CC=9 SCAR=22.0
Pod documented
183 59 ▶ ref( my $self = shift )
Branch: 1 branch
BranchTrueFalseTotal
ref(my $self = shift()) or die "I'm only an object method!" 58 1 59
184 or die "I'm only an object method!";
185  
186 58 my $value = shift;
187 58 my $sign = '';
188 58 ▶ if ( $value < 0
Branch: 1 branch
BranchTrueFalseTotal
if ($value < 0) 5 53 58
189 ) {
190 5 $sign = '-';
191 5 $value *= -1;
192 }
193  
194 58 my @result;
195  
196 58 for my $range
197 58 ( @{ $self -> {ranges} }
198 ) {
199 187 my $t;
200 187 ( $t, $value ) = $self -> _range_value( $value, $range -> [ 0 ] );
201 187 ▶ if ( $t
Branch: 1 branch
BranchTrueFalseTotal
if ($t or @result and not $self->{"skip_zeroes"}) 145 42 187
Condition100%6/6
$t or @result and not $self->{"skip_zeroes"}
l!l&&r!l&&!r
1301542
@result and not $self->{"skip_zeroes"}
!ll&&!rl&&r
37515
MC/DC100%3/3
$t or @result and not $self->{"skip_zeroes"}
$t @result not $self->{"skip_zeroes"}
Truth table100%4/4
A or B and not(C)
A= $tB= @resultC= not $self->{"skip_zeroes"}
ABCresult
1XX1
0111
00X0
0100
202 || ( @result
203 && ! $self -> {skip_zeroes}
204 )
205 ) {
206 145 push @result,
207 $self -> _formatter( $t, @$range[ 1 .. $#$range ] );
208 }
209  
210 last
211 187 ▶ unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 58 129 187
212 }
213  
214 $self
215 -> _post_process( ( $sign ? $sign : '' )
216 . join $self -> {joiner},
217 @result
218 58 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$sign ? : 5 53 58
219 }
220  
221 187 ▶sub _range_value { # returns numeric value for current range, value for the next range (undef if not needed to process further)
Subroutine _range_value: called CC=5 SCAR=16.1
222 187 my $self = shift;
223 187 my $value = shift;
224 187 my $range = shift;
225  
226 187 ▶ if ( $value >= $range
Branch: 1 branch
BranchTrueFalseTotal
if ($value >= $range) { } 130 57 187
227 ) { # there's something in this range
228 130 ▶ my $value_for_range = $range ?
Branch: 1 branch
BranchTrueFalseTotal
$range ? : 89 41 130
229 $value / $range
230 : $value;
231  
232 130 ▶ if ( $self -> {rounding}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'rounding'}) { } 18 112 130
233 ) { # range value is rounded. no need to continue
234 18 ( $value_for_range, undef );
235 } else { # range value is whole
236 112 $value_for_range = int( $value_for_range );
237  
238 112 ▶ my $for_next_range = $range ?
Branch: 1 branch
BranchTrueFalseTotal
$range ? : 72 40 112
239 $value % $range
240 : undef;
241  
242 112 ( $value_for_range, $for_next_range );
243 }
244 } else { # pass to the next range
245 57 ( 0, $value );
246 }
247 }
248  
249 145 ▶sub _formatter {
Subroutine _formatter: called CC=4 SCAR=13.9
250 145 my $self = shift;
251  
252 145 ▶ if ( $self -> {formatter}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'formatter'}) { } 84 61 145
253 ) {
254 84 $self -> {formatter} -> ( @_ );
255 } else {
256 61 my $value = shift;
257 61 my $string = shift;
258  
259 61 ▶ sprintf '%s %s%s',
Branch: 2 branches
BranchTrueFalseTotal
defined $string ? : 61 0 61
$value == 1 ? : 8 53 61
260 $value,
261 defined $string ?
262 ( $string,
263 $value == 1 ? '' : 's',
264 )
265 : ( '', '' ),
266 ;
267 }
268 }
269  
270 58 ▶sub _post_process {
Subroutine _post_process: called CC=2 SCAR=6.9
271 58 my $self = shift;
272  
273 58 ▶ if ( $self -> {post_process}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'post_process'}) { } 10 48 58
274 ) {
275 10 $self -> {post_process} -> ( @_ );
276 } else {
277 48 $_[ 0 ];
278 }
279 }
280  
281 =head1 AUTHOR
282  
283 Valery Kalesnik, C<< <valkoles at gmail.com> >>
284  
285 =head1 COPYRIGHT AND LICENSE
286  
287 This software is copyright (c) 2020 by Valery Kalesnik.
288  
289 This is free software; you can redistribute it and/or modify it under
290 the same terms as the Perl 5 programming language system itself.
291  
292 =cut
293  
294 3 1;
↑ Summary