domenic / count-to-6

An intro to some ES6 features via a set of self-guided workshops.
Other
325 stars 76 forks source link

Tagged Templates #15

Closed mdda closed 10 years ago

mdda commented 10 years ago

When I just ran through this, the tagged templates lesson was the last one. So wouldn't the following solution seem more like a 'finale' (though seems from the Issues you've already added that you plan adding more lessons) :

console.log(html`<b>${process.argv[2]} says</b>: "${process.argv[3]}"`);

function html(arr, ...vars) {
  var sanitize = 
    raw => raw
      .replace(/\&/g, "&amp;")
      .replace(/\'/g, "&#39;")
      .replace(/\"/g, "&#quot;")
      .replace(/\</g, "&#lt;")
      .replace(/\>/g, "&#gt;")
    ;

  return arr.splice(1).reduce(
    (acc, str, i) =>  acc + sanitize(vars[i]) + str
  , arr[0]);
}

Also (very minor) : Typo in #5 : "horrible arguments objet again"

Overall :: Very nice! Martin :-)

domenic commented 10 years ago

This seems much worse than the existing solution...

Although it does point out I should probably do the escape in the loop instead of doing substitutions = substitutions.map(escape).

I'll leave this open to track that fix and the typo. Thanks!

domenic commented 10 years ago

Fixed and released as 0.5.2. Thanks!

mdda commented 10 years ago

Thanks for responding so quickly.

On your first point, I guess my main idea was to use the arrow function creation syntax, since it's so straight-through. You're right that putting it outside the function makes sense for efficiency (at the expense of some name-space pollution).

On the second, Yes : Good point that .splice(2) is a whole array copy in Javascript, despite it aping .tail elsewhere...

Thanks again for the great nodeschool unit!

domenic commented 10 years ago

Note that this is module code, not global code, so only the local module is polluted


From: Martin Andrewsmailto:notifications@github.com Sent: ý2014-ý07-ý25 13:13 To: domenic/count-to-6mailto:count-to-6@noreply.github.com Cc: Domenic Denicolamailto:domenic@domenicdenicola.com Subject: Re: [count-to-6] Tagged Templates (#15)

Thanks for responding so quickly.

On your first point, I guess my main idea was to use the arrow function creation syntax, since it's so straight-through. You're right that putting it outside the function makes sense for efficiency (at the expense of some name-space pollution).

On the second, Yes : Good point that .splice(2) is a whole array copy in Javascript, despite it aping .tail elsewhere...

Thanks again for the great nodeschool unit!

— Reply to this email directly or view it on GitHubhttps://github.com/domenic/count-to-6/issues/15#issuecomment-50177882.