20 lines
713 B
Swift
20 lines
713 B
Swift
import Foundation
|
|
|
|
/// Phone number normalization utilities
|
|
public enum PhoneUtils {
|
|
/// Normalize a phone number to a consistent format:
|
|
/// - 10 digits → +1XXXXXXXXXX (US number)
|
|
/// - 11 digits starting with 1 → +1XXXXXXXXXX
|
|
/// - Already has + prefix → keep digits only
|
|
/// - Otherwise → digits only
|
|
public static func normalize(_ phone: String) -> String {
|
|
let digits = phone.filter { $0.isNumber || $0 == "+" }
|
|
if digits.hasPrefix("1") && !digits.hasPrefix("+") && digits.count == 11 {
|
|
return "+" + digits
|
|
}
|
|
if !digits.hasPrefix("+") && digits.count == 10 {
|
|
return "+1" + digits
|
|
}
|
|
return digits
|
|
}
|
|
}
|