Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

+44 mask is wrong #416

Open
Jhessiny opened this issue Dec 9, 2024 · 0 comments
Open

+44 mask is wrong #416

Jhessiny opened this issue Dec 9, 2024 · 0 comments

Comments

@Jhessiny
Copy link

Jhessiny commented Dec 9, 2024

I get an additional 0 in the phone mask when using 44 code with PhoneNumberFormat.NATIONAL only.

The result:
Screenshot from 2024-12-09 12-00-02

import { PhoneNumberUtil, PhoneNumberFormat } from 'google-libphonenumber'

import { PhoneValidator } from '@/app/presentation/common/protocols'
import { PhoneFormatter } from '@/app/presentation/common/protocols/phone-formatter'

export class GooglePhoneFormatter implements PhoneFormatter, PhoneValidator {
constructor (
private readonly phoneNumberFormat: PhoneNumberUtil = PhoneNumberUtil.getInstance()
) {}

validate (phoneNumber: string, countryCode: string): boolean {
if (!phoneNumber) return true
try {
const parsedNumber = this.phoneNumberFormat.parseAndKeepRawInput(
phoneNumber,
countryCode
)

  return this.phoneNumberFormat.isValidNumber(parsedNumber)
} catch (error) {
  console.error(error)
  return false
}

}

format (countryCode: string, phoneNumber: string): string {
if (!phoneNumber) return ''
try {
const parsedNumber = this.phoneNumberFormat.parseAndKeepRawInput(
phoneNumber,
countryCode
)

  const formattedNumber = this.phoneNumberFormat.format(
    parsedNumber,
    PhoneNumberFormat.NATIONAL
  )

  return formattedNumber
} catch (error) {
  console.error(error)
  return phoneNumber
}

}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant