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

1152
test/fixtures/json2_backbone.js vendored Normal file

File diff suppressed because it is too large Load Diff

1320
test/fixtures/uglify.js vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -123,6 +123,12 @@ class TestBlob < Test::Unit::TestCase
assert blob("MainMenu.nib").generated?
assert blob("project.pbxproj").generated?
# Long line
assert !blob("uglify.js").generated?
# Inlined JS, but mostly code
assert !blob("json2_backbone.js").generated?
# Minified JS
assert !blob("jquery-1.6.1.js").generated?
assert blob("jquery-1.6.1.min.js").generated?

View File

@@ -19,7 +19,7 @@ class TestRepository < Test::Unit::TestCase
end
def test_linguist_language
assert_equal Language['Ruby'], linguist_repo.language
assert_equal Language['JavaScript'], linguist_repo.language
end
def test_linguist_languages