Created
May 20, 2016 02:42
-
-
Save diorahman/09034e566fb1fe02156a81067fee4588 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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