Skip to content

Commit

Permalink
added flatten method to Mojo::Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 20, 2013
1 parent 83a2022 commit 3609300
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@

4.40 2013-09-20
- Added siblings method to Mojo::DOM.
- Added flatten method to Mojo::Collection.
- Fixed smart whitespace trimming bug in Mojo::DOM.
- Fixed table parsing bug in Mojo::DOM::HTML.

Expand Down
35 changes: 21 additions & 14 deletions lib/Mojo/Collection.pm
Expand Up @@ -50,16 +50,15 @@ sub first {
return List::Util::first { $_ =~ $cb } @$self;
}

sub flatten { $_[0]->new(_flatten(@{$_[0]})) }

sub grep {
my ($self, $cb) = @_;
return $self->new(grep { $cb->($_) } @$self) if ref $cb eq 'CODE';
return $self->new(grep { $_ =~ $cb } @$self);
}

sub join {
my ($self, $expr) = @_;
return Mojo::ByteStream->new(join $expr, map({"$_"} @$self));
}
sub join { Mojo::ByteStream->new(join $_[1], map({"$_"} @{$_[0]})) }

sub map {
my ($self, $cb) = @_;
Expand All @@ -71,15 +70,9 @@ sub pluck {
return $self->map(sub { $_->$method(@args) });
}

sub reverse {
my $self = shift;
return $self->new(reverse @$self);
}
sub reverse { $_[0]->new(reverse @{$_[0]}) }

sub shuffle {
my $self = shift;
return $self->new(List::Util::shuffle @$self);
}
sub shuffle { $_[0]->new(List::Util::shuffle @{$_[0]}) }

sub size { scalar @{$_[0]} }

Expand All @@ -101,6 +94,12 @@ sub uniq {
return $self->grep(sub { !$seen{$_}++ });
}

sub _flatten {
map { _ref($_) ? _flatten(@$_) : $_ } @_;
}

sub _ref { defined $_[0] && (ref $_[0] eq 'ARRAY' || $_[0]->isa(__PACKAGE__)) }

1;

=encoding utf8
Expand Down Expand Up @@ -160,8 +159,9 @@ string.
my @elements = $collection->each;
$collection = $collection->each(sub {...});
Evaluate callback for each element in collection. The element will be the
first argument passed to the callback and is also available as C<$_>.
Evaluate callback for each element in collection or return all elements as a
list if none has been provided. The element will be the first argument passed
to the callback and is also available as C<$_>.
$collection->each(sub {
my ($e, $count) = @_;
Expand All @@ -181,6 +181,13 @@ callback and is also available as C<$_>.
my $five = $collection->first(sub { $_ == 5 });
=head2 flatten
my $new = $collection->flatten;
Flatten nested collections/arrays recursively and create a new collection with
all elements.
=head2 grep
my $new = $collection->grep(qr/foo/);
Expand Down
6 changes: 6 additions & 0 deletions t/mojo/collection.t
Expand Up @@ -21,6 +21,12 @@ is_deeply [c(undef, 0, 1, '', 2, 3)->compact->each], [0, 1, 2, 3],
is_deeply [c(3, 2, 1)->compact->each], [3, 2, 1], 'right result';
is_deeply [c()->compact->each], [], 'right result';

# flatten
is_deeply [c(1, 2, [3, 4], 5, c(6, 7))->flatten->each], [1, 2, 3, 4, 5, 6, 7],
'right result';
is_deeply [c(undef, 1, [2, [3, c(4, 5)]], undef, 6)->flatten->each],
[undef, 1, 2, 3, 4, 5, undef, 6], 'right result';

# each
$collection = c(3, 2, 1);
is_deeply [$collection->each], [3, 2, 1], 'right elements';
Expand Down
4 changes: 4 additions & 0 deletions t/mojo/dom.t
Expand Up @@ -1390,6 +1390,10 @@ is $dom->find('tbody > tr > .gamma')->[0]->text, '', 'no text';
is $dom->find('tbody > tr > .gamma > a')->[0]->text, 'Gamma', 'right text';
is $dom->find('tbody > tr > .alpha')->[1]->text, 'Alpha Two', 'right text';
is $dom->find('tbody > tr > .gamma > a')->[1]->text, 'Gamma Two', 'right text';
my @siblings = $dom->find('tr > td:nth-child(1)')->siblings(':nth-child(even)')
->flatten->all_text->each;
is_deeply \@siblings, ['Beta', 'Delta', 'Beta Two', 'Delta Two'],
'right results';

# Real world list
$dom = Mojo::DOM->new->parse(<<EOF);
Expand Down

0 comments on commit 3609300

Please sign in to comment.