Skip to content

Commit

Permalink
Collection: Added longest() and longestIndex()
Browse files Browse the repository at this point in the history
  • Loading branch information
orchetect committed Jul 26, 2022
1 parent a9bc04a commit 1ef2ae7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Sources/OTCore/Extensions/Swift/Collections.swift
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,47 @@ extension Collection where Element: StringProtocol {
}
}

// MARK: - longest() / longestIndex()

extension Collection where Element: StringProtocol {

/// **OTCore:**
/// Returns the longest string in the collection.
/// If more than one element are the same length as the longest element, the first will be returned.
/// - complexity: O(*n*)
@inlinable @_disfavoredOverload
public func longest() -> Element? {

guard let idx = longestIndex() else { return nil }
return self[idx]

}

/// **OTCore:**
/// Returns the index of the longest string in the collection.
/// If more than one element are the same length as the longest element, the first will be returned.
/// - complexity: O(*n*)
@inlinable @_disfavoredOverload
public func longestIndex() -> Index? {

guard !isEmpty else { return nil }

var longestIndex: Index = startIndex
var longestLength: Int = self[startIndex].count

for index in indices {
let elementLength = self[index].count
if elementLength > longestLength {
longestIndex = index
longestLength = elementLength
}
}

return longestIndex

}
}

// MARK: - stringValueArrayLiteral

extension Collection where Element: BinaryInteger {
Expand Down
26 changes: 26 additions & 0 deletions Tests/OTCoreTests/Extensions/Swift/Collections Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,8 @@ class Extensions_Swift_Collections_Tests: XCTestCase {

}

// MARK: - shortest() / shortestIndex()

func testStringProtocol_Shortest() {

XCTAssertEqual([String]().shortest(), nil)
Expand All @@ -1187,6 +1189,30 @@ class Extensions_Swift_Collections_Tests: XCTestCase {

}

// MARK: - longest() / longestIndex()

func testStringProtocol_Longest() {

XCTAssertEqual([String]().longest(), nil)
XCTAssertEqual([""].longest(), "")
XCTAssertEqual(["A"].longest(), "A")
XCTAssertEqual(["A", "1", "xy", "", "abc"].longest(), "abc")
XCTAssertEqual(["abc", "123", "A", "1", "xyz"].longest(), "abc")
XCTAssertEqual(["123", "abc", "1", "A", "xyz"].longest(), "123")

}

func testStringProtocol_LongestIndex() {

XCTAssertEqual([String]().longestIndex(), nil)
XCTAssertEqual([""].longestIndex(), 0)
XCTAssertEqual(["A"].longestIndex(), 0)
XCTAssertEqual(["A", "1", "xy", "", "abc"].longestIndex(), 4)
XCTAssertEqual(["abc", "123", "A", "1", "xyz"].longestIndex(), 0)
XCTAssertEqual(["123", "abc", "1", "A", "xyz"].longestIndex(), 0)

}

// MARK: - .stringValueArrayLiteral

func teststringValueArrayLiteral() {
Expand Down

0 comments on commit 1ef2ae7

Please sign in to comment.