File Coverage

blib/lib/Data/Sah/Filter/perl/Filename/Safe/alphanum_identifier.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_identifier;
2              
3 1     1   337043 use 5.010001;
  1         4  
4 1     1   4 use strict;
  1         2  
  1         22  
5 1     1   3 use warnings;
  1         1  
  1         216  
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 digit as first character',
16             description => <<'MARKDOWN',
17              
18             This is just like the `alphanum`
19             () filter
20             except with an additional rule: if the first character of the result is a digit
21             ([0-9]) then an underscore prefix is added.
22              
23             MARKDOWN
24             might_fail => 0,
25             args => {
26             },
27             examples => [
28             {value => '', filtered_value => ''},
29             {value => 'foo', filtered_value => 'foo'},
30             {value => '123 456-789.foo.bar', filtered_value => '_123_456_789_foo.bar'},
31             {value => 'a123 456-789.foo.bar', filtered_value => 'a123_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[0-9]/; 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 digit as first character
56              
57             __END__