Use space rate to distinguish minified files.

Minified JS files usually contain less than 2% spaces, while minified
CSS files may contain about 4% spaces. However, an unminified CSS file
may also have as low as 6% spaces, especially when it includes some
resources inline. Consequently, the division might not be appreciate
for CSS files. Even though, it will only mis-recognize a normal file
as minified for a few special cases.
This commit is contained in:
Xidorn Quan
2013-03-02 13:15:42 +08:00
parent 806369ce7f
commit 9a5f9a5e9b

View File

@@ -72,15 +72,14 @@ module Linguist
# Internal: Is the blob minified files? # Internal: Is the blob minified files?
# #
# Consider a file minified if the average line length is # Consider a file minified if it contains more than 5% spaces.
# greater then 100c. Currently only JS and CSS files are # Currently, only JS and CSS files are detected by this method.
# detected by this method.
# #
# Returns true or false. # Returns true or false.
def minified_files? def minified_files?
return unless ['.js', '.css'].include? extname return unless ['.js', '.css'].include? extname
if lines.any? if data && data.length > 200
(lines.inject(0) { |n, l| n += l.length } / lines.length) > 100 (data.each_char.count{ |c| c <= ' ' } / data.length.to_f) < 0.05
else else
false false
end end