From 2dba8b2d17eb0b6459ef3b47985a66c4aa35752f Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Tue, 14 Jan 2025 14:24:13 +0100 Subject: [PATCH] fixup Signed-off-by: Matteo Collina --- lib/colors.js | 10 +++++++--- lib/utils/prettify-error-log.test.js | 4 +++- lib/utils/prettify-object.js | 2 +- lib/utils/prettify-object.test.js | 16 ++++++++++++++-- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/colors.js b/lib/colors.js index f135c338..321b7b7e 100644 --- a/lib/colors.js +++ b/lib/colors.js @@ -10,13 +10,14 @@ const plain = { 20: nocolor, 10: nocolor, message: nocolor, - greyMessage: nocolor + greyMessage: nocolor, + property: nocolor } const { createColors } = require('colorette') const getLevelLabelData = require('./utils/get-level-label-data') const availableColors = createColors({ useColor: true }) -const { white, bgRed, red, yellow, green, blue, gray, cyan } = availableColors +const { white, bgRed, red, yellow, green, blue, gray, cyan, magenta } = availableColors const colored = { default: white, @@ -27,7 +28,8 @@ const colored = { 20: blue, 10: gray, message: cyan, - greyMessage: gray + greyMessage: gray, + property: magenta } function resolveCustomColoredColorizer (customColors) { @@ -56,6 +58,7 @@ function plainColorizer (useOnlyCustomProps) { } customColoredColorizer.message = plain.message customColoredColorizer.greyMessage = plain.greyMessage + customColoredColorizer.property = plain.property customColoredColorizer.colors = createColors({ useColor: false }) return customColoredColorizer } @@ -66,6 +69,7 @@ function coloredColorizer (useOnlyCustomProps) { return newColoredColorizer(level, colored, opts) } customColoredColorizer.message = colored.message + customColoredColorizer.property = colored.property customColoredColorizer.greyMessage = colored.greyMessage customColoredColorizer.colors = availableColors return customColoredColorizer diff --git a/lib/utils/prettify-error-log.test.js b/lib/utils/prettify-error-log.test.js index 04e10f82..3c9ab093 100644 --- a/lib/utils/prettify-error-log.test.js +++ b/lib/utils/prettify-error-log.test.js @@ -2,6 +2,7 @@ const tap = require('tap') const prettifyErrorLog = require('./prettify-error-log') +const colors = require('../colors') const { ERROR_LIKE_KEYS, MESSAGE_KEY @@ -13,7 +14,8 @@ const context = { customPrettifiers: {}, errorLikeObjectKeys: ERROR_LIKE_KEYS, errorProps: [], - messageKey: MESSAGE_KEY + messageKey: MESSAGE_KEY, + objectColorizer: colors() } tap.test('returns string with default settings', async t => { diff --git a/lib/utils/prettify-object.js b/lib/utils/prettify-object.js index 5de0f5cb..b6ee0fcd 100644 --- a/lib/utils/prettify-object.js +++ b/lib/utils/prettify-object.js @@ -92,7 +92,7 @@ function prettifyObject ({ lines = lines.replace(/\\\\/gi, '\\') const joinedLines = joinLinesWithIndentation({ input: lines, ident, eol }) - result += `${ident}${keyName}:${joinedLines.startsWith(eol) ? '' : ' '}${joinedLines}${eol}` + result += `${ident}${objectColorizer.property(keyName)}:${joinedLines.startsWith(eol) ? '' : ' '}${joinedLines}${eol}` }) } diff --git a/lib/utils/prettify-object.test.js b/lib/utils/prettify-object.test.js index 2dde4a9a..9612eaf9 100644 --- a/lib/utils/prettify-object.test.js +++ b/lib/utils/prettify-object.test.js @@ -6,7 +6,6 @@ const prettifyObject = require('./prettify-object') const { ERROR_LIKE_KEYS } = require('../constants') -const getColorizer = require('../colors') const context = { EOL: '\n', @@ -15,7 +14,7 @@ const context = { errorLikeObjectKeys: ERROR_LIKE_KEYS, objectColorizer: colors(), singleLine: false, - colorizer: getColorizer() + colorizer: colors() } tap.test('returns empty string if no properties present', async t => { @@ -152,3 +151,16 @@ tap.test('errors skips prettifying if no lines are present', async t => { }) t.equal(str, '') }) + +tap.test('works with single level properties', async t => { + const colorizer = colors(true) + const str = prettifyObject({ + log: { foo: 'bar' }, + context: { + ...context, + objectColorizer: colorizer, + colorizer + } + }) + t.equal(str, ` ${colorizer.colors.magenta('foo')}: "bar"\n`) +})