Search⌘ K
AI Features

Solution Review: Tagged Template

Explore how to create a tagged template function that transforms embedded expressions to uppercase and eliminates leading whitespace using regex. Understand the use of spread operators, map, join, and trim methods to manipulate template literals effectively in JavaScript.

Solution

We ...

Javascript (babel-node)
'use strict';
const stripMargin = function(texts, ...expressions) {
const exceptLast = expressions.map(function(expression, index) {
return `${texts[index]}${expression.toString().toUpperCase()}`;
}).join('');
const result = `${exceptLast}${texts[texts.length - 1]}`;
return result.replace(/[\n][\t\s]+(\w)/g, ' $1').trim();
};
const name = 'Jane';
const processed = stripMargin` This is for
${name} and it needs to be
delivered by December 24th.`
console.log(processed);
//This is for JANE and it needs to be delivered by December 24th.

Explanation

There are two main ...