1function template(strings, ...keys) {
2 return (function(...values) {
3 var dict = values[values.length - 1] || {};
4 var result = [strings[0]];
5 keys.forEach(function(key, i) {
6 var value = Number.isInteger(key) ? values[key] : dict[key];
7 result.push(value, strings[i + 1]);
8 });
9 return result.join('');
10 });
11}
12
13var t1Closure = template`${0}${1}${0}!`;
14t1Closure('Y', 'A'); // "YAY!"
15var t2Closure = template`${0} ${'foo'}!`;
16t2Closure('Hello', {foo: 'World'}); // "Hello World!"
17
1var a = 5;
2var b = 10;
3
4function tag(strings, ...values) {
5 console.log(strings[0]); // "Hello "
6 console.log(strings[1]); // " world "
7 console.log(values[0]); // 15
8 console.log(values[1]); // 50
9
10 return "Bazinga!";
11}
12
13tag`Hello ${ a + b } world ${ a * b}`;
14// "Bazinga!"
15