Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added squish method to Mojo::ByteStream
  • Loading branch information
kraih committed Aug 17, 2012
1 parent 6c16c46 commit 53c0a7d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 24 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -3,6 +3,8 @@
- Added event sequentialization support to Mojo::IOLoop::Delay.
(judofyr, marcus, sri)
- Added tap method to Mojo::Base.
- Added squish method to Mojo::ByteStream.
- Added squish function to Mojo::Util.
- Improved documentation.
- Fixed json_has method in Test::Mojo.

Expand Down
12 changes: 10 additions & 2 deletions lib/Mojo/ByteStream.pm
Expand Up @@ -11,8 +11,8 @@ our @EXPORT_OK = ('b');
my @UTILS = (
qw(b64_decode b64_encode camelize decamelize hmac_md5_sum hmac_sha1_sum),
qw(html_escape html_unescape md5_bytes md5_sum punycode_decode),
qw(punycode_encode quote sha1_bytes sha1_sum slurp spurt trim unquote),
qw(url_escape url_unescape xml_escape)
qw(punycode_encode quote sha1_bytes sha1_sum slurp spurt squish trim),
qw(unquote url_escape url_unescape xml_escape)
);
{
no strict 'refs';
Expand Down Expand Up @@ -294,6 +294,14 @@ Turn bytestream into L<Mojo::Collection>.
b('a,b,c')->split(',')->pluck('quote')->join(',')->say;
=head2 C<squish>
$stream = $stream->squish;
Trim whitespace characters from both ends of bytestream and then change all
consecutive groups of whitespace into one space each with
L<Mojo::Util/"squish">.
=head2 C<to_string>
my $string = $stream->to_string;
Expand Down
12 changes: 2 additions & 10 deletions lib/Mojo/DOM.pm
Expand Up @@ -10,6 +10,7 @@ use Carp 'croak';
use Mojo::Collection;
use Mojo::DOM::CSS;
use Mojo::DOM::HTML;
use Mojo::Util 'squish';
use Scalar::Util qw(blessed weaken);

sub AUTOLOAD {
Expand Down Expand Up @@ -361,16 +362,7 @@ sub _text {
}

# Text
elsif ($type eq 'text') {
$content = $e->[1];

# Trim whitespace
if ($trim) {
$content =~ s/^\s*\n+\s*//;
$content =~ s/\s*\n+\s*$//;
$content =~ s/\s*\n+\s*/\ /g;
}
}
elsif ($type eq 'text') { $content = $trim ? squish($e->[1]) : $e->[1] }

# CDATA or raw text
elsif ($type eq 'cdata' || $type eq 'raw') { $content = $e->[1] }
Expand Down
23 changes: 17 additions & 6 deletions lib/Mojo/Util.pm
Expand Up @@ -40,8 +40,8 @@ our @EXPORT_OK = (
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode encode get_line hmac_md5_sum hmac_sha1_sum html_escape),
qw(html_unescape md5_bytes md5_sum punycode_decode punycode_encode quote),
qw(secure_compare sha1_bytes sha1_sum slurp spurt trim unquote url_escape),
qw(url_unescape xml_escape)
qw(secure_compare sha1_bytes sha1_sum slurp spurt squish trim unquote),
qw(url_escape url_unescape xml_escape)
);

sub b64_decode { decode_base64(shift) }
Expand Down Expand Up @@ -279,12 +279,16 @@ sub spurt {
return $content;
}

sub squish {
my $string = trim(shift);
$string =~ s/\s+/ /g;
return $string;
}

sub trim {
my $string = shift;
for ($string) {
s/^\s*//;
s/\s*$//;
}
$string =~ s/^\s*//;
$string =~ s/\s*$//;
return $string;
}

Expand Down Expand Up @@ -574,6 +578,13 @@ Read all data at once from file.
Write all data at once to file.
=head2 C<squish>
my $squished = squish $string;
Trim whitespace characters from both ends of string and then change all
consecutive groups of whitespace into one space each.
=head2 C<trim>
my $trimmed = trim $string;
Expand Down
5 changes: 4 additions & 1 deletion t/mojo/bytestream.t
Expand Up @@ -5,7 +5,7 @@ use utf8;
# "Homer, we're going to ask you a few simple yes or no questions.
# Do you understand?
# Yes. *lie dectector blows up*"
use Test::More tests => 44;
use Test::More tests => 45;

use File::Spec::Functions qw(catfile splitdir);
use File::Temp 'tempdir';
Expand Down Expand Up @@ -63,6 +63,9 @@ is b('"foo 23 \"bar"')->unquote, 'foo 23 "bar', 'right unquoted result';
# trim
is b(' la la la ')->trim, 'la la la', 'right trimmed result';

# squish
is b("\n la\nla la \n")->squish, 'la la la', 'right squished result';

# md5_bytes
is unpack('H*', b('foo bar baz ♥')->encode->md5_bytes),
'a740aeb6e066f158cbf19fd92e890d2d', 'right binary md5 checksum';
Expand Down
15 changes: 10 additions & 5 deletions t/mojo/util.t
Expand Up @@ -2,7 +2,7 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 143;
use Test::More tests => 146;

use File::Spec::Functions qw(catfile splitdir);
use File::Temp 'tempdir';
Expand All @@ -13,8 +13,8 @@ use Mojo::Util
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode encode get_line hmac_md5_sum hmac_sha1_sum html_escape),
qw(html_unescape md5_bytes md5_sum punycode_decode punycode_encode quote),
qw(trim unquote secure_compare sha1_bytes sha1_sum slurp spurt url_escape),
qw(url_unescape xml_escape);
qw(squish trim unquote secure_compare sha1_bytes sha1_sum slurp spurt),
qw(url_escape url_unescape xml_escape);

# camelize
is camelize('foo_bar_baz'), 'FooBarBaz', 'right camelized result';
Expand Down Expand Up @@ -273,10 +273,15 @@ is unquote('"foo 23 \"bar"'), 'foo 23 "bar', 'right unquoted result';
is unquote('"\"foo 23 \"bar\""'), '"foo 23 "bar"', 'right unquoted result';

# trim
is trim(' la la la '), 'la la la', 'right trimmed result';
is trim(' la la la '), 'la la la', 'right trimmed result';
is trim(" \n la la la \n "), 'la la la', 'right trimmed result';
is trim("\n la\nla la \n"), "la\nla la", 'right trimmed result';
is trim(" \nla\nla\nla\n "), "la\nla\nla", 'right trimmed result';
is trim(" \nla \n \nla\nla\n "), "la \n \nla\nla", 'right trimmed result';

# squish
is squish(' la la la '), 'la la la', 'right squished result';
is squish("\n la\nla la \n"), "la la la", 'right squished result';
is squish(" \nla \n \nla\nla\n "), "la la la", 'right squished result';

# md5_bytes
is unpack('H*', md5_bytes(encode 'UTF-8', 'foo bar baz ♥')),
Expand Down

0 comments on commit 53c0a7d

Please sign in to comment.