]> git.ardis.lu - csvio/commitdiff
Refactor to pass options through to private fields
authorArdis Lu <ardisglu@gmail.com>
Tue, 27 May 2025 05:01:57 +0000 (22:01 -0700)
committerArdis Lu <ardisglu@gmail.com>
Tue, 27 May 2025 05:01:57 +0000 (22:01 -0700)
This change makes the code consistent with previous refactoring on the core code, and also prevents unexpected behavior from mutating the options object after instantiation.

src/normalization.js

index 135245f8b4f51f6b5e55344d968137e35f2d47b2..9092d0b0092dfcce26e27b9ca012176d032d140c 100644 (file)
@@ -96,7 +96,8 @@ export function expandScientificNotation(str, truncate = false) {
 export class CSVNormalizer extends TransformStream {
   #columns = [];
   #firstChunk = true;
-  #options;
+  #passthroughEmptyRows;
+  #typeCastOnly;
 
   /**
    * @param {Array<CSVNormalizerHeader>} headers An array of metadata objects to configure the data casting and
@@ -108,9 +109,8 @@ export class CSVNormalizer extends TransformStream {
       transform: (chunk, controller) => this.#transform(chunk, controller)
     });
 
-    options.passthroughEmptyRows ??= false;
-    options.typeCastOnly ??= false;
-    this.#options = options;
+    this.#passthroughEmptyRows = options.passthroughEmptyRows ?? false;
+    this.#typeCastOnly = options.typeCastOnly ?? false;
 
     for (const { name, type, displayName = name, defaultValue = null } of headers) {
       let normalizedType = type.toLowerCase();
@@ -129,7 +129,6 @@ export class CSVNormalizer extends TransformStream {
   }
 
   #transform(chunk, controller) {
-    const { passthroughEmptyRows, typeCastOnly } = this.#options;
     const row = JSON.parse(chunk);
 
     // Assume first row is headers and use it to prepare the columns object
@@ -156,9 +155,9 @@ export class CSVNormalizer extends TransformStream {
       const emptyField = value === '' ? true : false;
       switch (type) {
         case 'string': 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;
+        case 'number': value = this.#typeCastOnly ? Number(value) : CSVNormalizer.fixExcelNumber(value); break;
+        case 'bigint': value = this.#typeCastOnly ? BigInt(value).toString() : CSVNormalizer.fixExcelBigInt(value); break;
+        case 'date': value = this.#typeCastOnly ? new Date(value).toISOString() : CSVNormalizer.fixExcelDate(value); break;
       }
       if (emptyField && defaultValue !== null) {
         value = defaultValue;
@@ -166,7 +165,7 @@ export class CSVNormalizer extends TransformStream {
       out.push({ name, displayName, value, emptyField });
     }
 
-    if (!passthroughEmptyRows && out.every(f => f.emptyField)) { return; }
+    if (!this.#passthroughEmptyRows && out.every(f => f.emptyField)) { return; }
 
     controller.enqueue(JSON.stringify(out));
   }
This page took 0.040956 seconds and 4 git commands to generate.