From 59e762f52eab109de327860c4ab904e8b599420e Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Thu, 22 Aug 2024 14:10:44 -0500 Subject: [PATCH] added == and != for equality Signed-off-by: Matt Bruce --- .../MoleculeComparisonProtocol.swift | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/MVMCoreUI/Atomic/Protocols/ModelProtocols/MoleculeComparisonProtocol.swift b/MVMCoreUI/Atomic/Protocols/ModelProtocols/MoleculeComparisonProtocol.swift index 97014092..5628faba 100644 --- a/MVMCoreUI/Atomic/Protocols/ModelProtocols/MoleculeComparisonProtocol.swift +++ b/MVMCoreUI/Atomic/Protocols/ModelProtocols/MoleculeComparisonProtocol.swift @@ -70,3 +70,43 @@ public extension Optional where Wrapped: Collection { return self.isVisuallyEquivalent(to: models) } } + +func == (lhs: (any MoleculeModelComparisonProtocol)?, rhs: (any MoleculeModelComparisonProtocol)?) -> Bool { + switch (lhs, rhs) { + case (.some(let lhs), .some(let rhs)): + return lhs.isEqual(to: rhs) + case (.none, .none): + return true + default: + return false + } +} + +func != (lhs: (any MoleculeModelComparisonProtocol)?, rhs: (any MoleculeModelComparisonProtocol)?) -> Bool { + return !(lhs == rhs) +} + +func == (lhs: [any MoleculeModelComparisonProtocol]?, rhs: [any MoleculeModelComparisonProtocol]?) -> Bool { + switch (lhs, rhs) { + case (.some(let lhs), .some(let rhs)): + return lhs == rhs + case (.none, .none): + return true + default: + return false + } +} + +func != (lhs: [any MoleculeModelComparisonProtocol]?, rhs: [any MoleculeModelComparisonProtocol]?) -> Bool { + return !(lhs == rhs) +} + +func == (lhs: [any MoleculeModelComparisonProtocol], rhs: [any MoleculeModelComparisonProtocol]) -> Bool { + return lhs.elementsEqual(rhs, by: { (lhsElement, rhsElement) -> Bool in + return lhsElement == rhsElement + }) +} + +func != (lhs: [any MoleculeModelComparisonProtocol], rhs: [any MoleculeModelComparisonProtocol]) -> Bool { + return !(lhs == rhs) +}