Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 14, 2012
1 parent a4ac04b commit f5482b3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
13 changes: 5 additions & 8 deletions lib/Mojolicious/Types.pm
Expand Up @@ -44,16 +44,13 @@ sub detect {
return [] if !$prioritize && @types > 1;

# Detect extensions from MIME types
my @exts;
my %reverse;
my $types = $self->types;
for my $type (@types) {
for my $ext (sort keys %$types) {
my @types = ref $types->{$ext} ? @{$types->{$ext}} : ($types->{$ext});
$type eq $_ and push @exts, $ext for map { s/\;.*$//; lc $_ } @types;
}
for my $ext (sort keys %$types) {
my @types = ref $types->{$ext} ? @{$types->{$ext}} : ($types->{$ext});
push @{$reverse{$_}}, $ext for map { s/\;.*$//; lc $_ } @types;
}

return \@exts;
return [map { @{$reverse{$_} // []} } @types];
}

sub type {
Expand Down
2 changes: 2 additions & 0 deletions t/mojolicious/types.t
Expand Up @@ -77,5 +77,7 @@ is_deeply $t->detect(' TEXT/HTML , text/plain;Q=0.9', 1),
['htm', 'html', 'txt'], 'right formats';
is_deeply $t->detect('text/plain;q=0.5, text/xml, application/xml;q=0.1', 1),
['xml', 'txt', 'xml'], 'right formats';
is_deeply $t->detect('application/json, text/javascript, */*; q=0.01', 1),
['json'], 'right formats';

done_testing();

0 comments on commit f5482b3

Please sign in to comment.