View | Details | Raw Unified | Return to bug 17302
Collapse All | Expand All

(-)a/Koha/Util/Normalize.pm (+105 lines)
Line 0 Link Here
1
package Koha::Util::Normalize;
2
3
# Copyright 2016 Koha Development Team
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
22
use parent qw( Exporter );
23
24
our @EXPORT = qw(
25
  legacy_default
26
  remove_spaces
27
  upper_case
28
  lower_case
29
);
30
31
=head1 NAME
32
33
Koha::Util::Normalize - utility class with string normalization routines
34
35
=head1 METHODS
36
37
=head2 legacy_default
38
39
Default normalization function
40
41
=cut
42
43
sub legacy_default {
44
45
    my $string = uc shift;
46
47
    $string =~ s/[.;:,\]\[\)\(\/'"]//g;
48
    $string =~ s/^\s+//;
49
    $string =~ s/\s+$//;
50
    $string =~ s/\s+/ /g;
51
52
    return $string;
53
}
54
55
=head2 remove_spaces
56
57
Normalization function removing spaces
58
59
=cut
60
61
sub remove_spaces {
62
63
    my $string = shift;
64
65
    $string =~ s/\s+//g;
66
67
    return $string;
68
}
69
70
=head2 upper_case
71
72
Normalization function converting characters into upper-case
73
74
=cut
75
76
sub upper_case {
77
78
    my $string = uc shift;
79
80
    return $string;
81
}
82
83
=head2 lower_case
84
85
Normalization function converting characters into lower-case
86
87
=cut
88
89
sub lower_case {
90
91
    my $string = lc shift;
92
93
    return $string;
94
}
95
96
1;
97
__END__
98
99
=head1 AUTHOR
100
101
Koha Development Team <http://koha-community.org/>
102
103
Tomas Cohen Arazi <tomascohen@theke.io>
104
105
=cut
(-)a/t/Koha/Util/Normalize.t (-1 / +67 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 5;
21
22
BEGIN {
23
    use_ok('Koha::Util::Normalize');
24
}
25
26
subtest 'legacy_default() normalizer' => sub {
27
28
    plan tests => 1;
29
30
    my $string = '  .; kY[]:,  (l)/E\'"';
31
32
    is( Koha::Util::Normalize::legacy_default( $string ), 'KY LE',
33
        'The \'legacy_default\' normalizer removes: .;:,][)(/\'" and shifts characters upper-case.
34
         Also removes spaces from the beginning and ending, and replaces multiple spaces with a single one.' );
35
};
36
37
subtest 'remove_spaces() normalizer' => sub {
38
39
    plan tests => 1;
40
41
    my $string = '  .; kY[]:,  (l)/E\'"';
42
43
    is( Koha::Util::Normalize::remove_spaces( $string ), '.;kY[]:,(l)/E\'"',
44
        'The \'remove_spaces\' normalizer removes all spaces' );
45
};
46
47
subtest 'upper_case() normalizer' => sub {
48
49
    plan tests => 1;
50
51
    my $string = '  .; kY[]:,  (l)/E\'"';
52
53
    is( Koha::Util::Normalize::upper_case( $string ), '  .; KY[]:,  (L)/E\'"',
54
        'The \'upper_case\' normalizer only makes characters upper-case' );
55
};
56
57
subtest 'lower_case() normalizer' => sub {
58
59
    plan tests => 1;
60
61
    my $string = '  .; kY[]:,  (l)/E\'"';
62
63
    is( Koha::Util::Normalize::lower_case( $string ), '  .; ky[]:,  (l)/e\'"',
64
        'The \'lower_case\' normalizer only makes characters lower-case' );
65
};
66
67
1;

Return to bug 17302