How to use the google-libphonenumber.PhoneNumberUtil.getInstance function in google-libphonenumber

To help you get started, we’ve selected a few google-libphonenumber examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jtorhoff / flier / src / components / auth / SendCode.tsx View on Github external
onInput(event: React.SyntheticEvent) {
        event.preventDefault();

        let phoneNumber = event.currentTarget.value as string;
        try {
            const phoneUtil = PhoneNumberUtil.getInstance();
            phoneNumber = phoneUtil.format(
                phoneUtil.parse(phoneNumber),
                PhoneNumberFormat.INTERNATIONAL);
        } catch (e) {
            // Ignore errors as we validate on submit
        }

        this.setState({
            phoneNumber: phoneNumber,
        });

        if (this.state.error) {
            this.validate(phoneNumber);
        }
    }
github MoveOnOrg / Spoke / src / lib / phone-format.js View on Github external
export const getFormattedPhoneNumber = (cell, country = "US") => {
  const phoneUtil = PhoneNumberUtil.getInstance();
  // we return an empty string vs null when the phone number is inValid
  // because when the cell is null, batch inserts into campaign contacts fail
  // then when contacts have cell.length < 12 (+1), it's deleted before assignments are created
  try {
    const inputNumber = phoneUtil.parse(cell, country);
    const isValid = phoneUtil.isValidNumber(inputNumber);
    if (isValid) {
      return phoneUtil.format(inputNumber, PhoneNumberFormat.E164);
    }
    return "";
  } catch (e) {
    log.error(e);
    return "";
  }
};
github celo-org / celo-monorepo / packages / utils / src / phoneNumbers.ts View on Github external
import {
  PhoneNumber,
  PhoneNumberFormat,
  PhoneNumberType,
  PhoneNumberUtil,
} from 'google-libphonenumber'
import * as Web3Utils from 'web3-utils'

export interface ParsedPhoneNumber {
  e164Number: string
  displayNumber: string
  countryCode?: number
  regionCode?: string
}

const phoneUtil = PhoneNumberUtil.getInstance()
const MIN_PHONE_LENGTH = 4

export function getCountryEmoji(
  e164PhoneNumber: string,
  countryCodePossible?: number,
  regionCodePossible?: string
) {
  // The country code and region code can both be passed in, or it can be inferred from the e164PhoneNumber
  let countryCode: any
  let regionCode: any
  countryCode = countryCodePossible
  regionCode = regionCodePossible
  if (!countryCode || !regionCode) {
    countryCode = getCountryCode(e164PhoneNumber)
    regionCode = getRegionCode(e164PhoneNumber)
  }
github celo-org / celo-monorepo / packages / attestation-service / src / sms.ts View on Github external
import { PhoneNumberUtil } from 'google-libphonenumber'
import Nexmo from 'nexmo'
import { fetchEnv } from './env'

const phoneUtil = PhoneNumberUtil.getInstance()

let nexmoClient: any
let nexmoNumbers: Array<{
  code: string
  phoneNumber: string
}> = []

export async function initializeSmsProviders() {
  nexmoClient = new Nexmo({
    apiKey: fetchEnv('NEXMO_KEY'),
    apiSecret: fetchEnv('NEXMO_SECRET'),
  })

  const availableNumbers = await getAvailableNumbers()

  nexmoNumbers = availableNumbers.map((number: any) => ({
github celo-org / celo-monorepo / packages / verification-pool-api / src / verification.ts View on Github external
} from './config'
import {
  deleteMessage,
  getActiveVerifiers,
  getMessagesForPhoneNumber,
  incrementVerifierAttemptCount,
  isMessageSent,
  saveMessage,
  setVerifierProperties,
} from './database'
import { MobileVerifier } from './types'

const SMS_LENGTH_LIMIT = 160
const NUM_VERIFIERS_TO_WAKE = 3
const MAX_VERIFIER_ATTEMPT_COUNT = 20
const phoneUtil = PhoneNumberUtil.getInstance()

export async function sendSmsCode(address: string, phoneNumber: string, message: string) {
  console.info('Attempting to send sms verification code.')

  message = getFormattedMessage(message)

  if (alwaysUseTwilio) {
    console.info('Config set to always use Twilio')
    await sendViaTextProvider(phoneNumber, message)
    return 'Twilio'
  }

  const verifiers = await getRandomActiveVerifiers(NUM_VERIFIERS_TO_WAKE, phoneNumber)
  if (verifiers === null || verifiers.length === 0) {
    console.info('No suitable verifiers found. Using Twilio')
    await sendViaTextProvider(phoneNumber, message)
github MoveOnOrg / Spoke / src / lib / phone-format.js View on Github external
export const getDisplayPhoneNumber = (e164Number, country = "US") => {
  const phoneUtil = PhoneNumberUtil.getInstance();
  const parsed = phoneUtil.parse(e164Number, country);
  return phoneUtil.format(parsed, PhoneNumberFormat.NATIONAL);
};
github Nightapes / ngx-validators / src / components / phone / phone-validators.ts View on Github external
const validator = (control: AbstractControl): { [key: string]: any } => {
            if (Util.isNotPresent(control)) {
                return undefined;
            }

            if (!PhoneValidators.checkRegionCode(local)) {
                return { 'noValidRegionCode': true };
            }

            let phoneParser: PhoneNumberUtil = PhoneNumberUtil.getInstance();

            let error = { 'noPhoneNumber': true };
            try {
                let phoneNumber: libPhoneNumber.PhoneNumber = phoneParser.parse(control.value, local);
                if (phoneParser.isValidNumber(phoneNumber)) {
                    error = undefined;
                }
            } catch (err) {
                error = { 'noPhoneNumber': true };
            }

            return error;
        };
        return validator;
github jtorhoff / flier / src / components / auth / SendCode.tsx View on Github external
validate(number: string): boolean {
        let valid = false;
        let error: string | undefined = undefined;
        try {
            const phoneUtil = PhoneNumberUtil.getInstance();
            const phoneNumber = phoneUtil.parse(number);
            valid = phoneUtil.isValidNumber(phoneNumber);
        } catch (e) {
            valid = false;
        }

        if (!valid) {
            error = "Invalid phone number";
        }
        this.setState({
            error: error,
        });

        return valid;
    }
github theslip / react-bootstrap-intl-tel-input / src / index.js View on Github external
constructor () {
    super()
    this.phoneUtil = PhoneNumberUtil.getInstance()
    this.countries = countries.callingCountries.all.filter((country) => country.status === 'assigned')
    this.mouseDownOnMenu = false
    this._pageClick = this.pageClick.bind(this)
    this.missingFlags = { AQ: 'WW', BQ: 'NL', EH: 'WW-AFR', MF: 'FR', SH: 'GB' }
    this.boxShadowStyle = '0 8px 10px 1px rgba(0, 0, 0, 0.14), 0 3px 14px 2px rgba(0, 0, 0, 0.12), 0 5px 5px -3px rgba(0, 0, 0, 0.4)'
    this.bgColorTransitionStyle = 'background-color .25s, color .25s'
    this.inputID = '__react-intl-tel-input'
    this.state = {
      open: false,
      selectedCountry: {},
      intlPhoneNumber: '',
      phoneNumber: '',
      searchTerm: '',
      valid: false,
      filteredCountries: [],
      preferredCountries: [],