A JavaScript class, which finalises the HTML of each page of my NodeJS (Express) website Background

A request (req in the code) is made to my server, and a response (res) returned. By the time the response has filtered through to the code below, most of the heavy lifting - looking up entries in a database, etc - has already been done. This code just puts some final tweaks on all responses before it's pushed out to the user's browser.

What This Code Does

More specifically, the code achieves three things:


A time stamp is added to the footer of the page.
The crude kind of apostrophes are transmuted into the prettier, curlier kind. (This is more important in some fonts than in others.)
-- and --- are transmuted into en and em dashes respectively.


Entry Point

This code is fired only when code in another file calls finaliser.protoRender(req, res, view, properties);. So the protoRender method is the only entry point; it's also the only exit.

The Code

/*
This code contains a class which handles any final, universal touches to the
page before it's passed to the browser.
*/

// The class in question.
class Finaliser
{
  constructor()
  {

  }

  // Ronseal.
  static fixApostrophes(input)
  {
    while(input.indexOf("``") &gt;= 0)
    {
      input = input.replace("``", "&amp;ldquo;");
    }
    while(input.indexOf("''") &gt;= 0)
    {
      input = input.replace("''", "&amp;rdquo;");
    }
    while(input.indexOf("`") &gt;= 0)
    {
      input = input.replace("`", "&amp;lsquo;");
    }
    while(input.indexOf("'") &gt;= 0)
    {
      input = input.replace("'", "&amp;rsquo;");
    }
    return input;
  }

  // Ronseal.
  static fixDashes(input)
  {
    while(input.indexOf("---") &gt;= 0)
    {
      input = input.replace("---", "&amp;mdash;");
    }
    while(input.indexOf("--") &gt;= 0)
    {
      input = input.replace("--", "&amp;ndash;");
    }
    return input;
  }

  // Render, and deliver the page to the browser.
  protoRender(req, res, view, properties)
  {
    var date = new Date();

    properties.footstamp = date.toISOString();
    res.render(view, properties, function(err, html){
      if(html === undefined)
      {
        res.render(view, properties);
      }
      else
      {
        html = Finaliser.fixApostrophes(html);
        html = Finaliser.fixDashes(html);
        res.send(html);
      }
    });
  }
}

// Exports.
module.exports = Finaliser;
Ways of improving/optimizations:

What fixApostrophes and fixDashes functions actually try to do is replacing a specific punctuation chars with respective HTML entities.
Instead of those numerous horrifying while loops - a more optimized, concise and extendable approach would be to:


compose a predefined replacement map (where keys are search patterns and values - respective entities values):

const replaceMap = {"``": "&amp;ldquo;", "''": "&amp;rdquo;", "`": "&amp;lsquo;",
                    "'": "&amp;rsquo;", "---": "&amp;mdash;", "--": "&amp;ndash;"};

perform all the replacements at once
with String.replace function based on combined regex pattern:

input = input.replace(new RegExp(Object.keys(replaceMap).join('|'), 'g'), function(m){
    return replaceMap[m] || m;
});  


where Object.keys(replaceMap).join('|') is used to compose regex alternation group from replaceMap keys like ''|'|---|--
the former 2 functions can be conceptually combined into a single function called, say punctToEntities ("punctuations to entities")


Eventually, the Finaliser class would look as:

const replaceMap = {"``": "&amp;ldquo;", "''": "&amp;rdquo;", "`": "&amp;lsquo;",
                    "'": "&amp;rsquo;", "---": "&amp;mdash;", "--": "&amp;ndash;"};

class Finaliser
{
    constructor()
    {

    }

    static punctToEntities(input) {
    /** Converts punctuation chars to respective HTML entities **/
        input = input.replace(new RegExp(Object.keys(replaceMap).join('|'), 'g'), function(m){
            return replaceMap[m] || m;
        });  

        return input;
    }

    // Render, and deliver the page to the browser.
    protoRender(req, res, view, properties) {
        var date = new Date();

        properties.footstamp = date.toISOString();
        res.render(view, properties, function(err, html){
            if (html === undefined) {
                res.render(view, properties);
            } else {
                html = Finaliser.punctToEntities(html);
                res.send(html);
            }
        });
    }
}