First version

This commit is contained in:
Christoph Hagen
2024-11-15 10:46:29 +01:00
commit 9b6fd627ac
20 changed files with 1105 additions and 0 deletions

View File

@ -0,0 +1,31 @@
import Foundation
extension String {
var singleLined: String {
components(separatedBy: .newlines)
.joined(separator: " ")
}
var trimmed: String {
trimmingCharacters(in: .whitespacesAndNewlines)
}
var nonEmpty: String? {
self != "" ? self : nil
}
}
extension Array where Element == String {
var list: String {
joined(separator: ", ")
}
}
extension Sequence where Element == String {
var sortedList: String {
sorted().list
}
}

View File

@ -0,0 +1,27 @@
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
extension URLSession {
func data(for request: URLRequest) async throws -> (Data, URLResponse) {
let result: Result<(response: URLResponse, data: Data), Error> = await withCheckedContinuation { continuation in
let task = dataTask(with: request) { data, response, error in
if let error {
continuation.resume(returning: .failure(error))
} else {
continuation.resume(returning: .success((response!, data!)))
}
}
task.resume()
}
switch result {
case .failure(let error):
throw error
case .success(let result):
return (result.data, result.response)
}
}
}
#endif

View File

@ -0,0 +1,10 @@
import Foundation
func wait(for block: @escaping @Sendable () async -> Void) {
let semaphore = DispatchSemaphore(value: 0)
Task {
await block()
semaphore.signal()
}
semaphore.wait()
}