Skip to content

Commit

Permalink
added last method to Mojo::Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 12, 2014
1 parent 94f6cfa commit 8854bca
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -2,6 +2,7 @@
4.80 2014-02-12
- Merged Mojo::DOM::Node into Mojo::DOM.
- Added next_sibling and previous_sibling methods to Mojo::DOM.
- Added last method to Mojo::Collection.
- Improved many methods in Mojo::DOM to work with all node types.
- Improved Mojo::DOM::HTML performance.
- Fixed list parsing bug in Mojo::DOM::HTML.
Expand Down
8 changes: 8 additions & 0 deletions lib/Mojo/Collection.pm
Expand Up @@ -55,6 +55,8 @@ sub grep {

sub join { Mojo::ByteStream->new(join $_[1] // '', map({"$_"} @{$_[0]})) }

sub last { shift->[-1] }

sub map {
my ($self, $cb) = @_;
return $self->new(map { $_->$cb } @$self);
Expand Down Expand Up @@ -215,6 +217,12 @@ Turn collection into L<Mojo::ByteStream>.
$collection->join("\n")->say;
=head2 last
my $last = $collection->last;
Return the last element in collection.
=head2 map
my $new = $collection->map(sub {...});
Expand Down
4 changes: 4 additions & 0 deletions t/mojo/collection.t
Expand Up @@ -50,6 +50,10 @@ $collection = c();
is $collection->first, undef, 'no result';
is $collection->first(sub { defined $_ }), undef, 'no result';

# last
is c(5, 4, 3)->last, 3, 'right result';
is c(5, 4, 3)->reverse->last, 5, 'right result';

# grep
$collection = c(1, 2, 3, 4, 5, 6, 7, 8, 9);
is_deeply [$collection->grep(qr/[6-9]/)->each], [6, 7, 8, 9], 'right elements';
Expand Down
26 changes: 13 additions & 13 deletions t/mojo/dom.t
Expand Up @@ -150,7 +150,7 @@ is $dom->at('p')->previous_sibling->previous_sibling, undef,
'no more siblings';
is $dom->at('p')->next_sibling->content, 'after', 'right content';
is $dom->at('p')->next_sibling->next_sibling, undef, 'no more siblings';
is $dom->at('p')->contents->[-1]->previous_sibling->previous_sibling->content,
is $dom->at('p')->contents->last->previous_sibling->previous_sibling->content,
'test', 'right content';
is $dom->at('p')->contents->first->next_sibling->next_sibling->content,
' 456 ', 'right content';
Expand All @@ -162,10 +162,10 @@ is $dom->all_contents->[2]->node, 'text', 'right node';
is $dom->all_contents->[2]->content, 'test', 'right content';
is $dom->all_contents->[5]->node, 'pi', 'right node';
is $dom->all_contents->[5]->content, 'after', 'right content';
is $dom->at('p')->all_contents->[0]->node, 'text', 'right node';
is $dom->at('p')->all_contents->[0]->content, 'test', 'right node';
is $dom->at('p')->all_contents->[-1]->node, 'comment', 'right node';
is $dom->at('p')->all_contents->[-1]->content, ' 456 ', 'right node';
is $dom->at('p')->all_contents->[0]->node, 'text', 'right node';
is $dom->at('p')->all_contents->[0]->content, 'test', 'right node';
is $dom->at('p')->all_contents->last->node, 'comment', 'right node';
is $dom->at('p')->all_contents->last->content, ' 456 ', 'right node';
is $dom->contents->[1]->contents->first->parent->type, 'p', 'right type';
is $dom->contents->[1]->contents->first->content, 'test', 'right content';
is $dom->contents->[1]->contents->first, 'test', 'right content';
Expand Down Expand Up @@ -865,14 +865,14 @@ is_deeply \@li, [qw(A C E G)], 'found all odd li elements';
@li = ();
$dom->find('li:nth-last-child(odd)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(B D F H)], 'found all odd li elements';
is $dom->find(':nth-child(odd)')->[0]->type, 'ul', 'right type';
is $dom->find(':nth-child(odd)')->[1]->text, 'A', 'right text';
is $dom->find(':nth-child(1)')->[0]->type, 'ul', 'right type';
is $dom->find(':nth-child(1)')->[1]->text, 'A', 'right text';
is $dom->find(':nth-last-child(odd)')->[0]->type, 'ul', 'right type';
is $dom->find(':nth-last-child(odd)')->[-1]->text, 'H', 'right text';
is $dom->find(':nth-last-child(1)')->[0]->type, 'ul', 'right type';
is $dom->find(':nth-last-child(1)')->[1]->text, 'H', 'right text';
is $dom->find(':nth-child(odd)')->[0]->type, 'ul', 'right type';
is $dom->find(':nth-child(odd)')->[1]->text, 'A', 'right text';
is $dom->find(':nth-child(1)')->[0]->type, 'ul', 'right type';
is $dom->find(':nth-child(1)')->[1]->text, 'A', 'right text';
is $dom->find(':nth-last-child(odd)')->[0]->type, 'ul', 'right type';
is $dom->find(':nth-last-child(odd)')->last->text, 'H', 'right text';
is $dom->find(':nth-last-child(1)')->[0]->type, 'ul', 'right type';
is $dom->find(':nth-last-child(1)')->[1]->text, 'H', 'right text';
@li = ();
$dom->find('li:nth-child(2n+1)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(A C E G)], 'found all odd li elements';
Expand Down

0 comments on commit 8854bca

Please sign in to comment.