File Coverage

blib/lib/Data/Sah/Filter/perl/Filename/Safe/alphanum_dash_no_dash_at_start.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_no_dash_at_start;
2              
3 1     1   514484 use 5.010001;
  1         5  
4 1     1   7 use strict;
  1         2  
  1         32  
5 1     1   12 use warnings;
  1         3  
  1         323  
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 (_), avoid dash at the beginning',
16             description => <<'MARKDOWN',
17              
18             This is just like the `alphanum_dash`
19             () filter except dash
20             at the beginning is avoided by prefixing with an additional underscore, since
21             dash at the beginning is often problematic with Unix/GNU command-line by being
22             confused with a command-line option.
23              
24             MARKDOWN
25             might_fail => 0,
26             args => {
27             },
28             examples => [
29             {value => '', filtered_value => ''},
30             {value => 'foo', filtered_value => 'foo'},
31             {value => '123 456-789.foo.bar', filtered_value => '123_456-789_foo.bar'},
32             {value => '-123 456-789.foo.bar', filtered_value => '_-123_456-789_foo.bar'},
33             ],
34             };
35             }
36              
37             sub filter {
38 0     0 0   my %fargs = @_;
39              
40 0           my $dt = $fargs{data_term};
41 0   0       my $gen_args = $fargs{args} // {};
42              
43 0           my $res = {};
44 0           $res->{expr_filter} = join(
45             "",
46             "do { ", (
47             "my \$tmp = $dt; my \$ext; \$tmp =~ s/\\.(\\w+)\\z// and \$ext = \$1; \$tmp =~ s/[^A-Za-z0-9_-]+/_/g; \$tmp = \"_\$tmp\" if \$tmp =~ /\\A-/; defined(\$ext) ? \"\$tmp.\$ext\" : \$tmp",
48             ), "}",
49             );
50              
51 0           $res;
52             }
53              
54             1;
55             # ABSTRACT: Replace characters that are not [A-Za-z0-9_-] with underscore (_), avoid dash at the beginning
56              
57             __END__