JSFiddle - React, Tailwind, and code Playground

by Alexander

JavaScript

var s;

s = template`${0}${1}${0}!`('Y', 'A');  // "YAY!" 
console.log(s);

s = template`${0} ${'foo'}!`('Hello', {foo: 'World'});  // "Hello World!"
console.log(s);


function template(strings, ...keys) {
  return (function(...values) {
    var dict = values[values.length - 1] || {},
    		result = [strings[0]];

    keys.forEach((key, i) => {
      let value = Number.isInteger(key) ? values[key] : dict[key];
      result.push(value, strings[i + 1]);
    });

    return result.join('');
  });
}