From 6bd2eacb88af72b95e6c0b2e353c95ac735ffab9 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 13 Apr 2023 10:02:05 -0400 Subject: [PATCH] Fix replied-to account not being first mention --- Tusker/Extensions/Array+Uniques.swift | 7 ++++--- TuskerTests/ArrayUniqueTests.swift | 6 ++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Tusker/Extensions/Array+Uniques.swift b/Tusker/Extensions/Array+Uniques.swift index e2e77eba..b5490ad5 100644 --- a/Tusker/Extensions/Array+Uniques.swift +++ b/Tusker/Extensions/Array+Uniques.swift @@ -11,10 +11,10 @@ import Foundation extension Array { func uniques(by identify: (Element) -> ID) -> [Element] { var uniques = Set>() - for elem in self { - uniques.insert(Hashed(element: elem, id: identify(elem))) + for (index, elem) in self.enumerated() { + uniques.insert(Hashed(element: elem, id: identify(elem), origIndex: index)) } - return uniques.map(\.element) + return uniques.sorted(by: { $0.origIndex < $1.origIndex }).map(\.element) } } @@ -27,6 +27,7 @@ extension Array where Element: Hashable { fileprivate struct Hashed: Hashable { let element: Element let id: ID + let origIndex: Int static func ==(lhs: Self, rhs: Self) -> Bool { return lhs.id == rhs.id diff --git a/TuskerTests/ArrayUniqueTests.swift b/TuskerTests/ArrayUniqueTests.swift index 3d6cc23b..6755e590 100644 --- a/TuskerTests/ArrayUniqueTests.swift +++ b/TuskerTests/ArrayUniqueTests.swift @@ -19,6 +19,12 @@ final class ArrayUniqueTests: XCTestCase { XCTAssertEqual([a, b].uniques(by: \.string), [a]) } + func testUniquesOrder() { + let a = Test(string: "a") + let b = Test(string: "b") + XCTAssertEqual([a, b].uniques(), [a, b]) + } + class Test: NSObject { let id = UUID() let string: String