UNPKG

@naturalcycles/nodejs-lib

Version:
36 lines 1.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const csvStringify = require("csv-stringify/lib/sync"); const stream_1 = require("stream"); /** * Transforms objects (objectMode=true) into chunks \n-terminated JSON strings (readableObjectMode=false). */ function transformToCSV(opt = {}) { const { strict = true } = opt; let { header } = opt; return new stream_1.Transform({ objectMode: true, readableObjectMode: false, transform(chunk, _encoding, cb) { try { const str = csvStringify([chunk], { ...opt, header, }); header = false; cb(null, str); } catch (err) { console.error(err); if (strict) { cb(err); // emit error } else { cb(); // emit no error, but no result neither } } }, }); } exports.transformToCSV = transformToCSV; //# sourceMappingURL=transformToCSV.js.map