Skip to content

Commit

Permalink
add the stripMargin utility
Browse files Browse the repository at this point in the history
  • Loading branch information
massimosiani committed Aug 29, 2022
1 parent 14c756d commit 2ee6276
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
12 changes: 12 additions & 0 deletions modules/core/shared/src/main/scala/Fragment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ final case class Fragment[A](
def ~[B](fb: Fragment[B]): Fragment[A ~ B] =
product(fb)

def stripMargin: Fragment[A] = stripMargin('|')

def stripMargin(marginChar: Char): Fragment[A] = {
val ps = parts.map {
_.bimap(
_.stripMargin(marginChar).replaceAll("\n", " "),
_.map(_.stripMargin(marginChar).replaceAll("\n", " "))
)
}
Fragment(ps, encoder, origin)
}

def apply(a: A): AppliedFragment =
AppliedFragment(this, a)

Expand Down
15 changes: 14 additions & 1 deletion modules/tests/shared/src/test/scala/FragmentTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,18 @@ class FragmentTest extends SkunkTest {
} yield "ok"
}
}
}

pureTest("stripMargin") {
val f = sql"""select
|$int4
|""".stripMargin
f.sql.trim == sql"select $int4".sql
}

pureTest("stripMargin with char") {
val f = sql"""select
^$int4
^""".stripMargin('^')
f.sql.trim == sql"select $int4".sql
}
}

0 comments on commit 2ee6276

Please sign in to comment.