]> git.ardis.lu - csvio/commitdiff
Accept `string` as a shorthand for `{ name: string }`
authorArdis Lu <ardisglu@gmail.com>
Sun, 19 Oct 2025 04:17:43 +0000 (21:17 -0700)
committerArdis Lu <ardisglu@gmail.com>
Sun, 19 Oct 2025 04:17:43 +0000 (21:17 -0700)
It is common to set headers using all the default values, so it is useful to have a shorthand for this situation.

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

index bc3cd09b02f9edb5ffe91cac7044a16955bc1437..32a57f9c45e42eae8e52fa93d8b5481991db3276 100644 (file)
@@ -115,8 +115,8 @@ export class CSVNormalizer extends TransformStream {
   #typeCastOnly;
 
   /**
-   * @param {Array<CSVNormalizerHeader>} headers An array of metadata objects to configure the data casting and
-   * cleanup transformations.
+   * @param {Array<CSVNormalizerHeader|string>} headers An array of `CSVNormalizerHeader` or `string` to configure the
+   * data casting and cleanup transformations. Passing a plain `string` is the same as passing `{ name: string }`.
    * @param {CSVNormalizerOptions} options Object containing flags to configure the stream logic. 
    */
   constructor(headers, options = {}) {
@@ -128,19 +128,32 @@ export class CSVNormalizer extends TransformStream {
     this.#passthroughEmptyRows = options.passthroughEmptyRows ?? false;
     this.#typeCastOnly = options.typeCastOnly ?? false;
 
-    for (const { name, type, displayName = name, defaultValue = '' } of headers) {
-      let normalizedType = type?.toLowerCase() ?? 'string';
-      if (!['string', 'number', 'bigint', 'date'].includes(normalizedType)) {
-        console.warn(`Type "${normalizedType}" is not supported, defaulting to string.`);
-        normalizedType = 'string';
+    for (const header of headers) {
+      if (typeof header === 'string') {
+        const name = this.#useLiteralNames ? header : toCamelCase(header);
+        this.#columns.push({
+          name,
+          type: 'string',
+          displayName: name,
+          defaultValue: '',
+          index: null
+        });
+      }
+      else {
+        const { name, type, displayName = name, defaultValue = '' } = header;
+        let normalizedType = type?.toLowerCase() ?? 'string';
+        if (!['string', 'number', 'bigint', 'date'].includes(normalizedType)) {
+          console.warn(`Type "${normalizedType}" is not supported, defaulting to string.`);
+          normalizedType = 'string';
+        }
+        this.#columns.push({
+          name: this.#useLiteralNames ? name : toCamelCase(name),
+          type: normalizedType,
+          displayName,
+          defaultValue,
+          index: null
+        });
       }
-      this.#columns.push({
-        name: this.#useLiteralNames ? name : toCamelCase(name),
-        type: normalizedType,
-        displayName,
-        defaultValue,
-        index: null
-      });
     }
   }
 
index d71e0ae094da26e526d715f24684541d97bcb585..d96e04b564ca18a7589731b362c008789befe2f1 100644 (file)
@@ -252,6 +252,33 @@ suite('CSVNormalizer.toFieldMap', { concurrency: true }, () => {
 });
 
 suite('CSVNormalizer', { concurrency: true }, () => {
+  test('accepts string shorthand to set headers', { concurrency: true }, async () => {
+    await createCSVMockStream([
+      ['columnA', 'columnB'],
+      ['a', 'b']
+    ])
+      .pipeThrough(new CSVNormalizer(['columnA', 'columnB']))
+      .pipeThrough(new CSVDenormalizer())
+      .pipeTo(csvStreamEqualWritable([
+        ['columnA', 'columnB'],
+        ['a', 'b']
+      ]));
+  });
+  test('accepts mixed string and CSVNormalizerHeader to set headers', { concurrency: true }, async () => {
+    await createCSVMockStream([
+      ['columnA', 'columnB'],
+      ['a', 'b']
+    ])
+      .pipeThrough(new CSVNormalizer([
+        'columnA',
+        { name: 'columnB' }
+      ]))
+      .pipeThrough(new CSVDenormalizer())
+      .pipeTo(csvStreamEqualWritable([
+        ['columnA', 'columnB'],
+        ['a', 'b']
+      ]));
+  });
   test('can remove extra columns in CSV', { concurrency: true }, async () => {
     await createCSVMockStream([
       ['extra1', 'columnA', 'extra2', 'extra3', 'columnB'],
This page took 0.042442 seconds and 4 git commands to generate.