Commits

Toby Inkster committed f5d143f

Add NodeList->map

  • Participants
  • Parent commits b3c6843

Comments (0)

Files changed (2)

lib/XML/LibXML/NodeList.pm

 sub to_literal {
     my $self = CORE::shift;
     return XML::LibXML::Literal->new(
-            join('', grep {defined $_} map { $_->string_value } @$self)
+            join('', grep {defined $_} CORE::map { $_->string_value } @$self)
             );
 }
 
     return undef;
 }
 
+sub map {
+    my $self = CORE::shift;
+    my $sub  = CORE::shift;
+    local $_;
+    my @results = CORE::map { @{[ $sub->($_) ]} } @$self;    
+    return wantarray ? @results : (ref $self)->new(@results);
+}
+
 1;
 __END__
 
 use strict;
 use warnings;
 
-use Test::More tests => 13;
+use Test::More tests => 17;
 
 use XML::LibXML;
 use IO::Handle;
 # TEST
 is(ref($dom->find("//CCC")), "XML::LibXML::NodeList", ' TODO : Add test name');
 
+my $numbers = XML::LibXML::NodeList->new(1..10);
+my $oddify  = sub { $_ + ($_%2?0:9) }; # add 9 to even numbers
+my @map = $numbers->map($oddify);
+
+# TEST
+is(scalar(@map), 10, 'map called in list context returns list');
+
+# TEST
+is(join('|',@map), '1|11|3|13|5|15|7|17|9|19', 'mapped data correct');
+
+my $map = $numbers->map($oddify);
+
+# TEST
+isa_ok($map => 'XML::LibXML::NodeList', '$map');
+
+my @map2 = $map->map(sub { $_ > 10 ? () : ($_,$_,$_) });
+
+# TEST
+is(join('|',@map2), '1|1|1|3|3|3|5|5|5|7|7|7|9|9|9', 'mapping can add/remove nodes');
+
 __DATA__
 <AAA>
 <BBB>OK</BBB>