Skip to content

Commit

Permalink
added to_array method to Mojo::Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 11, 2014
1 parent f711943 commit a3a14db
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 45 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

5.60 2014-11-08
5.60 2014-11-11
- Added to_array method to Mojo::Collection.
- Updated Net::DNS::Native requirement to 0.12 for some important bug fixes.

5.59 2014-11-07
Expand Down
8 changes: 8 additions & 0 deletions lib/Mojo/Collection.pm
Expand Up @@ -118,6 +118,8 @@ sub sort {

sub tap { shift->Mojo::Base::tap(@_) }

sub to_array { [@{shift()}] }

sub uniq {
my %seen;
return $_[0]->new(grep { !$seen{$_}++ } @{$_[0]});
Expand Down Expand Up @@ -329,6 +331,12 @@ from the results.
Alias for L<Mojo::Base/"tap">.
=head2 to_array
my $array = $collection->to_array;
Turn collection into array reference.
=head2 uniq
my $new = $collection->uniq;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Lite.pm
Expand Up @@ -874,7 +874,7 @@ L<Mojo::JSON> and L<Mojo::DOM> this can be a very powerful tool.
my $c = shift;
my $url = $c->param('url') || 'http://mojolicio.us';
my $dom = $c->ua->get($url)->res->dom;
$c->render(json => [$dom->find('h1, h2, h3')->map('text')->each]);
$c->render(json => $dom->find('h1, h2, h3')->map('text')->to_array);
};
# Non-blocking
Expand Down
12 changes: 6 additions & 6 deletions t/mojo/bytestream.t
Expand Up @@ -101,12 +101,12 @@ ok !ref $stream->to_string, 'nested bytestream stringified';

# split
$stream = b('1,2,3,4,5');
is_deeply [$stream->split(',')->each], [1, 2, 3, 4, 5], 'right elements';
is_deeply [$stream->split(qr/,/)->each], [1, 2, 3, 4, 5], 'right elements';
is_deeply [b('54321')->split('')->each], [5, 4, 3, 2, 1], 'right elements';
is_deeply [b('')->split('')->each], [], 'no elements';
is_deeply [b('')->split(',')->each], [], 'no elements';
is_deeply [b('')->split(qr/,/)->each], [], 'no elements';
is_deeply $stream->split(',')->to_array, [1, 2, 3, 4, 5], 'right elements';
is_deeply $stream->split(qr/,/)->to_array, [1, 2, 3, 4, 5], 'right elements';
is_deeply b('54321')->split('')->to_array, [5, 4, 3, 2, 1], 'right elements';
is_deeply b('')->split('')->to_array, [], 'no elements';
is_deeply b('')->split(',')->to_array, [], 'no elements';
is_deeply b('')->split(qr/,/)->to_array, [], 'no elements';
$stream = b('1/2/3');
is $stream->split('/')->map(sub { $_->quote })->join(', '), '"1", "2", "3"',
'right result';
Expand Down
70 changes: 36 additions & 34 deletions t/mojo/collection.t
Expand Up @@ -12,19 +12,19 @@ push @$collection, 3, 4, 5;
is_deeply [@$collection], [1, 2, 3, 4, 5], 'right result';

# Tap into method chain
is_deeply [c(1, 2, 3)->tap(sub { $_->[1] += 2 })->each], [1, 4, 3],
is_deeply c(1, 2, 3)->tap(sub { $_->[1] += 2 })->to_array, [1, 4, 3],
'right result';

# compact
is_deeply [c(undef, 0, 1, '', 2, 3)->compact->each], [0, 1, 2, 3],
is_deeply c(undef, 0, 1, '', 2, 3)->compact->to_array, [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';
is_deeply c(3, 2, 1)->compact->to_array, [3, 2, 1], 'right result';
is_deeply c()->compact->to_array, [], '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],
is_deeply c(1, 2, [3, 4], 5, c(6, 7))->flatten->to_array,
[1, 2, 3, 4, 5, 6, 7], 'right result';
is_deeply c(undef, 1, [2, {}, [3, c(4, 5)]], undef, 6)->flatten->to_array,
[undef, 1, 2, {}, 3, 4, 5, undef, 6], 'right result';

# each
Expand Down Expand Up @@ -57,16 +57,18 @@ is c()->last, undef, 'no 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';
is_deeply [$collection->grep(sub {/[6-9]/})->each], [6, 7, 8, 9],
is_deeply $collection->grep(qr/[6-9]/)->to_array, [6, 7, 8, 9],
'right elements';
is_deeply $collection->grep(sub {/[6-9]/})->to_array, [6, 7, 8, 9],
'right elements';
is_deeply $collection->grep(sub { $_ > 5 })->to_array, [6, 7, 8, 9],
'right elements';
is_deeply [$collection->grep(sub { $_ > 5 })->each], [6, 7, 8, 9],
is_deeply $collection->grep(sub { $_ < 5 })->to_array, [1, 2, 3, 4],
'right elements';
is_deeply [$collection->grep(sub { $_ < 5 })->each], [1, 2, 3, 4],
is_deeply $collection->grep(sub { shift == 5 })->to_array, [5],
'right elements';
is_deeply [$collection->grep(sub { shift == 5 })->each], [5], 'right elements';
is_deeply [$collection->grep(sub { $_ < 1 })->each], [], 'no elements';
is_deeply [$collection->grep(sub { $_ > 9 })->each], [], 'no elements';
is_deeply $collection->grep(sub { $_ < 1 })->to_array, [], 'no elements';
is_deeply $collection->grep(sub { $_ > 9 })->to_array, [], 'no elements';

# join
$collection = c(1, 2, 3);
Expand All @@ -90,18 +92,18 @@ is $collection->map(join => '-')->join("\n"), "1-2-3\n4-5-6\n7-8-9",

# reverse
$collection = c(3, 2, 1);
is_deeply [$collection->reverse->each], [1, 2, 3], 'right order';
is_deeply $collection->reverse->to_array, [1, 2, 3], 'right order';
$collection = c(3);
is_deeply [$collection->reverse->each], [3], 'right order';
is_deeply $collection->reverse->to_array, [3], 'right order';
$collection = c();
is_deeply [$collection->reverse->each], [], 'no elements';
is_deeply $collection->reverse->to_array, [], 'no elements';

# shuffle
$collection = c(0 .. 10000);
my $random = $collection->shuffle;
is $collection->size, $random->size, 'same number of elements';
isnt "@$collection", "@$random", 'different order';
is_deeply [c()->shuffle->each], [], 'no elements';
is_deeply c()->shuffle->to_array, [], 'no elements';

# size
$collection = c();
Expand All @@ -123,33 +125,33 @@ is c()->reduce(sub { $a + $b }), undef, 'no result';

# sort
$collection = c(2, 5, 4, 1);
is_deeply [$collection->sort->each], [1, 2, 4, 5], 'right order';
is_deeply [$collection->sort(sub { $b cmp $a })->each], [5, 4, 2, 1],
is_deeply $collection->sort->to_array, [1, 2, 4, 5], 'right order';
is_deeply $collection->sort(sub { $b cmp $a })->to_array, [5, 4, 2, 1],
'right order';
is_deeply [$collection->sort(sub { $_[1] cmp $_[0] })->each], [5, 4, 2, 1],
is_deeply $collection->sort(sub { $_[1] cmp $_[0] })->to_array, [5, 4, 2, 1],
'right order';
$collection = c(qw(Test perl Mojo));
is_deeply [$collection->sort(sub { uc(shift) cmp uc(shift) })->each],
is_deeply $collection->sort(sub { uc(shift) cmp uc(shift) })->to_array,
[qw(Mojo perl Test)], 'right order';
$collection = c();
is_deeply [$collection->sort->each], [], 'no elements';
is_deeply [$collection->sort(sub { $a cmp $b })->each], [], 'no elements';
is_deeply $collection->sort->to_array, [], 'no elements';
is_deeply $collection->sort(sub { $a cmp $b })->to_array, [], 'no elements';

# slice
$collection = c(1, 2, 3, 4, 5, 6, 7, 10, 9, 8);
is_deeply [$collection->slice(0)->each], [1], 'right result';
is_deeply [$collection->slice(1)->each], [2], 'right result';
is_deeply [$collection->slice(2)->each], [3], 'right result';
is_deeply [$collection->slice(-1)->each], [8], 'right result';
is_deeply [$collection->slice(-3, -5)->each], [10, 6], 'right result';
is_deeply [$collection->slice(1, 2, 3)->each], [2, 3, 4], 'right result';
is_deeply [$collection->slice(6, 1, 4)->each], [7, 2, 5], 'right result';
is_deeply [$collection->slice(6 .. 9)->each], [7, 10, 9, 8], 'right result';
is_deeply $collection->slice(0)->to_array, [1], 'right result';
is_deeply $collection->slice(1)->to_array, [2], 'right result';
is_deeply $collection->slice(2)->to_array, [3], 'right result';
is_deeply $collection->slice(-1)->to_array, [8], 'right result';
is_deeply $collection->slice(-3, -5)->to_array, [10, 6], 'right result';
is_deeply $collection->slice(1, 2, 3)->to_array, [2, 3, 4], 'right result';
is_deeply $collection->slice(6, 1, 4)->to_array, [7, 2, 5], 'right result';
is_deeply $collection->slice(6 .. 9)->to_array, [7, 10, 9, 8], 'right result';

# uniq
$collection = c(1, 2, 3, 2, 3, 4, 5, 4);
is_deeply [$collection->uniq->each], [1, 2, 3, 4, 5], 'right result';
is_deeply [$collection->uniq->reverse->uniq->each], [5, 4, 3, 2, 1],
is_deeply $collection->uniq->to_array, [1, 2, 3, 4, 5], 'right result';
is_deeply $collection->uniq->reverse->uniq->to_array, [5, 4, 3, 2, 1],
'right result';

done_testing();
6 changes: 3 additions & 3 deletions t/mojo/response.t
Expand Up @@ -1038,18 +1038,18 @@ is $res->version, '1.1', 'right version';
is $res->dom->at('p')->text, 'foo', 'right value';
is $res->dom->at('p > a')->text, 'bar', 'right value';
is $res->dom('p')->first->text, 'foo', 'right value';
is_deeply [$res->dom('p > a')->map('text')->each], [qw(bar baz)],
is_deeply $res->dom('p > a')->map('text')->to_array, [qw(bar baz)],
'right values';
my @text = $res->dom('a')->map(content => 'yada')->first->root->find('p > a')
->map('text')->each;
is_deeply \@text, [qw(yada yada)], 'right values';
is_deeply [$res->dom('p > a')->map('text')->each], [qw(yada yada)],
is_deeply $res->dom('p > a')->map('text')->to_array, [qw(yada yada)],
'right values';
@text
= $res->dom->find('a')->map(content => 'test')->first->root->find('p > a')
->map('text')->each;
is_deeply \@text, [qw(test test)], 'right values';
is_deeply [$res->dom->find('p > a')->map('text')->each], [qw(test test)],
is_deeply $res->dom->find('p > a')->map('text')->to_array, [qw(test test)],
'right values';

# Build DOM from response with charset
Expand Down

0 comments on commit a3a14db

Please sign in to comment.