From ada767d3aca26c749e9d6cbdebf86310c6f70f2e Mon Sep 17 00:00:00 2001 From: Espen Hovlandsdal Date: Mon, 13 May 2024 09:04:04 -0700 Subject: [PATCH 1/2] fix: broken ndjson implementation Fixes #17 --- package.json | 4 +++- src/cli.js | 12 +++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index fc258cc..e20733b 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/src/cli.js b/src/cli.js index 7f03f76..664ed79 100644 --- a/src/cli.js +++ b/src/cli.js @@ -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' } } @@ -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} } From 679f05e95b2858cacce22224d882c1da97f7e30d Mon Sep 17 00:00:00 2001 From: Sindre Gulseth Date: Tue, 14 May 2024 14:00:11 +0200 Subject: [PATCH 2/2] fix(ndjson): iterate over streams correctly with ndjson --- package.json | 2 +- src/cli.js | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index e20733b..130bb03 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/src/cli.js b/src/cli.js index 664ed79..cf1afd8 100644 --- a/src/cli.js +++ b/src/cli.js @@ -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' } }