From 5cb3467908cacffedddf25949a097b12ffd00551 Mon Sep 17 00:00:00 2001 From: Gor Sargsyan Date: Thu, 26 Jan 2023 22:58:39 +0400 Subject: [PATCH 1/3] Added functionality to return { label: , value: } and tests with native support --- country-list.js | 23 ++++++++- test/get-country-by-label.js | 11 ++++ test/get-country-by-value.js | 11 ++++ test/native.js | 97 +++++++++++++++++++++++++++++++++--- 4 files changed, 134 insertions(+), 8 deletions(-) create mode 100644 test/get-country-by-label.js create mode 100644 test/get-country-by-value.js diff --git a/country-list.js b/country-list.js index a054e67..c72e3f2 100644 --- a/country-list.js +++ b/country-list.js @@ -1,6 +1,6 @@ class CountryList { constructor() { - this.data = require('./data.json') + this.data = require('./data.json'); this.labelMap = {} this.valueMap = {} @@ -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) { @@ -61,7 +80,7 @@ class CountryList { } native() { - this.nativeData = require('./data-native.json') + this.nativeData = require('./data-native.json'); this.nativeData.forEach(country => { this.labelMap[country.label.toLowerCase()] = country.value this.valueMap[country.value.toLowerCase()] = country.label 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() }) From 8be91a8abfd859c15b7ea9dc79c3e2181620a003 Mon Sep 17 00:00:00 2001 From: Gor Sargsyan Date: Thu, 26 Jan 2023 23:21:06 +0400 Subject: [PATCH 2/3] README edit, double quote -> single quote --- country-list.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/country-list.js b/country-list.js index c72e3f2..8011d8a 100644 --- a/country-list.js +++ b/country-list.js @@ -1,6 +1,6 @@ class CountryList { constructor() { - this.data = require('./data.json'); + this.data = require('./data.json') this.labelMap = {} this.valueMap = {} @@ -80,7 +80,7 @@ class CountryList { } native() { - this.nativeData = require('./data-native.json'); + this.nativeData = require('./data-native.json') this.nativeData.forEach(country => { this.labelMap[country.label.toLowerCase()] = country.value this.valueMap[country.value.toLowerCase()] = country.label From 7c402d8b0fec28c3414c7ea1ad8d8cb6e6ab3d22 Mon Sep 17 00:00:00 2001 From: Gor Sargsyan Date: Thu, 26 Jan 2023 23:32:20 +0400 Subject: [PATCH 3/3] README edit, double quote -> single quote --- README.md | 19 +++++++++++++++++++ country-list.js | 4 ++-- 2 files changed, 21 insertions(+), 2 deletions(-) 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 c72e3f2..8011d8a 100644 --- a/country-list.js +++ b/country-list.js @@ -1,6 +1,6 @@ class CountryList { constructor() { - this.data = require('./data.json'); + this.data = require('./data.json') this.labelMap = {} this.valueMap = {} @@ -80,7 +80,7 @@ class CountryList { } native() { - this.nativeData = require('./data-native.json'); + this.nativeData = require('./data-native.json') this.nativeData.forEach(country => { this.labelMap[country.label.toLowerCase()] = country.value this.valueMap[country.value.toLowerCase()] = country.label