Search code examples
javascriptnode.jstemplate-literalstemplate-strings

Multi-line template string with no newline characters?


I have this single line of code:

 console.log(`now running each hook with name '${chalk.yellow(aBeforeOrAfterEach.desc)}', for test case with name '${chalk.magenta(test.desc)}'.`);

the problem is I have to put it on multiple lines in my IDE, for it to fit (I go with 100 columns). When I put a template string on multiple lines, the template string interprets it as newline characters, which is not what I want.

So my question is - how can I have multiple lines of template string code without having it be logged with new line characters?


Solution

  • use a backslash - \ - at the end of every line to avoid an insertion of \n

     console.log(`\
     now running each hook with name \
    '${'foo'}', for test case with name '${'bar'}' \
    .`);

    Another option should be to use .split("\n").join('') which will replace all newlines with a empty string