From: Ardis Lu Date: Thu, 23 Jan 2025 06:36:29 +0000 (-0800) Subject: Passthrough encoding option for CSVReader X-Git-Tag: v0.1.0~91 X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=30ba142f4d7b6ef648d48ee4485be6d6a054ef0d;p=csvio Passthrough encoding option for CSVReader --- diff --git a/src/core.js b/src/core.js index f47fe90..84486c6 100644 --- a/src/core.js +++ b/src/core.js @@ -27,6 +27,16 @@ export function parsePathLike(path) { } } +// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/e9296eba83c97aa1fc5d2689ad0539da9427977f/types/node/buffer.d.ts#L246-L258 +/** @typedef {"ascii"|"utf8"|"utf-8"|"utf16le"|"utf-16le"|"ucs2"|"ucs-2"|"base64"|"base64url"|"latin1"|"binary"|"hex"} BufferEncoding */ + +/** + * Options to configure `CSVReader`. + * @typedef {Object} CSVReaderOptions + * @property {BufferEncoding} [encoding='utf-8'] The character encoding of the input CSV file. Can be any value accepted by + * Node.js's [`Buffer`](https://nodejs.org/api/buffer.html#buffers-and-character-encodings). The default value is `utf-8`. + */ + /** * A simple streaming parser for CSV files. * @@ -46,9 +56,11 @@ export class CSVReader extends ReadableStream { /** * @param {PathLike} path A `string`, `Buffer`, or `URL` representing a path to a local CSV file. + * @param {CSVReaderOptions} options Object containing flags to configure the reader. */ - constructor(path) { - const readStream = createReadStream(path, { encoding: 'utf-8' }); + constructor(path, options = {}) { + const { encoding = 'utf-8' } = options; + const readStream = createReadStream(path, { encoding }); const readableStream = Readable.toWeb(readStream).pipeThrough(new TransformStream({ transform: (chunk, controller) => this.#transform(chunk, controller), flush: (controller) => this.#flush(controller) diff --git a/test/core.test.js b/test/core.test.js index 2e3ceeb..196dedf 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -147,6 +147,16 @@ suite('CSVReader', { concurrency: true }, () => { ['12', '34'] ] }, + { + name: 'parses UTF-16 LE CSV', + input: './test/data/utf-16le.csv', + options: { encoding: 'utf-16le' }, + output: [ + ['column1', 'column2'], + ['ab', 'cd'], + ['12', '34'] + ] + }, { name: 'parses single value (no commas or newlines) CSV', input: './test/data/single-value.csv', @@ -181,11 +191,11 @@ suite('CSVReader', { concurrency: true }, () => { ] } ]; - for (const { name, input, output, negativeOutput } of vectors) { + for (const { name, input, options = {}, output, negativeOutput } of vectors) { test(name, { concurrency: true }, async () => { - await new CSVReader(input).pipeTo(csvStreamEqualWritable(output)); + await new CSVReader(input, options).pipeTo(csvStreamEqualWritable(output)); if (negativeOutput !== undefined) { - await new CSVReader(input).pipeTo(csvStreamNotEqualWritable(negativeOutput)); + await new CSVReader(input, options).pipeTo(csvStreamNotEqualWritable(negativeOutput)); } }); } diff --git a/test/data/utf-16le.csv b/test/data/utf-16le.csv new file mode 100644 index 0000000..6c35335 Binary files /dev/null and b/test/data/utf-16le.csv differ