Fix replied-to account not being first mention

This commit is contained in:
Shadowfacts 2023-04-13 10:02:05 -04:00
parent b2fe2fdf9a
commit 6bd2eacb88
2 changed files with 10 additions and 3 deletions

View File

@ -11,10 +11,10 @@ import Foundation
extension Array { extension Array {
func uniques<ID: Hashable>(by identify: (Element) -> ID) -> [Element] { func uniques<ID: Hashable>(by identify: (Element) -> ID) -> [Element] {
var uniques = Set<Hashed<Element, ID>>() var uniques = Set<Hashed<Element, ID>>()
for elem in self { for (index, elem) in self.enumerated() {
uniques.insert(Hashed(element: elem, id: identify(elem))) 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<Element, ID: Hashable>: Hashable { fileprivate struct Hashed<Element, ID: Hashable>: Hashable {
let element: Element let element: Element
let id: ID let id: ID
let origIndex: Int
static func ==(lhs: Self, rhs: Self) -> Bool { static func ==(lhs: Self, rhs: Self) -> Bool {
return lhs.id == rhs.id return lhs.id == rhs.id

View File

@ -19,6 +19,12 @@ final class ArrayUniqueTests: XCTestCase {
XCTAssertEqual([a, b].uniques(by: \.string), [a]) 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 { class Test: NSObject {
let id = UUID() let id = UUID()
let string: String let string: String