Last active
June 26, 2022 19:35
-
-
Save mediym41/6cc741485f0903c3dc23bf7004853757 to your computer and use it in GitHub Desktop.
Easily compare two objects by using KeyPath
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class KeyPathEqualizer<Object> { | |
private let lhs: Object | |
private let rhs: Object | |
private var areEqual: Bool = true | |
init(lhs: Object, rhs: Object) { | |
self.lhs = lhs | |
self.rhs = rhs | |
} | |
func compare<T: Equatable>(by keyPath: KeyPath<Object, T>) -> Self { | |
guard areEqual else { | |
return self | |
} | |
if lhs[keyPath: keyPath] != rhs[keyPath: keyPath] { | |
areEqual = false | |
} | |
return self | |
} | |
func build() -> Bool { | |
return areEqual | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment