@govuk-pay/pay-js-commons
Version:
Reusable js scripts for GOV.UK Pay Node.js projects
47 lines (43 loc) • 1.49 kB
JavaScript
;
// NPM Dependencies
var lodash = require('lodash');
// Local Dependencies
var countries = require('../data/countries.json');
var extensions = require('../data/country-record-extension.json');
// Merge the additional data into the register data
countries.forEach(function (country) {
var extension = extensions.find(function (item) {
return item.country === country;
});
if (extension) {
country.entry.aliases = extension.aliases;
country.entry.weighting = extension.weighting;
}
});
countries = lodash.compact(countries);
countries = lodash.sortBy(countries, function (country) {
return country.entry.name.toLowerCase();
});
// Exports
exports.retrieveCountries = function (selectedCountry) {
var countriesList = lodash.clone(countries);
countriesList.forEach(function (country) {
country.entry.selected = country.entry.country === (selectedCountry || 'GB');
});
return countriesList;
};
exports.govukFrontendFormatted = function (selectedCountry) {
var countriesList = lodash.clone(countries);
countriesList.forEach(function (country) {
country.selected = country.entry.country === (selectedCountry || 'GB');
country.value = country.entry.country;
country.text = country.entry.name;
});
return countriesList;
};
exports.translateAlpha2 = function (alpha2Code) {
var match = countries.find(function (country) {
return country.entry.country === alpha2Code;
});
return match ? match.entry.name : undefined;
};