Skip to content

Commit

Permalink
JS is minified if its average line length is greater than 100c
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Jul 3, 2011
1 parent fe2f26d commit aeef1d8
Show file tree
Hide file tree
Showing 5 changed files with 2,502 additions and 5 deletions.
27 changes: 23 additions & 4 deletions lib/linguist/blob_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ def sloc
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
Expand All @@ -166,16 +177,24 @@ def sloc
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
Expand Down
Loading

0 comments on commit aeef1d8

Please sign in to comment.