在 Purescript 中去除缩进三引号字符串的边距?

Strip margin of indented triple-quote string in Purescript?

当在缩进位置使用三引号时,我肯定也会在输出 js 字符串中缩进:

在嵌套的 let 中比较这两个

let input1 = "T1\nX55.555Y-44.444\nX52.324Y-40.386"
let input2 = """T1
        X66.324Y-40.386
        X52.324Y-40.386"""

给予

// single quotes with \n
"T1\x0aX55.555Y-44.444\x0aX52.324Y-40.386"

// triple quoted
"T1\x0a        X66.324Y-40.386\x0a        X52.324Y-40.386"

在 Scala 中是否有像 stripMargin 这样的约定,这样我就可以使用它们而不必取消缩进到顶层?

更新,为了澄清我的意思,我目前正在做的事情:

    describe "header" do
      it "should parse example header" do
        let input = """M48
;DRILL file {KiCad 4.0.7} date Wednesday, 31 January 2018 'AMt' 11:08:53
;FORMAT={-:-/ absolute / metric / decimal}
FMAT,2
METRIC,TZ
T1C0.300
T2C0.400
T3C0.600
T4C0.800
T5C1.000
T6C1.016
T7C3.400
%
"""
        doesParse input header
    describe "hole" do
      it "should parse a simple hole" do
        doesParse "X52.324Y-40.386" hole

更新:

我被要求澄清来自 Scala 的 stripMargin。它是这样使用的:

val speech = """T1
                |X66.324Y-40.386
                |X52.324Y-40.386""".stripMargin

然后删除前导空格。 stripMargin 可以使用任何分隔符,但默认为 |.

更多示例:

Rust 有 https://docs.rs/trim-margin/0.1.0/trim_margin/ Kotlin 在 stdlib 中有:https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/trim-margin.html

我想这听起来像是要左垫 (:) ),但如果已经有东西了,我宁愿不自己酿造它……

很抱歉你没有得到及时的回复,但我已经实现了这个功能here. In case the pull request isn't merged, here's an implementation that just depends on purescript-strings:

import Data.String (joinWith, split) as String
import Data.String.CodeUnits (drop, dropWhile) as String
import Data.String.Pattern (Pattern(..))

stripMargin :: String -> String
stripMargin =
  let
    lines = String.split (Pattern "\n")
    unlines = String.joinWith "\n"
    mapLines f = unlines <<< map f <<< lines
  in
    mapLines (String.drop 1 <<< String.dropWhile (_ /= '|'))