File Coverage

blib/lib/String/Ident.pm
Criterion Covered Total %
statement 39 39 100.0
branch 13 16 81.2
condition n/a
subroutine 9 9 100.0
pod 4 4 100.0
total 65 68 95.5


line stmt bran cond sub pod time code
1             package String::Ident;
2              
3 1     1   393594 use warnings;
  1         2  
  1         52  
4 1     1   5 use strict;
  1         2  
  1         30  
5 1     1   3 use utf8;
  1         1  
  1         6  
6              
7             our $VERSION = 0.05;
8              
9 1     1   461 use Text::Unidecode 'unidecode';
  1         1086  
  1         65  
10 1     1   6 use Scalar::Util 'blessed';
  1         2  
  1         399  
11              
12             sub new {
13 9     9 1 2970 my ( $class, %args ) = @_;
14             return bless {
15             min_len => $args{min_len},
16             max_len => $args{max_len},
17 9         27 }, $class;
18             }
19              
20             sub min_len {
21 18     18 1 20 my ( $self, $new ) = @_;
22 18 50       27 $self->{min_len} = $new if @_ > 1;
23             $self->{min_len} = 4
24 18 100       29 unless defined( $self->{min_len} );
25 18         27 return $self->{min_len};
26             }
27              
28             sub max_len {
29 8     8 1 10 my ( $self, $new ) = @_;
30 8 50       14 $self->{max_len} = $new if @_ > 1;
31             $self->{max_len} = 30
32 8 100       15 unless defined( $self->{max_len} );
33 8         11 return $self->{max_len};
34             }
35              
36             sub cleanup {
37 12     12 1 2119 my ( $self, $text, $max_len ) = @_;
38              
39 12 100       34 $self = $self->new( max_len => $max_len )
40             unless blessed($self);
41 12 100       38 $max_len = ( defined($max_len) ? $max_len : $self->max_len );
42              
43 12 50       19 $text = '' unless defined($text);
44 12         21 $text = unidecode($text);
45 12         3439 $text =~ s/[^-A-Za-z0-9]/-/g;
46 12         28 $text =~ s/--+/-/g;
47 12         25 $text =~ s/-$//g;
48 12         13 $text =~ s/^-//g;
49              
50 12 100       26 if ( $max_len > 0 ) {
51 8         16 $text = substr( $text, 0, $max_len );
52             }
53 12         21 while ( length($text) < $self->min_len ) {
54 6         43 $text .= chr( ord('a') + rand( ord('z') - ord('a') + 1 ) );
55             }
56              
57 12         53 return $text;
58             }
59              
60             1;
61              
62             __END__