From 11feae32e67a6b2e37a74ec5be88be05ac3f13e3 Mon Sep 17 00:00:00 2001 From: Lucas Pickering Date: Fri, 5 Mar 2021 15:29:29 -0500 Subject: [PATCH] Fix trailing space in singleLine mode A bug in #158 left a trailing space on lines that had no extra log fields with singleLine=true. This commit fixes that, and updates a test accordingly. --- index.js | 9 +++++++-- test/basic.test.js | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index c13a6066..05204c6a 100644 --- a/index.js +++ b/index.js @@ -132,8 +132,8 @@ module.exports = function prettyFactory (options) { } } - if (line.length > 0) { - line += (singleLine ? ' ' : EOL) + if (line.length > 0 && !singleLine) { + line += EOL } if (log.type === 'Error' && log.stack) { @@ -157,6 +157,11 @@ module.exports = function prettyFactory (options) { singleLine, colorizer }) + + // In single line mode, include a space only if prettified version isn't empty + if (singleLine && !/^\s$/.test(prettifiedObject)) { + line += ' ' + } line += prettifiedObject } diff --git a/test/basic.test.js b/test/basic.test.js index 3984f9fb..f2c88f3b 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -724,7 +724,7 @@ test('basic prettifier tests', (t) => { const log = pino({}, new Writable({ write (chunk, enc, cb) { const formatted = pretty(chunk.toString()) - t.is(formatted, `[${epoch}] INFO (${pid} on ${hostname}): message \n`) + t.is(formatted, `[${epoch}] INFO (${pid} on ${hostname}): message\n`) cb() } }))