From 0f9bd5abf6447ec1f95ce67b8e6dce98553d6965 Mon Sep 17 00:00:00 2001 From: Tim Marinin Date: Sun, 6 Oct 2019 03:22:05 +0300 Subject: [PATCH] Pass through non-object valid JSON, fixes #72 * Add test for passing through double-quoted string from JSON.stringify(new Date()) * Check for parsed value being JS object, otherwise just fallthrough as on parse error --- index.js | 4 ++-- test/cli.test.js | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 2c9b020b..5a9db5bb 100644 --- a/index.js +++ b/index.js @@ -55,11 +55,11 @@ module.exports = function prettyFactory (options) { let log if (!isObject(inputData)) { const parsed = jsonParser(inputData) - log = parsed.value - if (parsed.err) { + if (parsed.err || !isObject(parsed.value)) { // pass through return inputData + EOL } + log = parsed.value } else { log = inputData } diff --git a/test/cli.test.js b/test/cli.test.js index 80afadf6..caf06f8e 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -76,5 +76,22 @@ test('cli', (t) => { t.tearDown(() => child.kill()) }) + t.test('passes through stringified date as string', (t) => { + t.plan(1) + const child = spawn(process.argv0, [bin]) + child.on('error', t.threw) + + const date = JSON.stringify(new Date(epoch)) + + child.stdout.on('data', (data) => { + t.is(data.toString(), date + '\n') + }) + + child.stdin.write(date) + child.stdin.write('\n') + + t.tearDown(() => child.kill()) + }) + t.end() })