Skip to content

Commit 230af8e

Browse files
authored
fix(csv-parse): ship stream cjs export (#490)
* fix: ship csv-parse stream cjs export * test: cover csv-parse stream in cjs demo
1 parent 46cab38 commit 230af8e

6 files changed

Lines changed: 2130 additions & 1 deletion

File tree

‎demo/cjs/lib/parse_stream.js‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const assert = require("node:assert");
2+
const version = parseInt(/^v(\d+)/.exec(process.version)[1], 10);
3+
const { parse } = require(
4+
version >= 14 ? "csv-parse/stream" : "csv-parse/dist/cjs/stream.cjs",
5+
);
6+
7+
(async function () {
8+
const output = [];
9+
const parser = parse({
10+
delimiter: ":",
11+
});
12+
const writer = parser.writable.getWriter();
13+
const reader = parser.readable.getReader();
14+
const encoder = new TextEncoder();
15+
const read = (async function () {
16+
while (true) {
17+
const { value, done } = await reader.read();
18+
if (done) break;
19+
output.push(value);
20+
}
21+
})();
22+
23+
await writer.write(encoder.encode("a:b:c\n"));
24+
await writer.write(encoder.encode("1:2:3\n"));
25+
await writer.close();
26+
await read;
27+
28+
assert.deepStrictEqual(output, [
29+
["a", "b", "c"],
30+
["1", "2", "3"],
31+
]);
32+
})().catch(function (err) {
33+
console.error(err);
34+
process.exitCode = 1;
35+
});

‎demo/cjs/lib/parse_stream.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import assert from "node:assert";
2+
// Node.js >= 14
3+
import { parse } from "csv-parse/stream";
4+
// Node.js < 14
5+
// import { parse } from 'csv-parse/dist/cjs/stream'
6+
7+
(async function () {
8+
const output: string[][] = [];
9+
const parser = parse({
10+
delimiter: ":",
11+
});
12+
const writer = parser.writable.getWriter();
13+
const reader = parser.readable.getReader();
14+
const encoder = new TextEncoder();
15+
const read = (async function () {
16+
while (true) {
17+
const { value, done } = await reader.read();
18+
if (done) break;
19+
output.push(value);
20+
}
21+
})();
22+
23+
await writer.write(encoder.encode("a:b:c\n"));
24+
await writer.write(encoder.encode("1:2:3\n"));
25+
await writer.close();
26+
await read;
27+
28+
assert.deepStrictEqual(output, [
29+
["a", "b", "c"],
30+
["1", "2", "3"],
31+
]);
32+
})().catch(function (err: unknown) {
33+
console.error(err);
34+
process.exitCode = 1;
35+
});

0 commit comments

Comments
 (0)