From 7f19af0e0ac9fa678ef64e4bbf5694ee5f245649 Mon Sep 17 00:00:00 2001 From: Zirak Date: Thu, 15 Aug 2019 20:24:10 +0300 Subject: [PATCH] Handle timestamp 0 in prettifyTime (#76) When trying to format a log object like the following: ```json { "level": 30, "time": 0, "name": "foobar", "msg": "I like cake", "v": 1 } ``` pino-pretty previously tried to format the timestamp `undefined` instead of taking the `0` time. This commit looks not at the truthiness of the value, but its existence. --- lib/utils.js | 15 ++++++++++++--- test/lib/utils.public.test.js | 10 ++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index da7e0636..a05963e1 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -326,9 +326,18 @@ function prettifyObject ({ * string. */ function prettifyTime ({ log, timestampKey = TIMESTAMP_KEY, translateFormat = undefined }) { - if (timestampKey in log === false && 'timestamp' in log === false) return undefined + let time = null + + if (timestampKey in log) { + time = log[timestampKey] + } else if ('timestamp' in log) { + time = log.timestamp + } + + if (time === null) return undefined if (translateFormat) { - return '[' + formatTime(log[timestampKey] || log.timestamp, translateFormat) + ']' + return '[' + formatTime(time, translateFormat) + ']' } - return `[${log[timestampKey] || log.timestamp}]` + + return `[${time}]` } diff --git a/test/lib/utils.public.test.js b/test/lib/utils.public.test.js index 4e330cb7..f3bc35ed 100644 --- a/test/lib/utils.public.test.js +++ b/test/lib/utils.public.test.js @@ -252,5 +252,15 @@ tap.test('prettifyTime', t => { t.is(str, '[2019-04-07T09:15:00.000-04:00]') }) + t.test('handles the 0 timestamp', async t => { + let log = { time: 0 } + let str = prettifyTime({ log }) + t.is(str, '[0]') + + log = { timestamp: 0 } + str = prettifyTime({ log }) + t.is(str, '[0]') + }) + t.end() })