From: Ardis Lu Date: Thu, 10 Jul 2025 04:12:44 +0000 (-0700) Subject: Set default normalization type to `'string'` X-Git-Tag: v0.1.3~9 X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=cebe35586202309ec0364c12325f29733dee3b79;p=csvio Set default normalization type to `'string'` This behavior is helpful to perform minimal normalization tasks but otherwise preserve the default CSV transformation behavior (i.e., have all input values be a string). This is not a breaking change. Previously, `type` would also default to `'string'` after logging a warning in console. --- diff --git a/src/normalization.js b/src/normalization.js index 8ddbfbf..7f2fd3d 100644 --- a/src/normalization.js +++ b/src/normalization.js @@ -70,8 +70,8 @@ export function expandScientificNotation(str, truncate = false) { * @property {string} name The name of the CSV column, in camelCase. Values in the header row of the CSV will be * transformed to camelCase for comparison purposes and for downstream usage (e.g., a header with the value `Example Column` * in the input CSV will match with a `name` value of `exampleColumn`). - * @property {'string'|'number'|'bigint'|'date'} type The JavaScript data type to attempt to cast this column to. If - * the data cannot be unmangled, the data passes through as a `string`. + * @property {'string'|'number'|'bigint'|'date'} [type='string'] The JavaScript data type to attempt to cast this column to. If + * the data cannot be unmangled, the data passes through as a `string`. The default value is `'string'`. * @property {string} [displayName=name] Optional `string` value to indicate the desired header name in the output CSV. * @property {string} [defaultValue] Optional `string` value to use to fill empty CSV fields. If no value is provided, the * field value will be `''` (empty string) in the output CSV. @@ -123,7 +123,7 @@ export class CSVNormalizer extends TransformStream { this.#typeCastOnly = options.typeCastOnly ?? false; for (const { name, type, displayName = name, defaultValue = null } of headers) { - let normalizedType = type.toLowerCase(); + let normalizedType = type?.toLowerCase() ?? 'string'; if (!['string', 'number', 'bigint', 'date'].includes(normalizedType)) { console.warn(`Type "${normalizedType}" is not supported, defaulting to string.`); normalizedType = 'string'; diff --git a/test/normalization.test.js b/test/normalization.test.js index 16ec558..1d355c1 100644 --- a/test/normalization.test.js +++ b/test/normalization.test.js @@ -272,6 +272,21 @@ suite('CSVNormalizer', { concurrency: true }, () => { ['a', 'b'] ])); }); + test('defaults to "string" type', { concurrency: true }, async () => { + await createCSVMockStream([ + ['columnA', 'columnB'], + ['1E9', '44309'] + ]) + .pipeThrough(new CSVNormalizer([ + { name: 'columnA' }, + { name: 'columnB' } + ])) + .pipeThrough(new CSVDenormalizer()) + .pipeTo(csvStreamEqualWritable([ + ['columnA', 'columnB'], + ['1E9', '44309'] + ])); + }); test('can pass through numbers', { concurrency: true }, async () => { await createCSVMockStream([ ['columnA', 'columnB'],