Skip to content

Commit

Permalink
Solving bug with short extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
marmelroy committed Dec 4, 2016
1 parent 62f9806 commit b71cbad
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion PhoneNumberKit/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ struct PhoneNumberPatterns {

static let capturingDigitPattern = "([0-90-9٠-٩۰-۹])"

static let extnPattern = "\\;(.*)"
static let extnPattern = "(?:;ext=([0-90-9٠-٩۰-۹]{1,7})|[ \\t,]*(?:e?xt(?:ensi(?:ó?|ó))?n?|e?xtn?|[,xxX##~~]|int|anexo|int)[:\\..]?[ \\t,-]*([0-90-9٠-٩۰-۹]{1,7})#?|[- ]+([0-90-9٠-٩۰-۹]{1,5})#)$"

static let iddPattern = "^(?:\\+|%@)"

Expand Down
11 changes: 7 additions & 4 deletions PhoneNumberKit/ParseManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ class ParseManager {
// Extract number (2)
var nationalNumber = numberString
let match = try regexManager.phoneDataDetectorMatch(numberString)
if let phoneNumber = match.phoneNumber {
nationalNumber = phoneNumber
}
let matchedNumber = nationalNumber.substring(with: match.range)
nationalNumber = matchedNumber

// Strip and extract extension (3)
let numberExtension = parser.stripExtension(&nationalNumber)
var numberExtension: String?
if let rawExtension = parser.stripExtension(&nationalNumber) {
numberExtension = parser.normalizePhoneNumber(rawExtension)
}
// Country code parse (4)
guard var regionMetadata = metadataManager.territoriesByCountry[region] else {
throw PhoneNumberError.invalidCountryCode
Expand Down
27 changes: 4 additions & 23 deletions PhoneNumberKit/RegexManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ class RegexManager {

var regularExpresionPool = [String : NSRegularExpression]()

var phoneDataDetector: NSDataDetector? = {
do {
let dataDetector = try NSDataDetector(types: NSTextCheckingResult.CheckingType.phoneNumber.rawValue)
return dataDetector
}
catch {
return nil
}
}()

var spaceCharacterSet: CharacterSet = {
let characterSet = NSMutableCharacterSet(charactersIn: "\u{00a0}")
characterSet.formUnion(with: CharacterSet.whitespacesAndNewlines)
Expand All @@ -32,7 +22,6 @@ class RegexManager {

deinit {
regularExpresionPool.removeAll()
phoneDataDetector = nil
}

// MARK: Regular expression
Expand Down Expand Up @@ -67,23 +56,15 @@ class RegexManager {
}

func phoneDataDetectorMatch(_ string: String) throws -> NSTextCheckingResult {
guard let matches = phoneDataDetector?.matches(in: string) else {
throw PhoneNumberError.generalError
}
if let firstMatch = matches.first {
let fallBackMatches = try regexMatches(PhoneNumberPatterns.validPhoneNumberPattern, string: string)
if let firstMatch = fallBackMatches.first {
return firstMatch
}
else {
let fallBackMatches = try regexMatches(PhoneNumberPatterns.validPhoneNumberPattern, string: string)
if let firstMatch = fallBackMatches.first {
return firstMatch
}
else {
throw PhoneNumberError.notANumber
}
throw PhoneNumberError.notANumber
}
}

// MARK: Match helpers

func matchesAtStart(_ pattern: String, string: String) -> Bool {
Expand Down
6 changes: 3 additions & 3 deletions PhoneNumberKitTests/PhoneNumberKitParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class PhoneNumberKitParsingTests: XCTestCase {

func testFailingNumber() {
do {
let phoneNumber1 = try phoneNumberKit.parse("+5491187654321 ABC123", withRegion: "AR")
XCTAssertNotNil(phoneNumber1)
_ = try phoneNumberKit.parse("+5491187654321 ABC123", withRegion: "AR")
XCTFail()
}
catch {
XCTFail()
XCTAssertTrue(true)
}
}

Expand Down
16 changes: 16 additions & 0 deletions PhoneNumberKitTests/PhoneNumberKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,22 @@ class PhoneNumberKitTests: XCTestCase {
}
}

// American number with short extension
func testAlternativeNumberWithExtension() {
let testNumber = "2129316760 x28"
do {
let phoneNumber = try phoneNumberKit.parse(testNumber, withRegion: "US", ignoreType: false)
XCTAssertEqual(phoneNumber.countryCode, 1)
XCTAssertEqual(phoneNumber.numberExtension, "28")
XCTAssertEqual(phoneNumber.nationalNumber, 2129316760)
XCTAssertEqual(phoneNumber.leadingZero, false)
}
catch {
XCTFail()
}
}


// French number with a plus
func testValidNumberWithPlusNoWhiteSpace() {
let testNumber = "+33689555555"
Expand Down

0 comments on commit b71cbad

Please sign in to comment.