Skip to content

Commit

Permalink
It's a collection
Browse files Browse the repository at this point in the history
  • Loading branch information
arennow committed May 2, 2020
1 parent feb0373 commit 36e42c8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
21 changes: 16 additions & 5 deletions Sources/KeyedArray/KeyedArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,25 @@ extension KeyedArray where Element: Identifiable, Key == Element.ID {
public init() {
self.init(keyExtractor: \.id)
}
}

extension KeyedArray: ExpressibleByArrayLiteral where Element: Identifiable, Key == Element.ID {
public init(arrayLiteral elements: Element...) {

public init<S: Sequence>(_ seq: S) where S.Element == Element {
self.init()

for element in elements {
for element in seq {
self.append(element)
}
}
}

extension KeyedArray: ExpressibleByArrayLiteral where Element: Identifiable, Key == Element.ID {
public init(arrayLiteral elements: Element...) {
self.init(elements)
}
}

extension KeyedArray: Collection {
public var startIndex: Int { array.startIndex }
public var endIndex: Int { array.endIndex }

public func index(after i: Int) -> Int { array.index(after: i) }
}
16 changes: 15 additions & 1 deletion Tests/KeyedArrayTests/KeyedArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ final class KeyedArrayTests: XCTestCase {
("testAppend", testAppend),
("testInsert", testInsert),
("testRemoveByIndex", testRemoveByIndex),
("testRemoveByKey", testRemoveByKey)
("testRemoveByKey", testRemoveByKey),
("testReplaceByIndex", testReplaceByIndex),
("testArrayIsArray", testArrayIsArray),
("testIterator", testIterator)
]

var ar: KeyedArray<Character, DemoVal>!
Expand Down Expand Up @@ -60,4 +63,15 @@ final class KeyedArrayTests: XCTestCase {
XCTAssertEqual(ar.array, ["alpha", "bravo"])
XCTAssertEqual(ar.dictionary, ["a": "alpha", "b": "bravo"])
}

func testArrayIsArray() {
XCTAssertEqual(Array(ar.array), ar.array)
}

func testIterator() {
var it = ar.makeIterator()
XCTAssertEqual(it.next(), "alpha")
XCTAssertEqual(it.next(), "beta")
XCTAssertEqual(it.next(), nil)
}
}

0 comments on commit 36e42c8

Please sign in to comment.