Skip to content

Commit

Permalink
Merge pull request #11 from molgin/case-insensitive-lookup
Browse files Browse the repository at this point in the history
Make lookup case-insensitive
  • Loading branch information
scsmith committed Jul 28, 2015
2 parents d3213f7 + 82d57b3 commit 07288b2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/language_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ def self.find_by_iso_639_3(code)
end

def self.find_by_name(name)
LanguageList::BY_NAME[name]
LanguageList::BY_NAME[name.downcase]
end

def self.find(code)
code.downcase!
find_by_iso_639_1(code) || find_by_iso_639_3(code) || find_by_name(code)
end
end
Expand All @@ -71,7 +72,7 @@ def self.find(code)
BY_ISO_639_1 = {}
BY_ISO_639_3 = {}
ALL_LANGUAGES.each do |lang|
BY_NAME[lang.name] = lang
BY_NAME[lang.name.downcase] = lang
BY_ISO_639_1[lang.iso_639_1] = lang if lang.iso_639_1
BY_ISO_639_3[lang.iso_639_3] = lang if lang.iso_639_3
end
Expand Down
21 changes: 21 additions & 0 deletions test/language_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,41 @@ def test_find_by_name
assert_equal 'English', english.name
end

def test_case_insensitive_find_by_name
english = LanguageList::LanguageInfo.find_by_name('english')
assert_equal 'en', english.iso_639_1
assert_equal 'eng', english.iso_639_3
assert_equal 'English', english.name
end

def test_find_with_iso_639_1_code
english = LanguageList::LanguageInfo.find('en')
assert_equal 'en', english.iso_639_1
assert_equal 'eng', english.iso_639_3
assert_equal 'English', english.name
end

def test_case_insensitive_find_with_iso_639_1_code
english = LanguageList::LanguageInfo.find('EN')
assert_equal 'en', english.iso_639_1
assert_equal 'eng', english.iso_639_3
assert_equal 'English', english.name
end

def test_find_with_iso_639_3_code
english = LanguageList::LanguageInfo.find('eng')
assert_equal 'en', english.iso_639_1
assert_equal 'eng', english.iso_639_3
assert_equal 'English', english.name
end

def test_case_insensitive_find_with_iso_639_3_code
english = LanguageList::LanguageInfo.find('Eng')
assert_equal 'en', english.iso_639_1
assert_equal 'eng', english.iso_639_3
assert_equal 'English', english.name
end

def test_find_with_name
english = LanguageList::LanguageInfo.find('English')
assert_equal 'en', english.iso_639_1
Expand Down

0 comments on commit 07288b2

Please sign in to comment.