Skip to content

Commit

Permalink
Merge pull request #1 from mikwat/clean-strings
Browse files Browse the repository at this point in the history
Clean row values
  • Loading branch information
zackify committed Feb 14, 2024
2 parents 28b3414 + e12a52a commit 0ea64b4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -5,7 +5,7 @@
Add to your Package.swift dependencies:

```
.package(url: "https://github.com/zackify/swift-csv.git", from: "0.1.0")
.package(url: "https://github.com/zackify/swift-csv.git", from: "0.2.0")
```

## Example
Expand Down
13 changes: 10 additions & 3 deletions Sources/SwiftCSV/SwiftCSV.swift
@@ -1,3 +1,5 @@
import Foundation

public struct SwiftCSV {
public static func generate<Row>(_ rows: [Row], renderRow: (RowRenderer<Row>) -> [Cell]) -> String {
let columns = Columns()
Expand Down Expand Up @@ -26,7 +28,12 @@ public struct SwiftCSV {
}

func arrayToCSVRow(_ strings: [String]) -> String {
return strings.map {
"\"\($0)\""
return strings.map {
let cleanString = $0
.replacingOccurrences(of: "\"", with: "\"\"")
.replacingOccurrences(of: "\r\n", with: " ")
.replacingOccurrences(of: "\r", with: " ")
.replacingOccurrences(of: "\n", with: " ")
return "\"\(cleanString)\""
}.joined(separator: ",")
}
}
13 changes: 11 additions & 2 deletions Tests/SwiftCSVTests/tests.swift
Expand Up @@ -23,16 +23,24 @@ let testRows = [
]
),
Person(
id: "1",
id: "2",
name: "Bob",
phoneNumber: "483884828",
addresses: [
Address(street: "Montana"),
Address(street: "California"),
Address(street: "Texas"),
]
),
Person(
id: "3",
name: "Jim",
phoneNumber: "5554443333",
addresses: [
Address(street: "Idaho,\n not \"Ohio\"")
]
)
]
]

final class swift_csvTests: XCTestCase {
func testExample() {
Expand All @@ -53,6 +61,7 @@ final class swift_csvTests: XCTestCase {
"Name","Address","Address #2","Address #3","Phone number"
"Test","New York","California","","5493939393"
"Bob","Montana","California","Texas","483884828"
"Jim","Idaho, not \"\"Ohio\"\"","","","5554443333"
"""
) }

Expand Down

0 comments on commit 0ea64b4

Please sign in to comment.