]> git.ardis.lu - csvio/commitdiff
Fall back on `name` if `displayName` is undefined
authorArdis Lu <ardisglu@gmail.com>
Thu, 10 Jul 2025 04:33:12 +0000 (21:33 -0700)
committerArdis Lu <ardisglu@gmail.com>
Thu, 10 Jul 2025 04:33:12 +0000 (21:33 -0700)
This behavior is useful when passing rows to `CSVDenormalizer` after a transformation *other* than `CSVNormalizer` (which automatically creates `displayName`).

src/normalization.js
test/normalization.test.js
test/utils.js

index 7f2fd3da180f7ab137d4ce6eea3d840b7cce9d9c..45a864ae72dda01cf7e92adff238ced15a41ee92 100644 (file)
@@ -308,7 +308,7 @@ export class CSVDenormalizer extends TransformStream {
         const row = JSON.parse(chunk);
         if (firstRow) {
           firstRow = false;
-          controller.enqueue(JSON.stringify(row.map(f => f.displayName)));
+          controller.enqueue(JSON.stringify(row.map(f => f?.displayName ?? f.name)));
         }
         controller.enqueue(JSON.stringify(row.map(f => f.value)));
       }
index 1d355c17fc6b5c5153d909329ef5a6847ec93bfb..c12577c28c0c81551f15a53a92f5c6615570e75c 100644 (file)
@@ -352,6 +352,33 @@ suite('CSVNormalizer', { concurrency: true }, () => {
   });
 });
 
+suite('CSVDenormalizer', { concurrency: true }, () => {
+  test('can process normalized rows', { concurrency: true }, async () => {
+    await createCSVMockStream([
+      [{ displayName: 'Column A', value: 'a1' }, { displayName: 'Column B', value: 'b1' }],
+      [{ displayName: 'Column A', value: 'a2' }, { displayName: 'Column B', value: 'b2' }]
+    ])
+      .pipeThrough(new CSVDenormalizer())
+      .pipeTo(csvStreamEqualWritable([
+        ['Column A', 'Column B'],
+        ['a1', 'b1'],
+        ['a2', 'b2']
+      ]));
+  });
+  test('falls back on name if displayName is undefined', { concurrency: true }, async () => {
+    await createCSVMockStream([
+      [{ name: 'Column A', value: 'a1' }, { displayName: 'Column B', value: 'b1' }],
+      [{ displayName: 'Column A', value: 'a2' }, { name: 'Column B', value: 'b2' }]
+    ])
+      .pipeThrough(new CSVDenormalizer())
+      .pipeTo(csvStreamEqualWritable([
+        ['Column A', 'Column B'],
+        ['a1', 'b1'],
+        ['a2', 'b2']
+      ]));
+  });
+});
+
 suite('CSVNormalizer and CSVDenormalizer end-to-end', { concurrency: true }, () => {
   test('can normalize', { concurrency: true }, async () => {
     const headers = [
index a56f73966f23fcb3131ec54f27e4d881275009c2..b14a1c4bb67fac136f1f25e49ec9d735db23ab0f 100644 (file)
@@ -132,8 +132,8 @@ export function assertSleep(context, expected) {
 /**
  * Simulate a stream of CSV data for test mocking purposes.
  * 
- * @param {Array<Array<string>>} data A 2-D array representing the CSV file to mock, where each inner array
- * is a row of the CSV.
+ * @param {Array<Array<string>>|Array<Array<Object>>} data A 2-D array representing the CSV file to mock, where
+ * each inner array is a row of the CSV.
  * @returns {ReadableStream} A `ReadableStream` where each row is emitted as a separate chunk.
  */
 export function createCSVMockStream(data) {
This page took 0.038406 seconds and 4 git commands to generate.