Alexander Mills
Alexander Mills

Reputation: 100020

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?

Upvotes: 4

Views: 6926

Answers (3)

Alexander Mills
Alexander Mills

Reputation: 100020

At this point I guess this is as best as we can do:

const trim = (v: string) => String(v || '').trim()
const mapTemplateStrings = (v :Array<string>) => v.map(trim).join(' ');

console.log(mapTemplateStrings([
    `first line ${'one'}`,
    `second line ${'second'} and yada`,
    `bidding war ${'three'}`
]));

Upvotes: 0

Hawkeye Parker
Hawkeye Parker

Reputation: 8620

Try

const foo = "foo"
const bar = "bar"

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

This should not preserve any whitespace, including the indents.

Upvotes: 0

Kristianmitk
Kristianmitk

Reputation: 4778

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

Upvotes: 9

Related Questions