String.raw
provides the string without escaped characters being processed
GIVEN using String.raw
as a tag-function
WHEN passing line-break (\n) THEN the String.raw
ing it the backslash stays "visible" (and processable)
WHEN passing an escaped backslash to String.raw
THEN this equals a string with two backslashes, where each is escaped
WHEN passing a unicode character THEN the leading backslash is made "visible"
a use case: WHEN having a raw unicode character THEN I can modify it
GIVEN using String.raw
as a function to call
WHEN String.raw()
is called without a parameter THEN it throws
WHEN String.raw()
is called with a string as parameter THEN it still throws
WHEN passing the first parameter {raw: []}
THEN this equals to an empty string
WHEN passing the first property raw
an array of strings THEN they are just concatenated
WHEN passing more parameters, the substitutions THEN these are used as the filler between each raw string, in their according place
Links
The original official specification when introduced, it does not explain what this function does, but very much in detail how it works which results in explaining the "what" too.
The more readable and usable specification, in the version of the continuously maintained specification document.
The Mozilla Developer Network docs, an easy to understand explanation.
This video explains `String.raw` nicely by working with it on the browser console.
Required Knowledge
- basics (Arrow functions)
- `const` declaration (Block scope)
- as parameter (Rest operator)
- with arrays (Spread operator)
Related Katas
Template strings
String API
string.includes()
string.repeat(count)
string.startsWith()
string.endsWith()
String.raw
string.trimStart()
string.matchAll()
Difficulty Level
ADVANCED
First Published
8 October 2023
Stats
9 tests to solve