Skip to content

Commit

Permalink
Add and expose method to test if a language is supported
Browse files Browse the repository at this point in the history
  • Loading branch information
matheuss committed Jul 12, 2016
1 parent 255b2c1 commit 3b20185
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,4 @@ function translate(text, opts) {
}

module.exports = translate;
module.exports.languages = languages;
19 changes: 19 additions & 0 deletions languages.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,23 @@ var langs = {
'zu': 'Zulu'
};

function isSupported(desiredLang) {
desiredLang = desiredLang.toLowerCase();

if (langs[desiredLang]) {
return true;
}

var result = Object.keys(langs).filter(function (key) {
if (typeof langs[key] !== 'string') {
return false;
}

return langs[key].toLowerCase().indexOf(desiredLang) !== -1;
});

return result.length > 0;
}

module.exports = langs;
module.exports.isSupported = isSupported;
22 changes: 22 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import test from 'ava';

import languages from './languages';
import translate from './index';

test('translate without any options', async t => {
Expand Down Expand Up @@ -66,3 +68,23 @@ test('translate some text and get the raw output alongside', async t => {
t.fail(err.code);
}
});

test('get a supported language by code', t => {
t.true(languages.isSupported('en'));
});

test('get an unsupported language by code', t => {
t.false(languages.isSupported('js'));
});

test('get a supported language by name', t => {
t.true(languages.isSupported('english'));
});

test('get an unsupported language by name', t => {
t.false(languages.isSupported('javascript'));
});

test('get a supported language by a part of its name', t => {
t.true(languages.isSupported('chinese'));
});

0 comments on commit 3b20185

Please sign in to comment.