Skip to content

Commit

Permalink
Add multiple field support
Browse files Browse the repository at this point in the history
  • Loading branch information
taylor-peterson committed Jun 16, 2024
1 parent 4b7be1e commit 36d3dc0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 3 additions & 3 deletions 004-cut/integration/src/test/scala/CutSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class CutSpec extends FixtureAnyWordSpec with Matchers {
"given a list of fields" should {
"return the correct fields separated by a single occurrence of the default field delimiter" in { cut =>
val expectedOutput =
"""f0\tf1
s"""f0\tf1
|0\t1
|5\t6
|10\t11
Expand All @@ -88,8 +88,8 @@ class CutSpec extends FixtureAnyWordSpec with Matchers {
}
"return the correct fields separated by a single occurrence of the provided field delimiter" in { cut =>
val expectedOutput =
"""Song title,Artist
|"10000 Reasons (Bless the Lord)",Matt Redman and Jonas Myrin
"""\uFEFFSong title,Artist
|"10000 Reasons (Bless the Lord)",Matt Redman\u00A0and\u00A0Jonas Myrin
|"20 Good Reasons",Thirsty Merc
|"Adore You",Harry Styles
|"Africa",Toto
Expand Down
6 changes: 5 additions & 1 deletion 004-cut/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ case class Config(

case class Selections(lines: List[String] = Nil) {
private def process(config: Config, line: String): Selections = {
Selections(lines :+ line.split(config.delim)(config.fields.toInt - 1))
// TODO validate that fields contains at most one of space/comma
val fieldsToGet = config.fields.split("[ ,]").map(_.toInt - 1) // 1 to 0 indexed
val fields = line.split(config.delim)
val newLine = (fieldsToGet map fields).mkString(config.delim) // Separate correct fields with delim
Selections(lines :+ newLine)
}

override def toString: String = {
Expand Down

0 comments on commit 36d3dc0

Please sign in to comment.