parent
69c0163ccb
commit
a47a0c26ee
@ -0,0 +1,59 @@ |
||||
import Foundation |
||||
|
||||
func areFrenchPhoneNumbersSimilar(_ phoneNumber1: String?, _ phoneNumber2: String?) -> Bool { |
||||
|
||||
if phoneNumber1?.canonicalVersion == phoneNumber2?.canonicalVersion { |
||||
return true |
||||
} |
||||
|
||||
// Helper function to normalize a phone number, now returning an optional String |
||||
func normalizePhoneNumber(_ numberString: String?) -> String? { |
||||
// 1. Safely unwrap the input string. If it's nil or empty, return nil immediately. |
||||
guard let numberString = numberString, !numberString.isEmpty else { |
||||
return nil |
||||
} |
||||
|
||||
// 2. Remove all non-digit characters |
||||
let digitsOnly = numberString.filter(\.isNumber) |
||||
|
||||
// If after filtering, there are no digits, return nil. |
||||
guard !digitsOnly.isEmpty else { |
||||
return nil |
||||
} |
||||
|
||||
// 3. Handle French specific prefixes and extract the relevant part |
||||
// We need at least 9 digits to get a meaningful 8-digit comparison from the end |
||||
if digitsOnly.count >= 9 { |
||||
if digitsOnly.hasPrefix("0") { |
||||
return String(digitsOnly.suffix(9)) |
||||
} else if digitsOnly.hasPrefix("33") { |
||||
// Ensure there are enough digits after dropping "33" |
||||
if digitsOnly.count >= 11 { // "33" + 9 digits = 11 |
||||
return String(digitsOnly.dropFirst(2).suffix(9)) |
||||
} else { |
||||
return nil // Not enough digits after dropping "33" |
||||
} |
||||
} else if digitsOnly.count == 9 { // Case like 612341234 |
||||
return digitsOnly |
||||
} else { // More digits but no 0 or 33 prefix, take the last 9 |
||||
return String(digitsOnly.suffix(9)) |
||||
} |
||||
} |
||||
|
||||
return nil // If it doesn't fit the expected patterns or is too short |
||||
} |
||||
|
||||
// Normalize both phone numbers. If either results in nil, we can't compare. |
||||
guard let normalizedNumber1 = normalizePhoneNumber(phoneNumber1), |
||||
let normalizedNumber2 = normalizePhoneNumber(phoneNumber2) else { |
||||
return false |
||||
} |
||||
|
||||
// Ensure both normalized numbers have at least 8 digits before comparing suffixes |
||||
guard normalizedNumber1.count >= 8 && normalizedNumber2.count >= 8 else { |
||||
return false // One or both numbers are too short to have 8 comparable digits |
||||
} |
||||
|
||||
// Compare the last 8 digits |
||||
return normalizedNumber1.suffix(8) == normalizedNumber2.suffix(8) |
||||
} |
||||
Loading…
Reference in new issue