json-colorizer icon indicating copy to clipboard operation
json-colorizer copied to clipboard

Allowing giving specific colors to certain JSON object keys/values

Open mdedetrich opened this issue 5 years ago • 2 comments

The basic idea is to add an ability that lets you give specific colors to specific keys for any JSON object that you happen to colorize. For example you can do something like

console.log(colorize({ "uri": "localhost:3000", "status":404}, {
  colors: {
    JSON_OBJECT_CUSTOM: function (key, value, currentJsonObject) {
      if (key === 'status' && currentJsonObject.hasOwnProperty('uri')) {
          if (value.toString.startsWith('2')) {
             return 'green';
          } else if (value.toString.startsWith('3') {
             return 'orange';
          } else if (value.toString.startsWith('4') {
             return 'red';
          } else if (value.toString.startsWith('5') {
             return 'red';
          }
          return null; // If we return null then we just use original color.
      }
    };
  }
}));

Would you be open to such functionality or is this considered scope creep?

mdedetrich avatar Jun 23 '20 08:06 mdedetrich

That would be really cool!

joeattardi avatar Jun 23 '20 14:06 joeattardi

Awesome, I will look try and cook up a PR when I have time (this week or more likely next week).

mdedetrich avatar Jun 25 '20 17:06 mdedetrich