UNPKG

libphonenumber-js

Version:

A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript

167 lines (163 loc) 10.2 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _createForOfIteratorHelperLoose(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (t) return (t = t.call(r)).next.bind(t); if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var o = 0; return function () { return o >= r.length ? { done: !0 } : { done: !1, value: r[o++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } // This is a port of Google Android `libphonenumber`'s // `phonenumberutil.js` of December 31th, 2018. // // https://github.com/googlei18n/libphonenumber/commits/master/javascript/i18n/phonenumbers/phonenumberutil.js import matchesEntirely from './helpers/matchesEntirely.js'; import formatNationalNumberUsingFormat from './helpers/formatNationalNumberUsingFormat.js'; import Metadata, { getCountryCallingCode } from './metadata.js'; import getIddPrefix from './helpers/getIddPrefix.js'; import { formatRFC3966 } from './helpers/RFC3966.js'; var DEFAULT_OPTIONS = { formatExtension: function formatExtension(formattedNumber, extension, metadata) { return "".concat(formattedNumber).concat(metadata.ext()).concat(extension); } }; /** * Formats a phone number. * * format(phoneNumberInstance, 'INTERNATIONAL', { ..., v2: true }, metadata) * format(phoneNumberInstance, 'NATIONAL', { ..., v2: true }, metadata) * * format({ phone: '8005553535', country: 'RU' }, 'INTERNATIONAL', { ... }, metadata) * format({ phone: '8005553535', country: 'RU' }, 'NATIONAL', undefined, metadata) * * @param {object|PhoneNumber} input — If `options.v2: true` flag is passed, the `input` should be a `PhoneNumber` instance. Otherwise, it should be an object of shape `{ phone: '...', country: '...' }`. * @param {string} format * @param {object} [options] * @param {object} metadata * @return {string} */ export default function formatNumber(input, format, options, metadata) { // Apply default options. if (options) { options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options); } else { options = DEFAULT_OPTIONS; } metadata = new Metadata(metadata); if (input.country && input.country !== '001') { // Validate `input.country`. if (!metadata.hasCountry(input.country)) { throw new Error("Unknown country: ".concat(input.country)); } metadata.country(input.country); } else if (input.countryCallingCode) { metadata.selectNumberingPlan(input.countryCallingCode); } else return input.phone || ''; var countryCallingCode = metadata.countryCallingCode(); var nationalNumber = options.v2 ? input.nationalNumber : input.phone; // This variable should have been declared inside `case`s // but Babel has a bug and it says "duplicate variable declaration". var number; switch (format) { case 'NATIONAL': // Legacy argument support. // (`{ country: ..., phone: '' }`) if (!nationalNumber) { return ''; } number = formatNationalNumber(nationalNumber, input.carrierCode, 'NATIONAL', metadata, options); return addExtension(number, input.ext, metadata, options.formatExtension); case 'INTERNATIONAL': // Legacy argument support. // (`{ country: ..., phone: '' }`) if (!nationalNumber) { return "+".concat(countryCallingCode); } number = formatNationalNumber(nationalNumber, null, 'INTERNATIONAL', metadata, options); number = "+".concat(countryCallingCode, " ").concat(number); return addExtension(number, input.ext, metadata, options.formatExtension); case 'E.164': // `E.164` doesn't define "phone number extensions". return "+".concat(countryCallingCode).concat(nationalNumber); case 'RFC3966': return formatRFC3966({ number: "+".concat(countryCallingCode).concat(nationalNumber), ext: input.ext }); // For reference, here's Google's IDD formatter: // https://github.com/google/libphonenumber/blob/32719cf74e68796788d1ca45abc85dcdc63ba5b9/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L1546 // Not saying that this IDD formatter replicates it 1:1, but it seems to work. // Who would even need to format phone numbers in IDD format anyway? case 'IDD': if (!options.fromCountry) { return; // throw new Error('`fromCountry` option not passed for IDD-prefixed formatting.') } var formattedNumber = formatIDD(nationalNumber, input.carrierCode, countryCallingCode, options.fromCountry, metadata); return addExtension(formattedNumber, input.ext, metadata, options.formatExtension); default: throw new Error("Unknown \"format\" argument passed to \"formatNumber()\": \"".concat(format, "\"")); } } function formatNationalNumber(number, carrierCode, formatAs, metadata, options) { var format = chooseFormatForNumber(metadata.formats(), number); if (!format) { return number; } return formatNationalNumberUsingFormat(number, format, { useInternationalFormat: formatAs === 'INTERNATIONAL', withNationalPrefix: format.nationalPrefixIsOptionalWhenFormattingInNationalFormat() && options && options.nationalPrefix === false ? false : true, carrierCode: carrierCode, metadata: metadata }); } export function chooseFormatForNumber(availableFormats, nationalNnumber) { for (var _iterator = _createForOfIteratorHelperLoose(availableFormats), _step; !(_step = _iterator()).done;) { var format = _step.value; // Validate leading digits. // The test case for "else path" could be found by searching for // "format.leadingDigitsPatterns().length === 0". if (format.leadingDigitsPatterns().length > 0) { // The last leading_digits_pattern is used here, as it is the most detailed var lastLeadingDigitsPattern = format.leadingDigitsPatterns()[format.leadingDigitsPatterns().length - 1]; // If leading digits don't match then move on to the next phone number format if (nationalNnumber.search(lastLeadingDigitsPattern) !== 0) { continue; } } // Check that the national number matches the phone number format regular expression if (matchesEntirely(nationalNnumber, format.pattern())) { return format; } } } function addExtension(formattedNumber, ext, metadata, formatExtension) { return ext ? formatExtension(formattedNumber, ext, metadata) : formattedNumber; } function formatIDD(nationalNumber, carrierCode, countryCallingCode, fromCountry, metadata) { var fromCountryCallingCode = getCountryCallingCode(fromCountry, metadata.metadata); // When calling within the same country calling code. if (fromCountryCallingCode === countryCallingCode) { var formattedNumber = formatNationalNumber(nationalNumber, carrierCode, 'NATIONAL', metadata); // For NANPA regions, return the national format for these regions // but prefix it with the country calling code. if (countryCallingCode === '1') { return countryCallingCode + ' ' + formattedNumber; } // If regions share a country calling code, the country calling code need // not be dialled. This also applies when dialling within a region, so this // if clause covers both these cases. Technically this is the case for // dialling from La Reunion to other overseas departments of France (French // Guiana, Martinique, Guadeloupe), but not vice versa - so we don't cover // this edge case for now and for those cases return the version including // country calling code. Details here: // http://www.petitfute.com/voyage/225-info-pratiques-reunion // return formattedNumber; } var iddPrefix = getIddPrefix(fromCountry, undefined, metadata.metadata); if (iddPrefix) { return "".concat(iddPrefix, " ").concat(countryCallingCode, " ").concat(formatNationalNumber(nationalNumber, null, 'INTERNATIONAL', metadata)); } } //# sourceMappingURL=format.js.map