Since their inclusion in Javascript tagged template literals have had developers on edge about possible use cases for them. There are the obvious ones. For me they seemed soo useful. Yet I found myself looking for a problem to solve, instead of it being obvious they were the right tool for the job.
Very recently i think i’ve happened upon a problem they solve elegantly.
So the background story is I am working on a large scale SPA project. This project interfaces with an api server. Nothing unusual there.
What is unusual is the id
for a lot of these resources is actually user submitted text strings. They have to be unique, not include certain characters etc, but the key is "its user submitted text". We had no option to change this (at least for now).
This creates what is at least for me an unusual situation. The api resource urls can contain certain “must be encoded” characters (spaces, commas, full stops, accents).
The Existing Solution
Another lead developer on the project created a solution i’ve seen before to construct the url, and escape the correct variables using indexed replacement from an array.
['/api/v1/{0}/{1}', varName, anotherVar]
This solution works and we have been using it successfully for a while now.
Alongside this a VueJs mixin was created which did some further magic to make replacements directly on a…