-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Array: add leftOf / rightOf accessor methods (#18)
* Array: add leftOf / rightOf accessor methods * Code review feedback, better naming.
- Loading branch information
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
Sources/FoundationExtensions/Collection/Array+LeftRightOf.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// Array+LeftRightOf.swift | ||
// FoundationExtensions | ||
// | ||
// Created by Thomas Mellenthin on 15.09.20. | ||
// Copyright © 2018 Lautsprecher Teufel GmbH. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
extension Array where Self.Element : Equatable { | ||
|
||
public func left(of element: Element) -> Element? { | ||
guard let elementIndex = firstIndex(of: element) else { return nil } | ||
if elementIndex == 0 { | ||
return self.last | ||
} | ||
return self[safe: (elementIndex - 1)] | ||
} | ||
|
||
public func right(of element: Element) -> Element? { | ||
guard let elementIndex = firstIndex(of: element) else { return nil } | ||
if elementIndex >= (count - 1) { | ||
return self.first | ||
} | ||
return self[safe: (elementIndex + 1)] | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
Tests/FoundationExtensionsTests/Array+LeftRightOfTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// ArrayLeftRightOfTests.swift | ||
// FoundationExtensionsTests | ||
// | ||
// Created by Thomas Mellenthin on 15.09.20. | ||
// Copyright © 2018 Lautsprecher Teufel GmbH. All rights reserved. | ||
// | ||
|
||
#if !os(watchOS) | ||
import FoundationExtensions | ||
import XCTest | ||
|
||
class ArrayLeftRightOfTests: XCTestCase { | ||
} | ||
|
||
extension ArrayLeftRightOfTests { | ||
func testRightOf() { | ||
// whe | ||
let sut = [1, 3, 7, 9, 25] | ||
|
||
// then | ||
XCTAssertEqual(sut.right(of: 1), 3) | ||
XCTAssertEqual(sut.right(of: 9), 25) | ||
XCTAssertEqual(sut.right(of: 25), 1) | ||
XCTAssertEqual(sut.right(of: 666), nil) | ||
} | ||
|
||
func testLeftOf() { | ||
// whe | ||
let sut = [1, 3, 7, 9, 25] | ||
|
||
// then | ||
XCTAssertEqual(sut.left(of: 1), 25) | ||
XCTAssertEqual(sut.left(of: 3), 1) | ||
XCTAssertEqual(sut.left(of: 25), 9) | ||
XCTAssertEqual(sut.left(of: 666), nil) | ||
} | ||
} | ||
#endif |