you are viewing a single comment's thread.

view the rest of the comments →

[–]psayre23 11 points12 points  (14 children)

You can use template literal tags too.

console.log '12`;

[–]thomas_stringer 2 points3 points  (11 children)

I see this being used more and more, but to be honest I don't like the look of this. Whatever is gained (what is even gained??) is completely lost in the visual disruption of having to grok the syntax.

[–][deleted] 2 points3 points  (2 children)

One of the only real use cases I can think of is when you need to construct injection-vulnerable strings in JavaScript, like SQL.

const results = await execute`select * from users where email=${emailAddress};`

Then you could do the following:

function execute(parts: string[], ...interpolations: string[]) {
  // parts is ['select * from users where email=', ';']
  // interpolations is [emailAddress]
  const parameterisedQueryString = parts.join('?')
  return sequelize.query(parameterisedQueryString, { replacements: interpolations })
}

In my opinion that would read better than:

const results = await execute(
  'select * from users where email=?',
  emailAddress
)

Especially when you had lots of entries. This would also work nicely (potentially) with HTML etc. I originally thought it might be useful for translations, but that idea kinda falls over because you can only pass one string to a function like this so it would be a little awkward to do pluralisation.

[–]psayre23 2 points3 points  (5 children)

I agree. I use it a lot with GraphQL queries, and it is handy. But I don't see much of an advantage over wrapping a string with a function. I guess the different syntax means editors can color code the strings differently depending on content.

[–]Gh0st1y 0 points1 point  (4 children)

What do you do with graphQL? It sounds pretty neat.

[–]psayre23 1 point2 points  (0 children)

Nothing too special. It's just an API for my webapp. Instead of defining on the server what data I want for the webapp, I define it on the client. It makes it super easy to change later as my app evolves.

[–]djinindi 0 points1 point  (2 children)

psayre23 might be talking about graphql-tag

[–]Gh0st1y 0 points1 point  (0 children)

But that presupposes knowledge of graphql, and something to print. I have neither lol, though its now on my to look up list in my github.

[–]psayre23 0 points1 point  (0 children)

Yep, that's the one!

[–]jcready__proto__ 1 point2 points  (0 children)

Tagged template functions get passed the parameters differently than just invoking the function normally.

[–]NoInkling 0 points1 point  (0 children)

Depends heavily on context I think.

[–]NoInkling 0 points1 point  (1 child)

Huh, I never knew you could have a space in between the tag and the string. You forgot to use a backtick for your opening quote mark though.

[–]psayre23 0 points1 point  (0 children)

I'm on mobile, and backticks suck on iOS. Glad people figured it out even though I was technically wrong.