@ideal-postcodes/jsutil
Version:
Browser Address Autocomplete for api.ideal-postcodes.co.uk
75 lines (74 loc) • 2.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateCountry = exports.toCountry = void 0;
const types_1 = require("./types");
const input_1 = require("./input");
const UK = "United Kingdom";
const IOM = "Isle of Man";
const EN = "England";
const SC = "Scotland";
const WA = "Wales";
const NI = "Northern Ireland";
const CI = "Channel Islands";
const toCountry = (address) => {
const country = address.country;
if (country === EN)
return UK;
if (country === SC)
return UK;
if (country === WA)
return UK;
if (country === NI)
return UK;
if (country === IOM)
return IOM;
if ((0, types_1.isGbrAddress)(address)) {
if (country === CI) {
if (/^GY/.test(address.postcode))
return "Guernsey";
if (/^JE/.test(address.postcode))
return "Jersey";
}
}
return country;
};
exports.toCountry = toCountry;
const updateCountry = (select, address) => {
if (!select)
return;
if ((0, input_1.isSelect)(select)) {
const bcc = (0, exports.toCountry)(address);
if ((0, input_1.hasValue)(select, bcc)) {
(0, input_1.change)({ e: select, value: bcc });
return;
}
if ((0, input_1.hasValue)(select, address.country_iso_2)) {
(0, input_1.change)({ e: select, value: address.country_iso_2 });
return;
}
if ((0, input_1.hasValue)(select, address.country_iso)) {
(0, input_1.change)({ e: select, value: address.country_iso });
return;
}
let text = (0, input_1.optionsHasText)(select, bcc);
if (text.length > 0) {
(0, input_1.change)({ e: select, value: text[0].value || "" });
return;
}
text = (0, input_1.optionsHasText)(select, address.country_iso_2);
if (text.length > 0) {
(0, input_1.change)({ e: select, value: text[0].value || "" });
return;
}
text = (0, input_1.optionsHasText)(select, address.country_iso);
if (text.length > 0) {
(0, input_1.change)({ e: select, value: text[0].value || "" });
return;
}
}
if ((0, input_1.isInput)(select)) {
const bcc = (0, exports.toCountry)(address);
(0, input_1.change)({ e: select, value: bcc });
}
};
exports.updateCountry = updateCountry;