From 974b1102aa0530fd039d0794daf48602ead4a253 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Mon, 26 May 2025 22:01:57 -0700 Subject: [PATCH] Refactor to pass options through to private fields 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 | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/normalization.js b/src/normalization.js index 135245f..9092d0b 100644 --- a/src/normalization.js +++ b/src/normalization.js @@ -96,7 +96,8 @@ export function expandScientificNotation(str, truncate = false) { export class CSVNormalizer extends TransformStream { #columns = []; #firstChunk = true; - #options; + #passthroughEmptyRows; + #typeCastOnly; /** * @param {Array} 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)); } -- 2.47.3