Skip to content

Commit

Permalink
fix: broken ndjson implementation
Browse files Browse the repository at this point in the history
Fixes #17
  • Loading branch information
rexxars authored and sgulseth committed May 14, 2024
1 parent c726590 commit 1fe0a76
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
"node": ">=18"
},
"scripts": {
"test": "echo '[{\"foo\": 1}]' | ./bin/groq.js --pretty '*[]{ foo }'",
"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 '*[]'",
"lint": "eslint . && prettier --check ./src ./bin"
},
"files": [
Expand Down
12 changes: 7 additions & 5 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,15 @@ async function* outputPrettyJSON(result) {
}

async function* outputNDJSON(result) {
if (result.getType() == 'array') {
for await (const value of result) {
yield JSON.stringify(await value.get())
const value = await result.get()

if (Array.isArray(value)) {
for await (const row of value) {
yield JSON.stringify(row)
yield '\n'
}
} else {
yield JSON.stringify(await result.get())
yield JSON.stringify(value)
yield '\n'
}
}
Expand All @@ -129,7 +131,7 @@ async function inputJSON() {
}

function inputNDJSON() {
const dataset = new S2A(process.stdin.pipe(ndjson()))
const dataset = new S2A(process.stdin.pipe(ndjson.parse()))
return {dataset}
}

Expand Down

0 comments on commit 1fe0a76

Please sign in to comment.