- **Fix date and number mangling**: Spreadsheet programs may [automatically cast data types in unexpected ways](https://www.theverge.com/2020/8/6/21355674/human-genes-rename-microsoft-excel-misreading-dates). `CSVNormalizer` will perform various transformations to reverse data type casting that was unlikely to be intentional.
- **Flexible column naming**: Column names are normalized to camelCase to prevent issues with subtly inconsistent names (e.g., `Column A`, `column A`, and `ColumnA` are all considered to be the same name).
- **Column ordering**: Columns may appear in any order on the input CSV. The columns in the output are re-ordered to match the order in the `CSVNormalizerHeader` array.
-- **Stream of objects**: The stream is transformed from an `Array<string>` to an `Array<CSVNormalizerRow>` to provide a better developer experience for subsequent transformations.
+- **Stream of objects**: The stream is transformed from an `Array<string>` to an `Array<CSVNormalizerField>` to provide a better developer experience for subsequent transformations.
-`CSVDenormalizer` is a `TransformStream` that will convert an `Array<CSVNormalizerRow>` created by `CSVNormalizer` back into an `Array<string>`, ready for writing to a file.
+`CSVDenormalizer` is a `TransformStream` that will convert an `Array<CSVNormalizerField>` created by `CSVNormalizer` back into an `Array<string>`, ready for writing to a file.
```javascript
import { CSVNormalizer, CSVDenormalizer } from '@ardislu/csvio';
/** @typedef {import('./core.js').CSVWriterStatus} CSVWriterStatus */
/** @typedef {import('./normalization.js').CSVNormalizerHeader} CSVNormalizerHeader */
/** @typedef {import('./normalization.js').CSVNormalizerOptions} CSVNormalizerOptions */
-/** @typedef {import('./normalization.js').CSVNormalizerRow} CSVNormalizerRow */
+/** @typedef {import('./normalization.js').CSVNormalizerField} CSVNormalizerField */
/** @typedef {import('./errorStrategies.js').RetryOptions} RetryOptions */
/** @typedef {import('./errorStrategies.js').BackoffOptions} BackoffOptions */
/**
* The output of `CSVNormalizer`.
- * @typedef {Object} CSVNormalizerRow
+ * @typedef {Object} CSVNormalizerField
* @property {string} name The column name after normalization to camelCase.
* @property {string} [displayName] The desired column name in the output CSV.
* @property {string|number} value The value of the field after attempted data casting. If the original field was empty, this value
}
}
- /** @type {import('node:stream/web').TransformerTransformCallback<Array<string>,Array<Required<CSVNormalizerRow>>>} */
+ /** @type {import('node:stream/web').TransformerTransformCallback<Array<string>,Array<Required<CSVNormalizerField>>>} */
#transform(chunk, controller) {
// Assume first row is headers and use it to prepare the columns object
// Note: the headers row is NOT forwarded downstream
return;
}
- /** @type {Array<Required<CSVNormalizerRow>>} */
+ /** @type {Array<Required<CSVNormalizerField>>} */
const out = [];
for (const { name, type, displayName, defaultValue, index } of this.#columns) {
let value = chunk[index] ?? '';
}
/**
- * A `TransformStream` to transform a `CSVNormalizerRow` into an array that can be converted to CSV data.
+ * A `TransformStream` to transform a `Array<CSVNormalizerField>` into an array that can be converted to CSV data.
* @extends TransformStream
*/
export class CSVDenormalizer extends TransformStream {
constructor() {
let firstRow = true;
super({
- /** @type {import('node:stream/web').TransformerTransformCallback<Array<CSVNormalizerRow>,Array<string>>} */
+ /** @type {import('node:stream/web').TransformerTransformCallback<Array<CSVNormalizerField>,Array<string>>} */
transform(chunk, controller) {
if (firstRow) {
firstRow = false;