Skip to content

Instantly share code, notes, and snippets.

@thiagorb
Created December 19, 2018 16:14
Show Gist options
  • Save thiagorb/4b38d94a6e1e6d3da9320752f9821eab to your computer and use it in GitHub Desktop.
Save thiagorb/4b38d94a6e1e6d3da9320752f9821eab to your computer and use it in GitHub Desktop.
Tagged template strings example
const wrapWithQuotes = (parts, ...values) => {
let result = parts[0];
values.forEach((value, i) => {
result += `"${value}"` + parts[i + 1];
});
return result;
};
const value = 'test';
console.log(wrapWithQuotes`this is a ${value}`); // logs 'this is a "test"'
@kesor
Copy link

kesor commented Feb 5, 2020

A oneliner that does this -
const wrapWithQuotes = (strings, ...values) => strings[0] + values.map( (v, idx) => '"' + v + '"' + strings[idx + 1] ).join('')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment