]> git.ardis.lu - csvio/commitdiff
Passthrough encoding option for CSVReader
authorArdis Lu <ardisglu@gmail.com>
Thu, 23 Jan 2025 06:36:29 +0000 (22:36 -0800)
committerArdis Lu <ardisglu@gmail.com>
Thu, 23 Jan 2025 06:36:29 +0000 (22:36 -0800)
src/core.js
test/core.test.js
test/data/utf-16le.csv [new file with mode: 0644]

index f47fe908a96f289c8fc48d5a909e65918dca469c..84486c64ff997310d0c5d2e05fbcfc3cc037cbf2 100644 (file)
@@ -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)
index 2e3ceeb7eb4c3fe52570471b6006d6b878a54832..196dedf9f27a5eef60e71bc220cb13eeb5a4df61 100644 (file)
@@ -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 (file)
index 0000000..6c35335
Binary files /dev/null and b/test/data/utf-16le.csv differ
This page took 0.039726 seconds and 4 git commands to generate.