File Coverage

blib/lib/Data/Sah/Filter/perl/Filename/Safe/alphanum_dash.pm
Criterion Covered Total %
statement 8 15 53.3
branch n/a
condition 0 2 0.0
subroutine 3 5 60.0
pod 0 2 0.0
total 11 24 45.8


line stmt bran cond sub pod time code
1             package Data::Sah::Filter::perl::Filename::Safe::alphanum_dash;
2              
3 1     1   275422 use 5.010001;
  1         4  
4 1     1   3 use strict;
  1         2  
  1         18  
5 1     1   3 use warnings;
  1         2  
  1         304  
6              
7             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8             our $DATE = '2024-01-22'; # DATE
9             our $DIST = 'Data-Sah-FilterBundle-Filename-Safe'; # DIST
10             our $VERSION = '0.001'; # VERSION
11              
12             sub meta {
13             +{
14 0     0 0   v => 1,
15             summary => 'Replace characters that are not [A-Za-z0-9_-] with underscore (_)',
16             description => <<'MARKDOWN',
17              
18             This is just like the `alphanum`
19             () filter except dash
20             (`-`) is also allowed.
21              
22             If you want to avoid the first character being dash, you can use the
23             `alphanum_dash_no_dash_at_start`
24             ()
25             filter instead.
26              
27             MARKDOWN
28             might_fail => 0,
29             args => {
30             },
31             examples => [
32             {value => '', filtered_value => ''},
33             {value => 'foo', filtered_value => 'foo'},
34             {value => '123 456-789.foo.bar', filtered_value => '123_456-789_foo.bar'},
35             {value => '-123 456-789.foo.bar', filtered_value => '-123_456-789_foo.bar'},
36             ],
37             };
38             }
39              
40             sub filter {
41 0     0 0   my %fargs = @_;
42              
43 0           my $dt = $fargs{data_term};
44 0   0       my $gen_args = $fargs{args} // {};
45              
46 0           my $res = {};
47 0           $res->{expr_filter} = join(
48             "",
49             "do { ", (
50             "my \$tmp = $dt; my \$ext; \$tmp =~ s/\\.(\\w+)\\z// and \$ext = \$1; \$tmp =~ s/[^A-Za-z0-9_-]+/_/g; defined(\$ext) ? \"\$tmp.\$ext\" : \$tmp",
51             ), "}",
52             );
53              
54 0           $res;
55             }
56              
57             1;
58             # ABSTRACT: Replace characters that are not [A-Za-z0-9_-] with underscore (_)
59              
60             __END__