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 `;
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.
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.
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} .
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.
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.
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With