diff --git a/README.md b/README.md index 70a95ec..83537ab 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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' } ``` diff --git a/country-list.js b/country-list.js index a054e67..8011d8a 100644 --- a/country-list.js +++ b/country-list.js @@ -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) { diff --git a/test/get-country-by-label.js b/test/get-country-by-label.js new file mode 100644 index 0000000..30a06e3 --- /dev/null +++ b/test/get-country-by-label.js @@ -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() +}) \ No newline at end of file diff --git a/test/get-country-by-value.js b/test/get-country-by-value.js new file mode 100644 index 0000000..c05dce5 --- /dev/null +++ b/test/get-country-by-value.js @@ -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() +}) \ No newline at end of file diff --git a/test/native.js b/test/native.js index 1462c6d..2871851 100644 --- a/test/native.js +++ b/test/native.js @@ -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() })