UNPKG

@artmajeur/react-native-paper-phone-number-input

Version:

A performant phone number input component for react-native-paper with country picker

46 lines (45 loc) 2.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.liveFormatPhoneNumber = exports.getDialCodeByCode = exports.getCountryByCode = exports.extractPhoneInfo = void 0; var _libphonenumberJs = _interopRequireWildcard(require("libphonenumber-js")); var _countries = require("./data/countries"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } const getCountryByCode = (code = '##') => { const country = _countries.countriesMap[code]; if (!country) { throw new Error(`Country with code ${code} not found`); } return country; }; exports.getCountryByCode = getCountryByCode; const getDialCodeByCode = (code = '##') => { const country = _countries.countriesMap[code]; if (!country) { throw new Error(`Country with code ${code} not found`); } return country.dialCode; }; exports.getDialCodeByCode = getDialCodeByCode; const extractPhoneInfo = input => { const phoneNumber = (0, _libphonenumberJs.default)(input); if (!phoneNumber || !phoneNumber.isValid()) { return { error: 'Invalid phone number' }; } return { dialCode: phoneNumber.countryCallingCode, nationalNumber: phoneNumber.formatNational(), code: phoneNumber.country }; }; exports.extractPhoneInfo = extractPhoneInfo; const liveFormatPhoneNumber = (input, countryCode) => { const formatter = new _libphonenumberJs.AsYouType(countryCode); return formatter.input(input); }; exports.liveFormatPhoneNumber = liveFormatPhoneNumber; //# sourceMappingURL=utils.js.map