/
SequenceExt.swift
76 lines (62 loc) · 4.25 KB
/
SequenceExt.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import Foundation
#warning("🧑💻 consider adding to HandySwift")
extension Sequence {
/// Returns the first matching element in a sequence with an Equatable keypath value equal to the provided value.
public func first<Value: Equatable>(where keyPath: KeyPath<Element, Value>, equalsTo otherValue: Value) -> Element? {
self.first { $0[keyPath: keyPath] == otherValue }
}
/// Returns a Boolean value indicating whether the sequence contains an element with an Equatable keypath value equal to the provided value.
public func contains<Value: Equatable>(where keyPath: KeyPath<Element, Value>, equalTo otherValue: Value) -> Bool {
self.contains { $0[keyPath: keyPath] == otherValue }
}
/// Returns a Boolean value indicating whether the sequence contains an element with an Equatable keypath value not equal to the provided value.
public func contains<Value: Equatable>(where keyPath: KeyPath<Element, Value>, notEqualTo otherValue: Value) -> Bool {
self.contains { $0[keyPath: keyPath] == otherValue }
}
/// Returns a Boolean value indicating whether the sequence contains an element with a String keypath value that begins with the specified prefix.
public func contains(where keyPath: KeyPath<Element, String>, prefixedBy prefix: String) -> Bool {
self.contains { $0[keyPath: keyPath].hasPrefix(prefix) }
}
/// Returns a Boolean value indicating whether the sequence contains an element with a String keypath value that begins with one of the specified prefixes.
public func contains(where keyPath: KeyPath<Element, String>, prefixedByOneOf prefixes: some Sequence<String>) -> Bool {
prefixes.contains { self.contains(where: keyPath, prefixedBy: $0) }
}
/// Returns a Boolean value indicating whether the sequence contains an element with a String keypath value that contains the specified substring.
public func contains(where keyPath: KeyPath<Element, String>, contains substring: String) -> Bool {
self.contains { $0[keyPath: keyPath].contains(substring) }
}
/// Returns a Boolean value indicating whether the sequence contains an element with a String keypath value that contains one of the specified substrings.
public func contains(where keyPath: KeyPath<Element, String>, containsOneOf substrings: [String]) -> Bool {
substrings.contains { self.contains(where: keyPath, contains: $0) }
}
/// Returns a Boolean value indicating whether the sequence contains an element with a String keypath value that ends with the specified suffix.
public func contains(where keyPath: KeyPath<Element, String>, suffixedBy suffix: String) -> Bool {
self.contains { $0[keyPath: keyPath].hasSuffix(suffix) }
}
/// Returns a Boolean value indicating whether the sequence contains an element with a String keypath value that ends with one of the specified suffixes.
public func contains(where keyPath: KeyPath<Element, String>, suffixedByOneOf suffixes: [String]) -> Bool {
suffixes.contains { self.contains(where: keyPath, suffixedBy: $0) }
}
}
extension Sequence<String> {
/// Returns a Boolean value indicating whether the sequence contains a String element prefixed by the provided value.
public func contains(prefixedBy prefix: String) -> Bool {
self.contains { $0.hasPrefix(prefix) }
}
/// Returns a Boolean value indicating whether the sequence contains a String element suffixed by the provided value.
public func contains(suffixedBy suffix: String) -> Bool {
self.contains { $0.hasSuffix(suffix) }
}
/// Returns a Boolean value indicating whether the sequence contains a String element prefixed by one of the provided values.
public func contains(prefixedByOneOf prefixes: [String]) -> Bool {
prefixes.contains { self.contains(prefixedBy: $0) }
}
/// Returns a Boolean value indicating whether the sequence contains a String element suffixed by one of the provided values.
public func contains(suffixedByOneOf suffixes: [String]) -> Bool {
suffixes.contains { self.contains(suffixedBy: $0) }
}
/// Returns a Boolean value indicating whether the sequence contains a String element containing one of the provided values.
public func contains(oneOf substrings: [String]) -> Bool {
substrings.contains { self.contains($0) }
}
}