Skip to content

Commit

Permalink
Merge pull request meikidd#1 from ridgeboy40/allcodes
Browse files Browse the repository at this point in the history
methods getAllCodes(), getAllNames(), and getAllNativeNames()
  • Loading branch information
meikidd committed Apr 4, 2016
2 parents 467035e + 9eb9ae1 commit 80023b0
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 10 deletions.
23 changes: 23 additions & 0 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -54,6 +72,11 @@ var ISO6391 = (function () {
}
return '';
}
}, {
key: 'getAllCodes',
value: function getAllCodes() {
return DATA.CODE_LIST;
}
}, {
key: 'validate',
value: function validate(code) {
Expand Down
34 changes: 27 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand All @@ -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:'中文'}]
```
```
21 changes: 19 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
})
Expand All @@ -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]
Expand All @@ -28,11 +42,14 @@ export default class ISO6391 {
}
return ''
}
static getAllCodes(){
return DATA.CODE_LIST;
}
static validate(code) {
if(DATA.CODE_LIST.indexOf(code) === -1) {
return false
} else {
return true
}
}
}
}
29 changes: 28 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
@@ -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(){
Expand All @@ -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')
Expand All @@ -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)
Expand All @@ -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:'中文'}])
})
})
})

0 comments on commit 80023b0

Please sign in to comment.