JS is minified if its average line length is greater than 100c

Fixes #27
This commit is contained in:
Joshua Peek
2011-07-02 20:28:42 -05:00
parent fe2f26d02e
commit aeef1d8f33
5 changed files with 2502 additions and 5 deletions

View File

@@ -151,6 +151,17 @@ module Linguist
lines.grep(/\S/).size
end
# Internal: Compute average line length.
#
# Returns Integer.
def average_line_length
if lines.any?
lines.inject(0) { |n, l| n += l.length } / lines.length
else
0
end
end
# Public: Is the blob a generated file?
#
# Generated source code is supressed in diffs and is ignored by
@@ -166,16 +177,24 @@ module Linguist
def generated?
if ['.xib', '.nib', '.pbxproj'].include?(extname)
true
elsif generated_coffeescript?
elsif generated_coffeescript? || minified_javascript?
true
elsif extname == '.js'
# JS is minified if any lines are longer than 500c
lines.any? { |l| l.length > 500 }
else
false
end
end
# Internal: Is the blob minified JS?
#
# Consider JS minified if the average line length is
# greater then 100c.
#
# Returns true or false.
def minified_javascript?
return unless extname == '.js'
average_line_length > 100
end
# Internal: Is the blob JS generated by CoffeeScript?
#
# Requires Blob#data