Skip to content

Commit

Permalink
made split method in Mojo::ByteStream a little smarter
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 23, 2011
1 parent fb657af commit 6dfd618
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/Mojo/ByteStream.pm
Expand Up @@ -164,8 +164,8 @@ sub sha1_sum {
sub size { length shift->{bytestream} }

sub split {
my ($self, $pattern) = @_;
Mojo::Collection->new(split $pattern, $self->{bytestream});
my ($self, $p) = @_;
Mojo::Collection->new(map { $self->new($_) } split $p, $self->{bytestream});
}

sub to_string { shift->{bytestream} }
Expand Down Expand Up @@ -401,7 +401,7 @@ Size of bytestream.
Turn bytestream into L<Mojo::Collection>.
Note that this method is EXPERIMENTAL and might change without warning!
$stream->split(',')->join("\n")->say;
$stream->split(',')->map(sub { $_->quote })->join("\n")->say;
=head2 C<to_string>
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Collection.pm
Expand Up @@ -45,8 +45,8 @@ sub grep {
}

sub join {
my ($self, $chunk) = @_;
Mojo::ByteStream->new(join $chunk, map({"$_"} @$self));
my ($self, $e) = @_;
Mojo::ByteStream->new(join $e, map({"$_"} @$self));
}

sub map {
Expand Down
7 changes: 6 additions & 1 deletion t/mojo/bytestream.t
Expand Up @@ -10,7 +10,7 @@ use Test::More;

plan skip_all => 'Perl 5.10 or Digest::SHA required for this test!'
unless eval { require Digest::SHA; 1 };
plan tests => 137;
plan tests => 139;

use_ok 'Mojo::Util', 'md5_bytes';
use_ok 'Mojo::ByteStream', 'b';
Expand Down Expand Up @@ -367,6 +367,11 @@ 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';
$stream = b('1/2/3');
is $stream->split('/')->map(sub { $_->quote })->join(', '),
'"1", "2", "3"', 'right result';
is $stream->split('/')->map(sub { shift->quote })->join(', '),
'"1", "2", "3"', 'right result';

# say and autojoin
my $buffer = '';
Expand Down

0 comments on commit 6dfd618

Please sign in to comment.