@automattic/wpcom-checkout
Version:
Functions and components used by WordPress.com checkout.
109 lines • 3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.tryToGuessPostalCodeFormat = tryToGuessPostalCodeFormat;
exports.isValidPostalCode = isValidPostalCode;
function defaultFormatter(postalCode, delimiter, partLength) {
return postalCode.substring(0, partLength) + delimiter + postalCode.substring(partLength);
}
const twoPartPostalCodes = {
BR: {
length: [8],
delimiter: '-',
partLength: 5,
},
CA: {
length: [6],
delimiter: ' ',
partLength: 3,
},
GB: {
length: [5, 6, 7],
delimiter: ' ',
formatter: (postalCodeInput, delimiter) => {
return (postalCodeInput.substring(0, postalCodeInput.length - 3) +
delimiter +
postalCodeInput.substring(postalCodeInput.length - 3));
},
},
IE: {
length: [7],
delimiter: ' ',
partLength: 3,
},
JP: {
length: [7],
delimiter: '-',
partLength: 3,
},
KY: {
length: [7],
delimiter: '-',
partLength: 3,
},
NL: {
length: [6],
delimiter: ' ',
partLength: 4,
},
PL: {
length: [5],
delimiter: '-',
partLength: 2,
},
PT: {
length: [7],
delimiter: '-',
partLength: 4,
},
SE: {
length: [5],
delimiter: ' ',
partLength: 3,
},
US: {
length: [9],
delimiter: '-',
partLength: 5,
},
};
function isCountryCodeDataWithFormatter(countryCodeData) {
return !!countryCodeData.formatter;
}
/**
* Tries to convert given postal code based on the country code into a standardised format
* @param {string} postalCode user inputted postal code
* @param {string|null|undefined} countryCode user selected country
* @returns {string} formatted postal code
*/
function tryToGuessPostalCodeFormat(postalCode, countryCode) {
if (!countryCode) {
return postalCode;
}
const countryCodeData = twoPartPostalCodes[countryCode];
if (!countryCodeData) {
return postalCode;
}
const postalCodeWithoutDelimiters = postalCode.replace(/[\s-]/g, '');
if (countryCodeData.length.includes(postalCodeWithoutDelimiters.length)) {
if (isCountryCodeDataWithFormatter(countryCodeData)) {
return countryCodeData.formatter(postalCodeWithoutDelimiters, countryCodeData.delimiter);
}
return defaultFormatter(postalCodeWithoutDelimiters, countryCodeData.delimiter, countryCodeData.partLength);
}
return postalCode;
}
const postalCodePatterns = {
US: /^\d{5}$/,
};
function isValidPostalCode(postalCode, countryCode = 'US') {
// TODO - every other country
if (!postalCode) {
return false;
}
const pattern = postalCodePatterns[countryCode];
if (!pattern) {
return true;
}
return pattern.test(postalCode);
}
//# sourceMappingURL=postal-code.js.map