Skip to content

Instantly share code, notes, and snippets.

@ancms2600
Created May 13, 2018 17:54
Show Gist options
  • Save ancms2600/5abc8dd534ad4cf76ab3c40c538d48ce to your computer and use it in GitHub Desktop.
Save ancms2600/5abc8dd534ad4cf76ab3c40c538d48ce to your computer and use it in GitHub Desktop.
ES6 Template Tags exampple
// Template Tag function replaces string references to variable names with eval()'ed variable's value,
// and rpads the value string with spaces so that it takes up exactly the same space that the variable name used to.
// Makes it easier to maintain block column formatting across rows of text.
var apples = 1, bananas = 2, cucumbers = 3,
result = ((apples) * bananas) - cucumbers;
console.log(vpad`((apples) * bananas) - cucumbers = result`);
console.log(vpad`((${'apples'}) * ${'bananas'}) - ${'cucumbers'} = ${'result'}`);
// output is:
// ((apples) * bananas) - cucumbers = result
// ((1 ) * 2 ) - 3 = -1
// WARNING: variable name must always be longer than value string or value is truncated.
var vpad = (a, ...args) => a.map((s,i)=> s + ((null == args[i]) ? '' :
(eval(args[i]) + args[i].replace(/./g, ' ')).substr(0,args[i].length) )).join('');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment