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

CSV Convenience #15

Open
adam-rocska opened this issue Jan 14, 2024 · 0 comments
Open

CSV Convenience #15

adam-rocska opened this issue Jan 14, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@adam-rocska
Copy link
Member

Add a better, professional and well tested version of this quick hackery I made a few days ago.

The idea is that anything that is:

  • a RangeReplacableCollection,
  • who's elements are RangeReplacableCollections
  • who's subitems are ExpressibleByStringLiteral

can all be initialized from a CSV.

The additional stuff to add:

  • Anything that is expressible by an array literal
  • Bunch of tests
  • Bunch of examples
import Foundation

typealias RawCsvTable = [[String]]

extension RangeReplaceableCollection
where
  Element: RangeReplaceableCollection,
  Element.Element: ExpressibleByStringLiteral,
  Element.Element.StringLiteralType == String
{

  typealias CsvTable = Self
  typealias CsvRow = Element
  typealias CsvCell = Element.Element

  init?(fromCsv url: URL) {
    guard let raw = try? String(contentsOf: url) else { return nil }
    self.init(
      raw
        .components(separatedBy: .newlines)
        .map { .init($0.components(separatedBy: ";").map { CsvCell(stringLiteral: $0) }) }
    )
  }

}
@adam-rocska adam-rocska added the enhancement New feature or request label Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant