macos: add string extension

This commit is contained in:
Mitchell Hashimoto
2024-06-30 14:58:28 -07:00
parent 27fd05d112
commit 6c01d20eb7
2 changed files with 24 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
extension String {
func truncate(length: Int, trailing: String = "") -> String {
let maxLength = length - trailing.count
guard maxLength > 0, !self.isEmpty, self.count > length else {
return self
}
return self.prefix(maxLength) + trailing
}
func temporaryFile(_ filename: String = "temp") -> URL {
let url = FileManager.default.temporaryDirectory
.appendingPathComponent(filename)
.appendingPathExtension("txt")
let string = self
try? string.write(to: url, atomically: true, encoding: .utf8)
return url
}
}