File Coverage

blib/lib/Data/Sah/Filter/perl/Filename/Safe/alphanum.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;
2              
3 1     1   447848 use 5.010001;
  1         4  
4 1     1   5 use strict;
  1         3  
  1         36  
5 1     1   6 use warnings;
  1         2  
  1         342  
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             Multiple consecutive unwanted characters will be replaced with a single
19             underscore.
20              
21             A single filename extension is allowed, e.g. "foo bar.exe" -> "foo_bar.exe", but
22             additional filename extensions will be made 'safe' too, e.g. "foo bar.doc.pdf"
23             -> 'foo_bar_doc.pdf'.
24              
25             If you want to avoid having digit as the first character, use the
26             `alphanum_identifier`
27             () filter
28             instead.
29              
30             MARKDOWN
31             might_fail => 0,
32             args => {
33             },
34             examples => [
35             {value => '', filtered_value => ''},
36             {value => 'foo', filtered_value => 'foo'},
37             {value => '123 456-789.foo.bar', filtered_value => '123_456_789_foo.bar'},
38             ],
39             };
40             }
41              
42             sub filter {
43 0     0 0   my %fargs = @_;
44              
45 0           my $dt = $fargs{data_term};
46 0   0       my $gen_args = $fargs{args} // {};
47              
48 0           my $res = {};
49 0           $res->{expr_filter} = join(
50             "",
51             "do { ", (
52             "my \$tmp = $dt; my \$ext; \$tmp =~ s/\\.(\\w+)\\z// and \$ext = \$1; \$tmp =~ s/[^A-Za-z0-9_]+/_/g; defined(\$ext) ? \"\$tmp.\$ext\" : \$tmp",
53             ), "}",
54             );
55              
56 0           $res;
57             }
58              
59             1;
60             # ABSTRACT: Replace characters that are not [A-Za-z0-9_] with underscore (_)
61              
62             __END__