Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty column, was shifting the column values #8

Merged
merged 4 commits into from
Apr 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions CSwiftV.xcodeproj/xcshareddata/xcschemes/CSwiftVIOS.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@
ReferencedContainer = "container:CSwiftV.xcodeproj">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "18B665021AF7BC4B009D5195"
BuildableName = "CSwiftVOSXTests.xctest"
BlueprintName = "CSwiftVOSXTests"
ReferencedContainer = "container:CSwiftV.xcodeproj">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "1878D43B19B27FB800FF84A7"
BuildableName = "CSwiftVTests.xctest"
BlueprintName = "CSwiftVTests"
ReferencedContainer = "container:CSwiftV.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
Expand Down
40 changes: 24 additions & 16 deletions CSwiftV/CSwiftV.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@

import Foundation

extension String {
func isEmptyOrWhitespace() -> Bool {

if(self.isEmpty) {
return true
}

return (self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) == "")
}

func isNotEmptyOrWhitespace() -> Bool {
return !isEmptyOrWhitespace()
}
}

//MARK: Parser
public class CSwiftV {

Expand All @@ -17,21 +32,23 @@ public class CSwiftV {
public let rows: [[String]]

public init(String string: String, separator:String = ",", headers:[String]? = nil) {

var parsedLines = recordsFromString(string.stringByReplacingOccurrencesOfString("\r\n", withString: "\n")).map { cellsFromString($0, separator: separator) }

let tempHeaders = headers ?? parsedLines[0]

let tempHeaders = headers ?? parsedLines.removeFirst()
self.rows = parsedLines

self.columnCount = tempHeaders.count

let keysAndRows = self.rows.map { (field :[String]) -> [String:String] in

var row = [String:String]()

for (index, value) in field.enumerate() {
row[tempHeaders[index]] = value
//only store value which are not empty
if let v: String? = .Some(value) where value.isNotEmptyOrWhitespace() {
row[tempHeaders[index]] = v
}
}

return row
Expand Down Expand Up @@ -61,9 +78,7 @@ func cellsFromString(rowString:String, separator: String = ",") -> [String] {
}

func recordsFromString(string: String) -> [String] {

return split("\n", string: string)

return split("\n", string: string).filter { (string) -> Bool in return string.isNotEmptyOrWhitespace() }
}

func split(separator: String, string: String) -> [String] {
Expand All @@ -74,7 +89,7 @@ func split(separator: String, string: String) -> [String] {
return string.componentsSeparatedByString("\"").count % 2 == 0
}

let merged = initial.reduce([]) { (prevArray, newString) -> [String] in
return initial.reduce([]) { (prevArray, newString) -> [String] in

if let record = prevArray.last {
if (oddNumberOfQuotes(record)) {
Expand All @@ -91,11 +106,4 @@ func split(separator: String, string: String) -> [String] {
}

}

let final = merged
.filter { (string) -> Bool in return string.characters.count > 0 }


return final

}
23 changes: 23 additions & 0 deletions CSwiftVTests/CSwiftVTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import XCTest

import CSwiftV

public let emptyColumns = "Year,Make,Model,Description,Price\r\n1997,Ford,,descrition,3000.00\r\n1999,Chevy,Venture,another description,\r\n"


public let newLineSeparation = "Year,Make,Model,Description,Price\r\n1997,Ford,E350,descrition,3000.00\r\n1999,Chevy,Venture,another description,4900.00\r\n"

public let newLineSeparationNoCR = "Year,Make,Model,Description,Price\n1997,Ford,E350,descrition,3000.00\n1999,Chevy,Venture,another description,4900.00\n"
Expand Down Expand Up @@ -291,5 +294,25 @@ class CSwiftVTests: XCTestCase {
XCTAssertEqual(arrayUnderTest, expectedArray)
}

func testWhenCellsAreEmpty() {

testString = emptyColumns
let csv = CSwiftV(String: testString)

let expectedArray = [
["1997","Ford","","descrition","3000.00"],
["1999","Chevy","Venture","another description",""]
]

XCTAssertEqual(csv.rows, expectedArray)

let expectedKeyedRows = [
["Year":"1997", "Make": "Ford", "Description":"descrition", "Price":"3000.00"],
["Year":"1999", "Make": "Chevy", "Model":"Venture", "Description":"another description"]
]

XCTAssertEqual(csv.keyedRows!, expectedKeyedRows)
}


}