example of template literal tags with suitescript logging

by Gerald Gillespie

HTML

<code>
  usage:<br />
  // put this in important places<br />
  const D = AUDIT`some string`;
  <br />
  // follow it up with calls to D<br />
  D(anyvariable); // title will be 'some string'
</code>
<h1>
  pretend this is the system log
</h1>
<table>
  <tr>
    <th class="method">Method</th>
    <th class="title">Title</th>
    <th class="details">Details</th>
  </tr>
</table>
<table id="log">
  
</table>

CSS

td {
   border: 1px solid;
 }
 
 .method {
   width: 150px;
 }
 .title{
   width: 150px;
 }
 
 .details{
   width: 100%;
 }

JavaScript

const X = {};
const LOG = document.getElementById('log');
const define = ([m], cb) => Object.values(cb(X[m])).pop()();
const log = {
  generic({
    title,
    details,
    method
  }) {
    const newEntry = document.createElement('tr');
    newEntry.innerHTML = [
      `<td class="method">${method}</td>`,
      `<td class="title">${title}</td>`,
      `<td class="details"><textarea cols=80 rows=5>${JSON.stringify({title,details},null,2)}</textarea></td>`
    ].join('');
    LOG.prepend(newEntry);
    //  console.info('audit',{title,details});
  },
  debug({
    title,
    details
  }) {
    log.generic({
      title,
      details,
      method: 'debug'
    });
    //console.info('debug', {title,details});
  },
  audit({
    title,
    details
  }) {
    log.generic({
      title,
      details,
      method: 'audit'
    });
    //console.info('debug', {title,details});
  }

};

X.myLogger = {

  AUDIT([title, ...strings], ...parameters) {
    const details = [title, ...strings].reduce((acc, x) => ({
      ...acc,
      [x]: parameters.shift()
    }), Object.create({}));

    let x = 0;
    log.audit({
      title,
      details
    });
    return (details) => {
      log.debug({
        title: `${title}:${x++}`,
        details
      });
      return details;
    }
  },
  D(anything) {
    log.debug({
      title: 'logging',
      details: anything
    });
    return anything;
  }
};

/******************** Some suitesript let's pretend */
define(['myLogger'], ({
  AUDIT
}) => {

  const beforeLoad = (context) => {
    const D = AUDIT `hi`;
    const {
      foo
    } = D({
      foo: 'bar'
    });


    return D(foo);
  };
  
  return {
    beforeLoad
  };
});