Created
March 21, 2018 14:15
-
-
Save iAmrSalman/09143e373a748ded9109c831474bed7d to your computer and use it in GitHub Desktop.
[PhoneNumberManager] validate and format phoneNumber using PhoneNumberKit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import PhoneNumberKit | |
enum Format { | |
case e164 | |
case national | |
case international | |
} | |
class PhoneNumberManager { | |
class func validate(_ number: String, with format: Format) throws -> String { | |
let phoneKit = PhoneNumberKit() | |
let phoneNumber = try phoneKit.parse(number, ignoreType: true) | |
switch format { | |
case .e164: | |
return phoneKit.format(phoneNumber, toType: .e164) | |
case .international: | |
return phoneKit.format(phoneNumber, toType: .international) | |
case .national: | |
return phoneKit.format(phoneNumber, toType: .national) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment