Skip to content

Commit

Permalink
added regular expression support to grep method in Mojo::Collection (c…
Browse files Browse the repository at this point in the history
…loses #347)
  • Loading branch information
kraih committed Jul 3, 2012
1 parent f77b0ed commit 1585084
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 13 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

3.02 2012-07-02
3.02 2012-07-03
- Added regular expression support to grep method in Mojo::Collection.
- Improved documentation.
- Improved tests.
- Fixed JSON Pointer escaping.
Expand Down
11 changes: 7 additions & 4 deletions lib/Mojo/Collection.pm
Expand Up @@ -36,7 +36,8 @@ sub first {
# I can get by with one."
sub grep {
my ($self, $cb) = @_;
return $self->new(grep { $_->$cb } @$self);
return $self->new(grep { $_->$cb } @$self) if ref $cb eq 'CODE';
return $self->new(grep { $_ =~ $cb } @$self);

This comment has been minimized.

Copy link
@Suor

Suor Jul 3, 2012

Using ~~ operator here would add some extra functionality, also ref check would be unnecessary.

}

sub join {
Expand Down Expand Up @@ -141,12 +142,14 @@ which the closure returns true.
=head2 C<grep>
my $new = $collection->grep(qr/foo/);
my $new = $collection->grep(sub {...});
Evaluate closure for each element in collection and create a new collection
with all elements for which the closure returned true.
Evaluate regular expression or closure for each element in collection and
create a new collection with all elements that matched the regular expression,
or for which the closure returned true.
my $interesting = $collection->grep(sub { /mojo/i });
my $interesting = $collection->grep(qr/mojo/i);
=head2 C<join>
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Lite.pm
Expand Up @@ -499,8 +499,8 @@ just make a list of possible values.
app->start;
All placeholders get compiled to a regex internally, this process can also be
easily customized.
All placeholders get compiled to a regular expression internally, this process
can also be easily customized.
use Mojolicious::Lite;
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Routes/Pattern.pm
Expand Up @@ -282,7 +282,7 @@ L<Mojolicious::Routes::Pattern> implements the following attributes.
my $constraints = $pattern->constraints;
$pattern = $pattern->constraints({foo => qr/\w+/});
Regex constraints.
Regular expression constraints.
=head2 C<defaults>
Expand All @@ -296,7 +296,7 @@ Default parameters.
my $regex = $pattern->format_regex;
$pattern = $pattern->format_regex($regex);
Compiled regex for format matching.
Compiled regular expression for format matching.
=head2 C<pattern>
Expand Down Expand Up @@ -338,7 +338,7 @@ Character indicating the start of a quoted placeholder, defaults to C<(>.
my $regex = $pattern->regex;
$pattern = $pattern->regex($regex);
Pattern in compiled regex form.
Pattern in compiled regular expression form.
=head2 C<relaxed_start>
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/bytestream.t
Expand Up @@ -406,7 +406,7 @@ is $buffer, "test\n123\n", 'right output';
my $file = catfile(splitdir($FindBin::Bin), qw(templates exception.mt));
$stream = b($file)->slurp;
is $stream, "test\n% die;\n123\n", 'right content';
$stream = b($file)->slurp->split("\n")->grep(sub {/die/})->join('');
$stream = b($file)->slurp->split("\n")->grep(qr/die/)->join('');
is $stream, '% die;', 'right content';

# secure_compare
Expand Down
3 changes: 1 addition & 2 deletions t/mojo/collection.t
Expand Up @@ -36,8 +36,7 @@ is $collection->first(sub { defined $_ }), undef, 'no result';

# grep
$collection = c(1, 2, 3, 4, 5, 6, 7, 8, 9);
is_deeply [$collection->grep(sub { $_ > 5 })->each], [6, 7, 8, 9],
'right elements';
is_deeply [$collection->grep(qr/[6-9]/)->each], [6, 7, 8, 9], 'right elements';
is_deeply [$collection->grep(sub { $_ < 5 })->each], [1, 2, 3, 4],
'right elements';
is_deeply [$collection->grep(sub { shift == 5 })->each], [5], 'right elements';
Expand Down

0 comments on commit 1585084

Please sign in to comment.