]> git.ardis.lu - csvio/commitdiff
Set default normalization type to `'string'`
authorArdis Lu <ardisglu@gmail.com>
Thu, 10 Jul 2025 04:12:44 +0000 (21:12 -0700)
committerArdis Lu <ardisglu@gmail.com>
Thu, 10 Jul 2025 04:12:44 +0000 (21:12 -0700)
This behavior is helpful to perform minimal normalization tasks but otherwise preserve the default CSV transformation behavior (i.e., have all input values be a string).

This is not a breaking change. Previously, `type` would also default to `'string'` after logging a warning in console.

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

index 8ddbfbf17a2ca78e86e7e7ab4fe06b4c56d050e5..7f2fd3da180f7ab137d4ce6eea3d840b7cce9d9c 100644 (file)
@@ -70,8 +70,8 @@ export function expandScientificNotation(str, truncate = false) {
  * @property {string} name The name of the CSV column, in camelCase. Values in the header row of the CSV will be
  * transformed to camelCase for comparison purposes and for downstream usage (e.g., a header with the value `Example Column`
  * in the input CSV will match with a `name` value of `exampleColumn`).
- * @property {'string'|'number'|'bigint'|'date'} type The JavaScript data type to attempt to cast this column to. If
- * the data cannot be unmangled, the data passes through as a `string`.
+ * @property {'string'|'number'|'bigint'|'date'} [type='string'] The JavaScript data type to attempt to cast this column to. If
+ * the data cannot be unmangled, the data passes through as a `string`. The default value is `'string'`.
  * @property {string} [displayName=name] Optional `string` value to indicate the desired header name in the output CSV.
  * @property {string} [defaultValue] Optional `string` value to use to fill empty CSV fields. If no value is provided, the
  * field value will be `''` (empty string) in the output CSV.
@@ -123,7 +123,7 @@ export class CSVNormalizer extends TransformStream {
     this.#typeCastOnly = options.typeCastOnly ?? false;
 
     for (const { name, type, displayName = name, defaultValue = null } of headers) {
-      let normalizedType = type.toLowerCase();
+      let normalizedType = type?.toLowerCase() ?? 'string';
       if (!['string', 'number', 'bigint', 'date'].includes(normalizedType)) {
         console.warn(`Type "${normalizedType}" is not supported, defaulting to string.`);
         normalizedType = 'string';
index 16ec5589153385b4a297f83eec445798a61e6aad..1d355c17fc6b5c5153d909329ef5a6847ec93bfb 100644 (file)
@@ -272,6 +272,21 @@ suite('CSVNormalizer', { concurrency: true }, () => {
         ['a', 'b']
       ]));
   });
+  test('defaults to "string" type', { concurrency: true }, async () => {
+    await createCSVMockStream([
+      ['columnA', 'columnB'],
+      ['1E9', '44309']
+    ])
+      .pipeThrough(new CSVNormalizer([
+        { name: 'columnA' },
+        { name: 'columnB' }
+      ]))
+      .pipeThrough(new CSVDenormalizer())
+      .pipeTo(csvStreamEqualWritable([
+        ['columnA', 'columnB'],
+        ['1E9', '44309']
+      ]));
+  });
   test('can pass through numbers', { concurrency: true }, async () => {
     await createCSVMockStream([
       ['columnA', 'columnB'],
This page took 0.043901 seconds and 4 git commands to generate.