Skip to content

Latest commit

 

History

History
123 lines (99 loc) · 3.71 KB

README.md

File metadata and controls

123 lines (99 loc) · 3.71 KB

CI Status Code Coverage Latest Release License Swift Version

CSV

CSV.swift is a powerful swift library for parsing CSV files that supports reading as [String], [String: String] and Decodable, without sacrificing performance.

CSV.swift also supports the new async/await functionality of swift 5.5.

Why?

There are already a few CSV parsers for Swift, but I was not able to find one that had support for high-speed parsing, while still offering convenience features, such as parsing Decodables.

⚡ Speed Comparison

[Coming Soon]

Installation

Swift Package Manager

CSV.Swift is compatible with Swift Package Manager. Simply add it to your project's Package.swift.

dependencies: [
	.package(url: "https://github.com/benkoska/csv.swift.git", from: "0.1.0")
],
targets: [
	.target(
		name: "Project",
		dependencies: [
			.product(name: "CSV", package: "csv")
		]
	)
]

After the installation you can import CSV in your .swift files.

import CSV

Docs

Docs avaliable at https://docs.benkoska.com/csv

Usage

let string = "joe,doe,28"
let parser = try CSVParser(url: url, header: ["firstName", "lastName", "age"])

Get next as array

parser.next() // => Optional<["joe", "doe", "28"]>

Get next as dictionary

try parser.nextAsDict() // => Optional<["firstName": "joe", "lastName": "doe", "age": "28"]>

Decode next from Decodable

struct Person: Decodable {
	let firstName: String
	let lastName: String
	let age: Int
}

try parser.next(as: Person.self) // => Optional<Person(firstName: "joe", lastName: "doe", age: 28)>

Create parser from URL

let parser = try CSVParser(url: url)
let parser = try CSVParser(url: url, delimiter: "|")
let parser = try CSVParser(url: url, delimiter: "|", hasHeader: true)
let parser = try CSVParser(url: url, delimiter: "|", header: ["firstName", "lastName", "age"])

Create parser from String

let string = "joe,doe,28\njane,doe,21"
let string2 = "joe|doe|28\njane|doe|21"
let string3 = "firstName|lastName|age\njoe|doe|28\njane|doe|21"

let parser = try CSVParser(string: string)
let parser = try CSVParser(string: string2, delimiter: "|")
let parser = try CSVParser(string: string3, delimiter: "|", hasHeader: true)
let parser = try CSVParser(string: string2, delimiter: "|", header: ["firstName", "lastName", "age"])

Create parser from Data

let parser = try CSVParser(data: data)
let parser = try CSVParser(data: data, delimiter: "|")
let parser = try CSVParser(data: data, delimiter: "|", hasHeader: true)
let parser = try CSVParser(data: data, delimiter: "|", header: ["firstName", "lastName", "age"])

Disclaimer

Until Swift.CSV reaches version 1.0.0 the API is subject to breaking changes between minor version jumps.

The being said, I will try to minimize breaking changes to the public API.

Author

Ben Koska, [email protected]

License

Swift.CSV is available under the MIT license. See the LICENSE file for more info.