From 7e9a1634c8e6e9cab1f952e21a1d2a55acc2de40 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Sun, 26 Oct 2025 23:08:16 -0700 Subject: [PATCH] Add explicit types --- examples/ex1_1.js | 2 ++ examples/ex1_2.js | 2 ++ examples/ex1_5.js | 3 +++ examples/ex1_7.js | 2 ++ examples/ex1_9.js | 3 +++ examples/ex4_2.js | 2 ++ 6 files changed, 14 insertions(+) diff --git a/examples/ex1_1.js b/examples/ex1_1.js index 11389cd..e376ace 100644 --- a/examples/ex1_1.js +++ b/examples/ex1_1.js @@ -9,7 +9,9 @@ // - 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]; } diff --git a/examples/ex1_2.js b/examples/ex1_2.js index 68ac864..13b8f51 100644 --- a/examples/ex1_2.js +++ b/examples/ex1_2.js @@ -7,7 +7,9 @@ // - 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])]; } diff --git a/examples/ex1_5.js b/examples/ex1_5.js index 0d49b52..4796d69 100644 --- a/examples/ex1_5.js +++ b/examples/ex1_5.js @@ -8,13 +8,16 @@ // 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); diff --git a/examples/ex1_7.js b/examples/ex1_7.js index abfadd3..1ba78fb 100644 --- a/examples/ex1_7.js +++ b/examples/ex1_7.js @@ -7,8 +7,10 @@ // 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(); diff --git a/examples/ex1_9.js b/examples/ex1_9.js index e4c10a9..ed41521 100644 --- a/examples/ex1_9.js +++ b/examples/ex1_9.js @@ -9,12 +9,15 @@ 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]; } diff --git a/examples/ex4_2.js b/examples/ex4_2.js index 7764201..707a1b7 100644 --- a/examples/ex4_2.js +++ b/examples/ex4_2.js @@ -5,6 +5,7 @@ // 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 @@ -18,6 +19,7 @@ const headers = [ 'sparse3' ]; +/** @type {TransformationFunction} */ function transform(row) { let c = 0 for (const field of row) { -- 2.47.3