Skip to content

Commit 11b2afe

Browse files
committed
fix: add missing PhoneUtil for extract i18n numbers
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 2cc43ef commit 11b2afe

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

  • apps/flipcash/shared/phone/src/main/kotlin/com/flipcash/app/phone

apps/flipcash/shared/phone/src/main/kotlin/com/flipcash/app/phone/PhoneUtils.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,27 @@ class PhoneUtils @Inject constructor(
129129
}
130130
}
131131

132+
/**
133+
* Parse an international phone number (e.g. "+15551234567") into its country locale
134+
* and national number digits. Returns null if parsing fails.
135+
*/
136+
fun parseInternationalNumber(number: String): Pair<CountryLocale, String>? {
137+
val cleaned = number.filter { it.isDigit() || it == '+' }
138+
if (cleaned.isBlank() || !cleaned.startsWith("+")) return null
139+
140+
return try {
141+
val parsed = phoneNumberUtil.parse(cleaned, defaultCountryLocale.countryCode)
142+
if (!phoneNumberUtil.isValidNumber(parsed)) return null
143+
144+
val regionCode = phoneNumberUtil.getRegionCodeForNumber(parsed) ?: return null
145+
val locale = countryLocales.find { it.countryCode == regionCode } ?: return null
146+
val nationalNumber = parsed.nationalNumber.toString()
147+
locale to nationalNumber
148+
} catch (_: NumberParseException) {
149+
null
150+
}
151+
}
152+
132153
fun toE164(rawNumber: String): String? {
133154
val cleaned = rawNumber.filter { it.isDigit() || it == '+' }
134155
if (cleaned.isBlank()) return null

0 commit comments

Comments
 (0)