// - The expected output of the transformation function is another array representing 1 row of the output CSV.
import { CSVReader, CSVTransformer, CSVWriter } from '../src/index.js';
+/** @import { TransformationFunction } from '../src/index.js'; */
+/** @type {TransformationFunction} */
function timesTwo(row) {
return [Number(row[0]) * 2, Number(row[1]) * 2];
}
// - Setting `handleHeaders` to an array is the simplest way to rename headers, delete headers, or add new headers.
import { CSVReader, CSVTransformer, CSVWriter } from '../src/index.js';
+/** @import { TransformationFunction } from '../src/index.js'; */
+/** @type {TransformationFunction} */
function sum(row) {
return [...row, Number(row[0]) + Number(row[1])];
}
// or other arbitrary logic is required to determine the output header names.
import { CSVReader, CSVTransformer, CSVWriter } from '../src/index.js';
+/** @import { TransformationFunction } from '../src/index.js'; */
// It is useful to split the header transformation into a separate function because the logic for the header
// is likely different from the rest of the CSV.
+/** @type {TransformationFunction} */
function handleHeaders(headers) {
return [...headers, 'min', 'max', 'mean']; // Duplicate input headers as the first headers in the output
}
+/** @type {TransformationFunction} */
function calculate(row) {
const numbers = row.map(Number);
const min = Math.min(...numbers);
// Aborting throws an error, so you must also add a `.catch()` to handle the abort.
import { CSVReader, CSVTransformer, CSVWriter } from '../src/index.js';
+/** @import { TransformationFunction } from '../src/index.js'; */
const controller = new AbortController();
+/** @type {TransformationFunction} */
function identityWithAbort(row) {
if (row[0] === 'abort') { // Arbitrary logic for aborting the transformation
controller.abort();
import { createWriteStream } from 'node:fs';
import { Writable } from 'node:stream';
+import { DecompressionStream, TextDecoderStream, CompressionStream } from 'node:stream/web';
import { CSVReader, CSVTransformer, CSVWriter } from '../src/index.js';
+/** @import { TransformationFunction } from '../src/index.js'; */
import { createFileStream } from '../src/core.js';
import { setGzipOSByteToUnknown } from '../test/utils.js';
+/** @type {TransformationFunction} */
function timesTwo(row) {
return [Number(row[0]) * 2, Number(row[1]) * 2];
}
// common CSV mangling by spreadsheet programs.
import { CSVReader, CSVWriter, CSVNormalizer, CSVDenormalizer, CSVTransformer } from '../src/index.js';
+/** @import { TransformationFunction } from '../src/index.js'; */
/** @import { CSVNormalizerHeader } from '../src/normalization.js'; */
// A minimal declaration to tell `CSVNormalizer` to trim empty rows and columns but otherwise don't touch the data
'sparse3'
];
+/** @type {TransformationFunction} */
function transform(row) {
let c = 0
for (const field of row) {