Skip to content

Commit

Permalink
fix(ndjson): iterate over streams correctly with ndjson
Browse files Browse the repository at this point in the history
  • Loading branch information
sgulseth committed May 14, 2024
1 parent 1fe0a76 commit 8e55835
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"scripts": {
"test": "npm run test:json && npm run test:ndjson",
"test:json": "echo '[{\"foo\": 1}]' | ./bin/groq.js --pretty '*[]{ foo }'",
"test:ndjson": "echo '{\"foo\": 1}\n{\"bar\": 2}' | ./bin/groq.js -n --pretty '*[]'",
"test:ndjson": "echo '{\"foo\": [1,2,3,4]}\n{\"bar\": [2]}' | ./bin/groq.js -n --pretty '*[].foo'",
"lint": "eslint . && prettier --check ./src ./bin"
},
"files": [
Expand Down
10 changes: 4 additions & 6 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,13 @@ async function* outputPrettyJSON(result) {
}

async function* outputNDJSON(result) {
const value = await result.get()

if (Array.isArray(value)) {
for await (const row of value) {
yield JSON.stringify(row)
if (result.type == 'stream') {
for await (const value of result) {
yield JSON.stringify(await value.get())
yield '\n'
}
} else {
yield JSON.stringify(value)
yield JSON.stringify(await result.get())
yield '\n'
}
}
Expand Down

0 comments on commit 8e55835

Please sign in to comment.