Bug 23324: (QA follow-up) Use existing NormalizeISBN function
[koha.git] / Koha / Util / Normalize.pm
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 use Business::ISBN;
22
23 use parent qw( Exporter );
24
25 our @EXPORT = qw(
26   legacy_default
27   remove_spaces
28   upper_case
29   lower_case
30   ISBN
31 );
32
33 =head1 NAME
34
35 Koha::Util::Normalize - utility class with string normalization routines
36
37 =head1 METHODS
38
39 =head2 legacy_default
40
41 Default normalization function
42
43 =cut
44
45 sub legacy_default {
46     my ( $string ) = @_;
47     return if !defined( $string );
48
49     $string = uc $string;
50
51     $string =~ s/[.;:,\]\[\)\(\/'"]//g;
52     $string =~ s/^\s+//;
53     $string =~ s/\s+$//;
54     $string =~ s/\s+/ /g;
55
56     return $string;
57 }
58
59 =head2 remove_spaces
60
61 Normalization function removing spaces
62
63 =cut
64
65 sub remove_spaces {
66     my ( $string ) = @_;
67     return if !defined( $string );
68
69     $string =~ s/\s+//g;
70
71     return $string;
72 }
73
74 =head2 upper_case
75
76 Normalization function converting characters into upper-case
77
78 =cut
79
80 sub upper_case {
81     my ( $string ) = @_;
82     return if !defined( $string );
83
84     $string = uc $string;
85
86     return $string;
87 }
88
89 =head2 lower_case
90
91 Normalization function converting characters into lower-case
92
93 =cut
94
95 sub lower_case {
96     my ( $string ) = @_;
97     return if !defined( $string );
98
99     $string = lc $string;
100
101     return $string;
102 }
103
104 =head2 ISBN
105
106 Normalization function converting ISBN strings to ISBN13
107 If string is not a valid ISBN we pass it through unaltered
108
109 =cut
110
111 sub ISBN {
112     my ( $string ) = @_;
113     return if !defined( $string );
114
115     my $isbn = C4::Koha::NormalizeISBN({
116         isbn => $string,
117         format => 'ISBN-13',
118         strip_hyphens  => 1,
119         return_invalid => 1,
120     });
121
122     return $isbn;
123 }
124
125 1;
126 __END__
127
128 =head1 AUTHOR
129
130 Koha Development Team <http://koha-community.org/>
131
132 Tomas Cohen Arazi <tomascohen@theke.io>
133
134 =cut