From 08b982dbd38cc56ce837ab88d95feaff6a027472 Mon Sep 17 00:00:00 2001 From: Roma Sosnovsky Date: Wed, 29 Sep 2021 13:33:29 +0300 Subject: [PATCH 1/2] issue #516 fix unique method wrong order --- FlowCryptCommon/Extensions/CollectionExtensions.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/FlowCryptCommon/Extensions/CollectionExtensions.swift b/FlowCryptCommon/Extensions/CollectionExtensions.swift index a7da04acd..38da7c315 100644 --- a/FlowCryptCommon/Extensions/CollectionExtensions.swift +++ b/FlowCryptCommon/Extensions/CollectionExtensions.swift @@ -53,6 +53,7 @@ public extension Array where Element == String { public extension Collection where Element: Hashable { func unique() -> [Element] { - return Array(Set(self)) + var seen: Set = [] + return filter { seen.insert($0).inserted } } } From 7320d10cdff294bd66e3e430bb2dcc5f938d1213 Mon Sep 17 00:00:00 2001 From: Roma Sosnovsky Date: Wed, 29 Sep 2021 13:42:09 +0300 Subject: [PATCH 2/2] issue #516 add test for unique() order --- FlowCryptAppTests/ExtensionTests.swift | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/FlowCryptAppTests/ExtensionTests.swift b/FlowCryptAppTests/ExtensionTests.swift index b3b7e4dc0..4cb4faf04 100644 --- a/FlowCryptAppTests/ExtensionTests.swift +++ b/FlowCryptAppTests/ExtensionTests.swift @@ -63,6 +63,14 @@ extension ExtensionTests { XCTAssertNotNil(collection?[safe: 0]) } + + func test_unique() { + let collection = [1, 2, 2, 3, 4, 4] + let uniqueCollection = collection.unique() + + XCTAssertEqual(uniqueCollection, [1, 2, 3, 4]) + XCTAssertEqual(uniqueCollection.unique(), uniqueCollection) + } } // MARK: - Calendar