Skip to content

Instantly share code, notes, and snippets.

@diorahman
Created May 20, 2016 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diorahman/09034e566fb1fe02156a81067fee4588 to your computer and use it in GitHub Desktop.
Save diorahman/09034e566fb1fe02156a81067fee4588 to your computer and use it in GitHub Desktop.
/* eslint-disable one-var */
/**
* Sanitize msisdn
*
* @param {string} msisdn - The MSISDN to be sanitized
* @param {object} config - The config: countryCode, stripCountryCode, leadingZero
* stripCountryCode means remove the countryCode from the msisdn
* leadingZero true means replace the stripped countryCode with zero
*/
const sanitize = (msisdn, {countryCode, stripCountryCode, leadingZero} = {}) => {
if (!msisdn) {
return null;
}
if (!countryCode) {
return msisdn;
}
const prefixExp = `^${countryCode}`,
prefix = new RegExp(prefixExp);
if (prefix.test(msisdn)) {
if (stripCountryCode) {
msisdn = msisdn.replace(prefix, '');
if (leadingZero) {
msisdn = `0${msisdn}`;
}
}
return msisdn;
}
if (leadingZero && !/^0/.test(msisdn)) {
msisdn = `0${msisdn}`;
}
return msisdn;
};
export { sanitize };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment