Created
May 18, 2021 17:03
-
-
Save ralbertini/d281b069839a876c69a38ed5b9a0afb3 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 Foundation | |
public extension String { | |
var containsEmoji: Bool { | |
for scalar in unicodeScalars { | |
if scalar.value == 0x1F600 || scalar.value == 0x1F64F || | |
scalar.value == 0x1F300 || scalar.value == 0x1F5FF { | |
return true | |
} else if scalar.value == 0x1F680 || scalar.value == 0x1F6FF || | |
scalar.value == 0x2600 || scalar.value == 0x26FF { | |
return true | |
} else if scalar.value == 0x2700 || scalar.value == 0x27BF || | |
scalar.value == 0xFE00 || scalar.value == 0xFE0F { | |
return true | |
} else if scalar.value == 0x1F900 || scalar.value == 0x1F9FF || | |
scalar.value == 0x1F1E6 || scalar.value == 0x1F1FF { | |
return true | |
} else { | |
continue | |
} | |
} | |
return false | |
} | |
var isBackspace: Bool { | |
guard let char = cString(using: .utf8) else { | |
return false | |
} | |
let stringComponent = strcmp(char, "\\b") | |
return stringComponent == -92 | |
} | |
} | |
public extension String { | |
func contains(substring: String) -> Bool { | |
return self.range(of: substring) != nil | |
} | |
func containsIgnoringCase(substring: String) -> Bool { | |
return self.range(of: substring, options: .caseInsensitive) != nil | |
} | |
func containsIgnoringCase(_ string: String) -> Bool { | |
return self.lowercased().contains(string.lowercased()) | |
} | |
func stringByRemovingAll(_ subStrings: [String]) -> String { | |
var resultString = self | |
_ = subStrings.map { resultString = resultString.replacingOccurrences(of: $0, with: "") } | |
return resultString | |
} | |
func formatCompetence() -> String { | |
if self.range(of: "/") == nil { | |
if self.count == 6 { | |
var newDate = self | |
newDate.insert("/", at: newDate.index(newDate.startIndex, offsetBy: 2)) | |
return newDate | |
} else { | |
return "" | |
} | |
} else { | |
return self | |
} | |
} | |
func matches(for regex: String) -> String { | |
do { | |
let regex = try NSRegularExpression(pattern: regex) | |
let string = self as NSString | |
let results = regex.matches(in: self, range: NSRange(location: 0, length: string.length)) | |
return results.map { | |
string.substring(with: $0.range) | |
}.joined() | |
} catch _ { | |
return "" | |
} | |
} | |
func matches(_ regex: String) -> Bool { | |
return self.range(of: regex, options: .regularExpression, range: nil, locale: nil) != nil | |
} | |
func onlyDigits() -> String { | |
return self.matches(for: "\\d") | |
} | |
func doubleValueDigits() -> String { | |
return self.matches(for: "[0-9.]") | |
} | |
func index(from: Int) -> Index { | |
return self.index(startIndex, offsetBy: from) | |
} | |
func substring(from: Int) -> String { | |
let fromIndex = index(from: from) | |
return String(self[fromIndex...]) | |
} | |
func substring(to: Int) -> String { | |
let toIndex = index(from: to) | |
return String(self[..<toIndex]) | |
} | |
func substring(with range: Range<Int>) -> String { | |
let fromIndex = self.index(self.startIndex, offsetBy: range.lowerBound) | |
let toIndex = self.index(self.startIndex, offsetBy: range.upperBound) | |
return String(self[fromIndex..<toIndex]) | |
} | |
func onlyAlphanumeric() -> String { | |
var newString = self | |
let charSet = CharacterSet.alphanumerics.inverted | |
newString = newString.components(separatedBy: charSet).joined() | |
return newString | |
} | |
func onlyNumbers() -> String { | |
var newString = self | |
let charSet = CharacterSet.decimalDigits.inverted | |
newString = newString.components(separatedBy: charSet).joined() | |
return newString | |
} | |
func stringClear() -> String { | |
let charactersAtRemove = [" ", "\t", "(", ")", ".", ",", "R$", "/", "-", "%"] | |
var newString = self | |
for char in charactersAtRemove { | |
newString = newString.replacingOccurrences(of: char, with: "") | |
} | |
return newString | |
} | |
func substringWithSizeMax(size: Int) -> String { | |
if self.count < size { | |
return self | |
} | |
return (self as NSString).substring(to: size) | |
} | |
var getFirstChar: String { | |
return String(self[startIndex]) | |
} | |
func height(constraintedWidth width: CGFloat, font: UIFont) -> CGFloat { | |
let label = UILabel(frame: CGRect(x: 0, | |
y: 0, | |
width: width, | |
height: .greatestFiniteMagnitude)) | |
label.numberOfLines = 0 | |
label.text = self | |
label.font = font | |
label.sizeToFit() | |
return label.frame.height | |
} | |
func width(constrainedHeight height: CGFloat, font: UIFont) -> CGFloat { | |
let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height) | |
let boundingBox = self.boundingRect(with: constraintRect, | |
options: .usesLineFragmentOrigin, | |
attributes: [NSAttributedString.Key.font: font], | |
context: nil) | |
return ceil(boundingBox.width) | |
} | |
func removeWhiteSpace(from string: String) -> String { | |
let formatted = string.trimmingCharacters(in: .whitespacesAndNewlines) | |
return formatted | |
} | |
mutating func removeNonDecimalCharacters() { | |
let digitSet = CharacterSet.decimalDigits | |
self = String(self.unicodeScalars.filter { digitSet.contains($0) }) | |
} | |
func formatNumberToTwoDigts() -> String { | |
if let integer = Int(self) { | |
if integer == 0 { | |
return "00" | |
} else if integer < 10, | |
let firstCharacter = self.first, | |
firstCharacter != "0" { | |
return "0\(self)" | |
} else { | |
return self | |
} | |
} else { | |
return "00" | |
} | |
} | |
func defaultValue(_ value: String, for text: String) -> String { | |
if self == text { | |
return value | |
} | |
return self | |
} | |
func safelyLimitedTo(length n: Int) -> String { | |
if self.count <= n { | |
return self | |
} | |
return String(Array(self).prefix(upTo: n)) | |
} | |
func toBool() -> Bool? { | |
switch self.uppercased() { | |
case "TRUE", "1": | |
return true | |
case "FALSE", "0": | |
return false | |
default: | |
return nil | |
} | |
} | |
func leftPadding(toLength: Int, withPad: String = " ") -> String { | |
guard toLength > self.count else { return self } | |
let padding = String(repeating: withPad, count: toLength - self.count) | |
return padding + self | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment