Skip to content

Commit 44fa2c4

Browse files
Merge pull request #14 from adsign/fix-cjs-esm-bridge
fix: import cjs in a way so it does not break esm projects
2 parents 5d4fa12 + ac2cbab commit 44fa2c4

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

src/ui/Field/helpers.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import * as libphonenumber from 'google-libphonenumber';
1+
import libphonenumber from 'google-libphonenumber';
2+
const { PhoneNumberUtil, PhoneNumberFormat, AsYouTypeFormatter } = libphonenumber;
23

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

5-
const phoneUtil = libphonenumber.PhoneNumberUtil.getInstance();
6-
const AsYouTypeFormatter = libphonenumber.AsYouTypeFormatter;
7-
const PNF = libphonenumber.PhoneNumberFormat;
6+
const phoneUtil = PhoneNumberUtil.getInstance();
87

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

1918
return { regionCode, national };
2019
} catch {
@@ -35,7 +34,7 @@ export function formatToNationalAsYouType(input: string, regionCode: RegionCode)
3534
export function convertToE164(nationalPhoneNumber: string, regionCode: RegionCode): { e164: string; detectedRegion: RegionCode | null } | null {
3635
try {
3736
const number = phoneUtil.parseAndKeepRawInput(nationalPhoneNumber, regionCode);
38-
const e164 = phoneUtil.format(number, PNF.E164);
37+
const e164 = phoneUtil.format(number, PhoneNumberFormat.E164);
3938
const detectedRegion = phoneUtil.getRegionCodeForNumber(number) || null;
4039

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

6059
return { regionCode, national };
6160
} catch {

src/utilities/getPhoneNumberDetails.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import type { PhoneNumber } from '../types.js';
22

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

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

78
let phoneUtil: libphonenumber.PhoneNumberUtil | null = null;
8-
let PNF: typeof libphonenumber.PhoneNumberFormat | null = null;
99

1010
export function getPhoneNumberDetails(e164PhoneNumber: string): PhoneNumber | null {
11-
if (!phoneUtil || !PNF) {
12-
phoneUtil = libphonenumber.PhoneNumberUtil.getInstance();
13-
PNF = libphonenumber.PhoneNumberFormat;
11+
if (!phoneUtil) {
12+
phoneUtil = PhoneNumberUtil.getInstance();
1413
}
1514

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

24-
const national = phoneUtil.format(number, PNF.NATIONAL);
25-
const international = phoneUtil.format(number, PNF.INTERNATIONAL);
26-
const e164Formatted = phoneUtil.format(number, PNF.E164);
23+
const national = phoneUtil.format(number, PhoneNumberFormat.NATIONAL);
24+
const international = phoneUtil.format(number, PhoneNumberFormat.INTERNATIONAL);
25+
const e164Formatted = phoneUtil.format(number, PhoneNumberFormat.E164);
2726

2827
const country = countries.find((c) => c.regionCode === regionCode);
2928
const callingCode = country?.callingCode || `+${number.getCountryCode()}`;

src/utilities/validate.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { TextField, Validate } from 'payload';
22

3-
import * as libphonenumber from 'google-libphonenumber';
3+
import libphonenumber from 'google-libphonenumber';
4+
const { PhoneNumberUtil } = libphonenumber;
45

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

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

1516
if (!value) {

0 commit comments

Comments
 (0)