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
26 changes: 25 additions & 1 deletion scripts/generate-phone-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,32 @@ interface CountryPhoneData {
code: string;
callingCode: string;
formats: PhoneFormat[];
nationalPrefix?: string;
name: string;
flag: string;
}

function processCountry(code: string, data: MetadataCountry): CountryPhoneData {
const callingCode = data[0];
const nationalPrefix = data[5];
const rawFormats = Array.isArray(data[4]) ? data[4] : [];

// A format's national prefix formatting rule (e.g. "0$1") means the national
// prefix is part of how the number is displayed. We only carry the prefix for
// countries that display it, so e.g. US/CA (prefix "1", no rule) keep their
// bare-number formatting.
let usesNationalPrefix = false;
const formats: PhoneFormat[] = rawFormats.map((f) => {
const leadingDigitsArray = f[2];
// Use the last (most specific) leading digits pattern
const leadingDigits = leadingDigitsArray
? leadingDigitsArray[leadingDigitsArray.length - 1]
: undefined;

if (f[3]) {
usesNationalPrefix = true;
}

return {
pattern: f[0],
template: f[1],
Expand All @@ -78,6 +89,11 @@ function processCountry(code: string, data: MetadataCountry): CountryPhoneData {
code,
callingCode,
formats,
...(usesNationalPrefix &&
typeof nationalPrefix === 'string' &&
nationalPrefix.length > 0
? { nationalPrefix }
: {}),
name: getCountryName(code),
flag: countryCodeToFlag(code),
};
Expand Down Expand Up @@ -107,6 +123,10 @@ function generate(): string {
const mainCountry = mainCountries[0]!;
if (mainCountry !== code && allData[mainCountry]) {
allData[code]!.formats = allData[mainCountry]!.formats;
if (allData[mainCountry]!.nationalPrefix) {
allData[code]!.nationalPrefix =
allData[mainCountry]!.nationalPrefix;
}
}
}
}
Expand All @@ -131,6 +151,7 @@ function generate(): string {
lines.push(' code: string;');
lines.push(' callingCode: string;');
lines.push(' formats: PhoneFormat[];');
lines.push(' nationalPrefix?: string;');
lines.push(' name: string;');
lines.push(' flag: string;');
lines.push('};');
Expand All @@ -154,12 +175,15 @@ function generate(): string {
})
.join(', ');

const nationalPrefixStr = data.nationalPrefix
? `, nationalPrefix: ${JSON.stringify(data.nationalPrefix)}`
: '';
lines.push(
` ${JSON.stringify(code)}: { code: ${JSON.stringify(
data.code,
)}, callingCode: ${JSON.stringify(
data.callingCode,
)}, formats: [${formatsStr}], name: ${JSON.stringify(
)}, formats: [${formatsStr}]${nationalPrefixStr}, name: ${JSON.stringify(
data.name,
)}, flag: ${JSON.stringify(data.flag)} },`,
);
Expand Down
55 changes: 55 additions & 0 deletions src/formatters/__tests__/PhoneNumberTransformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,61 @@ describe('PhoneNumberTransformer', () => {
});
});

describe('national trunk prefix', () => {
const fr = new PhoneNumberTransformer({
country: 'FR',
includeCallingCode: false,
});
const gb = new PhoneNumberTransformer({
country: 'GB',
includeCallingCode: false,
});
const jp = new PhoneNumberTransformer({
country: 'JP',
includeCallingCode: false,
});
const au = new PhoneNumberTransformer({
country: 'AU',
includeCallingCode: false,
});
const us = new PhoneNumberTransformer({
country: 'US',
includeCallingCode: false,
});

it('formats a French mobile typed with the leading 0', () => {
expect(transform(fr, '0612345678')?.value).toBe('06 12 34 56 78');
});

it('formats a French landline typed with the leading 0', () => {
expect(transform(fr, '0123456789')?.value).toBe('01 23 45 67 89');
});

it('formats a UK mobile typed with the leading 0', () => {
expect(transform(gb, '07911123456')?.value).toBe('07911 123456');
});

it('formats a UK London number typed with the leading 0', () => {
expect(transform(gb, '02012345678')?.value).toBe('020 1234 5678');
});

it('formats a Japanese number typed with the leading 0', () => {
expect(transform(jp, '0312345678')?.value).toBe('03-1234-5678');
});

it('formats an Australian mobile typed with the leading 0', () => {
expect(transform(au, '0412345678')?.value).toBe('0412 345 678');
});

it('still formats the significant number when no trunk prefix is typed', () => {
expect(transform(fr, '612345678')?.value).toBe('6 12 34 56 78');
});

it('does not treat a leading 1 as a trunk prefix for US', () => {
expect(transform(us, '4155552671')?.value).toBe('(415) 555-2671');
});
});

describe('country switching', () => {
it('creates different transformers for different countries', () => {
const us = new PhoneNumberTransformer({ country: 'US' });
Expand Down
Loading
Loading