From a47c38ca29db8e4e6861302f733283c38c628518 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Sat, 18 Jan 2025 23:25:37 -0800 Subject: [PATCH] Move arrayToCSVString to be a static method of CSVWriter 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 | 42 +++++++++++++++++++++--------------------- test/core.test.js | 6 +++--- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/core.js b/src/core.js index f9f4762..e13a578 100644 --- a/src/core.js +++ b/src/core.js @@ -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} 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} 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`; + } } diff --git a/test/core.test.js b/test/core.test.js index eddc5d1..2e3ceeb 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -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); }); } }); -- 2.47.3