Last active
October 30, 2021 23:10
-
-
Save swift-al-dente/047dbd6a33261f6778319f6bd25d5aa1 to your computer and use it in GitHub Desktop.
String+extension
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
// | |
// String+extension.swift | |
// | |
// Created by swift-al-dente on 31/10/21. | |
// | |
import Foundation | |
extension String { | |
// MARK: - Regex | |
var isValidItalianFiscalCode: Bool { | |
let fiscalCodeRegEx = "[A-Za-z]{6}[0-9lmnpqrstuvLMNPQRSTUV]{2}[abcdehlmprstABCDEHLMPRST]{1}[0-9lmnpqrstuvLMNPQRSTUV]{2}[A-Za-z]{1}[0-9lmnpqrstuvLMNPQRSTUV]{3}[A-Za-z]{1})|([0-9]{11}" | |
let emailPred = NSPredicate(format:"SELF MATCHES %@", fiscalCodeRegEx) | |
return emailPred.evaluate(with: self) | |
} | |
var isValidEmail: Bool { | |
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}" | |
let emailPred = NSPredicate(format:"SELF MATCHES %@", emailRegEx) | |
return emailPred.evaluate(with: self) | |
} | |
var isValidItalianBankAccount: Bool { | |
let bankAccountRegEx = "IT[0-9]{2}[ ][a-zA-Z][0-9]{3}[ ][0-9]{4}[ ][0-9]{4}[ ][0-9]{4}[ ][0-9]{4}[ ][0-9]{3}|IT[0-9]{2}[a-zA-Z][0-9]{22}" | |
let bankAccountPred = NSPredicate(format:"SELF MATCHES %@", bankAccountRegEx) | |
return bankAccountPred.evaluate(with: self) | |
} | |
// MARK: - Date | |
var epochToDate: Date? { | |
if let timeInterval = TimeInterval(self) { | |
return Date(timeIntervalSince1970: timeInterval) | |
} else { return nil } | |
} | |
func toDate(with dateFormat: String) -> Date? { | |
//e.g. "2020-05-01 11:30" | |
let dateFormatter = DateFormatter() | |
dateFormatter.dateFormat = dateFormat | |
dateFormatter.timeZone = TimeZone.current | |
dateFormatter.locale = Locale.current | |
return dateFormatter.date(from: self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment