Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ We can even chain `setLabel` and `setEmpty` together to have list with an empty
countries.setLabel('VN', 'Vietnam').setEmpty('Select a Country').getLabel('VN') // 'Vietnam'
```

### getCountryByLabel(label)

You may want to get the exact country object of specific country by using label, this is the method

### Usage
```js
countries.getCountryByLabel('Armenia') // { label: 'Armenia', value: 'AM' }`
```

### getCountryByValue(value)

You may want to get the exact country object of specific country by using value, this is the method

### Usage
```js
countries.getCountryByValue('AM') // { label: 'Armenia', value: 'AM' }
```

### native()

You may want to display native name of countries, this is the method for you.
Expand All @@ -108,6 +126,7 @@ The data source of native names can be found [here](https://annexare.github.io/C
#### Usage
```js
countries.native().getLabel('TW') // 臺灣
countries.native().getCountryByValue('AM') // { label: 'Հայաստան', value: 'AM' }
```


Expand Down
19 changes: 19 additions & 0 deletions country-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ class CountryList {
return this.data
}

getCountryByValue(value) {
const labelTemp = this.valueMap[value.toLowerCase()];
if(labelTemp) {
return {
value : this.labelMap[labelTemp.toLowerCase()],
label : labelTemp
}
}
return undefined;
}

getCountryByLabel(label) {
const nonNativeCountry = this.data.find(country=>country.label.toLowerCase() === label.toLowerCase());
if(nonNativeCountry){
return this.getCountryByValue(nonNativeCountry.value)
}
return undefined;
}

setLabel(value, label) {
this.data.forEach(country => {
if (country.value === value) {
Expand Down
11 changes: 11 additions & 0 deletions test/get-country-by-label.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

const test = require('tap').test
const countries = require('../country-list')()

test('get country object by label', function (t) {
t.same(countries.getCountryByLabel('Iceland'), { value: 'IS', label: 'Iceland', }, 'name "Iceland" should return { label: "Iceland", value: "IS" }')
t.same(countries.getCountryByLabel('iceland'), { value: 'IS', label: 'Iceland', }, 'name "iceland" should return { label: "Iceland", value: "IS" }')
t.equal(countries.getCountryByLabel('invalid country label'), undefined, 'name "invalid country label" should return undefined')
t.end()
})
11 changes: 11 additions & 0 deletions test/get-country-by-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

const test = require('tap').test
const countries = require('../country-list')()

test('get country object by label', function (t) {
t.same(countries.getCountryByValue('IS'), { value: 'IS', label: 'Iceland', }, 'name "Iceland" should return { label: "Iceland", value: "IS" }')
t.same(countries.getCountryByValue('is'), { value: 'IS', label: 'Iceland', }, 'name "iceland" should return { label: "Iceland", value: "IS" }')
t.equal(countries.getCountryByValue('invalid country value'), undefined, 'name "invalid country" should return undefined')
t.end()
})
97 changes: 91 additions & 6 deletions test/native.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,125 @@
'use strict'

const test = require('tap').test
const countries = require('../')()
const countries = require('../')().native()
const data = require('../data-native.json')
const isEmpty = require('lodash.isempty')

test('get native name of a country', function (t) {
t.equal(
countries.native().getLabel('TW'),
countries.getLabel('TW'),
'臺灣',
'return label should be 臺灣'
)

t.equal(
countries.native().getLabel('RU'),
countries.getLabel('RU'),
'Россия',
'return label should be Россия'
)

t.equal(
countries.native().getLabel('SA'),
countries.getLabel('SA'),
'العربية السعودية',
'return label should be العربية السعودية'
)

t.equal(
countries.native().getLabel('KR'),
countries.getLabel('KR'),
'대한민국',
'return label should be 대한민국'
)

t.equal(
countries.native().getLabel('TH'),
countries.getLabel('TH'),
'ประเทศไทย',
'return label should be ประเทศไทย'
)

t.same(
countries.getCountryByLabel('Armenia'),
{
label: 'Հայաստան',
value: 'AM'
},
'return country should be { label: "Հայաստան", value: "AM" },'
)

t.same(
countries.getCountryByLabel('armenia'),
{
label: 'Հայաստան',
value: 'AM'
},
'return country should be { label: "Հայաստան", value: "AM" },'
)
console.log(countries.getCountryByLabel('Taiwan'),"lol")

t.same(
countries.getCountryByLabel('Taiwan, Province of China'),
{
label: '臺灣',
value: 'TW'
},
'return country should be { label: "臺灣", value: "TW" },'
)

t.same(
countries.getCountryByLabel('taiwan, province of china'),
{
label: '臺灣',
value: 'TW'
},
'return country should be { label: "臺灣", value: "TW" },'
)

t.equal(
countries.getCountryByLabel('invalid country label'),
undefined,
'return value should be undefined,'
)

t.same(
countries.getCountryByValue('AM'),
{
label: 'Հայաստան',
value: 'AM'
},
'return country should be { label: "Հայաստան", value: "AM" },'
)

t.same(
countries.getCountryByValue('am'),
{
label: 'Հայաստան',
value: 'AM'
},
'return country should be { label: "Հայաստան", value: "AM" },'
)

t.same(
countries.getCountryByValue('TW'),
{
label: '臺灣',
value: 'TW'
},
'return country should be { label: "臺灣", value: "TW" },'
)

t.same(
countries.getCountryByValue('tw'),
{
label: '臺灣',
value: 'TW'
},
'return country should be { label: "臺灣", value: "TW" },'
)

t.equal(
countries.getCountryByValue('invalid country value'),
undefined,
'return value should be undefined,'
)

t.end()
})