1

I have this object:

{
  services:
   { 'account-service': 1,
     'activity-service': 1,
     'cancellation-service': 1,
     'chat-service': 1,
     'integrator-service': 1,
     'lh-app': 1,
     'notification-service': 1,
     'patient-web-app': 0,   // make this line red if 0
     'reminder-service': 1,
     'rest-service': 1,
     'shortener-service': 1,
     'socket-service': 1,
     'tunnel-service': 1,
     'web-app': 1 } }}

if one of the numbers is 0, I would like to be red instead of yellow. Anyone know of a way to create a custom util.inspect function that can do this?

enter image description here

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • Could you adapt [techniques like this](https://stackoverflow.com/questions/7505623/colors-in-javascript-console)? – tadman Oct 12 '18 at 20:39

2 Answers2

4

When you JSON.stringify the output from util.inspect with its colors option set to true. For instance;

console.log('%s', JSON.stringify(util.inspect(obj, { colors: true })));

it reveals the ANSI color/formatting codes applied.

Given your patient-web-app property with its value set to 0 you'll see it is formatted as:

\u001b[32m'patient-web-app'\u001b[39m: \u001b[33m0\u001b[39m,\n

Consider using a regexp to perform a replacement. For instance:

const util = require('util');

const obj = {
  services: {
    'account-service': 1,
    'activity-service': 1,
    'cancellation-service': 1,
    'chat-service': 1,
    'integrator-service': 1,
    'lh-app': 1,
    'notification-service': 1,
    'patient-web-app': 0,   // make this line red if 0
    'reminder-service': 1,
    'rest-service': 1,
    'shortener-service': 1,
    'socket-service': 1,
    'tunnel-service': 1,
    'web-app': 1
  }
}

const colorizedZeroValues = JSON.parse(JSON.stringify(util.inspect(obj, { colors: true }))
      .replace(/(: \\u001b)(\[33m)(0\\u001b\[39m,\\n)/g, '$1[31m$3'));

console.log('%s', colorizedZeroValues);

Prints

enter image description here

RobC
  • 22,977
  • 20
  • 73
  • 80
  • yeah I am not a fan of regex in general, it's hard to update 2 months later when you no longer understand the regex...I wonder if there is a formatter "callback" function we can use – Alexander Mills Oct 22 '18 at 19:42
1

You can use the util.inspect.custom symbol to return a custom string that will be logged to the console as-is when inspect is called on it.

Here is the official documentation.

Example: custom console.table rendering

console.table actually calls inspect on every cell value to render. If you want to have custom rendering on a cell, just make it an object with the [util.inspect.custom] property returning your custom string:

import colors from 'colors/safe';  // i want to use custom colors

// ...

// utilty function to make any result render as-is
function overrideInspect(result) {
  return {
    [util.inspect.custom](depth, options) {
      return result;
    }
  };
}


function renderUsers(users) {
  const table = [];
  for (const user of users) {
    // ... get all our data ...
    let modInfo = '';
    if (role >= ModeratorRole) {
      modInfo = colors.yellow(' M');
    }

    // add to table
    table.push({
      name: overrideInspect(displayName + modInfo), // append yellow `M` to white name if it's a mod
      email,
      signup,
      stats,
      uid
    });
  }

  // render table
  console.table(table, [
    'name',
    'email',
    'signup',
    'stats',
    'uid'
  ]);
}
Domi
  • 22,151
  • 15
  • 92
  • 122