effect icon indicating copy to clipboard operation
effect copied to clipboard

`String.dedent`

Open harrysolovay opened this issue 6 months ago • 5 comments

What is the problem this feature would solve?

When defining tagged template functions, it's often useful to format/dedent the TemplateStringsArray to allow for more legible use. Ie.

Bad

myTagFn`something

another thing.`

Good

myTagFn`
  something

  another thing.
`

Without any dedenting, the easier-to-read sample includes a carriage return and some leading whitespace on each line––this might not be desirable.

What is the feature you are proposing to solve the problem?

It would be useful if Effect's String namespace provided a String.dedentTemplate utility to solve this problem.

function myTagFn(template: TemplateStringArray, ...substitutions: Array<unknown>) {
  const dedentedTemplate = String.dedentTemplate(template)
  const recombined = String.raw(template, ...substitutions)
  // ...
}

What alternatives have you considered?

There is a widely-used dedent library. Also, the Deno standard library has a nice dedent function, although it needs to be installed from JSR. Ditching the extra dep and using an Effect-provided utility would be preferable.

harrysolovay avatar Jun 05 '25 18:06 harrysolovay

@harrysolovay - we have something akin to this in the String module already called String.stripMargin. It's very similar to Scala's stripMargin. We could probably adapt it to tagged template literals.

import { String } from "effect"

String.stripMargin(
  `|something
   |
   |another thing.
   |`
)

You can also customize the margin char with String.stripMarginWith.

Yes, the difference is that it forces you to have the margin char in the string, but I think it is better since it's abundantly clear how ambiguous space is handled.

IMax153 avatar Jun 09 '25 12:06 IMax153

I agree it's better. Although I don't know if library developers will agree. Would a PR with such a util as String.dedentTemplate be acceptable?

harrysolovay avatar Jun 09 '25 14:06 harrysolovay

Could you elaborate? I don't understand what about this library authors would content with.

IMax153 avatar Jun 09 '25 16:06 IMax153

I'm building a library that provides tagged template functions. I'd prefer not to force end devs to prefix supplied lines with pipes, as this is non-standard. My guess is that this preference may also be held by other tagged template function lib devs.

harrysolovay avatar Jun 09 '25 18:06 harrysolovay

If added, I would suggest the utility be a dedent method similar to what Deno has (i.e. not specialized for tagged template literals), as that is much too specific a use case to be added to Effect.

Also, if added, it must be implemented natively with an accompanying test suite to avoid any third party deps.

IMax153 avatar Jun 09 '25 18:06 IMax153