How to use the google-libphonenumber.AsYouTypeFormatter 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 blockchain / bc-phone-number / src / js / libphonenumber.js View on Github external
function formatNumber(val, countryCode, addSuffix, allowExtension, isAllowedKey) {
  try {
    var clean = val.replace(/\D/g, ""),
      // NOTE: we use AsYouTypeFormatter because the default format function can't handle incomplete numbers e.g. "+17024" formats to "+1 7024" as opposed to "+1 702-4"
      formatter = new libphonenumber.AsYouTypeFormatter(countryCode),
      // if clean is empty, we still need this to be a string otherwise we get errors later
      result = "",
      next,
      extSuffix = " ext. ";

    if (val.substr(0, 1) == "+") {
      clean = "+" + clean;
    }

    for (var i = 0; i < clean.length; i++) {
      // TODO: improve this so don't just pump in every digit every time - we should just cache this formatter object, and just call inputDigit once each time the user enters a new digit
      next = formatter.inputDigit(clean.charAt(i));
      // if adding this char didn't change the length, or made it smaller (and there's no longer any spaces): that means that formatting failed which means the number was no longer a potentially valid number, so if we're allowing extensions: assume the rest is the ext
      if (allowExtension && result && next.length <= result.length && next.indexOf(" ") == -1) {
        // set flag for extension
        next = -1;
github keybase / client / shared / util / phone-numbers / index.tsx View on Github external
// Return phone number in international format, e.g. +1 800 555 0123
// or e.164 if parsing fails
export const e164ToDisplay = (e164: string): string => {
  try {
    const phoneNumber = phoneUtil.parse(e164)
    if (phoneNumber.getCountryCode() === 1) {
      return '+1 ' + phoneUtil.format(phoneNumber, PNF.NATIONAL)
    }
    return phoneUtil.format(phoneNumber, PNF.INTERNATIONAL)
  } catch (e) {
    return e164
  }
}

export const AsYouTypeFormatter = libphonenumber.AsYouTypeFormatter

export const formatPhoneNumberInternational = (rawNumber: string): string | undefined => {
  try {
    const phoneNumber = phoneUtil.parse(rawNumber)
    return phoneUtil.format(phoneNumber, PNF.INTERNATIONAL)
  } catch {
    return undefined
  }
}
github theslip / react-bootstrap-intl-tel-input / src / index.js View on Github external
formatNumber (alpha2, number) {
    const unformattedNumber = this.unformatNumber(unformattedNumber)
    const formatter = new AsYouTypeFormatter(alpha2)
    const formattedNumberArray = `+${number}`.split('').map((char) => formatter.inputDigit(char))
    const intlPhoneNumber = formattedNumberArray.length ? formattedNumberArray[formattedNumberArray.length - 1] : unformattedNumber
    formatter.clear()
    return intlPhoneNumber
  }