Skip to content

feat: handle national trunk prefix in PhoneNumberTransformer#10

Merged
janicduplessis merged 1 commit into
mainfrom
@janic/phone-number-national-prefix
Jun 18, 2026
Merged

feat: handle national trunk prefix in PhoneNumberTransformer#10
janicduplessis merged 1 commit into
mainfrom
@janic/phone-number-national-prefix

Conversation

@janicduplessis

Copy link
Copy Markdown
Contributor

Description

Phone numbers in countries with a national trunk prefix were mis-formatted when the prefix was typed. PhoneNumberTransformer treated the leading prefix digit as part of the national significant number, so e.g. France 0612345678 rendered as 0 61 23 45 67 (wrong grouping, last digit dropped) instead of 06 12 34 56 78. NANP countries (US/CA) were unaffected — they have no displayed trunk prefix.

Solution

libphonenumber's format patterns describe the national significant number (without the trunk prefix), which is reapplied via a per-format nationalPrefixFormattingRule (e.g. "0$1").

  • The generator emits nationalPrefix for a country only when one of its formats has a nationalPrefixFormattingRule — so display-prefix countries (FR/GB/DE/JP/AU/…) get it, while US/CA (prefix "1", no rule) keep bare-number formatting.
  • The worklet strips the national prefix before selecting/applying a format, then prepends it back. It re-adds the raw prefix digit (rather than dropping it as E.164 would) so the per-digit cursor mapping stays aligned with the input.

Note: phone-data.ts is regenerated, so the diff also carries a small libphonenumber-js data refresh (it was last generated against older metadata) on top of the nationalPrefix additions.

Test plan

  • yarn test — added PhoneNumberTransformer.test.ts cases for FR/GB/JP/AU typed with the leading 0, plus a US case asserting a leading 1 is not treated as a trunk prefix.
  • Outputs match libphonenumber-js AsYouType, e.g. FR 061234567806 12 34 56 78, GB 02012345678020 1234 5678.
  • Verified on-device (iOS): the national-only field (includeCallingCode: false) now formats FR/GB correctly as you type.

Numbers in trunk-prefix countries (e.g. France 06…, UK 07…) were mis-grouped because the formatter treated the leading national-prefix digit as part of the significant number. The generator now emits nationalPrefix for countries whose formats display it (via nationalPrefixFormattingRule), and the worklet strips that prefix before selecting/applying a format, then re-adds it so cursor mapping stays aligned.

US/CA (prefix "1", no display rule) are unaffected. Verified against libphonenumber-js AsYouType for FR/GB/JP/AU; tests added to PhoneNumberTransformer.test.ts.
@janicduplessis janicduplessis marked this pull request as ready for review June 18, 2026 16:23
@janicduplessis janicduplessis merged commit ca7bed1 into main Jun 18, 2026
5 checks passed
@janicduplessis janicduplessis deleted the @janic/phone-number-national-prefix branch June 18, 2026 16:23
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

Successfully merging this pull request may close these issues.

1 participant