Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a comment inside a es6 Template-String?

Let's say we have a multiline es6 Template-String to describe e.g. some URL params for a request:

const fields = `     id,     message,     created_time,     permalink_url,     type `; 

Is there any way to have comments inside that backtick Template-String? Like:

const fields = `     // post id     id,     // post status/message     message,     // .....     created_time,     permalink_url,     type `; 
like image 236
lukash Avatar asked Oct 14 '16 15:10

lukash


People also ask

What is template string in ES6?

Template literals are a new feature introduced in ECMAScript 2015/ ES6. It provides an easy way to create multiline strings and perform string interpolation. Template literals are the string literals and allow embedded expressions. Before ES6, template literals were called as template strings.

Do template strings support multiline strings?

Template Strings significantly simplify multiline strings. Simply include newlines where they are needed and BOOM. Here's an example: Any whitespace inside of the backtick syntax will also be considered part of the string.

How do you declare a JavaScript expression inside a template literal?

Description. Template literals are enclosed by backtick (`) characters instead of double or single quotes. Along with having normal strings, template literals can also contain other parts called placeholders, which are embedded expressions delimited by a dollar sign and curly braces: ${expression} .

What is string interpolation in ES6?

String interpolation is a new feature of ES6, that can make multi-line strings without the need for an escape character. We can use apostrophes and quotes easily that they can make our strings and therefore our code easier to read as well.

What is a template string in ES6?

ES6 template strings were referred to as template strings prior to ES6. Template strings are contained by the backtick (‘ ‘) character. The dollar symbol and curly braces ($ (expression)) are used to signify placeholders in template strings.

What is the difference between ES6 and ES6 string literals?

with this approach, String manipulations are a tedious task, and the possibility of error occurrence is more. String literals are enclosed in either a single quote or a double quote. With Es6, Back-ticks are introduced for string literals for interpolation.

Are template strings supported in production?

Template Strings are in Chrome 41 beta+, IE Tech Preview, Firefox 35+ and io.js. Practically speaking if you would like to use them in production today, they're supported in major ES6 Transpilers, including Traceur and 6to5. Check out our Template Strings sample over on the Chrome samples repo if you'd like to try them out.

Do template strings include newlines and indent?

Unlike ordinary strings, template strings can cover multiple lines: All whitespace in the template string, including newlines and indentation, is included verbatim in the output. OK. Because of my promise last week, I feel responsible for your brain health.


1 Answers

Option 1: Interpolation

We can create interpolation blocks that return an empty string, and embed the comments inside them.

const fields = `    id,${ /* post id */'' }    message,${ /* post status/message */'' }    created_time,    permalink_url,    type  `;    console.log(fields);

Option 2: Tagged Templates

Using tagged templates we can clear the comments and reconstruct the strings. Here is a simple commented function that uses Array.map(), String.replace(), and a regex expression (which needs some work) to clear comments, and return the clean string:

const commented = (strings, ...values) => {    const pattern = /\/{2}.+$/gm; // basic idea      return strings      .map((str, i) =>         `${str}${values[i] !== undefined ? values[i] : ''}`)      .join('')      .replace(pattern, '');  };    const d = 10;  const fields = commented`    ${d}    id, // post ID    ${d}    message, // post/status message    created_time, // ...    permalink_uri,    type  `;    console.log(fields);
like image 127
Ori Drori Avatar answered Sep 21 '22 23:09

Ori Drori