Last active
September 1, 2017 19:47
-
-
Save gregszero/d092c7f5faeb888592c5b1a231b7069e to your computer and use it in GitHub Desktop.
This file contains 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 UIKit | |
extension String { | |
subscript (r: CountableClosedRange<Int>) -> String { | |
get { | |
let startIndex = self.index(self.startIndex, offsetBy: r.lowerBound) | |
let endIndex = self.index(startIndex, offsetBy: r.upperBound - r.lowerBound) | |
return self[startIndex...endIndex] | |
} | |
} | |
subscript(i: Int) -> Character { | |
return self[self.characters.index(self.startIndex, offsetBy: i)] | |
} | |
subscript(i: Int) -> String { | |
return String(self[i] as Character) | |
} | |
subscript(r: Range<Int>) -> String { | |
let start = characters.index(startIndex, offsetBy: r.lowerBound) | |
let end = characters.index(start, offsetBy: r.upperBound - r.lowerBound) | |
return self[Range(start ..< end)] | |
} | |
var isEmail: Bool { | |
do { | |
let regex = try NSRegularExpression(pattern: "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$", options: .caseInsensitive) | |
return regex.firstMatch(in: self, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, self.characters.count)) != nil | |
} catch { | |
return false | |
} | |
} | |
func maskForDocument() -> String { | |
var newString = self | |
if newString.characters.count == 11 { | |
newString.insert(".", at: newString.characters.index(newString.startIndex, offsetBy: 3)) | |
newString.insert(".", at: newString.characters.index(newString.startIndex, offsetBy: 7)) | |
newString.insert("-", at: newString.characters.index(newString.startIndex, offsetBy: 11)) | |
} else { | |
newString.insert(".", at: newString.characters.index(newString.startIndex, offsetBy: 2)) | |
newString.insert(".", at: newString.characters.index(newString.startIndex, offsetBy: 6)) | |
newString.insert("/", at: newString.characters.index(newString.startIndex, offsetBy: 10)) | |
newString.insert("-", at: newString.characters.index(newString.startIndex, offsetBy: 15)) | |
} | |
return newString | |
} | |
} | |
class Mask { | |
class func maskCep(CepString: String?) -> String { | |
guard let CepString = CepString else { | |
return "" | |
} | |
var text = CepString.replacingOccurrences(of: "-", with: "") | |
if (text.characters.count) > 5 { | |
text.insert("-", at: (text.characters.index((text.startIndex), offsetBy: 5))) | |
} | |
return text | |
} | |
class func maskCpf(CPFString: String?) -> String { | |
guard let CPFString = CPFString else { | |
return "" | |
} | |
var text = CPFString.replacingOccurrences(of: ".", with: "").replacingOccurrences(of: "-", with: "").replacingOccurrences(of: "/", with: "") | |
if (text.characters.count) > 3 { | |
text.insert(".", at: (text.characters.index((text.startIndex), offsetBy: 3))) | |
} | |
if (text.characters.count) > 7 { | |
text.insert(".", at: (text.characters.index((text.startIndex), offsetBy: 7))) | |
} | |
if (text.characters.count) > 11 { | |
text.insert("-", at: (text.characters.index((text.startIndex), offsetBy: 11))) | |
} | |
return text | |
} | |
class func maskCnpj(CnpjString: String?) -> String { | |
guard let CnpjString = CnpjString else { | |
return "" | |
} | |
var text = CnpjString.replacingOccurrences(of: ".", with: "").replacingOccurrences(of: "-", with: "").replacingOccurrences(of: "/", with: "") | |
if text.characters.count > 2 { | |
text.insert(".", at: text.characters.index(text.startIndex, offsetBy: 2)) | |
} | |
if text.characters.count > 6 { | |
text.insert(".", at: text.characters.index(text.startIndex, offsetBy: 6)) | |
} | |
if text.characters.count > 10 { | |
text.insert("/", at: text.characters.index(text.startIndex, offsetBy: 10)) | |
} | |
if text.characters.count > 15 { | |
text.insert("-", at: text.characters.index(text.startIndex, offsetBy: 15)) | |
} | |
return text | |
} | |
class func maskFone(phone: String?) -> String { | |
guard let phone = phone else { | |
return "" | |
} | |
var text = phone.replacingOccurrences(of: "(", with: "").replacingOccurrences(of: ")", with: "").replacingOccurrences(of: "-", with: "") | |
if text.characters.count > 1 { | |
text.insert("(", at: text.characters.index(text.startIndex, offsetBy: 0)) | |
} | |
if text.characters.count > 3 { | |
text.insert(")", at: text.characters.index(text.startIndex, offsetBy: 3)) | |
} | |
if text.characters.count > 8 { | |
text.insert("-", at: text.characters.index(text.startIndex, offsetBy: 8)) | |
} | |
return text | |
} | |
class func maskMobile(phone: String?) -> String { | |
guard let phone = phone else { | |
return "" | |
} | |
var text = phone.replacingOccurrences(of: "(", with: "").replacingOccurrences(of: ")", with: "").replacingOccurrences(of: "-", with: "") | |
if text.characters.count > 1 { | |
text.insert("(", at: text.characters.index(text.startIndex, offsetBy: 0)) | |
} | |
if text.characters.count > 3 { | |
text.insert(")", at: text.characters.index(text.startIndex, offsetBy: 3)) | |
} | |
if text.characters.count > 9 { | |
text.insert("-", at: text.characters.index(text.startIndex, offsetBy: 9)) | |
} | |
return text | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment