Log & Arrow

I’ve released a node module to solve the simplest, edgiest of cases that really bugs me during development.

Let’s say you have a case:

const someFunction = variable => ({
  object,
  full,
  of,
  keys
})

Debugging at this point is a little bit annoying because you’d have to pull apart the arrow function and return the object manually with a breakpoint in front. Additionally, you can’t just wrap the return in a console.log because it won’t return the original value.

Enter logandarrow.

const someFunction = variable => la()({
  object,
  full,
  of,
  keys
})

Looks pretty simple - hey. Log & Arrow sits directly before the return of your arrow function and produces the same output as usual, but offers a console.log output prior to the return.

You can also make use of log, error and trace functions with a prefix using a nice little constructor.

import { debug } from 'logandarrow';

let la = debug('some prefix');

la.log('some output');
la.trace(variable, otherVariable);

So, what’s in this mystery package and where do I get it?

module.exports = function(type = console.log) {
  if (typeof type !== 'function') {
    throw Error(`Output handler must be a function, you provided ${typeof type}.`);
  }
  return function(variable) {
    var prefix = typeof variable;
    if (typeof variable !== 'object' && typeof variable.name !== 'undefined') {
      prefix = variable.name;
    }
    type(`[${prefix}]`, variable);
    return variable;
  }
}

You can get it on NPM:
npm install logandarrow

The source code is over on GitHub:
https://github.com/OpenClubDev/logandarrow

 
0
Kudos
 
0
Kudos

Now read this

Using Web Workers to run JavaScript in parallel

JavaScript, in the browser, runs in a single thread. This is fine, and works fairly well for most websites. However, when running large, complex tasks or long scripts, it makes the webpage unresponsive. The best way to speed up these... Continue →