COVIDSafe code from version 1.6 (#6)

This commit is contained in:
COVIDSafe Support 2020-06-19 17:43:33 +10:00 committed by GitHub
parent 149daee2e9
commit 2063cea613
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
38 changed files with 1963 additions and 1664 deletions

View file

@ -5,8 +5,11 @@ import Foundation
final class PhoneNumberParser {
// e.g. 412 345 678
static let numberOfDigits = 9
static let mobilePhoneNumberPrefix = "4"
static let numberOfDigitsAu = 9
static let minNumberOfDigitsNorfolkIsland = 5
static let maxNumberOfDigitsNorfolkIsland = 6
static let mobilePhoneNumberNorfolkIslandPrefix = "3"
static let mobilePhoneNumberAuPrefix = "4"
enum Error: Swift.Error {
case notDigits
@ -14,33 +17,66 @@ final class PhoneNumberParser {
case incorrectMobilePhoneNumberPrefix
}
static func parse(_ string: String) -> Result<String, Error> {
static func parse(_ string: String, countryCode: String) -> Result<String, Error> {
// Remove all spaces
var string = string.replacingOccurrences(of: " ", with: "")
// Remove leading "+61"
if string.hasPrefix("+61") {
string.removeFirst(3)
if countryCode == "61" {
// Remove leading "+61"
if string.hasPrefix("+61") {
string.removeFirst(3)
}
// Check for digit only
guard string.rangeOfCharacter(from: CharacterSet.decimalDigits.inverted) == nil else {
return .failure(.notDigits)
}
// Remove leading "0"
if string.hasPrefix("0") {
string.removeFirst()
}
guard string.hasPrefix(mobilePhoneNumberAuPrefix) else {
return .failure(.incorrectMobilePhoneNumberPrefix)
}
// Check number of digits
guard string.count == Self.numberOfDigitsAu else {
return .failure(.incorrectDigitCount)
}
return .success(string)
}
// Check for digit only
guard string.rangeOfCharacter(from: CharacterSet.decimalDigits.inverted) == nil else {
return .failure(.notDigits)
if countryCode == "672" {
// Remove leading "+672"
if string.hasPrefix("+672") {
string.removeFirst(4)
}
// Check for digit only
guard string.rangeOfCharacter(from: CharacterSet.decimalDigits.inverted) == nil else {
return .failure(.notDigits)
}
// Check number of digits
guard string.count >= self.minNumberOfDigitsNorfolkIsland && string.count <= self.maxNumberOfDigitsNorfolkIsland else {
return .failure(.incorrectDigitCount)
}
if string.count == self.maxNumberOfDigitsNorfolkIsland && !string.hasPrefix(mobilePhoneNumberNorfolkIslandPrefix) {
return .failure(.incorrectMobilePhoneNumberPrefix)
}
return .success(string)
}
// Remove leading "0"
if string.hasPrefix("0") {
string.removeFirst()
// remove country code if present in the phone number
if string.hasPrefix("+\(countryCode)") {
string.removeFirst(countryCode.count+1)
}
guard string.hasPrefix(mobilePhoneNumberPrefix) else {
return .failure(.incorrectMobilePhoneNumberPrefix)
}
// Check number of digits
guard string.count == Self.numberOfDigits else {
return .failure(.incorrectDigitCount)
}
return .success(string)
}