From 1e9e2413928139ca5f8da8874c41ef8118fc5e69 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 14 Sep 2016 14:48:00 -0300 Subject: [PATCH] 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. --- Koha/Util/Normalize.pm | 105 ++++++++++++++++++++++++++++++++++++++++++++++++ t/Koha/Util/Normalize.t | 67 ++++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 Koha/Util/Normalize.pm create mode 100644 t/Koha/Util/Normalize.t diff --git a/Koha/Util/Normalize.pm b/Koha/Util/Normalize.pm new file mode 100644 index 0000000..487d5da --- /dev/null +++ b/Koha/Util/Normalize.pm @@ -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 + +Tomas Cohen Arazi + +=cut diff --git a/t/Koha/Util/Normalize.t b/t/Koha/Util/Normalize.t new file mode 100644 index 0000000..9ebdf06 --- /dev/null +++ b/t/Koha/Util/Normalize.t @@ -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 . + +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; -- 2.7.4