Skip to content

feat: add international auto-detect mode to PhoneNumberTransformer#11

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

feat: add international auto-detect mode to PhoneNumberTransformer#11
janicduplessis merged 1 commit into
mainfrom
@janic/phone-number-international

Conversation

@janicduplessis

Copy link
Copy Markdown
Contributor

Description

PhoneNumberTransformer formats for a single fixed country, so a field built on it can't let the user type a different country's calling code — the worklet rewrites the prefix back to the configured country, and country changes have to go through a separate picker.

Solution

Add an international option. The calling code becomes part of the editable text and the country is detected from it as you type: typing +44… switches formatting to the UK, live on the UI thread. The country option is ignored in this mode.

  • The generator emits a COUNTRY_CALLING_CODES index (calling code → ISO codes, primary first); the worklet matches the longest calling-code prefix (1–3 digits) against it and formats the national part with that country's formats. (The phone-data.ts diff is only this appended export — existing COUNTRY_PHONE_DATA is unchanged, so ~235 of the changed lines are generated data.)
  • detectCountry(value) is exported (JS thread) so a consumer can drive a flag/country indicator from the value.

Ambiguous codes (e.g. +1 NANP) resolve to the primary country; per-area-code disambiguation is left for later.

Test plan

  • Outputs match libphonenumber-js AsYouType, e.g. +447911123456+44 7911 123456.
  • yarn test — added cases covering +1/+33/+44 formatting (incl. a French trunk-0 number, and that country is ignored), bare-code display, and detectCountry US/GB/FR/undefined.
  • Verified on-device (Android): typing +44… switches formatting + the flag inline; the picker sets the +code; continuing to type formats correctly.

Adds an `international` option where the calling code is part of the editable text and the country is detected from it as the user types (e.g. typing +44 switches formatting to the UK). The worklet matches the longest calling-code prefix against a generated COUNTRY_CALLING_CODES index and formats the national part for that country, so detection + formatting stay on the UI thread.

Also exports detectCountry(value) (JS thread) to drive a flag/country indicator from the value. Ambiguous codes (e.g. +1 NANP) resolve to the primary country; area-code disambiguation is left for later. Tests added for international formatting and detectCountry.
@janicduplessis janicduplessis marked this pull request as ready for review June 18, 2026 18:30
@janicduplessis janicduplessis merged commit 502653a into main Jun 18, 2026
5 checks passed
@janicduplessis janicduplessis deleted the @janic/phone-number-international branch June 18, 2026 18:30
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