]> git.ardis.lu - csvio/commitdiff
Push one empty row if CSV is blank
authorArdis Lu <ardisglu@gmail.com>
Wed, 8 Jan 2025 06:24:25 +0000 (22:24 -0800)
committerArdis Lu <ardisglu@gmail.com>
Wed, 8 Jan 2025 06:24:25 +0000 (22:24 -0800)
Added a negative test to catch this edge case because it was not caught with the positive test case.

src/core.js
test/core.test.js
test/utils.js

index 209198798539cbee6b395f60b8cdbffdba0c6dd4..18920cb25c7e31b5b53166d102c681ba982fb30a 100644 (file)
@@ -95,6 +95,9 @@ export function createCSVReadableStream(path) {
         row.push(field); // Last field is always un-pushed if CSV terminated with nothing
         controller.enqueue(JSON.stringify(row));
       }
+      else if (lastChar === null) { // CSV is blank
+        controller.enqueue(JSON.stringify(['']));
+      }
     }
   }));
 }
index fe960b3ce484f61ce2122860ac51d1b7ac106503..e52b6bc5942f15f6f5c4f611194cbd4650e7ae2e 100644 (file)
@@ -2,7 +2,7 @@ import { suite, test } from 'node:test';
 import { deepStrictEqual } from 'node:assert/strict';
 import { unlink } from 'node:fs/promises';
 
-import { csvStreamEqualWritable, createCSVMockStream, createTempFile } from './utils.js';
+import { csvStreamEqualWritable, csvStreamNotEqualWritable, createCSVMockStream, createTempFile } from './utils.js';
 import { arrayToCSVString, createCSVReadableStream, createCSVTransformStream, createCSVWritableStream } from '../src/core.js';
 
 suite('arrayToCSVString', { concurrency: true }, () => {
@@ -146,12 +146,18 @@ suite('createCSVReadableStream', { concurrency: true }, () => {
       input: './test/data/blank.csv',
       output: [
         ['']
+      ],
+      negativeOutput: [
+        ['a']
       ]
     }
   ];
-  for (const { name, input, output } of vectors) {
+  for (const { name, input, output, negativeOutput } of vectors) {
     test(name, { concurrency: true }, async () => {
       await createCSVReadableStream(input).pipeTo(csvStreamEqualWritable(output));
+      if (negativeOutput !== undefined) {
+        await createCSVReadableStream(input).pipeTo(csvStreamNotEqualWritable(negativeOutput));
+      }
     });
   }
 });
index d95aa4c8bc8d7bb135056d8de3f31cac606361ab..8d725c9858a553ecb6a6b4e7d41a9d3be2b2572b 100644 (file)
@@ -1,4 +1,4 @@
-import { deepStrictEqual } from 'node:assert/strict';
+import { deepStrictEqual, notStrictEqual } from 'node:assert/strict';
 import { tmpdir } from 'node:os';
 import { open } from 'node:fs/promises';
 import { normalize } from 'node:path';
@@ -66,6 +66,43 @@ export function csvStreamEqualWritable(csv) {
   });
 }
 
+/**
+ * Negative version of `csvStreamEqual`. Tests that the given stream is NOT equal to the given CSV.
+ * 
+ * @param {ReadableStream} stream A stream of CSV data.
+ * @param {Array<Array<any>>} csv A 2-D array representing the CSV file, where each inner array
+ */
+export async function csvStreamNotEqual(stream, csv) {
+  let i = 0;
+  for await (const value of stream) {
+    const row = JSON.parse(value);
+    for (let j = 0; j < csv[i].length; j++) {
+      notStrictEqual(row[j], csv[i][j]);
+    }
+    i++;
+  }
+}
+
+/**
+ * Negative version of `csvStreamEqualWritable`. Tests that the piped stream is NOT equal to the given CSV.
+ * 
+ * @param {Array<Array<string>>} csv A 2-D array representing the CSV file, where each inner array
+ * is a row of the CSV.
+ * @returns {WritableStream} A `WritableStream` sink of a CSV stream that will be compared against the given array.
+ */
+export function csvStreamNotEqualWritable(csv) {
+  let i = 0;
+  return new WritableStream({
+    write(chunk) {
+      const row = JSON.parse(chunk);
+      for (let j = 0; j < csv[i].length; j++) {
+        notStrictEqual(row[j], csv[i][j]);
+      }
+      i++;
+    }
+  });
+}
+
 /**
  * Simulate a stream of CSV data for test mocking purposes.
  * 
This page took 0.04387 seconds and 4 git commands to generate.