Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/ui/Field/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as libphonenumber from 'google-libphonenumber';
import libphonenumber from 'google-libphonenumber';
const { PhoneNumberUtil, PhoneNumberFormat, AsYouTypeFormatter } = libphonenumber;

import type { PhoneNumberValue, RegionCode } from '../../types.js';

const phoneUtil = libphonenumber.PhoneNumberUtil.getInstance();
const AsYouTypeFormatter = libphonenumber.AsYouTypeFormatter;
const PNF = libphonenumber.PhoneNumberFormat;
const phoneUtil = PhoneNumberUtil.getInstance();

export function extractE164FromValue(value: PhoneNumberValue) {
return typeof value === 'object' && value !== null ? value.e164 : value || '';
Expand All @@ -14,7 +13,7 @@ export function parseE164ToNationalFormat(e164PhoneNumber: string): { regionCode
try {
const number = phoneUtil.parse(e164PhoneNumber);
const regionCode = phoneUtil.getRegionCodeForNumber(number) || null;
const national = phoneUtil.format(number, PNF.NATIONAL);
const national = phoneUtil.format(number, PhoneNumberFormat.NATIONAL);

return { regionCode, national };
} catch {
Expand All @@ -35,7 +34,7 @@ export function formatToNationalAsYouType(input: string, regionCode: RegionCode)
export function convertToE164(nationalPhoneNumber: string, regionCode: RegionCode): { e164: string; detectedRegion: RegionCode | null } | null {
try {
const number = phoneUtil.parseAndKeepRawInput(nationalPhoneNumber, regionCode);
const e164 = phoneUtil.format(number, PNF.E164);
const e164 = phoneUtil.format(number, PhoneNumberFormat.E164);
const detectedRegion = phoneUtil.getRegionCodeForNumber(number) || null;

return { e164, detectedRegion };
Expand All @@ -55,7 +54,7 @@ export function parseInternationalNumber(input: string): { regionCode: RegionCod
const cleanInput = extractDigitsWithPlus(input);
const number = phoneUtil.parse(cleanInput);
const regionCode = phoneUtil.getRegionCodeForNumber(number) || null;
const national = phoneUtil.format(number, PNF.NATIONAL);
const national = phoneUtil.format(number, PhoneNumberFormat.NATIONAL);

return { regionCode, national };
} catch {
Expand Down
15 changes: 7 additions & 8 deletions src/utilities/getPhoneNumberDetails.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import type { PhoneNumber } from '../types.js';

import * as libphonenumber from 'google-libphonenumber';
import libphonenumber from 'google-libphonenumber';
const { PhoneNumberUtil, PhoneNumberFormat } = libphonenumber;

import { countries } from './countries.js';

let phoneUtil: libphonenumber.PhoneNumberUtil | null = null;
let PNF: typeof libphonenumber.PhoneNumberFormat | null = null;

export function getPhoneNumberDetails(e164PhoneNumber: string): PhoneNumber | null {
if (!phoneUtil || !PNF) {
phoneUtil = libphonenumber.PhoneNumberUtil.getInstance();
PNF = libphonenumber.PhoneNumberFormat;
if (!phoneUtil) {
phoneUtil = PhoneNumberUtil.getInstance();
}

try {
Expand All @@ -21,9 +20,9 @@ export function getPhoneNumberDetails(e164PhoneNumber: string): PhoneNumber | nu
return null;
}

const national = phoneUtil.format(number, PNF.NATIONAL);
const international = phoneUtil.format(number, PNF.INTERNATIONAL);
const e164Formatted = phoneUtil.format(number, PNF.E164);
const national = phoneUtil.format(number, PhoneNumberFormat.NATIONAL);
const international = phoneUtil.format(number, PhoneNumberFormat.INTERNATIONAL);
const e164Formatted = phoneUtil.format(number, PhoneNumberFormat.E164);

const country = countries.find((c) => c.regionCode === regionCode);
const callingCode = country?.callingCode || `+${number.getCountryCode()}`;
Expand Down
5 changes: 3 additions & 2 deletions src/utilities/validate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { TextField, Validate } from 'payload';

import * as libphonenumber from 'google-libphonenumber';
import libphonenumber from 'google-libphonenumber';
const { PhoneNumberUtil } = libphonenumber;

import type { RegionCode } from '../types.js';

Expand All @@ -9,7 +10,7 @@ let phoneUtil: libphonenumber.PhoneNumberUtil | null = null;
export const createPhoneNumberValidator = (allowedCountries?: RegionCode[]): Validate<string, unknown, unknown, TextField> => {
return (value, { req, required }) => {
if (!phoneUtil) {
phoneUtil = libphonenumber.PhoneNumberUtil.getInstance();
phoneUtil = PhoneNumberUtil.getInstance();
}

if (!value) {
Expand Down