Return early in Classifier.classify if no languages supplied (#3471)

* Return early if no languages supplied

There's no need to tokenise the data when attempting to classify without a limited language scope as no action will be performed when it comes to scoring anyway.

* Add test for empty languages array
This commit is contained in:
Colin Seymour
2017-02-13 18:22:54 +00:00
committed by GitHub
parent 62d285fce6
commit 01de40faaa
2 changed files with 5 additions and 1 deletions

View File

@@ -95,7 +95,7 @@ module Linguist
# Returns sorted Array of result pairs. Each pair contains the
# String language name and a Float score.
def classify(tokens, languages)
return [] if tokens.nil?
return [] if tokens.nil? || languages.empty?
tokens = Tokenizer.tokenize(tokens) if tokens.is_a?(String)
scores = {}

View File

@@ -53,4 +53,8 @@ class TestClassifier < Minitest::Test
assert_equal language.name, results.first[0], "#{sample[:path]}\n#{results.inspect}"
end
end
def test_classify_empty_languages
assert_equal [], Classifier.classify(Samples.cache, fixture("Ruby/foo.rb"), [])
end
end