]> git.ardis.lu - csvio/commitdiff
Replace passthrough options with typeCastOnly
authorArdis Lu <ardisglu@gmail.com>
Thu, 13 Feb 2025 07:45:40 +0000 (23:45 -0800)
committerArdis Lu <ardisglu@gmail.com>
Thu, 13 Feb 2025 07:45:40 +0000 (23:45 -0800)
To replicate old behavior (passthrough as string), simply use `type: "string"`.

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

index b7faa553294747605266ce8619b22916543a0d86..3debefe00a9ebb5621306d5b484a490c63261de9 100644 (file)
@@ -67,12 +67,8 @@ export function expandScientificNotation(str, truncate = false) {
  * @typedef {Object} CSVNormalizerOptions
  * @property {boolean} [passthroughEmptyRows=false] Set to `true` to send empty rows (i.e., rows where all field values are
  * `''`) downstream. Otherwise, empty rows will be removed. The default value is `false`.
- * @property {boolean} [passthroughNumber=false] Set to `true` to NOT apply data casting to `type='number'` columns. Otherwise,
- * there will be an attempt to cast columns with `type='number'` to a `number`. The default value is `false`.
- * @property {boolean} [passthroughBigInt=false] Set to `true` to NOT apply data casting to `type='bigint'` columns. Otherwise,
- * there will be an attempt to cast columns with `type='bigint'` to a `BigInt`. The default value is `false`.
- * @property {boolean} [passthroughDate=false] Set to `true` to NOT apply data casting to `type='date'` columns. Otherwise,
- * there will be an attempt to cast columns with `type='date'` to a `Date`. The default value is `false`.
+ * @property {boolean} [typeCastOnly=false] Set to `true` to NOT apply any error correction to 'number', `bigint`, or `date`
+ * columns. Only type casting will be applied to the data. The default value is `false`.
  */
 
 /**
@@ -108,9 +104,7 @@ export class CSVNormalizer extends TransformStream {
     });
 
     options.passthroughEmptyRows ??= false;
-    options.passthroughNumber ??= false;
-    options.passthroughBigInt ??= false;
-    options.passthroughDate ??= false;
+    options.typeCastOnly ??= false;
     this.#options = options;
 
     for (const { name, type, displayName = name, defaultValue = null } of headers) {
@@ -130,7 +124,7 @@ export class CSVNormalizer extends TransformStream {
   }
 
   #transform(chunk, controller) {
-    const { passthroughNumber, passthroughBigInt, passthroughDate, passthroughEmptyRows } = this.#options;
+    const { passthroughEmptyRows, typeCastOnly } = this.#options;
     const row = JSON.parse(chunk);
 
     // Assume first row is headers and use it to prepare the columns object
@@ -157,9 +151,9 @@ export class CSVNormalizer extends TransformStream {
       const emptyField = value === '' ? true : false;
       switch (type) {
         case 'string': break;
-        case 'number': value = passthroughNumber ? value : CSVNormalizer.fixExcelNumber(value); break;
-        case 'bigint': value = passthroughBigInt ? value : CSVNormalizer.fixExcelBigInt(value); break;
-        case 'date': value = passthroughDate ? value : CSVNormalizer.fixExcelDate(value); break;
+        case 'number': value = typeCastOnly ? Number(value) : CSVNormalizer.fixExcelNumber(value); break;
+        case 'bigint': value = typeCastOnly ? BigInt(value).toString() : CSVNormalizer.fixExcelBigInt(value); break;
+        case 'date': value = typeCastOnly ? new Date(value).toISOString() : CSVNormalizer.fixExcelDate(value); break;
       }
       if (emptyField && defaultValue !== null) {
         value = defaultValue;
index 1f01b8fed7bb1dbc4646d74628421b9c32d83b14..81342ceff2c59824b5c4a9a202d62175ac163f4b 100644 (file)
@@ -215,11 +215,11 @@ suite('CSVNormalizer', { concurrency: true }, () => {
       .pipeThrough(new CSVNormalizer([
         { name: 'columnA', type: 'number' },
         { name: 'columnB', type: 'number' }
-      ], { passthroughNumber: true }))
+      ], { typeCastOnly: true }))
       .pipeThrough(new CSVDenormalizer())
       .pipeTo(csvStreamEqualWritable([
         ['columnA', 'columnB'],
-        ['123', '456']
+        [123, 456]
       ]));
   });
   test('can pass through BigInt', { concurrency: true }, async () => {
@@ -230,7 +230,7 @@ suite('CSVNormalizer', { concurrency: true }, () => {
       .pipeThrough(new CSVNormalizer([
         { name: 'columnA', type: 'bigint' },
         { name: 'columnB', type: 'bigint' }
-      ], { passthroughBigInt: true }))
+      ], { typeCastOnly: true }))
       .pipeThrough(new CSVDenormalizer())
       .pipeTo(csvStreamEqualWritable([
         ['columnA', 'columnB'],
@@ -245,11 +245,11 @@ suite('CSVNormalizer', { concurrency: true }, () => {
       .pipeThrough(new CSVNormalizer([
         { name: 'columnA', type: 'date' },
         { name: 'columnB', type: 'date' }
-      ], { passthroughDate: true }))
+      ], { typeCastOnly: true }))
       .pipeThrough(new CSVDenormalizer())
       .pipeTo(csvStreamEqualWritable([
         ['columnA', 'columnB'],
-        ['2025-02-10', '2025-02-09']
+        ['2025-02-10T00:00:00.000Z', '2025-02-09T00:00:00.000Z']
       ]));
   });
   test('can ignore incorrect data types', { concurrency: true }, async (t) => {
This page took 0.065627 seconds and 4 git commands to generate.