File Coverage

blib/lib/Data/Sah/Coerce/perl/To_float/From_str/suffix_filesize.pm
Criterion Covered Total %
statement 22 22 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod 0 2 0.0
total 29 31 93.5


line stmt bran cond sub pod time code
1             package Data::Sah::Coerce::perl::To_float::From_str::suffix_filesize;
2              
3             # AUTHOR
4             our $DATE = '2019-11-29'; # DATE
5             our $DIST = 'Sah-Schemas-DataSizeSpeed'; # DIST
6             our $VERSION = '0.004'; # VERSION
7              
8 2     2   46912 use 5.010001;
  2         8  
9 2     2   10 use strict;
  2         4  
  2         38  
10 2     2   8 use warnings;
  2         5  
  2         61  
11              
12 2     2   939 use Data::Size::Suffix::Filesize;
  2         8  
  2         61  
13 2     2   415 use Data::Dmp;
  2         1411  
  2         521  
14              
15             sub meta {
16             +{
17 2     2 0 29 v => 4,
18             summary => 'Parse number from string containing size suffixes',
19             prio => 50,
20             precludes => [qr/\AFrom_str::suffix_(\w+)\z/],
21             };
22             }
23              
24             my $re = '(\+?[0-9]+(?:\.[0-9]+)?)\s*('.join("|", sort {length($b)<=>length($a)} sort keys %Data::Size::Suffix::Filesize::suffixes).')'; $re = qr/\A$re\z/i;
25              
26             sub coerce {
27 2     2 0 30 my %args = @_;
28              
29 2         4 my $dt = $args{data_term};
30              
31 2         4 my $res = {};
32              
33 2         10 $res->{expr_match} = "$dt =~ ".dmp($re);
34 2         2317 $res->{modules}{"Data::Size::Suffix::Filesize"} = 0;
35 2         17 $res->{expr_coerce} = join(
36             "",
37             "\$1 * \$Data::Size::Suffix::Filesize::suffixes{(lc \$2)}",
38             );
39              
40 2         14 $res;
41             }
42              
43             1;
44             # ABSTRACT: Parse number from string containing size suffixes
45              
46             __END__
47              
48             =pod
49              
50             =encoding UTF-8
51              
52             =head1 NAME
53              
54             Data::Sah::Coerce::perl::To_float::From_str::suffix_filesize - Parse number from string containing size suffixes
55              
56             =head1 VERSION
57              
58             This document describes version 0.004 of Data::Sah::Coerce::perl::To_float::From_str::suffix_filesize (from Perl distribution Sah-Schemas-DataSizeSpeed), released on 2019-11-29.
59              
60             =head1 SYNOPSIS
61              
62             To use in a Sah schema:
63              
64             ["float",{"x.perl.coerce_rules"=>["From_str::suffix_filesize"]}]
65              
66             =head1 DESCRIPTION
67              
68             This rule accepts strings containing number with size suffixes, and return a
69             number with the number multiplied by the suffix multiplier, e.g.:
70              
71             2KB -> 20248
72             3.5mb -> 3670016
73             3.5Mib -> 3500000
74              
75             =for Pod::Coverage ^(meta|coerce)$
76              
77             =head1 HOMEPAGE
78              
79             Please visit the project's homepage at L<https://metacpan.org/release/Sah-Schemas-DataSizeSpeed>.
80              
81             =head1 SOURCE
82              
83             Source repository is at L<https://github.com/perlancar/perl-Sah-Schemas-DataSizeSpeed>.
84              
85             =head1 BUGS
86              
87             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Sah-Schemas-DataSizeSpeed>
88              
89             When submitting a bug or request, please include a test-file or a
90             patch to an existing test-file that illustrates the bug or desired
91             feature.
92              
93             =head1 AUTHOR
94              
95             perlancar <perlancar@cpan.org>
96              
97             =head1 COPYRIGHT AND LICENSE
98              
99             This software is copyright (c) 2019 by perlancar@cpan.org.
100              
101             This is free software; you can redistribute it and/or modify it under
102             the same terms as the Perl 5 programming language system itself.
103              
104             =cut