Bug 17302: Add Koha::Util::Normalize for normalization functions

This patch introduces Koha::Util::Normalize, which includes the following normalization routines
that need no explanation:

- remove_spaces
- upper_case
- lower_case

and it also includes:

- legacy_default: which basically does what C4::Matcher::_normalize does.

All routines functionality are fully tested with the included in the included tests.

To test:
- Apply the patch
- Run:
  $ prove t/Koha/Util/Normalize.t
=> SUCCESS: All tests pass
- Sign off :-D

Edit: Added Exporter to explicitly export the routines.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Katrin Fischer  <katrin.fischer@bsz-bw.de>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
This commit is contained in:
Tomas Cohen Arazi 2016-09-14 14:48:00 -03:00 committed by Kyle M Hall
parent 7cb3a607d8
commit bbf2aff7d2
2 changed files with 172 additions and 0 deletions

105
Koha/Util/Normalize.pm Normal file
View File

@ -0,0 +1,105 @@
package Koha::Util::Normalize;
# Copyright 2016 Koha Development Team
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with Koha; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
use Modern::Perl;
use parent qw( Exporter );
our @EXPORT = qw(
legacy_default
remove_spaces
upper_case
lower_case
);
=head1 NAME
Koha::Util::Normalize - utility class with string normalization routines
=head1 METHODS
=head2 legacy_default
Default normalization function
=cut
sub legacy_default {
my $string = uc shift;
$string =~ s/[.;:,\]\[\)\(\/'"]//g;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
$string =~ s/\s+/ /g;
return $string;
}
=head2 remove_spaces
Normalization function removing spaces
=cut
sub remove_spaces {
my $string = shift;
$string =~ s/\s+//g;
return $string;
}
=head2 upper_case
Normalization function converting characters into upper-case
=cut
sub upper_case {
my $string = uc shift;
return $string;
}
=head2 lower_case
Normalization function converting characters into lower-case
=cut
sub lower_case {
my $string = lc shift;
return $string;
}
1;
__END__
=head1 AUTHOR
Koha Development Team <http://koha-community.org/>
Tomas Cohen Arazi <tomascohen@theke.io>
=cut

67
t/Koha/Util/Normalize.t Normal file
View File

@ -0,0 +1,67 @@
#!/usr/bin/perl
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Test::More tests => 5;
BEGIN {
use_ok('Koha::Util::Normalize');
}
subtest 'legacy_default() normalizer' => sub {
plan tests => 1;
my $string = ' .; kY[]:, (l)/E\'"';
is( Koha::Util::Normalize::legacy_default( $string ), 'KY LE',
'The \'legacy_default\' normalizer removes: .;:,][)(/\'" and shifts characters upper-case.
Also removes spaces from the beginning and ending, and replaces multiple spaces with a single one.' );
};
subtest 'remove_spaces() normalizer' => sub {
plan tests => 1;
my $string = ' .; kY[]:, (l)/E\'"';
is( Koha::Util::Normalize::remove_spaces( $string ), '.;kY[]:,(l)/E\'"',
'The \'remove_spaces\' normalizer removes all spaces' );
};
subtest 'upper_case() normalizer' => sub {
plan tests => 1;
my $string = ' .; kY[]:, (l)/E\'"';
is( Koha::Util::Normalize::upper_case( $string ), ' .; KY[]:, (L)/E\'"',
'The \'upper_case\' normalizer only makes characters upper-case' );
};
subtest 'lower_case() normalizer' => sub {
plan tests => 1;
my $string = ' .; kY[]:, (l)/E\'"';
is( Koha::Util::Normalize::lower_case( $string ), ' .; ky[]:, (l)/e\'"',
'The \'lower_case\' normalizer only makes characters lower-case' );
};
1;