First version
This commit is contained in:
31
Sources/App/Util/String+Extensions.swift
Normal file
31
Sources/App/Util/String+Extensions.swift
Normal 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
|
||||
}
|
||||
}
|
27
Sources/App/Util/URLSession+Async.swift
Normal file
27
Sources/App/Util/URLSession+Async.swift
Normal 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
|
10
Sources/App/Util/Wait.swift
Normal file
10
Sources/App/Util/Wait.swift
Normal 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()
|
||||
}
|
Reference in New Issue
Block a user