File Coverage

blib/lib/Data/Verifier/Filters.pm
Criterion Covered Total %
statement 24 24 100.0
branch 6 10 60.0
condition n/a
subroutine 7 7 100.0
pod 5 5 100.0
total 42 46 91.3


line stmt bran cond sub pod time code
1             package Data::Verifier::Filters;
2             $Data::Verifier::Filters::VERSION = '0.63';
3 17     17   136 use strict;
  17         46  
  17         676  
4 17     17   106 use warnings;
  17         40  
  17         5552  
5              
6             # ABSTRACT: Filters for values
7              
8              
9             sub collapse {
10 2     2 1 5 my ($self, $val) = @_;
11 2 50       5 return $val if not defined $val;
12              
13 2         12 $val =~ s/\s+/ /g;
14 2         8 return $val;
15             }
16              
17              
18             sub flatten {
19 3     3 1 10 my ($self, $val) = @_;
20 3 50       8 return $val if not defined $val;
21              
22 3         15 $val =~ s/\s//g;
23 3         13 return $val;
24             }
25              
26              
27             sub lower {
28 1     1 1 4 my ($self, $val) = @_;
29 1 50       3 return $val if not defined $val;
30              
31 1         5 return lc($val);
32             }
33              
34              
35             sub trim {
36 10     10 1 24 my ($self, $val) = @_;
37 10 100       32 return $val if not defined $val;
38              
39 9         42 $val =~ s/^\s+|\s+$//g;
40              
41 9         44 return $val;
42             }
43              
44              
45             sub upper {
46 2     2 1 6 my ($self, $val) = @_;
47 2 50       6 return $val if not defined $val;
48              
49 2         8 return uc($val);
50             }
51              
52             1;
53              
54             __END__
55              
56             =pod
57              
58             =head1 NAME
59              
60             Data::Verifier::Filters - Filters for values
61              
62             =head1 VERSION
63              
64             version 0.63
65              
66             =head1 SYNOPSIS
67              
68             use Data::Verifier;
69              
70             my $dv = Data::Verifier->new(profile => {
71             name => {
72             type => 'Str',
73             filters => [ qw(collapse trim) ]
74             }
75             });
76              
77             $dv->verify({ name => ' foo bar '});
78             $dv->get_value('name'); # 'foo bar'
79              
80             =head1 CUSTOM FILTERS
81              
82             Adding a custom filter may be done by providing a coderef as one of the
83             filters:
84              
85             # Remove all whitespace
86             my $sub = sub { my ($val) = @_; $val =~ s/\s//g; $val }
87              
88             $dv->verify({
89             name => {
90             type => 'Str'
91             filters => [ $sub ]
92             }
93             });
94             $dv->get_value('name'); # No whitespace!
95              
96             =head1 METHODS
97              
98             =head2 collapse
99              
100             Collapses all consecutive whitespace into a single space
101              
102             =head2 flatten
103              
104             Removes B<all whitespace>.
105              
106             =head2 lower
107              
108             Converts the value to lowercase.
109              
110             =head2 trim
111              
112             Removes leading and trailing whitespace
113              
114             =head2 upper
115              
116             Converts the value to uppercase.
117              
118             =head1 AUTHOR
119              
120             Cory G Watson <gphat@cpan.org>
121              
122             =head1 COPYRIGHT AND LICENSE
123              
124             This software is copyright (c) 2019 by Cold Hard Code, LLC.
125              
126             This is free software; you can redistribute it and/or modify it under
127             the same terms as the Perl 5 programming language system itself.
128              
129             =cut