diff --git a/build/index.js b/build/index.js index 72db9a7..e5d86e8 100644 --- a/build/index.js +++ b/build/index.js @@ -38,12 +38,30 @@ var ISO6391 = (function () { if (!ISO6391.validate(code)) return ''; return DATA.LANGUAGES_LIST[code].name; } + }, { + key: 'getAllNames', + value: function getAllNames() { + var list = []; + for (var code in DATA.LANGUAGES_LIST) { + list.push(DATA.LANGUAGES_LIST[code].name); + } + return list; + } }, { key: 'getNativeName', value: function getNativeName(code) { if (!ISO6391.validate(code)) return ''; return DATA.LANGUAGES_LIST[code].nativeName; } + }, { + key: 'getAllNativeNames', + value: function getAllNativeNames() { + var list = []; + for (var code in DATA.LANGUAGES_LIST) { + list.push(DATA.LANGUAGES_LIST[code].nativeName); + } + return list; + } }, { key: 'getCode', value: function getCode(name) { @@ -54,6 +72,11 @@ var ISO6391 = (function () { } return ''; } + }, { + key: 'getAllCodes', + value: function getAllCodes() { + return DATA.CODE_LIST; + } }, { key: 'validate', value: function validate(code) { diff --git a/readme.md b/readme.md index bfe688a..e94c4c0 100644 --- a/readme.md +++ b/readme.md @@ -14,32 +14,47 @@ npm install iso-639-1 ## Methods -### getName(code) +### getName(code) - @param code {string} - @return {string} Lookup language english name by code -### getNativeName(code) +### getAllNames() + - @return {array} + +Get array of all language english names + +### getNativeName(code) - @param code {string} - @return {string} Lookup language native name by code +### getAllNativeNames() + - @return {array} + +Get array of all language native names + -### getCode(name) +### getCode(name) - @param name {string} - @return {string} Lookup code by english name or native name -### validate(code) +### getAllCodes() + - @return {array} + +Get array of all codes + +### validate(code) - @param code {string} - @return {boolean} Check whether the given code is in the list of [ISO-639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) -### getLanguages(codes) +### getLanguages(codes) - @param codes {array} - @return {array} @@ -53,13 +68,18 @@ var ISO6391 = require('./iso-639-1') console.log(ISO6391.getName('zh')) // 'Chinese' console.log(ISO6391.getNativeName('zh')) // '中文' +console.log(ISO6391.getAllNames()) // ['Afar','Abkhaz', ... ,'Zulu'] +console.log(ISO6391.getAllNativeNames()) //['Afaraf','аҧсуа бызшәа', ... ,'isiZulu' ] + console.log(ISO6391.getCode('Chinese')) // 'zh' console.log(ISO6391.getCode('中文')) // 'zh' +console.log(ISO6391.getAllCodes()) //['aa','ab',...,'zu'] + console.log(ISO6391.validate('en')) // true console.log(ISO6391.validate('xxx')) // false -console.log(ISO6391.getLanguages(['en', 'zh'])) +console.log(ISO6391.getLanguages(['en', 'zh'])) // [{code:'en',name:'English',nativeName:'English'},{code:'zh',name:'Chinese',nativeName:'中文'}] -``` \ No newline at end of file +``` diff --git a/src/index.js b/src/index.js index 3cd7134..4c2de9d 100644 --- a/src/index.js +++ b/src/index.js @@ -5,7 +5,7 @@ export default class ISO6391 { var list = [] for (var i = 0; i < codes.length; i++) { list.push({ - code:codes[i], + code:codes[i], name: ISO6391.getName(codes[i]), nativeName: ISO6391.getNativeName(codes[i]) }) @@ -16,10 +16,24 @@ export default class ISO6391 { if(!ISO6391.validate(code)) return '' return DATA.LANGUAGES_LIST[code].name } + static getAllNames(){ + var list = []; + for( var code in DATA.LANGUAGES_LIST ){ + list.push(DATA.LANGUAGES_LIST[code].name) + } + return list; + } static getNativeName(code) { if(!ISO6391.validate(code)) return '' return DATA.LANGUAGES_LIST[code].nativeName } + static getAllNativeNames(){ + var list = []; + for( var code in DATA.LANGUAGES_LIST ){ + list.push(DATA.LANGUAGES_LIST[code].nativeName) + } + return list; + } static getCode(name) { for (var i = 0; i < DATA.CODE_LIST.length; i++) { var code = DATA.CODE_LIST[i] @@ -28,6 +42,9 @@ export default class ISO6391 { } return '' } + static getAllCodes(){ + return DATA.CODE_LIST; + } static validate(code) { if(DATA.CODE_LIST.indexOf(code) === -1) { return false @@ -35,4 +52,4 @@ export default class ISO6391 { return true } } -} \ No newline at end of file +} diff --git a/test/test.js b/test/test.js index 011f375..d8a9d87 100644 --- a/test/test.js +++ b/test/test.js @@ -1,6 +1,15 @@ var assert = require('assert') var ISO6391 = require('../build/index') +var DATA = require('../build/data') +var codeList = DATA.CODE_LIST +var nameList = [] +var nativeNameList = [] + +for( var code in DATA.LANGUAGES_LIST ){ + nameList.push( DATA.LANGUAGES_LIST[code].name) + nativeNameList.push( DATA.LANGUAGES_LIST[code].nativeName) +} describe('getName()', function() { it('en', function(){ @@ -20,6 +29,18 @@ describe('getNativeName()', function() { }) }) +describe( 'getAllNames()', function(){ + it('All languages english names match',function(){ + assert.deepEqual(ISO6391.getAllNames(), nameList) + }) +}) + +describe( 'getAllNativeNames()', function(){ + it('All languages native names match',function(){ + assert.deepEqual(ISO6391.getAllNativeNames(), nativeNameList) + }) +}) + describe('getCode()', function() { it('English', function(){ assert.equal(ISO6391.getCode('English'), 'en') @@ -32,6 +53,12 @@ describe('getCode()', function() { }) }) +describe('getAllCodes()', function() { + it( 'All Codes Match', function(){ + assert.deepEqual(ISO6391.getAllCodes(), codeList) + }) +}) + describe('validate()', function() { it('en', function(){ assert.equal(ISO6391.validate('en'), true) @@ -51,4 +78,4 @@ describe('getLanguages()', function() { it('[en,zh]', function(){ assert.deepEqual(ISO6391.getLanguages(['en','zh']), [{code:'en',name:'English',nativeName:'English'},{code:'zh',name:'Chinese',nativeName:'中文'}]) }) -}) \ No newline at end of file +})