]> git.ardis.lu - csvio/commitdiff
Move arrayToCSVString to be a static method of CSVWriter
authorArdis Lu <ardisglu@gmail.com>
Sun, 19 Jan 2025 07:25:37 +0000 (23:25 -0800)
committerArdis Lu <ardisglu@gmail.com>
Sun, 19 Jan 2025 07:25:37 +0000 (23:25 -0800)
This function is useful as part of the external API to make it easier to generate raw outputs for low-level transformations.

src/core.js
test/core.test.js

index f9f476226cc31b65d52c927df9b3d0c67ebe51c0..e13a5782e827a24a17d3d31f3e78578b22de061f 100644 (file)
@@ -5,26 +5,6 @@ import { Readable } from 'node:stream';
 import { fileURLToPath } from 'node:url';
 /** @import { PathLike } from 'node:fs' */
 
-/**
- * Convert a JavaScript array into a CSV string.
- * 
- * @param {Array<string>} arr An array of strings representing one row of a CSV file.
- * @returns {string} A string representing one row of a CSV file, with character escaping and line endings compliant with
- * [RFC 4180](https://www.ietf.org/rfc/rfc4180.txt).
- */
-export function arrayToCSVString(arr) {
-  let out = '';
-  for (const field of arr) {
-    if (/[,"\r\n]/g.test(field)) {
-      out += `"${field.replaceAll('"', '""')}",`;
-    }
-    else {
-      out += `${field},`;
-    }
-  }
-  return `${out.slice(0, -1)}\r\n`;
-}
-
 /**
  * Parse and normalize a `PathLike` object into a string.
  * 
@@ -303,7 +283,7 @@ export class CSVWriter extends WritableStream {
       async write(chunk) {
         let data;
         try {
-          data = arrayToCSVString(JSON.parse(chunk));
+          data = CSVWriter.arrayToCSVString(JSON.parse(chunk));
         }
         catch {
           data = chunk;
@@ -315,4 +295,24 @@ export class CSVWriter extends WritableStream {
       }
     });
   }
+
+  /**
+  * Convert a JavaScript array into a CSV string.
+  * 
+  * @param {Array<string>} arr An array of strings representing one row of a CSV file.
+  * @returns {string} A string representing one row of a CSV file, with character escaping and line endings compliant with
+  * [RFC 4180](https://www.ietf.org/rfc/rfc4180.txt).
+  */
+  static arrayToCSVString(arr) {
+    let out = '';
+    for (const field of arr) {
+      if (/[,"\r\n]/g.test(field)) {
+        out += `"${field.replaceAll('"', '""')}",`;
+      }
+      else {
+        out += `${field},`;
+      }
+    }
+    return `${out.slice(0, -1)}\r\n`;
+  }
 }
index eddc5d197cf7fa799bbd74d4b5689904660d4e4d..2e3ceeb7eb4c3fe52570471b6006d6b878a54832 100644 (file)
@@ -5,9 +5,9 @@ import { normalize, resolve } from 'node:path';
 import { pathToFileURL } from 'node:url';
 
 import { csvStreamEqualWritable, csvStreamNotEqualWritable, createCSVMockStream, createTempFile } from './utils.js';
-import { arrayToCSVString, parsePathLike, CSVReader, CSVTransformer, CSVWriter } from '../src/core.js';
+import { parsePathLike, CSVReader, CSVTransformer, CSVWriter } from '../src/core.js';
 
-suite('arrayToCSVString', { concurrency: true }, () => {
+suite('CSVWriter.arrayToCSVString', { concurrency: true }, () => {
   const vectors = [
     { name: 'converts simple row', input: ['abc', '123'], output: 'abc,123\r\n' },
     { name: 'converts row with commas', input: ['a,bc', '12,3'], output: '"a,bc","12,3"\r\n' },
@@ -20,7 +20,7 @@ suite('arrayToCSVString', { concurrency: true }, () => {
   ];
   for (const { name, input, output } of vectors) {
     test(name, { concurrency: true }, () => {
-      deepStrictEqual(arrayToCSVString(input), output);
+      deepStrictEqual(CSVWriter.arrayToCSVString(input), output);
     });
   }
 });
This page took 0.051393 seconds and 4 git commands to generate.