I find them to be really useful for simple and targeted tasks. For example, simplur[1] for pluralizing strings and dedent[2] just for developer ergonomics.
dedent`something ${code}`
and dedent(`something ${code}`)
? Not sure to understand the advantage of tagged strings here... function exampleTag(stringSegment, ...values) {
console.log(stringSegment);
console.log(values);
}
const myNumber = 123;
exampleTag`foo ${myNumber} bar ${myNumber}`;
// Prints:
// ['foo', 'bar', '']
// [123, 123]
This gives the tag function a way to access the interpolated values themselves, before they get coerced to a String. You're correct, though, that it really doesn't make much of a difference for a dedent function. function fmt(q, a) {
return dedent`\
Question: ${q}
Answer: ${a}
`;
}
Since `dedent` is basically syntactic sugar for developer convenience, we want this function to be equivalent to this: function fmtDesugared(q, a) {
return `Question: ${q}\nAnswer: ${a}\n`;
}
Now consider this input: console.log(fmt("a\nb", "c\nd"));
// should print:
Question: a
b
Answer: c
d
But if `dedent` only got to see the string after interpolation—like this— function fmtWrong(q, a) {
return dedent(`\
Question: ${q}
Answer: ${a}
`);
}
then the input to `dedent` would be Question: a
b
Answer: c
d
and so `dedent` would not strip any indentation. That is, `dedent` is meant to identify the maximal common leading indentation in the template as written by the developer, which should not depend on the values of the interpolands.A stale GitHub issue on the dmnd/dedent repo has a real-world example where this matters and led to a subtle bug: https://github.com/dmnd/dedent/issues/22