]> git.ardis.lu - csvio/commitdiff
Refactor to simplify functions
authorArdis Lu <ardisglu@gmail.com>
Sun, 16 Feb 2025 07:38:41 +0000 (23:38 -0800)
committerArdis Lu <ardisglu@gmail.com>
Sun, 16 Feb 2025 07:38:41 +0000 (23:38 -0800)
test/normalization.test.js
test/utils.js

index 81342ceff2c59824b5c4a9a202d62175ac163f4b..83986518815e511dc0837e4e7e63e4b4a1133e9b 100644 (file)
@@ -1,7 +1,7 @@
 import { suite, test } from 'node:test';
 import { deepStrictEqual } from 'node:assert/strict';
 
-import { csvStreamEqual, csvStreamEqualWritable, createCSVMockStream, assertConsole } from './utils.js';
+import { csvStreamEqualWritable, createCSVMockStream, assertConsole } from './utils.js';
 import { CSVReader } from '../src/core.js';
 import { toCamelCase, expandScientificNotation, CSVNormalizer, CSVDenormalizer } from '../src/normalization.js';
 
@@ -278,14 +278,14 @@ suite('CSVNormalizer and CSVDenormalizer end-to-end', { concurrency: true }, ()
       { name: 'bigintCol', displayName: 'BigInt Column', type: 'bigint' },
       { name: 'dateCol', displayName: 'Date Column', type: 'date' }
     ]
-    const stream = new CSVReader('./test/data/normalization.csv')
+    await new CSVReader('./test/data/normalization.csv')
       .pipeThrough(new CSVNormalizer(headers))
-      .pipeThrough(new CSVDenormalizer());
-    await csvStreamEqual(stream, [
-      ['String Column', 'Number Column', 'BigInt Column', 'Date Column'],
-      ['abc ๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ', 123456789.123456, '1000000000000000000', '2024-01-01T00:00:00.000Z'],
-      [', " ๐Ÿดโ€โ˜ ๏ธ', 1000, '-1234567890000000000000000', '2024-06-01T00:00:00.000Z'],
-      ['N/A', 123100, '1000000000000000000', '2024-12-31T08:00:00.000Z']
-    ]);
+      .pipeThrough(new CSVDenormalizer())
+      .pipeTo(csvStreamEqualWritable([
+        ['String Column', 'Number Column', 'BigInt Column', 'Date Column'],
+        ['abc ๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ', 123456789.123456, '1000000000000000000', '2024-01-01T00:00:00.000Z'],
+        [', " ๐Ÿดโ€โ˜ ๏ธ', 1000, '-1234567890000000000000000', '2024-06-01T00:00:00.000Z'],
+        ['N/A', 123100, '1000000000000000000', '2024-12-31T08:00:00.000Z']
+      ]));
   });
 });
\ No newline at end of file
index ea1a7c45fb4bdfaa3b9883c3c64595f3421b0118..7fdb41fe69d189d58135f283867b30deb5af0d90 100644 (file)
@@ -5,50 +5,8 @@ import { normalize } from 'node:path';
 /** @import { TestContext } from 'node:test'; */
 
 /**
- * Log an entire stream along with the field count for each row and the total row count.
- * 
- * @param {ReadableStream} stream A stream of CSV data.
- * @param {string} [id] Optional label to include in the log.
- */
-export async function csvStreamLog(stream, id) {
-  let out = '============================================================\n';
-  out += `\x1b[45m${id ?? ''}\x1b[0m`;
-  out += '\n------------------------------------------------------------\n';
-  let totalRows = 0;
-  for await (const value of stream) {
-    const row = JSON.parse(value);
-    const rowNoNewlines = row.map(f => f.replaceAll('\n', '\\n').replaceAll('\r', '\\r'));
-    out += `${row.length} || ${rowNoNewlines.join(' | ')}\n`;
-    totalRows++;
-  }
-  out += '------------------------------------------------------------\n';
-  out += `Total row count: ${totalRows}\n`;
-  out += '============================================================';
-  console.log(out);
-}
-
-/**
- * Compare each field in a CSV stream with a given array, assuming each chunk of the stream is a
- * serialized `Array<any>` representing a row of a 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
- * is a row of the CSV.
- */
-export async function csvStreamEqual(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++) {
-      deepStrictEqual(row[j], csv[i][j]);
-    }
-    i++;
-  }
-}
-
-/**
- * A `WritableStream` version of `csvStreamEqual`. Compares each field in a CSV stream with a given
- * array, assuming each chunk of the stream is a serialized `Array<any>` representing a row of a CSV.
+ * A `WritableStream` sink that compares each field in a CSV stream with a given array, assuming each chunk of the
+ * stream is a serialized `Array<any>` representing a row of a CSV.
  * 
  * @param {Array<Array<any>>} csv A 2-D array representing the CSV file, where each inner array
  * is a row of the CSV.
@@ -67,23 +25,6 @@ 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.
  * 
This page took 0.038409 seconds and 4 git commands to generate.