Fixes #225 - Use a properly defined user agent

* Set a proper user agent
* Add fallback when UA comes out nil
* Remove unused Bundle extension
* Return nil when asciification fails
* Add whitespace handling and further emoji test case
* Explicitly check if string is already in ASCII
* Rename factory to builder
* Use allSatisfy instead of reduce
* Use key path syntax
This commit is contained in:
Johannes Marbach
2022-10-28 16:11:10 +02:00
committed by GitHub
parent ba4064cc2c
commit 09b2f939ab
12 changed files with 306 additions and 57 deletions
@@ -51,4 +51,22 @@ extension String {
}
return abs(hash)
}
var isASCII: Bool {
allSatisfy(\.isASCII)
}
func asciified() -> String? {
guard !isASCII else {
return self
}
guard !canBeConverted(to: .ascii) else {
return nil
}
let mutableString = NSMutableString(string: self)
guard CFStringTransform(mutableString, nil, "Any-Latin; Latin-ASCII; [:^ASCII:] Remove" as CFString, false) else {
return nil
}
return mutableString.trimmingCharacters(in: .whitespaces)
}
}