Member-only story

My First Useful Tagged Template Literal

Lee Mason
2 min readMay 22, 2019

--

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 string using something similar to literal syntax: /api/v1/{varName}. Where varName could be accessed directly off the Vue instance.

The problem with these 2 methods are:

  1. The array syntax feels awkward
  2. Option 2 (the clearer option) can only be used inside a component instance, not for example in a store module
  3. A limitation of option 2 is that it requires the “vars” used in the string be accessible on this
  4. Option 2 caught a few members of the team out, thinking it was actually a string literal when they started to reuse from examples

The (possible) Tagged Template Literal Solution

So i had a bit of an idea, it takes the existing concept further and addresses some of the issues.

  1. It does use a template literal with a tagged function

--

--

Lee Mason
Lee Mason

Written by Lee Mason

Director @GambitNashLtd. Laravel Artisan, WordPress Code Monkey, Node & Javascript Junkie.

No responses yet

Write a response