diff --git a/PhoneNumberKit/PartialFormatter.swift b/PhoneNumberKit/PartialFormatter.swift index fe48e5722..28f2d8d2c 100644 --- a/PhoneNumberKit/PartialFormatter.swift +++ b/PhoneNumberKit/PartialFormatter.swift @@ -61,7 +61,7 @@ public final class PartialFormatter { func updateMetadataForDefaultRegion() { guard let metadataManager else { return } if let regionMetadata = metadataManager.filterTerritories(byCountry: defaultRegion) { - self.defaultMetadata = metadataManager.mainTerritory(forCode: regionMetadata.countryCode) + self.defaultMetadata = regionMetadata } else { self.defaultMetadata = nil } @@ -80,15 +80,12 @@ public final class PartialFormatter { public var currentRegion: String { if ignoreIntlNumbers, currentMetadata?.codeID == "001" { return defaultRegion + } else if self.phoneNumberKit.countryCode(for: self.defaultRegion) != 1 { + return currentMetadata?.codeID ?? "US" } else { - let countryCode = self.phoneNumberKit.countryCode(for: self.defaultRegion) - if countryCode != 1, countryCode != 7 { - return currentMetadata?.codeID ?? "US" - } else { - return self.currentMetadata?.countryCode == 1 || self.currentMetadata?.countryCode == 7 - ? self.defaultRegion - : self.currentMetadata?.codeID ?? self.defaultRegion - } + return self.currentMetadata?.countryCode == 1 ? + self.defaultRegion : + self.currentMetadata?.codeID ?? self.defaultRegion } } @@ -305,7 +302,10 @@ public final class PartialFormatter { var tempPossibleFormats = [MetadataPhoneNumberFormat]() var possibleFormats = [MetadataPhoneNumberFormat]() if let metadata = currentMetadata { - let formatList = metadata.numberFormats + var formatList = metadata.numberFormats + if formatList.isEmpty { + formatList = metadataManager?.mainTerritory(forCode: metadata.countryCode)?.numberFormats ?? [] + } for format in formatList { if self.isFormatEligible(format) { tempPossibleFormats.append(format) diff --git a/PhoneNumberKitTests/PartialFormatterTests.swift b/PhoneNumberKitTests/PartialFormatterTests.swift index 0ba472c2c..63241c66d 100644 --- a/PhoneNumberKitTests/PartialFormatterTests.swift +++ b/PhoneNumberKitTests/PartialFormatterTests.swift @@ -292,6 +292,33 @@ final class PartialFormatterTests: XCTestCase { XCTAssertEqual(partialFormatter.formatPartial(testNumber), "(314) 852-5477") } + // Issue: https://github.com/marmelroy/PhoneNumberKit/issues/602 + // Input: 4372234563 + // Expected result: https://libphonenumber.appspot.com/phonenumberparser?number=4372234563&country=CA + func testCANumber() { + let partialFormatter = PartialFormatter(phoneNumberKit: phoneNumberKit, defaultRegion: "CA") + var testNumber = "4" + XCTAssertEqual(partialFormatter.formatPartial(testNumber), "4") + testNumber = "43" + XCTAssertEqual(partialFormatter.formatPartial(testNumber), "43") + testNumber = "437" + XCTAssertEqual(partialFormatter.formatPartial(testNumber), "437") + testNumber = "4372" + XCTAssertEqual(partialFormatter.formatPartial(testNumber), "437-2") + testNumber = "43722" + XCTAssertEqual(partialFormatter.formatPartial(testNumber), "437-22") + testNumber = "437223" + XCTAssertEqual(partialFormatter.formatPartial(testNumber), "437-223") + testNumber = "4372234" + XCTAssertEqual(partialFormatter.formatPartial(testNumber), "437-2234") + testNumber = "43722345" + XCTAssertEqual(partialFormatter.formatPartial(testNumber), "(437) 223-45") + testNumber = "437223456" + XCTAssertEqual(partialFormatter.formatPartial(testNumber), "(437) 223-456") + testNumber = "4372234563" + XCTAssertEqual(partialFormatter.formatPartial(testNumber), "(437) 223-4563") + } + // 07739555555 func testUKMobileNumber() { let partialFormatter = PartialFormatter(phoneNumberKit: phoneNumberKit, defaultRegion: "GB") @@ -653,5 +680,17 @@ final class PartialFormatterTests: XCTestCase { testNumber = "121;4" XCTAssertEqual(partialFormatter.formatPartial(testNumber), "121;4") } + + func testMinimalRUNumberFromESRegion() { + let partialFormatter = PartialFormatter(phoneNumberKit: phoneNumberKit, defaultRegion: "ES") + _ = partialFormatter.formatPartial("+7") + XCTAssertEqual(partialFormatter.currentRegion, "RU") + } + + func testMinimalRUNumberFromUSRegion() { + let partialFormatter = PartialFormatter(phoneNumberKit: phoneNumberKit, defaultRegion: "US") + _ = partialFormatter.formatPartial("+7") + XCTAssertEqual(partialFormatter.currentRegion, "RU") + } } #endif