Mukesh Gupta
Mukesh Gupta

Reputation: 1433

Es6 formatting Error?

This string variable define in js.

bodycontent = 'Hello ' + studentName + '%0D%0AThe course ' + courseName
                + ' is using the TEAMMATES System to collect feedback.%0D%'
                + '0ATo \'join\' the course, please go to this Web address: '
                + encodeURIComponent(uri) + '%0D%0A*If prompted to log in, '
                + 'use your Googleaccount to log in. If you do not '
                + 'have a Google account, please create one from the '
                + encodeURIComponent('https://accounts.google.com/NewAccount')
                + '%0D%0A*The above link is unique to you. Please do not '
                + 'share it with your classmates.%0D%0ANote that If you '
                + 'wish to access TEAMMATES without using your Googleaccount, '
                + 'you do not need to \'join\' the course as instructed '
                + 'above. You will still be able to submit/view feedback '
                + 'by following the instructions sent to you by TEAMMATES at the '
                + 'appropriate times. However, we recommend joining the courseusing '
                + 'your Google account, because it gives you more convenient '
                + 'access to all your feedback stored in TEAMMATES.%0D%0A%0D%0A'
                + 'If you encounter any problems when using the system, you can '
                + 'email TEAMMATES support team at [email protected].%0D%0A%0D%0A'
                + 'Regards,%0D%0ATEAMMATES Team.';

For exceeding 125 characters in a line i break it to the next line by using + now the problem is when i converted this into ES6.

        bodycontent = `Hello ${studentName}%0D%0AThe following feedback session is ${status}%0D%0A`
                   + `Course: [${Id}]${courseName}%0D%0AFeedback Session Name: ${Id}%0D%0A`
                   + `The link of the feedback for the above session, please go to this Web `
                   + `address: ${encodeURIComponent(uri)}%0D%0A*The above link is unique to you.`
                   + `Please do not share it with others.%0D%0A%0D%0AIf you encounter any problems`
                   + `when using the system, you can email TEAMMATES support team at [email protected].%0D%0A%0D%0ARegards,%0D%0ATEAMMATES Team.`;

This shows me erro "Strings must use singlequote quotes" in each line which doesn't contain variable.

I can break the line like this also

bodycontent = `Hello ${studentName}%0D%0AThe following feedback session is ${status}%0D%0A
                    Course: [${Id}]${courseName}%0D%0AFeedback Session Name: ${Id}%0D%0A
                    The link of the feedback for the above session, please go to this Web 
                   address: ${encodeURIComponent(uri)}%0D%0A*The above link is unique to you.`
                   ......

But the problem is i am getting "\n" addition of newline in my string which i don't want kindly tell me the efficient way to break the line without adding \n in my string.

Upvotes: 1

Views: 60

Answers (2)

Pineda
Pineda

Reputation: 7593

Any whitespace included in a template literal will also be included which is why you are receiving newlines. This is the expected behaviour of template literals. This feature of a template literal allows it to be used to create strings over multiple lines without the need for workarounds, e.g.:

$('div.post').html("<h3>Some title here</h3> " +
  "<p>Lorem ipsum doloramet, onsectetur adipiscing elit " +
  "sed do eiusmod tempor incididunt ut labore...");

becomes:

$('div.post').html(`
  <h3>Some title here</h3>
  <p>Lorem ipsum doloramet, onsectetur adipiscing elit
  sed do eiusmod tempor incididunt ut labore...
`);

It seems like you only want to run over multiple lines as a result of the length of your String, so this issue is about your subjective text editing preference.

Suggested Soution:

Have you tried using text-wrap in your IDE?

Upvotes: 1

Tatsuyuki Ishi
Tatsuyuki Ishi

Reputation: 4031

Use the awesome prefixed template literals with common-tags package. You can also copy the implementation if you don't use npm.

The oneLine tag will change all newlines and indents to one whitespace. If you want to strip the last whitespace as well, use oneLineTrim.

Upvotes: 0

Related Questions