Skip to content

Commit ced16e2

Browse files
cpcloudclaude
andcommitted
fix(locale): handle shared calling codes for fictional numbers
For fictional numbers (no parsed region) with a "+" prefix under a shared calling code (e.g. +1 for US/CA), use INTERNATIONAL format to avoid misattribution. Local numbers without "+" prefix still format as NATIONAL since the user is entering a domestic number. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent e698c0a commit ced16e2

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

internal/locale/phone.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,25 @@ func FormatPhoneNumber(number, regionCode string) string {
2222
return number
2323
}
2424
// Prefer region comparison (distinguishes US/CA under shared +1 code).
25-
// Fall back to country-code comparison for fictional numbers (e.g. 555-xxxx)
26-
// that parse successfully but return an empty region.
2725
parsedRegion := phonenumbers.GetRegionCodeForNumber(parsed)
2826
if parsedRegion != "" {
2927
if parsedRegion == regionCode {
3028
return phonenumbers.Format(parsed, phonenumbers.NATIONAL)
3129
}
3230
return phonenumbers.Format(parsed, phonenumbers.INTERNATIONAL)
3331
}
34-
if int(parsed.GetCountryCode()) == phonenumbers.GetCountryCodeForRegion(regionCode) {
35-
return phonenumbers.Format(parsed, phonenumbers.NATIONAL)
32+
// Fictional numbers (e.g. 555-xxxx) parse but have no region. When
33+
// the input lacks a "+" prefix, the user entered a local number — use
34+
// NATIONAL if the country code matches. When "+" is present, the user
35+
// explicitly typed an international code; for shared codes like +1
36+
// (US/CA) use INTERNATIONAL to avoid misattribution.
37+
cc := int(parsed.GetCountryCode())
38+
if cc != phonenumbers.GetCountryCodeForRegion(regionCode) {
39+
return phonenumbers.Format(parsed, phonenumbers.INTERNATIONAL)
40+
}
41+
if strings.HasPrefix(trimmed, "+") &&
42+
len(phonenumbers.GetRegionCodesForCountryCode(cc)) > 1 {
43+
return phonenumbers.Format(parsed, phonenumbers.INTERNATIONAL)
3644
}
37-
return phonenumbers.Format(parsed, phonenumbers.INTERNATIONAL)
45+
return phonenumbers.Format(parsed, phonenumbers.NATIONAL)
3846
}

internal/locale/phone_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ func TestFormatPhoneNumber(t *testing.T) {
2222
{"US national", "5551234567", "US", "(555) 123-4567"},
2323
{"UK national", "02079460958", "GB", "020 7946 0958"},
2424
{"international prefix", "+442079460958", "US", "+44 20 7946 0958"},
25-
{"same-region prefix", "+15551234567", "US", "(555) 123-4567"},
25+
{"same-region prefix real", "+12025551234", "US", "(202) 555-1234"},
26+
{"shared-code fictional prefix", "+15551234567", "US", "+1 555-123-4567"},
2627
{"already formatted", "(555) 123-4567", "US", "(555) 123-4567"},
2728
{"cross-border shared code", "+16135551234", "US", "+1 613-555-1234"},
2829
{"garbage passthrough", "not a phone", "US", "not a phone"},

0 commit comments

Comments
 (0)