Skip to content

Commit

Permalink
Pass through non-object valid JSON, fixes #72 (#81)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
timmarinin authored and mcollina committed Oct 8, 2019
1 parent d1fd01b commit 71935ea
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Expand Up @@ -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
}
Expand Down
17 changes: 17 additions & 0 deletions test/cli.test.js
Expand Up @@ -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()
})

0 comments on commit 71935ea

Please sign in to comment.