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
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();
}
#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
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;
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));
}