Skip to content

Commit

Permalink
- Fix flag issue for regions with same country code (#754)
Browse files Browse the repository at this point in the history
- Fix partial formats for regions with same country code
  • Loading branch information
bguidolim authored Mar 25, 2024
1 parent 55f16de commit 53378b5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
20 changes: 10 additions & 10 deletions PhoneNumberKit/PartialFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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)
Expand Down
39 changes: 39 additions & 0 deletions PhoneNumberKitTests/PartialFormatterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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

0 comments on commit 53378b5

Please sign in to comment.