Last active
May 18, 2021 15:19
-
-
Save ralbertini/009e054bde6c9f2b3a9b173a7ed6ee01 to your computer and use it in GitHub Desktop.
escolastico
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 Foundation | |
public extension String { | |
func stringWithFormattedAgency() -> String { | |
var formattedAgency = self.onlyNumbers() | |
let maximumSize = 4 | |
formattedAgency = formattedAgency.substringWithSizeMax(size: maximumSize) | |
return formattedAgency | |
} | |
func stringWithFormattedAccount() -> String { | |
var cleanString = self.onlyNumbers() | |
let maximumSize = 6 | |
cleanString = cleanString.substringWithSizeMax(size: maximumSize) | |
var pattern = "" | |
switch cleanString.count { | |
case 2: | |
pattern = "N-N" | |
case 3: | |
pattern = "NN-N" | |
case 4: | |
pattern = "NNN-N" | |
case 5: | |
pattern = "NNNN-N" | |
case 6: | |
pattern = "NNNNN-N" | |
default: | |
pattern = "NNNNN-N" | |
} | |
let maskedString = cleanString.format(pattern, oldString: cleanString) | |
return maskedString | |
} | |
func stringWithFormattedCPF() -> String { | |
var cleanString = self.stringClear().onlyNumbers() | |
let maximumSize = 11 | |
cleanString = cleanString.substringWithSizeMax(size: maximumSize) | |
let maskText = NSMutableString(string: cleanString) | |
let pattern = "(.{1,3})(.{1,3})(.{1,3})(.{1,2})" | |
let regex = try? NSRegularExpression(pattern: pattern, options: []) | |
regex?.replaceMatches(in: maskText, | |
options: .reportProgress, | |
range: NSRange(location: 0, length: maskText.length), | |
withTemplate: "$1.$2.$3-$4") | |
return maskText as String | |
} | |
func stringWithFormattedCNPJ() -> String { | |
var cleanString = self.stringClear().onlyNumbers() | |
let maximumSize = 14 | |
cleanString = cleanString.substringWithSizeMax(size: maximumSize) | |
var pattern = "" | |
switch cleanString.count { | |
case 12: | |
pattern = "N.NN.NNN/NNNN-NN" | |
case 13: | |
pattern = "N.NNN.NNN/NNNN-NN" | |
case 14: | |
pattern = "NN.NNN.NNN/NNNN-NN" | |
default: | |
pattern = "NN.NNN.NNN/NN-N" | |
} | |
let maskedString = cleanString.format(pattern, oldString: cleanString) | |
return maskedString | |
} | |
func stringWithFormattedZipCode() -> String { | |
var cleanString = self.onlyNumbers() | |
let maximumSize = 8 | |
cleanString = cleanString.substringWithSizeMax(size: maximumSize) | |
var pattern = "NNNNNNNN" | |
if cleanString.count > 5 { | |
pattern = "NNNNN-NNN" | |
} | |
let maskedString = cleanString.format(pattern, oldString: cleanString) | |
return maskedString | |
} | |
func stringWithMaskedTelephone() -> String { | |
var cleanString = self.stringClear() | |
let maximumSize = 11 | |
if cleanString.hasPrefix("00") { | |
cleanString = cleanString.substring(from: 2) | |
} else if cleanString.hasPrefix("0") { | |
cleanString = cleanString.substring(from: 1) | |
} else { | |
cleanString = cleanString.substringWithSizeMax(size: maximumSize) | |
} | |
var pattern = "NN NNNNN-NNNN" | |
if cleanString.count == 10 { | |
pattern = "NN NNNN-NNNN" | |
} else { | |
pattern = "NN NNNNN-NNNN" | |
} | |
let maskedString = cleanString.format(pattern, oldString: cleanString) | |
var setToRemove = CharacterSet() | |
setToRemove.formUnion(.whitespaces) | |
setToRemove.formUnion(.punctuationCharacters) | |
setToRemove.formUnion(.symbols) | |
return maskedString.trimmingCharacters(in: setToRemove) | |
} | |
func stringWithMaskedPhoneWithParenthesis() -> String { | |
var cleanString = self.stringClear() | |
let maximumSize = 11 | |
cleanString = cleanString.substringWithSizeMax(size: maximumSize) | |
let regexMap = [ "", | |
"N", | |
"(NN)", | |
"(NN) N", | |
"(NN) NN", | |
"(NN) NNN", | |
"(NN) NNNN", | |
"(NN) NNNNN", | |
"(NN) NNNNN-N", | |
"(NN) NNNNN-NN", | |
"(NN) NNNNN-NNN", | |
"(NN) NNNNN-NNNN"] | |
let regexIndex = cleanString.count | |
let pattern = regexMap[regexIndex] | |
let maskedString = cleanString.format(pattern, oldString: cleanString) | |
return maskedString | |
} | |
func formatDDD() -> String { | |
if self.count > 2 { | |
return String(self.suffix(2)) | |
} | |
return self | |
} | |
func formatTelephoneWithoutDDD() -> String { | |
var phone = self | |
if self.count > 9 { | |
phone = String(self.suffix(9)) | |
} | |
if phone.count > 4 { | |
phone.insert("-", at: phone.index(from: phone.count - 4)) | |
} | |
return phone | |
} | |
func with(mask: String, erasing: Bool = false) -> String { | |
var resultString = String() | |
var text = self | |
var chars = self | |
if chars.first == "0" && !erasing { | |
text.remove(at: text.startIndex) | |
chars = text | |
} | |
let maskChars = mask | |
var stringIndex = chars.startIndex | |
var maskIndex = mask.startIndex | |
while stringIndex < chars.endIndex && maskIndex < maskChars.endIndex { | |
if maskChars[maskIndex] == "#" { | |
resultString.append(chars[stringIndex]) | |
stringIndex = text.index(after: stringIndex) | |
} else { | |
resultString.append(maskChars[maskIndex]) | |
} | |
maskIndex = mask.index(after: maskIndex) | |
} | |
return resultString | |
} | |
} | |
public extension String { | |
func accountFormattedWithSevenDigits() -> String { | |
var accountFormatted = self | |
if self.count == 6 { | |
accountFormatted = "0\(accountFormatted)" | |
} else { | |
accountFormatted = "00\(accountFormatted)" | |
} | |
return accountFormatted | |
} | |
func trimCpfCnpj() -> String { | |
return self.replacingOccurrences(of: "-", with: "") | |
.replacingOccurrences(of: ".", with: "") | |
.replacingOccurrences(of: "/", with: "") | |
} | |
func formatAccountSize() -> String { | |
var newAccNumber = self | |
let charsNeeded = 7 | |
let charsMissing = charsNeeded - self.count | |
for _ in 0 ..< charsMissing { | |
newAccNumber = "0\(newAccNumber)" | |
} | |
return newAccNumber | |
} | |
func formatCnpjOrCei() -> String { | |
let cnpjCei = self | |
if cnpjCei.count == 14 { | |
return cnpjCei.stringWithFormattedCNPJ() | |
} else { | |
return cnpjCei | |
} | |
} | |
func formatCnpjOrCpf() -> String { | |
let cnpjCpf = self | |
if cnpjCpf.count == 14 { | |
return cnpjCpf.stringWithFormattedCNPJ() | |
} else { | |
return cnpjCpf.stringWithFormattedCPF() | |
} | |
} | |
func formatCnpj() -> String { | |
let cnpjCei = self | |
if cnpjCei.count == 14 { | |
return cnpjCei.stringWithFormattedCNPJ() | |
} else { | |
return cnpjCei | |
} | |
} | |
func removeFirstsElementsFromCPF(_ number: Int) -> String { | |
return String(self.dropFirst(number)) | |
} | |
func putMaskOnInformation(number: Int) -> String { | |
let conditionIndex = self.count - number | |
let maskedName = String(self.enumerated().map { (index, element) -> Character in | |
index < conditionIndex && (element != "." || element != " ") ? "*" : element | |
}) | |
return maskedName | |
} | |
func fillWithLeftZeros(until maxCount: Int) -> String { | |
let count = self.count | |
let diff = maxCount - count | |
if diff == 0 || diff < 0 { | |
return self | |
} | |
let prefix = String(repeating: "0", count: diff) | |
return "\(prefix)\(self)" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment