normalizePhone.js 703 B

12345678910111213141516171819202122
  1. var trim = require('./trim');
  2. exports = function(phone, options) {
  3. phone = trim(phone);
  4. var countryCode = options.countryCode,
  5. _options$trunkPrefix = options.trunkPrefix,
  6. trunkPrefix =
  7. _options$trunkPrefix === void 0 ? false : _options$trunkPrefix;
  8. var plusSign = regPlusSign.test(phone);
  9. phone = phone.replace(regNotDigit, '');
  10. if (plusSign) {
  11. phone = phone.replace(new RegExp('^'.concat(countryCode)), '');
  12. }
  13. if (trunkPrefix) {
  14. phone = phone.replace(regTrunkPrefix, '');
  15. }
  16. return '+'.concat(countryCode + phone);
  17. };
  18. var regPlusSign = /^\+/;
  19. var regNotDigit = /\D/g;
  20. var regTrunkPrefix = /^\d/;
  21. module.exports = exports;