react-select-country-list
Advanced tools
Comparing version 1.0.7 to 2.0.0
@@ -1,50 +0,68 @@ | ||
'use strict' | ||
class CountryList { | ||
constructor() { | ||
this.data = require('./data.json') | ||
this.labelMap = {} | ||
this.valueMap = {} | ||
var data = require('./data.json') | ||
this.data.forEach(country => { | ||
this.labelMap[country.label.toLowerCase()] = country.value | ||
this.valueMap[country.value.toLowerCase()] = country.label | ||
}) | ||
} | ||
/** | ||
* Precompute label and value lookups. | ||
*/ | ||
var labelMap = {} | ||
var valueMap = {} | ||
data.forEach(function (country) { | ||
labelMap[country.label.toLowerCase()] = country.value | ||
valueMap[country.value.toLowerCase()] = country.label | ||
}) | ||
getValue(label) { | ||
return this.labelMap[label.toLowerCase()] | ||
} | ||
module.exports = CountryList | ||
function CountryList () { | ||
if (!(this instanceof CountryList)) return new CountryList() | ||
}; | ||
getLabel(value) { | ||
return this.valueMap[value.toLowerCase()] | ||
} | ||
CountryList.prototype.getValue = function getValue (label) { | ||
return labelMap[label.toLowerCase()] | ||
} | ||
getLabels() { | ||
return this.data.map(country => country.label) | ||
} | ||
CountryList.prototype.getLabel = function getLabel (value) { | ||
return valueMap[value.toLowerCase()] | ||
} | ||
getValues() { | ||
return this.data.map(country => country.value) | ||
} | ||
CountryList.prototype.getLabels = function getLabels () { | ||
return data.map(function (country) { | ||
return country.label | ||
}) | ||
} | ||
getLabelList() { | ||
return this.labelMap | ||
} | ||
CountryList.prototype.getValues = function getValues () { | ||
return data.map(function (country) { | ||
return country.value | ||
}) | ||
} | ||
getValueList() { | ||
return this.valueMap | ||
} | ||
CountryList.prototype.getValueList = function () { | ||
return valueMap | ||
getData() { | ||
return this.data | ||
} | ||
setLabel(value, label) { | ||
this.data.forEach(country => { | ||
if (country.value === value) { | ||
country.label = label | ||
this.valueMap[country.value.toLowerCase()] = country.label | ||
} | ||
}) | ||
return this | ||
} | ||
setEmpty(label) { | ||
this.data.unshift({ | ||
value: '', | ||
label: label, | ||
}) | ||
this.valueMap[''] = label | ||
this.labelMap[label] = '' | ||
return this | ||
} | ||
} | ||
CountryList.prototype.getLabelList = function () { | ||
return labelMap | ||
const countryList = () => { | ||
if (!(this instanceof CountryList)) return new CountryList() | ||
} | ||
CountryList.prototype.getData = function getData () { | ||
return data | ||
} | ||
module.exports = countryList |
{ | ||
"name": "react-select-country-list", | ||
"version": "1.0.7", | ||
"version": "2.0.0", | ||
"description": "Maps ISO 3166-1-alpha-2 codes to English country names and match react-select options props", | ||
@@ -9,10 +9,10 @@ "main": "country-list.js", | ||
], | ||
"dependencies": {}, | ||
"dependencies": { | ||
"lodash": "^4.17.11" | ||
}, | ||
"devDependencies": { | ||
"csv-parser": "^1.11.0", | ||
"standard": "^8.4.0", | ||
"tap": "^7.1.2" | ||
}, | ||
"scripts": { | ||
"pretest": "standard", | ||
"test": "tap test/*.js" | ||
@@ -27,2 +27,3 @@ }, | ||
"countries", | ||
"country-list", | ||
"react-select" | ||
@@ -29,0 +30,0 @@ ], |
@@ -69,3 +69,3 @@ # react-select-country-list | ||
### getLabel(code) | ||
### getLabel(value) | ||
@@ -96,4 +96,29 @@ Expects a two-digit country code. | ||
### setLabel(value, label) | ||
Due to different perspectives among different regions, this method can help developers customize the label of specific country. What's more, it can be chained with another methods above. | ||
#### Usage | ||
```js | ||
// Make 'Viet Nam' -> 'Vietnam' | ||
countries.setLabel('VN', 'Vietnam').getLabel('VN') // 'Vietnam' | ||
``` | ||
### setEmpty(label) | ||
You may want an empty value option in the list, so here's the helper function for you. Again, it can be chained with another methods above. | ||
#### Usage | ||
```js | ||
countries.setEmpty('Select a Country').getLabel('') // 'Select a Country' | ||
``` | ||
We can even chain `setLabel` and `setEmpty` together to have list with an empty option and the modified label. | ||
```js | ||
countries.setLabel('VN', 'Vietnam').setEmpty('Select a Country').getLabel('VN') // 'Vietnam' | ||
``` | ||
## License | ||
MIT |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19665
2
1052
123
1
+ Addedlodash@^4.17.11
+ Addedlodash@4.17.21(transitive)