From 449a8b93517cd4ba514c382baa15e7fa47e7976d Mon Sep 17 00:00:00 2001 From: Ben Myers Date: Tue, 25 Oct 2022 16:37:29 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Append=20under=20condition?= =?UTF-8?q?s;=20last=20word?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/FoundationPlus/Extensions/Swift/Array.swift | 10 ++++++++-- Sources/FoundationPlus/Extensions/Swift/String.swift | 5 +++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Sources/FoundationPlus/Extensions/Swift/Array.swift b/Sources/FoundationPlus/Extensions/Swift/Array.swift index 7f513b2..216af0f 100644 --- a/Sources/FoundationPlus/Extensions/Swift/Array.swift +++ b/Sources/FoundationPlus/Extensions/Swift/Array.swift @@ -101,10 +101,16 @@ public extension Array where Element: Equatable { Appends elements of an array to the array uniquely. - parameter newElements: The new elements to add + - parameter condition: The condition to filter new unique elements with. */ - @inlinable mutating func appendUniquely(contentsOf newElements: S) where Element == S.Element, S: Sequence { + @inlinable mutating func appendUniquely( + contentsOf newElements: S, + where condition: (S.Element) -> Bool = { _ in true } + ) where Element == S.Element, S: Sequence { for element in newElements { - self.appendUniquely(element) + if condition(element) { + self.appendUniquely(element) + } } } diff --git a/Sources/FoundationPlus/Extensions/Swift/String.swift b/Sources/FoundationPlus/Extensions/Swift/String.swift index 83e6564..cc534cb 100644 --- a/Sources/FoundationPlus/Extensions/Swift/String.swift +++ b/Sources/FoundationPlus/Extensions/Swift/String.swift @@ -21,6 +21,11 @@ public extension String { return components(separatedBy: " ").first ?? "" } + /// The first word in the string. + var lastWord: String { + return components(separatedBy: " ").last ?? "" + } + // MARK: - Static Methods /**