| 1 | package Catmandu::Fix::trim; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 1 | ▶ | use Catmandu::Sane; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | |||||||||||||||||||
| 5 | 1 | our $VERSION = '1.0201_02'; | |||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 1 | ▶ | use Unicode::Normalize; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 1 | ▶ | use namespace::clean; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | 1 | ▶ | use Catmandu::Fix::Has; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | |||||||||||||||||||
| 12 | 1 | has path => (fix_arg => 1); | |||||||||||||||||
| 13 | 2 | ▶ | has mode => (fix_arg => 1, default => sub {'whitespace'}); | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | 1 | with 'Catmandu::Fix::SimpleGetValue'; | |||||||||||||||||
| 16 | |||||||||||||||||||
| 17 | sub emit_value { | ||||||||||||||||||
| 18 | 7 | ▶ | my ($self, $var) = @_; | ||||||||||||||||
|
Subroutine
emit_value: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | 7 | my $perl = "if (is_string(${var})) {"; | |||||||||||||||||
| 21 | 7 | ▶ | if ($self->mode eq 'whitespace') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 22 | 5 | $perl .= "${var} = trim(${var});"; | |||||||||||||||||
| 23 | } | ||||||||||||||||||
| 24 | elsif ($self->mode eq 'nonword') { | ||||||||||||||||||
| 25 | 1 | $perl .= $var . ' =~ s/^\W+//;'; | |||||||||||||||||
| 26 | 1 | $perl .= $var . ' =~ s/\W+$//;'; | |||||||||||||||||
| 27 | } | ||||||||||||||||||
| 28 | elsif ($self->mode eq 'diacritics') { | ||||||||||||||||||
| 29 | 1 | $perl .= "${var} = Unicode::Normalize::NFKD(${var});"; | |||||||||||||||||
| 30 | 1 | $perl .= "${var} =~ s/\\p{NonspacingMark}//g;"; | |||||||||||||||||
| 31 | } | ||||||||||||||||||
| 32 | 7 | $perl .= "}"; | |||||||||||||||||
| 33 | 7 | $perl; | |||||||||||||||||
| 34 | } | ||||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | 1 | 1; | |||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | __END__ | ||||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | =pod | ||||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | =encoding utf-8 | ||||||||||||||||||
| 43 | |||||||||||||||||||
| 44 | =head1 NAME | ||||||||||||||||||
| 45 | |||||||||||||||||||
| 46 | Catmandu::Fix::trim - trim leading and ending junk from the value of a field | ||||||||||||||||||
| 47 | |||||||||||||||||||
| 48 | =head1 SYNOPSIS | ||||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | # the default mode trims whitespace | ||||||||||||||||||
| 51 | # e.g. foo => ' abc '; | ||||||||||||||||||
| 52 | |||||||||||||||||||
| 53 | trim(foo) # foo => 'abc'; | ||||||||||||||||||
| 54 | trim(foo, whitespace) # foo => 'abc'; | ||||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | # trim non-word characters | ||||||||||||||||||
| 57 | # e.g. foo => ' abc / : .'; | ||||||||||||||||||
| 58 | trim(foo, nonword) # foo => 'abc'; | ||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | # trim accents | ||||||||||||||||||
| 61 | # e.g. foo => 'français' ; | ||||||||||||||||||
| 62 | trim(foo,diacritics) # foo => 'francais' | ||||||||||||||||||
| 63 | |||||||||||||||||||
| 64 | =head1 SEE ALSO | ||||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | L<Catmandu::Fix> | ||||||||||||||||||
| 67 | |||||||||||||||||||
| 68 | =cut | ||||||||||||||||||