Skip to content

Commit

Permalink
fix missing cells at end of enumerated rows
Browse files Browse the repository at this point in the history
  • Loading branch information
DivineDominion committed Jun 4, 2020
1 parent 8c65a81 commit a8af063
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
23 changes: 18 additions & 5 deletions SwiftCSV/EnumeratedView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import Foundation

public struct EnumeratedView: View {

public struct Column {
public let header: String
public let rows: [String]
Expand All @@ -21,20 +20,21 @@ public struct EnumeratedView: View {
public private(set) var columns: [Column]

public init(header: [String], text: String, delimiter: Character, loadColumns: Bool = false) throws {

var rows = [[String]]()
var columns: [EnumeratedView.Column] = []

try Parser.enumerateAsArray(text: text, delimiter: delimiter, startAt: 1) { fields in
rows.append(fields)
}

// Fill in gaps at the end of rows that are too short.
rows = makingRectangular(rows: rows)

if loadColumns {
columns = header.enumerated().map { (index: Int, header: String) -> EnumeratedView.Column in

return EnumeratedView.Column(
header: header,
rows: rows.map { $0[index] })
rows: rows.map { $0[safe: index] ?? "" })
}
}

Expand All @@ -43,7 +43,6 @@ public struct EnumeratedView: View {
}

public func serialize(header: [String], delimiter: Character) -> String {

let head = header
.map(enquoteContentsIfNeeded(cell:))
.joined(separator: ",") + "\n"
Expand All @@ -55,5 +54,19 @@ public struct EnumeratedView: View {

return head + content
}
}

extension Collection {
subscript (safe index: Self.Index) -> Self.Iterator.Element? {
return index < endIndex ? self[index] : nil
}
}

fileprivate func makingRectangular(rows: [[String]]) -> [[String]] {
let cellsPerRow = rows.map { $0.count }.max() ?? 0
return rows.map { row -> [String] in
let missingCellCount = cellsPerRow - row.count
let appendix = Array(repeating: "", count: missingCellCount)
return row + appendix
}
}
4 changes: 2 additions & 2 deletions SwiftCSVTests/CSVTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class CSVTests: XCTestCase {
}
}

func testInit_whenThereAreIncompleteRows_makesRows() {
csv = try! CSV<NamedView>(string: "id,name,age\n1,Alice,18\n2,Bob,19\n3,Charlie")
func testInit_whenThereAreIncompleteRows_makesRows() throws {
csv = try CSV<NamedView>(string: "id,name,age\n1,Alice,18\n2,Bob,19\n3,Charlie")
let expected = [
["id": "1", "name": "Alice", "age": "18"],
["id": "2", "name": "Bob", "age": "19"],
Expand Down
12 changes: 12 additions & 0 deletions SwiftCSVTests/EnumeratedViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ class EnumeratedViewTests: XCTestCase {
csv = try! CSV<EnumeratedView>(string: "id,name,age\n1,Alice,18\n2,Bob,19\n3,Charlie,20", delimiter: ",", loadColumns: true)
}

func testInit_whenThereAreIncompleteRows_makesRows() throws {
csv = try CSV<EnumeratedView>(string: "id,name,age\n1,Alice,18\n2\n3,Charlie", delimiter: ",", loadColumns: true)
let expected = [
["1", "Alice", "18"],
["2", "", ""],
["3", "Charlie", ""]
]
for (index, row) in csv.rows.enumerated() {
XCTAssertEqual(expected[index], row)
}
}

func testExposesRows() {
let expected: [[String]] = [
["1", "Alice", "18"],
Expand Down

0 comments on commit a8af063

Please sign in to comment.