Use first(where:) instead of filter().first

This commit is contained in:
Damiano Galassi committed 2020-02-04 14:03:30 +01:00
1 parent 6c29faf8f4
commit 93e82651ea
6 files changed
+11 -14

No files matched your search

+1 -2
View File
@@ -30,8 +30,7 @@ final class DocumentController : NSDocumentController {
} }
override func document(for url: URL) -> NSDocument? { override func document(for url: URL) -> NSDocument? {
let filteredDocuments = self.documents.filter { $0.fileURL == url } return self.documents.first { $0.fileURL == url }
return filteredDocuments.first
} }
} }
+4 -4
View File
@@ -168,8 +168,8 @@ public struct AppleTV: MetadataService {
details.roles.filter { $0.type == "Voice" }.map { $0.personName }.joined(separator: ", ") details.roles.filter { $0.type == "Voice" }.map { $0.personName }.joined(separator: ", ")
metadata[.screenwriters] = details.roles.filter { $0.type == "Writer" }.map { $0.personName }.joined(separator: ", ") metadata[.screenwriters] = details.roles.filter { $0.type == "Writer" }.map { $0.personName }.joined(separator: ", ")
metadata[.producers] = details.roles.filter { $0.type == "Producer" }.map { $0.personName }.joined(separator: ", ") metadata[.producers] = details.roles.filter { $0.type == "Producer" }.map { $0.personName }.joined(separator: ", ")
metadata[.director] = details.roles.filter { $0.type == "Director" }.map { $0.personName }.first metadata[.director] = details.roles.first { $0.type == "Director" }.map { $0.personName }
metadata[.composer] = details.roles.filter { $0.type == "Music" }.map { $0.personName }.first metadata[.composer] = details.roles.first { $0.type == "Music" }.map { $0.personName }
if let season = metadata[.season] as? Int { if let season = metadata[.season] as? Int {
let index = metadata.remoteArtworks.count > 1 ? 1 : 0 let index = metadata.remoteArtworks.count > 1 ? 1 : 0
@@ -205,8 +205,8 @@ public struct AppleTV: MetadataService {
details.roles.filter { $0.type == "Voice" }.map { $0.personName }.joined(separator: ", ") details.roles.filter { $0.type == "Voice" }.map { $0.personName }.joined(separator: ", ")
metadata[.screenwriters] = details.roles.filter { $0.type == "Writer" }.map { $0.personName }.joined(separator: ", ") metadata[.screenwriters] = details.roles.filter { $0.type == "Writer" }.map { $0.personName }.joined(separator: ", ")
metadata[.producers] = details.roles.filter { $0.type == "Producer" }.map { $0.personName }.joined(separator: ", ") metadata[.producers] = details.roles.filter { $0.type == "Producer" }.map { $0.personName }.joined(separator: ", ")
metadata[.director] = details.roles.filter { $0.type == "Director" }.map { $0.personName }.first metadata[.director] = details.roles.first { $0.type == "Director" }.map { $0.personName }
metadata[.composer] = details.roles.filter { $0.type == "Music" }.map { $0.personName }.first metadata[.composer] = details.roles.first { $0.type == "Music" }.map { $0.personName }
return metadata return metadata
} }
+3 -5
View File
@@ -38,7 +38,7 @@ private struct SquaredTVArtJsonApi {
let caption: String? let caption: String?
func toRemoteImage() -> Artwork { func toRemoteImage() -> Artwork {
let thumbURL = alt_sizes.filter { $0.width > 200 && $0.width < 400 }.first?.url ?? original_size.url let thumbURL = alt_sizes.first { $0.width > 200 && $0.width < 400 }?.url ?? original_size.url
return Artwork(url: original_size.url, thumbURL: thumbURL, service: "Squared TV Art", type: .season, size: .square) return Artwork(url: original_size.url, thumbURL: thumbURL, service: "Squared TV Art", type: .season, size: .square)
} }
} }
@@ -208,16 +208,14 @@ private struct SquaredTVArtHTMLScraper {
} }
private func parse(info: [Substring], type: String) -> Int? { private func parse(info: [Substring], type: String) -> Int? {
let filtered = info.filter { $0.hasPrefix(type) } if let seriesID = info.first(where: { $0.hasPrefix(type) }) {
if let seriesID = filtered.first {
return Int(String(seriesID).replacingOccurrences(of: type, with: "")) return Int(String(seriesID).replacingOccurrences(of: type, with: ""))
} }
return nil return nil
} }
private func parseString(info: [Substring], type: String) -> String? { private func parseString(info: [Substring], type: String) -> String? {
let filtered = info.filter { $0.hasPrefix(type) } if let seriesID = info.first(where: { $0.hasPrefix(type) }) {
if let seriesID = filtered.first {
return String(seriesID).replacingOccurrences(of: type, with: "") return String(seriesID).replacingOccurrences(of: type, with: "")
} }
return nil return nil
+1 -1
View File
@@ -266,7 +266,7 @@ public struct iTunesStore: MetadataService {
}() }()
init?(language: String) { init?(language: String) {
guard let store = Store.stores.filter({ "\($0.country) (\($0.language))" == language }).first else { return nil } guard let store = Store.stores.first(where :{ "\($0.country) (\($0.language))" == language }) else { return nil }
self.storeCode = store.storeCode self.storeCode = store.storeCode
self.country3 = store.country3 self.country3 = store.country3
self.country2 = store.country2 self.country2 = store.country2
+1 -1
View File
@@ -77,7 +77,7 @@ class PrefsWindowController: NSWindowController, NSWindowDelegate {
window?.toolbar?.allowsUserCustomization = false window?.toolbar?.allowsUserCustomization = false
window?.toolbar?.selectedItemIdentifier = general window?.toolbar?.selectedItemIdentifier = general
if let items = window?.toolbar?.items, let item = (items.filter { $0.itemIdentifier == general }).first { if let items = window?.toolbar?.items, let item = (items.first { $0.itemIdentifier == general }) {
selectItem(item, animate: false) selectItem(item, animate: false)
} }
} }
+1 -1
View File
@@ -81,7 +81,7 @@ final class PresetManager {
} }
func item(name: String) -> Preset? { func item(name: String) -> Preset? {
return presets.filter { $0.title == name }.first return presets.first { $0.title == name }
} }
private func sort() { private func sort() {