r1571@llin: dpavlin | 2007-11-08 18:19:49 +0100
authorDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 8 Nov 2007 17:19:50 +0000 (17:19 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 8 Nov 2007 17:19:50 +0000 (17:19 +0000)
 added frec_eq and frec_ne to work-around our parser which removes spaces

git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@1015 07558da8-63fa-0310-ba24-9fe276d99e06

lib/WebPAC/Normalize.pm
t/3-normalize.t

index ea4c49e..305d3be 100644 (file)
@@ -16,7 +16,7 @@ our @EXPORT = qw/
        marc_template
 
        rec1 rec2 rec
-       frec
+       frec frec_eq frec_ne
        regex prefix suffix surround
        first lookup join_with
        save_into_lookup
@@ -48,7 +48,7 @@ WebPAC::Normalize - describe normalisaton rules using sets
 
 =cut
 
-our $VERSION = '0.33';
+our $VERSION = '0.34';
 
 =head1 SYNOPSIS
 
@@ -1168,6 +1168,39 @@ sub frec {
        return shift @out;
 }
 
+=head2 frec_eq
+
+=head2 frec_ne
+
+Check if first values from two fields are same or different
+
+  if ( frec_eq( 900 => 'a', 910 => 'c' ) ) {
+       # values are same
+  } else {
+    # values are different
+  }
+
+Strictly speaking C<frec_eq> and C<frec_ne> wouldn't be needed if you
+could write something like:
+
+  if ( frec( '900','a' ) eq frec( '910','c' ) ) {
+       # yada tada
+  }
+
+but you can't since our parser L<WebPAC::Parser> will remove all whitespaces
+in order to parse text and create invalid function C<eqfrec>.
+
+=cut
+
+sub frec_eq {
+       my ( $f1,$sf1, $f2, $sf2 ) = @_;
+       return (rec( $f1, $sf1 ))[0] eq (rec( $f2, $sf2 ))[0];
+}
+
+sub frec_ne {
+       return ! frec_eq( @_ );
+}
+
 =head2 regex
 
 Apply regex to some or all values
index a04a529..fd5302e 100755 (executable)
@@ -881,6 +881,17 @@ sub test_s {
        test_rule( 'frec', $rec, qq{ frec(200,'b') }, [ 'b1' ] );
        test_rule( 'frec', $rec, qq{ frec(200,'c') }, [ 'c1' ] );
 
+       $rec->{'900'} = $rec->{'200'};
+       foreach my $sf ( qw/a b c/ ) {
+               ok( frec_eq( '200' => $sf, '900' => $sf ), "frec_eq 200 == 900 $sf");
+               ok( ! frec_ne( '200' => $sf, '900' => $sf ), "! frec_ne 200 == 900 $sf");
+       }
+
+       foreach my $sf ( qw/a b/ ) {
+               ok( ! frec_eq( '200' => $sf, '200' => 'c' ), "! frec_eq 200 $sf == 200 c");
+               ok( frec_ne( '200' => $sf, '200' => 'c' ), "frec_ne 200 $sf == 200 c");
+       }
+
        # marc_template
 
        test_rec_rules(