DandyLyons/SelectiveEquatable
Check equality by selectively choosing which properties you'd like to compare.
Usage
Suppose we have the following Person struct:
struct Person: Identifiable {
let firstName: String
let lastName: String
let age: Int
let id: UUID
let profileImage: UIImage
}Suppose we had a collection of Persons and we'd like to determine if any persons have the same firstName, lastName, and age but different ids and profileImages. We can use SelectiveEquatable to perform this comparison:
extension Person: SelectiveEquatable {}
person1.isEqual(to: person2, by: \.firstName, \.lastName, \.age) // returns true or falseBy simply conforming our type to SelectiveEquatable, we can now selectively compare instances of Person based on the properties we specify. In this case, we're comparing person1 and person2 based on their firstName, lastName, and age.
Motivation and Design
The Equatable protocol in Swift is great for comparing instances of a type based on all of their properties. However, there are times when we only want to compare instances based on a subset of their properties. There are also some scenarios where the cost of conforming to Equatable for a type is not practical. SelectiveEquatable was designed to address these issues. To learn more about the motivation and design of SelectiveEquatable, check out these blog posts:
Installation
SelectiveEquatable is an extremely simple protocol. In order to use it, you can simply copy the SelectiveEquatable.swift file into your project.
Swift Package Manager
You can also use the Swift Package Manager to install SelectiveEquatable. Add the following to your Package.swift file:
dependencies: [
.package(url: "https://github.com/DandyLyons/SelectiveEquatable", from: "1.0.0")
]License
SelectiveEquatable is available under the MIT license. See the LICENSE file for more info.
Package Metadata
Repository: DandyLyons/SelectiveEquatable
Stars: 2
Forks: 0
Open issues: 0
Default branch: main
Primary language: swift
License: MIT
README: README.md