Created
July 4, 2017 23:26
-
-
Save RxDx/2da818c56ae1cbcd0cec80134ff41d9a to your computer and use it in GitHub Desktop.
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
// MARK: - String mask | |
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 == 14 { | |
text = String(text.characters.dropFirst(3)) | |
} | |
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 | |
} | |
class func maskParticipation(participation: String?) -> String { | |
guard let participation = participation else { | |
return "" | |
} | |
var text = participation.replacingOccurrences(of: ",", with: "") | |
if text.characters.count > 4 { | |
text.insert(",", at: text.characters.index(text.startIndex, offsetBy: 3)) | |
} | |
return text | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment