Skip to content

Commit

Permalink
Add a stable sort
Browse files Browse the repository at this point in the history
- This probably has a negative performance impact, so I’m not sure I’m going to
  merge this.
  • Loading branch information
halostatue committed Nov 21, 2020
1 parent 156aba6 commit 50e63b7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
19 changes: 12 additions & 7 deletions lib/mime/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ def [](type_id, complete: false, registered: false)
@type_variants[MIME::Type.simplified(type_id)]
end

prune_matches(matches, complete, registered).sort { |a, b|
a.priority_compare(b)
}
stable_sort(prune_matches(matches, complete, registered))
end

# Return the list of MIME::Types which belongs to the file based on its
Expand All @@ -149,11 +147,12 @@ def [](type_id, complete: false, registered: false)
# puts MIME::Types.type_for(%w(citydesk.xml citydesk.gif))
# => [application/xml, image/gif, text/xml]
def type_for(filename)
Array(filename).flat_map { |fn|
results =
Array(filename).flat_map { |fn|
@extension_index[fn.chomp.downcase[/\.?([^.]*?)$/, 1]]
}.compact.inject(Set.new, :+).sort { |a, b|
a.priority_compare(b)
}
}.compact.inject(Set.new, :+)

stable_sort(results)
end
alias of type_for

Expand Down Expand Up @@ -221,6 +220,12 @@ def match(pattern)
k =~ pattern
}.values.inject(Set.new, :+)
end

def stable_sort(list)
list.lazy.each_with_index.sort { |(a, ai), (b, bi)|
a.priority_compare(b).nonzero? || ai <=> bi
}.map(&:first)
end
end

require 'mime/types/cache'
Expand Down
12 changes: 6 additions & 6 deletions mime-types.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Gem::Specification.new do |s|

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<mime-types-data>.freeze, ["~> 3.2015"])
s.add_development_dependency(%q<minitest>.freeze, ["~> 5.13"])
s.add_development_dependency(%q<minitest>.freeze, ["~> 5.14"])
s.add_development_dependency(%q<hoe-doofus>.freeze, ["~> 1.0"])
s.add_development_dependency(%q<hoe-gemspec2>.freeze, ["~> 1.1"])
s.add_development_dependency(%q<hoe-git>.freeze, ["~> 1.6"])
Expand All @@ -38,10 +38,10 @@ Gem::Specification.new do |s|
s.add_development_dependency(%q<rake>.freeze, [">= 10.0", "< 14.0"])
s.add_development_dependency(%q<simplecov>.freeze, ["~> 0.7"])
s.add_development_dependency(%q<rdoc>.freeze, [">= 4.0", "< 7"])
s.add_development_dependency(%q<hoe>.freeze, ["~> 3.20"])
s.add_development_dependency(%q<hoe>.freeze, ["~> 3.22"])
else
s.add_dependency(%q<mime-types-data>.freeze, ["~> 3.2015"])
s.add_dependency(%q<minitest>.freeze, ["~> 5.13"])
s.add_dependency(%q<minitest>.freeze, ["~> 5.14"])
s.add_dependency(%q<hoe-doofus>.freeze, ["~> 1.0"])
s.add_dependency(%q<hoe-gemspec2>.freeze, ["~> 1.1"])
s.add_dependency(%q<hoe-git>.freeze, ["~> 1.6"])
Expand All @@ -53,11 +53,11 @@ Gem::Specification.new do |s|
s.add_dependency(%q<rake>.freeze, [">= 10.0", "< 14.0"])
s.add_dependency(%q<simplecov>.freeze, ["~> 0.7"])
s.add_dependency(%q<rdoc>.freeze, [">= 4.0", "< 7"])
s.add_dependency(%q<hoe>.freeze, ["~> 3.20"])
s.add_dependency(%q<hoe>.freeze, ["~> 3.22"])
end
else
s.add_dependency(%q<mime-types-data>.freeze, ["~> 3.2015"])
s.add_dependency(%q<minitest>.freeze, ["~> 5.13"])
s.add_dependency(%q<minitest>.freeze, ["~> 5.14"])
s.add_dependency(%q<hoe-doofus>.freeze, ["~> 1.0"])
s.add_dependency(%q<hoe-gemspec2>.freeze, ["~> 1.1"])
s.add_dependency(%q<hoe-git>.freeze, ["~> 1.6"])
Expand All @@ -69,6 +69,6 @@ Gem::Specification.new do |s|
s.add_dependency(%q<rake>.freeze, [">= 10.0", "< 14.0"])
s.add_dependency(%q<simplecov>.freeze, ["~> 0.7"])
s.add_dependency(%q<rdoc>.freeze, [">= 4.0", "< 7"])
s.add_dependency(%q<hoe>.freeze, ["~> 3.20"])
s.add_dependency(%q<hoe>.freeze, ["~> 3.22"])
end
end
13 changes: 9 additions & 4 deletions test/test_mime_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def mime_types
'content-type' => 'application/gzip',
'extensions' => 'gz',
'registered' => true
)
),
*MIME::Types.type_for('foo.webm')
}
end

Expand All @@ -38,8 +39,8 @@ def mime_types
end

it 'is countable with an enumerator' do
assert_equal 6, mime_types.each.count
assert_equal 6, mime_types.lazy.count
assert_equal 8, mime_types.each.count
assert_equal 8, mime_types.lazy.count
end
end

Expand Down Expand Up @@ -159,11 +160,15 @@ def mime_types
plain_text.add_extensions('xtxt')
assert_includes mime_types.type_for('xtxt'), 'text/plain'
end

it 'returns a stable order for types with equal priority' do
assert_equal %w(audio/webm video/webm), mime_types.type_for('foo.webm')
end
end

describe '#count' do
it 'can count the number of types inside' do
assert_equal 6, mime_types.count
assert_equal 8, mime_types.count
end
end
end
4 changes: 4 additions & 0 deletions test/test_mime_types_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ def setup
plain_text.add_extensions('xtxt')
assert_includes MIME::Types.type_for('xtxt'), 'text/plain'
end

it 'returns a stable order for types with equal priority' do
assert_equal %w(audio/webm video/webm), MIME::Types.type_for('foo.webm')
end
end

describe '.count' do
Expand Down

0 comments on commit 50e63b7

Please sign in to comment.