According to MDN,
String.raw() is the only built-in template literal tag. It has close semantics to an untagged literal since it concatenates all arguments and returns a string. You can even re-implement it with normal JavaScript code.
How would you "re-implement String.raw() with normal JavaScript code"? If I write my own template literal function const raw = (s, v) => ...
, the string array s
contains strings that are no longer raw. For example,
raw `anb`
will pass a-newline-b, whereas the raw string would be a-backslash-n-b.
My raw() template literal function could reverse the quoting by turning newline into backslash-n, but that’s not reliable. How does it distinguish between:
raw `anb`
and
raw `a
b`
?
2
Answers
The array that is passed as first argument to the template tag function has an additional property
raw
, as documented here. It contains the same asstrings
itself but as raw strings, without parsing escape sequences.This means that
String.raw
could be replicated as follows:Note that if we’d replace
strings.raw.reduce
with juststrings.reduce
, we’d get a "do-nothing" template tag function.For this to work properly, you have to make use of the
raw
property of the first argument to the template function. It contains an array of the raw contents of the string as it exists in the source code, without backslash being treated as an escape character.For example: