From: Ardis Lu Date: Tue, 27 May 2025 05:01:57 +0000 (-0700) Subject: Refactor to pass options through to private fields X-Git-Tag: v0.1.0~12 X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=974b1102aa0530fd039d0794daf48602ead4a253;p=csvio 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. --- 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)); }