Skip to content

Commit

Permalink
added compact method to Mojo::Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 17, 2013
1 parent cbfa6b7 commit 9e4cc39
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@

4.25 2013-08-17
- Added support for calling element methods to Mojo::Collection.
- Added compact method to Mojo::Collection.

4.24 2013-08-08
- Added ancestors method to Mojo::DOM.
Expand Down
11 changes: 11 additions & 0 deletions lib/Mojo/Collection.pm
Expand Up @@ -29,6 +29,10 @@ sub new {

sub c { __PACKAGE__->new(@_) }

sub compact {
shift->grep(sub {length});
}

sub each {
my ($self, $cb) = @_;
return @$self unless $cb;
Expand Down Expand Up @@ -142,6 +146,13 @@ L<Mojo::Collection> implements the following methods.
Construct a new array-based L<Mojo::Collection> object.
=head2 compact
my $new = $collection->compact;
Create a new collection with all elements that are defined and not an empty
string.
=head2 each
my @elements = $collection->each;
Expand Down
6 changes: 6 additions & 0 deletions t/mojo/collection.t
Expand Up @@ -15,6 +15,12 @@ is_deeply [@$collection], [1, 2, 3, 4, 5], 'right result';
is_deeply [c(1, 2, 3)->tap(sub { $_->[1] += 2 })->each], [1, 4, 3],
'right result';

# compact
is_deeply [c(undef, 0, 1, '', 2, 3)->compact->each], [0, 1, 2, 3],
'right result';
is_deeply [c(3, 2, 1)->compact->each], [3, 2, 1], 'right result';
is_deeply [c()->compact->each], [], 'right result';

# each
$collection = c(3, 2, 1);
is_deeply [$collection->each], [3, 2, 1], 'right elements';
Expand Down

0 comments on commit 9e4cc39

Please sign in to comment.